]> src.bluestatic.org Git - macgdbp.git/blob - Source/DebuggerController.m
Improve MessageQueueDelegate.
[macgdbp.git] / Source / DebuggerController.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2011, 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
19 #import "AppDelegate.h"
20 #import "BSSourceView.h"
21 #import "BreakpointManager.h"
22 #import "EvalController.h"
23 #import "NSXMLElementAdditions.h"
24
25 @interface DebuggerController (Private)
26 - (void)updateSourceViewer;
27 - (void)updateStackViewer;
28 - (void)expandVariables;
29 @end
30
31 @implementation DebuggerController
32
33 @synthesize connection, sourceViewer, inspector;
34
35 /**
36 * Initializes the window controller and sets the connection using preference
37 * values
38 */
39 - (id)init
40 {
41 if (self = [super initWithWindowNibName:@"Debugger"])
42 {
43 stackController = [[StackController alloc] init];
44 pendingProperties_ = [[NSMutableDictionary alloc] init];
45
46 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
47
48 connection = [[DebuggerBackEnd alloc] initWithPort:[defaults integerForKey:@"Port"]];
49 connection.delegate = self;
50 expandedVariables = [[NSMutableSet alloc] init];
51 [[self window] makeKeyAndOrderFront:nil];
52 [[self window] setDelegate:self];
53
54 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"InspectorWindowVisible"])
55 [inspector orderFront:self];
56 }
57 return self;
58 }
59
60 /**
61 * Dealloc
62 */
63 - (void)dealloc
64 {
65 [connection release];
66 [expandedVariables release];
67 [stackController release];
68 [pendingProperties_ release];
69 [super dealloc];
70 }
71
72 /**
73 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
74 */
75 - (void)awakeFromNib
76 {
77 [[self window] setExcludedFromWindowsMenu:YES];
78 [[self window] setTitle:[NSString stringWithFormat:@"MacGDBp @ %d", [connection port]]];
79 [sourceViewer setDelegate:self];
80 [stackArrayController setSortDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"index" ascending:YES] autorelease]]];
81 self.connection.attached = [attachedCheckbox_ state] == NSOnState;
82 }
83
84 /**
85 * Validates the menu items for the "Debugger" menu
86 */
87 - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem
88 {
89 SEL action = [anItem action];
90
91 if (action == @selector(stepOut:)) {
92 return ([connection isConnected] && [stackController.stack count] > 1);
93 } else if (action == @selector(stepIn:) ||
94 action == @selector(stepOver:) ||
95 action == @selector(run:) ||
96 action == @selector(stop:) ||
97 action == @selector(showEvalWindow:)) {
98 return [connection isConnected];
99 }
100 return [[self window] validateUserInterfaceItem:anItem];
101 }
102
103 /**
104 * Shows the inspector window
105 */
106 - (IBAction)showInspectorWindow:(id)sender
107 {
108 if (![inspector isVisible])
109 [inspector makeKeyAndOrderFront:sender];
110 else
111 [inspector orderOut:sender];
112 }
113
114 /**
115 * Runs the eval window sheet.
116 */
117 - (IBAction)showEvalWindow:(id)sender
118 {
119 // The |controller| will release itself on close.
120 EvalController* controller = [[EvalController alloc] initWithBackEnd:connection];
121 [controller runModalForWindow:[self window]];
122 }
123
124 /**
125 * Resets all the displays to be empty
126 */
127 - (void)resetDisplays
128 {
129 [variablesTreeController setContent:nil];
130 [stackController.stack removeAllObjects];
131 [stackArrayController rearrangeObjects];
132 [[sourceViewer textView] setString:@""];
133 sourceViewer.file = nil;
134 }
135
136 /**
137 * Sets the status to be "Error" and then displays the error message
138 */
139 - (void)setError:(NSString*)anError
140 {
141 [errormsg setStringValue:anError];
142 [errormsg setHidden:NO];
143 }
144
145 /**
146 * Handles a GDBpConnection error
147 */
148 - (void)errorEncountered:(NSString*)error
149 {
150 [self setError:error];
151 }
152
153 /**
154 * Delegate function for GDBpConnection for when the debugger connects.
155 */
156 - (void)debuggerConnected
157 {
158 [errormsg setHidden:YES];
159 if (!self.connection.attached)
160 return;
161 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"BreakOnFirstLine"])
162 [self stepIn:self];
163 }
164
165 /**
166 * Called once the debugger disconnects.
167 */
168 - (void)debuggerDisconnected
169 {
170 // Invalidate the marked line so we don't look like we're still running.
171 sourceViewer.markedLine = -1;
172 [sourceViewer setNeedsDisplay:YES];
173 }
174
175 /**
176 * Forwards the message to run script execution to the connection
177 */
178 - (IBAction)run:(id)sender
179 {
180 [connection run];
181 }
182
183 - (IBAction)attachedToggled:(id)sender
184 {
185 connection.attached = [sender state] == NSOnState;
186 }
187
188 /**
189 * Forwards the message to "step in" to the connection
190 */
191 - (IBAction)stepIn:(id)sender
192 {
193 if ([[variablesTreeController selectedObjects] count] > 0)
194 selectedVariable = [[variablesTreeController selectedObjects] objectAtIndex:0];
195
196 [connection stepIn];
197 }
198
199 /**
200 * Forwards the message to "step out" to the connection
201 */
202 - (IBAction)stepOut:(id)sender
203 {
204 if ([[variablesTreeController selectedObjects] count] > 0)
205 selectedVariable = [[variablesTreeController selectedObjects] objectAtIndex:0];
206
207 [connection stepOut];
208 }
209
210 /**
211 * Forwards the message to "step over" to the connection
212 */
213 - (IBAction)stepOver:(id)sender
214 {
215 if ([[variablesTreeController selectedObjects] count] > 0)
216 selectedVariable = [[variablesTreeController selectedObjects] objectAtIndex:0];
217
218 [connection stepOver];
219 }
220
221 /**
222 * Forwards the detach/"stop" message to the back end.
223 */
224 - (IBAction)stop:(id)sender
225 {
226 [connection stop];
227 }
228
229 - (void)fetchChildProperties:(VariableNode*)node
230 {
231 NSArray* selection = [stackArrayController selectedObjects];
232 if (![selection count])
233 return;
234 assert([selection count] == 1);
235 NSInteger depth = [[selection objectAtIndex:0] index];
236 NSInteger txn = [connection getChildrenOfProperty:node atDepth:depth];
237 [pendingProperties_ setObject:node forKey:[NSNumber numberWithInt:txn]];
238 }
239
240 /**
241 * NSTableView delegate method that informs the controller that the stack selection did change and that
242 * we should update the source viewer
243 */
244 - (void)tableViewSelectionDidChange:(NSNotification*)notif
245 {
246 [self updateSourceViewer];
247 // TODO: This is very, very hacky because it's nondeterministic. The issue
248 // is that calling |-[NSOutlineView expandItem:]| while the table is still
249 // doing its redraw will translate to a no-op. Instead, we need to restructure
250 // this controller so that when everything has been laid out we call
251 // |-expandVariables|; but NSOutlineView doesn't have a |-didFinishDoingCrap:|
252 // method. The other issue is that we need to call this method from
253 // selectionDidChange but ONLY when it was the result of a user-initiated
254 // action and not the stack viewer updating causing a selection change.
255 // If it happens in the latter, then we run into the same issue that causes
256 // this to no-op.
257 [self performSelector:@selector(expandVariables) withObject:nil afterDelay:0.05];
258 }
259
260 /**
261 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
262 */
263 - (void)outlineViewItemDidExpand:(NSNotification*)notif
264 {
265 NSTreeNode* node = [[notif userInfo] objectForKey:@"NSObject"];
266 [expandedVariables addObject:[[node representedObject] fullName]];
267 }
268
269 /**
270 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
271 */
272 - (void)outlineViewItemDidCollapse:(NSNotification*)notif
273 {
274 [expandedVariables removeObject:[[[[notif userInfo] objectForKey:@"NSObject"] representedObject] fullName]];
275 }
276
277 #pragma mark Private
278
279 /**
280 * Does the actual updating of the source viewer by reading in the file
281 */
282 - (void)updateSourceViewer
283 {
284 NSArray* selection = [stackArrayController selectedObjects];
285 if (!selection || [selection count] < 1)
286 return;
287 if ([selection count] > 1)
288 NSLog(@"INVALID SELECTION");
289 StackFrame* frame = [selection objectAtIndex:0];
290
291 if (!frame.loaded) {
292 [connection loadStackFrame:frame];
293 return;
294 }
295
296 // Get the filename.
297 NSString* filename = [[NSURL URLWithString:frame.filename] path];
298 if ([filename isEqualToString:@""])
299 return;
300
301 // Replace the source if necessary.
302 if (frame.source && ![sourceViewer.file isEqualToString:filename])
303 {
304 [sourceViewer setString:frame.source asFile:filename];
305
306 NSSet* breakpoints = [NSSet setWithArray:[[BreakpointManager sharedManager] breakpointsForFile:filename]];
307 [sourceViewer setMarkers:breakpoints];
308 }
309
310 [sourceViewer setMarkedLine:frame.lineNumber];
311 [sourceViewer scrollToLine:frame.lineNumber];
312
313 [[sourceViewer textView] display];
314 }
315
316 /**
317 * Does some house keeping to the stack viewer
318 */
319 - (void)updateStackViewer
320 {
321 [stackArrayController rearrangeObjects];
322 [stackArrayController setSelectionIndex:0];
323 }
324
325 /**
326 * Expands the variables based on the stored set
327 */
328 - (void)expandVariables
329 {
330 NSString* selection = [selectedVariable fullName];
331
332 for (NSInteger i = 0; i < [variablesOutlineView numberOfRows]; i++) {
333 NSTreeNode* node = [variablesOutlineView itemAtRow:i];
334 NSString* fullName = [[node representedObject] fullName];
335
336 // see if it needs expanding
337 if ([expandedVariables containsObject:fullName])
338 [variablesOutlineView expandItem:node];
339
340 // select it if we had it selected before
341 if ([fullName isEqualToString:selection])
342 [variablesTreeController setSelectionIndexPath:[node indexPath]];
343 }
344 }
345
346 #pragma mark BSSourceView Delegate
347
348 /**
349 * The gutter was clicked, which indicates that a breakpoint needs to be changed
350 */
351 - (void)gutterClickedAtLine:(int)line forFile:(NSString*)file
352 {
353 BreakpointManager* mngr = [BreakpointManager sharedManager];
354
355 if ([mngr hasBreakpointAt:line inFile:file])
356 {
357 [mngr removeBreakpointAt:line inFile:file];
358 }
359 else
360 {
361 Breakpoint* bp = [[Breakpoint alloc] initWithLine:line inFile:file];
362 [mngr addBreakpoint:bp];
363 [bp release];
364 }
365
366 [sourceViewer setMarkers:[NSSet setWithArray:[mngr breakpointsForFile:file]]];
367 [sourceViewer setNeedsDisplay:YES];
368 }
369
370 #pragma mark GDBpConnectionDelegate
371
372 - (void)clobberStack
373 {
374 aboutToClobber_ = YES;
375 [pendingProperties_ removeAllObjects];
376 }
377
378 - (void)newStackFrame:(StackFrame*)frame
379 {
380 if (aboutToClobber_)
381 {
382 [stackController.stack removeAllObjects];
383 aboutToClobber_ = NO;
384 }
385 [stackController push:frame];
386 [self updateStackViewer];
387 [self updateSourceViewer];
388 }
389
390 - (void)sourceUpdated:(StackFrame*)frame
391 {
392 [self updateSourceViewer];
393 }
394
395 - (void)receivedProperties:(NSArray*)properties forTransaction:(NSInteger)transaction
396 {
397 NSNumber* key = [NSNumber numberWithInt:transaction];
398 VariableNode* node = [pendingProperties_ objectForKey:key];
399 if (node) {
400 [node setChildrenFromXMLChildren:properties];
401 [variablesTreeController rearrangeObjects];
402 [pendingProperties_ removeObjectForKey:key];
403 }
404 }
405
406 - (void)scriptWasEvaluatedWithResult:(NSString*)result
407 {
408 [EvalController scriptWasEvaluatedWithResult:result];
409 }
410
411 @end