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