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