Fix the run command by having it obliterate the current stack and set a new one
[macgdbp.git] / Source / DebuggerController.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2009, Blue Static <http://www.bluestatic.org>
4 *
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17 #import "DebuggerController.h"
18 #import "GDBpConnection.h"
19 #import "NSXMLElementAdditions.h"
20 #import "AppDelegate.h"
21 #import "BreakpointManager.h"
22
23 @interface DebuggerController (Private)
24 - (void)updateSourceViewer;
25 - (void)updateStackViewer;
26 - (void)expandVariables;
27 @end
28
29 @implementation DebuggerController
30
31 @synthesize connection, sourceViewer, inspector;
32
33 /**
34 * Initializes the window controller and sets the connection using preference
35 * values
36 */
37 - (id)init
38 {
39 if (self = [super initWithWindowNibName:@"Debugger"])
40 {
41 stackController = [[StackController alloc] init];
42
43 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
44 connection = [[GDBpConnection alloc] initWithPort:[defaults integerForKey:@"Port"] session:[defaults stringForKey:@"IDEKey"]];
45 expandedVariables = [[NSMutableSet alloc] init];
46 [[self window] makeKeyAndOrderFront:nil];
47 [[self window] setDelegate:self];
48
49 [[NSNotificationCenter defaultCenter]
50 addObserver:self
51 selector:@selector(handleConnectionError:)
52 name:kErrorOccurredNotif
53 object:connection
54 ];
55
56 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"InspectorWindowVisible"])
57 [inspector orderFront:self];
58 }
59 return self;
60 }
61
62 /**
63 * Dealloc
64 */
65 - (void)dealloc
66 {
67 [connection release];
68 [expandedVariables release];
69 [stackController release];
70 [super dealloc];
71 }
72
73 /**
74 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
75 */
76 - (void)awakeFromNib
77 {
78 [[self window] setExcludedFromWindowsMenu:YES];
79 [[self window] setTitle:[NSString stringWithFormat:@"GDBp @ %@:%d/%@", [connection remoteHost], [connection port], [connection session]]];
80 [sourceViewer setDelegate:self];
81 [stackArrayController setSortDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"index" ascending:YES] autorelease]]];
82 }
83
84 /**
85 * Called right before the window closes so that we can tell the socket to close down
86 */
87 - (void)windowWillClose:(NSNotification *)notif
88 {
89 [[connection socket] close];
90 }
91
92 /**
93 * Validates the menu items for the "Debugger" menu
94 */
95 - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem
96 {
97 SEL action = [anItem action];
98
99 if (action == @selector(stepOut:))
100 return ([connection isConnected] && [stackController.stack count] > 1);
101 else if (action == @selector(stepIn:) || action == @selector(stepOver:) || action == @selector(run:))
102 return [connection isConnected];
103 else if (action == @selector(reconnect:))
104 return ![connection isConnected];
105
106 return [[self window] validateUserInterfaceItem:anItem];
107 }
108
109 /**
110 * Resets all the displays to be empty
111 */
112 - (void)resetDisplays
113 {
114 [variablesTreeController setContent:nil];
115 [stackController.stack removeAllObjects];
116 [stackArrayController rearrangeObjects];
117 [[sourceViewer textView] setString:@""];
118 sourceViewer.file = nil;
119 }
120
121 /**
122 * Sets the status to be "Error" and then displays the error message
123 */
124 - (void)setError:(NSString *)anError
125 {
126 [errormsg setStringValue:anError];
127 [errormsg setHidden:NO];
128 }
129
130 /**
131 * Handles a GDBpConnection error
132 */
133 - (void)handleConnectionError:(NSNotification *)notif
134 {
135 [self setError:[[notif userInfo] valueForKey:@"NSString"]];
136 }
137
138 /**
139 * Called once the socket accepts and MacGDBp is connected to the debugger
140 */
141 - (void)startDebugger
142 {
143 [self stepIn:self];
144 }
145
146 /**
147 * Forwards the message to run script execution to the connection
148 */
149 - (IBAction)run:(id)sender
150 {
151 NSArray *frames = [connection run];
152
153 if ([connection isConnected] && frames != nil)
154 {
155 [stackController.stack removeAllObjects];
156 [stackController.stack addObjectsFromArray:frames];
157 [self updateStackViewer];
158 [self updateSourceViewer];
159 }
160 }
161
162 /**
163 * Tells the connection to ask the server to reconnect
164 */
165 - (IBAction)reconnect:(id)sender
166 {
167 [connection reconnect];
168 [self resetDisplays];
169 }
170
171 /**
172 * Forwards the message to "step in" to the connection
173 */
174 - (IBAction)stepIn:(id)sender
175 {
176 if ([[variablesTreeController selectedObjects] count] > 0)
177 selectedVariable = [[variablesTreeController selectedObjects] objectAtIndex:0];
178
179 StackFrame *frame = [connection stepIn];
180 if ([frame isShiftedFrame:[stackController peek]])
181 [stackController pop];
182 [stackController push:frame];
183 [self updateStackViewer];
184 }
185
186 /**
187 * Forwards the message to "step out" to the connection
188 */
189 - (IBAction)stepOut:(id)sender
190 {
191 if ([[variablesTreeController selectedObjects] count] > 0)
192 selectedVariable = [[variablesTreeController selectedObjects] objectAtIndex:0];
193
194 StackFrame *frame = [connection stepOut];
195 [stackController pop]; // frame we were out of
196 [stackController pop]; // frame we are returning to
197 [stackController push:frame];
198 [self updateStackViewer];
199 }
200
201 /**
202 * Forwards the message to "step over" to the connection
203 */
204 - (IBAction)stepOver:(id)sender
205 {
206 if ([[variablesTreeController selectedObjects] count] > 0)
207 selectedVariable = [[variablesTreeController selectedObjects] objectAtIndex:0];
208
209 StackFrame *frame = [connection stepOver];
210 [stackController pop];
211 [stackController push:frame];
212 [self updateStackViewer];
213 }
214
215 /**
216 * NSTableView delegate method that informs the controller that the stack selection did change and that
217 * we should update the source viewer
218 */
219 - (void)tableViewSelectionDidChange:(NSNotification *)notif
220 {
221 [self updateSourceViewer];
222 [self expandVariables];
223 }
224
225 /**
226 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
227 */
228 - (void)outlineViewItemDidExpand:(NSNotification *)notif
229 {
230 NSTreeNode *node = [[notif userInfo] objectForKey:@"NSObject"];
231 [expandedVariables addObject:[[node representedObject] fullname]];
232 }
233
234 /**
235 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
236 */
237 - (void)outlineViewItemDidCollapse:(NSNotification *)notif
238 {
239 [expandedVariables removeObject:[[[[notif userInfo] objectForKey:@"NSObject"] representedObject] fullname]];
240 }
241
242 #pragma mark Private
243
244 /**
245 * Does the actual updating of the source viewer by reading in the file
246 */
247 - (void)updateSourceViewer
248 {
249 id selection = [stackArrayController selection];
250 if ([selection valueForKey:@"filename"] == NSNoSelectionMarker)
251 return;
252
253 // get the filename
254 NSString *filename = [selection valueForKey:@"filename"];
255 filename = [[NSURL URLWithString:filename] path];
256 if ([filename isEqualToString:@""])
257 return;
258
259 // replace the source if necessary
260 if (![sourceViewer.file isEqualToString:filename])
261 {
262 NSString *source = [selection valueForKey:@"source"];
263 [sourceViewer setString:source asFile:filename];
264
265 NSSet *breakpoints = [NSSet setWithArray:[[BreakpointManager sharedManager] breakpointsForFile:filename]];
266 [[sourceViewer numberView] setMarkers:breakpoints];
267 }
268
269 int line = [[selection valueForKey:@"lineNumber"] intValue];
270 [sourceViewer setMarkedLine:line];
271 [sourceViewer scrollToLine:line];
272
273 [[sourceViewer textView] display];
274 }
275
276 /**
277 * Does some house keeping to the stack viewer
278 */
279 - (void)updateStackViewer
280 {
281 [stackArrayController rearrangeObjects];
282 [stackArrayController setSelectionIndex:0];
283 [self expandVariables];
284 }
285
286 /**
287 * Expands the variables based on the stored set
288 */
289 - (void)expandVariables
290 {
291 NSString *selection = [selectedVariable fullname];
292
293 for (int i = 0; i < [variablesOutlineView numberOfRows]; i++)
294 {
295 NSTreeNode *node = [variablesOutlineView itemAtRow:i];
296 NSString *fullname = [[node representedObject] fullname];
297
298 // see if it needs expanding
299 if ([expandedVariables containsObject:fullname])
300 [variablesOutlineView expandItem:node];
301
302 // select it if we had it selected before
303 if ([fullname isEqualToString:selection])
304 [variablesTreeController setSelectionIndexPath:[node indexPath]];
305 }
306 }
307
308 #pragma mark BSSourceView Delegate
309
310 /**
311 * The gutter was clicked, which indicates that a breakpoint needs to be changed
312 */
313 - (void)gutterClickedAtLine:(int)line forFile:(NSString *)file
314 {
315 BreakpointManager *mngr = [BreakpointManager sharedManager];
316
317 if ([mngr hasBreakpointAt:line inFile:file])
318 {
319 [mngr removeBreakpointAt:line inFile:file];
320 }
321 else
322 {
323 Breakpoint *bp = [[Breakpoint alloc] initWithLine:line inFile:file];
324 [mngr addBreakpoint:bp];
325 [bp release];
326 }
327
328 [[sourceViewer numberView] setMarkers:[NSSet setWithArray:[mngr breakpointsForFile:file]]];
329 [[sourceViewer numberView] setNeedsDisplay:YES];
330 }
331
332 @end