Renaming because there's too many Debugger* classes
[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 = [[DebuggerBackEnd 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:@"MacGDBp @ %d", [connection port]]];
76 [sourceViewer setDelegate:self];
77 [stackArrayController setSortDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"index" ascending:YES] autorelease]]];
78 self.connection.attached = [attachedCheckbox_ state] == NSOnState;
79 }
80
81 /**
82 * Validates the menu items for the "Debugger" menu
83 */
84 - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem
85 {
86 SEL action = [anItem action];
87
88 if (action == @selector(stepOut:))
89 return ([connection isConnected] && [stackController.stack count] > 1);
90 else if (action == @selector(stepIn:) || action == @selector(stepOver:) || action == @selector(run:))
91 return [connection isConnected];
92
93 return [[self window] validateUserInterfaceItem:anItem];
94 }
95
96 /**
97 * Shows the inspector window
98 */
99 - (IBAction)showInspectorWindow:(id)sender
100 {
101 if (![inspector isVisible])
102 [inspector makeKeyAndOrderFront:sender];
103 else
104 [inspector orderOut:sender];
105 }
106
107 /**
108 * Resets all the displays to be empty
109 */
110 - (void)resetDisplays
111 {
112 [variablesTreeController setContent:nil];
113 [stackController.stack removeAllObjects];
114 [stackArrayController rearrangeObjects];
115 [[sourceViewer textView] setString:@""];
116 sourceViewer.file = nil;
117 }
118
119 /**
120 * Sets the status to be "Error" and then displays the error message
121 */
122 - (void)setError:(NSString*)anError
123 {
124 [errormsg setStringValue:anError];
125 [errormsg setHidden:NO];
126 }
127
128 /**
129 * Handles a GDBpConnection error
130 */
131 - (void)errorEncountered:(NSString*)error
132 {
133 [self setError:error];
134 }
135
136 /**
137 * Delegate function for GDBpConnection for when the debugger connects.
138 */
139 - (void)debuggerConnected
140 {
141 [errormsg setHidden:YES];
142 if (!self.connection.attached)
143 return;
144 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"BreakOnFirstLine"])
145 [self stepIn:self];
146 }
147
148 /**
149 * Called once the debugger disconnects.
150 */
151 - (void)debuggerDisconnected
152 {
153 // Invalidate the marked line so we don't look like we're still running.
154 sourceViewer.markedLine = -1;
155 [sourceViewer setNeedsDisplay:YES];
156 }
157
158 /**
159 * Forwards the message to run script execution to the connection
160 */
161 - (IBAction)run:(id)sender
162 {
163 [connection run];
164 }
165
166 - (IBAction)attachedToggled:(id)sender
167 {
168 connection.attached = [sender state] == NSOnState;
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 [connection stepIn];
180 }
181
182 /**
183 * Forwards the message to "step out" to the connection
184 */
185 - (IBAction)stepOut:(id)sender
186 {
187 if ([[variablesTreeController selectedObjects] count] > 0)
188 selectedVariable = [[variablesTreeController selectedObjects] objectAtIndex:0];
189
190 [connection stepOut];
191 }
192
193 /**
194 * Forwards the message to "step over" to the connection
195 */
196 - (IBAction)stepOver:(id)sender
197 {
198 if ([[variablesTreeController selectedObjects] count] > 0)
199 selectedVariable = [[variablesTreeController selectedObjects] objectAtIndex:0];
200
201 [connection stepOver];
202 }
203
204 - (void)fetchChildProperties:(VariableNode*)node
205 {
206 NSArray* selection = [stackArrayController selectedObjects];
207 assert([selection count] == 1);
208 NSInteger depth = [[selection objectAtIndex:0] index];
209 NSInteger txn = [connection getChildrenOfProperty:node atDepth:depth];
210 [pendingProperties_ setObject:node forKey:[NSNumber numberWithInt:txn]];
211 }
212
213 /**
214 * NSTableView delegate method that informs the controller that the stack selection did change and that
215 * we should update the source viewer
216 */
217 - (void)tableViewSelectionDidChange:(NSNotification*)notif
218 {
219 [self updateSourceViewer];
220 [self expandVariables];
221 }
222
223 /**
224 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
225 */
226 - (void)outlineViewItemDidExpand:(NSNotification*)notif
227 {
228 NSTreeNode* node = [[notif userInfo] objectForKey:@"NSObject"];
229 [expandedVariables addObject:[[node representedObject] fullName]];
230 }
231
232 /**
233 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
234 */
235 - (void)outlineViewItemDidCollapse:(NSNotification*)notif
236 {
237 [expandedVariables removeObject:[[[[notif userInfo] objectForKey:@"NSObject"] representedObject] fullName]];
238 }
239
240 #pragma mark Private
241
242 /**
243 * Does the actual updating of the source viewer by reading in the file
244 */
245 - (void)updateSourceViewer
246 {
247 NSArray* selection = [stackArrayController selectedObjects];
248 if (!selection || [selection count] < 1)
249 return;
250 if ([selection count] > 1)
251 NSLog(@"INVALID SELECTION");
252 StackFrame* frame = [selection objectAtIndex:0];
253
254 if (!frame.loaded) {
255 [connection loadStackFrame:frame];
256 return;
257 }
258
259 // Get the filename.
260 NSString* filename = [[NSURL URLWithString:frame.filename] path];
261 if ([filename isEqualToString:@""])
262 return;
263
264 // Replace the source if necessary.
265 if (frame.source && ![sourceViewer.file isEqualToString:filename])
266 {
267 [sourceViewer setString:frame.source asFile:filename];
268
269 NSSet* breakpoints = [NSSet setWithArray:[[BreakpointManager sharedManager] breakpointsForFile:filename]];
270 [[sourceViewer numberView] setMarkers:breakpoints];
271 }
272
273 [sourceViewer setMarkedLine:frame.lineNumber];
274 [sourceViewer scrollToLine:frame.lineNumber];
275
276 [[sourceViewer textView] display];
277 }
278
279 /**
280 * Does some house keeping to the stack viewer
281 */
282 - (void)updateStackViewer
283 {
284 [stackArrayController rearrangeObjects];
285 [stackArrayController setSelectionIndex:0];
286 [self expandVariables];
287 }
288
289 /**
290 * Expands the variables based on the stored set
291 */
292 - (void)expandVariables
293 {
294 NSString* selection = [selectedVariable fullName];
295
296 for (int i = 0; i < [variablesOutlineView numberOfRows]; i++)
297 {
298 NSTreeNode* node = [variablesOutlineView itemAtRow:i];
299 NSString* fullName = [[node representedObject] fullName];
300
301 // see if it needs expanding
302 if ([expandedVariables containsObject:fullName])
303 [variablesOutlineView expandItem:node];
304
305 // select it if we had it selected before
306 if ([fullName isEqualToString:selection])
307 [variablesTreeController setSelectionIndexPath:[node indexPath]];
308 }
309 }
310
311 #pragma mark BSSourceView Delegate
312
313 /**
314 * The gutter was clicked, which indicates that a breakpoint needs to be changed
315 */
316 - (void)gutterClickedAtLine:(int)line forFile:(NSString*)file
317 {
318 BreakpointManager* mngr = [BreakpointManager sharedManager];
319
320 if ([mngr hasBreakpointAt:line inFile:file])
321 {
322 [mngr removeBreakpointAt:line inFile:file];
323 }
324 else
325 {
326 Breakpoint* bp = [[Breakpoint alloc] initWithLine:line inFile:file];
327 [mngr addBreakpoint:bp];
328 [bp release];
329 }
330
331 [[sourceViewer numberView] setMarkers:[NSSet setWithArray:[mngr breakpointsForFile:file]]];
332 [[sourceViewer numberView] setNeedsDisplay:YES];
333 }
334
335 #pragma mark GDBpConnectionDelegate
336
337 - (void)clobberStack
338 {
339 aboutToClobber_ = YES;
340 [pendingProperties_ removeAllObjects];
341 }
342
343 - (void)newStackFrame:(StackFrame*)frame
344 {
345 if (aboutToClobber_)
346 {
347 [stackController.stack removeAllObjects];
348 aboutToClobber_ = NO;
349 }
350 [stackController push:frame];
351 [self updateStackViewer];
352 [self updateSourceViewer];
353 }
354
355 - (void)sourceUpdated:(StackFrame*)frame
356 {
357 [self updateSourceViewer];
358 }
359
360 - (void)receivedProperties:(NSArray*)properties forTransaction:(NSInteger)transaction
361 {
362 NSNumber* key = [NSNumber numberWithInt:transaction];
363 VariableNode* node = [pendingProperties_ objectForKey:key];
364 if (node) {
365 [node setChildrenFromXMLChildren:properties];
366 [variablesTreeController rearrangeObjects];
367 [pendingProperties_ removeObjectForKey:key];
368 }
369 }
370
371 @end