* Remove the AutoReconnect preference item now that reconnecting is a thing of the...
[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
92 return [[self window] validateUserInterfaceItem:anItem];
93 }
94
95 /**
96 * Shows the inspector window
97 */
98 - (IBAction)showInspectorWindow:(id)sender
99 {
100 if (![inspector isVisible])
101 [inspector makeKeyAndOrderFront:sender];
102 else
103 [inspector orderOut:sender];
104 }
105
106 /**
107 * Resets all the displays to be empty
108 */
109 - (void)resetDisplays
110 {
111 [variablesTreeController setContent:nil];
112 [stackController.stack removeAllObjects];
113 [stackArrayController rearrangeObjects];
114 [[sourceViewer textView] setString:@""];
115 sourceViewer.file = nil;
116 }
117
118 /**
119 * Sets the status to be "Error" and then displays the error message
120 */
121 - (void)setError:(NSString*)anError
122 {
123 [errormsg setStringValue:anError];
124 [errormsg setHidden:NO];
125 }
126
127 /**
128 * Handles a GDBpConnection error
129 */
130 - (void)errorEncountered:(NSString*)error
131 {
132 [self setError:error];
133 }
134
135 /**
136 * Delegate function for GDBpConnection for when the debugger connects.
137 */
138 - (void)debuggerConnected
139 {
140 [errormsg setHidden:YES];
141 [self startDebugger];
142 }
143
144 /**
145 * Called once the socket accepts and MacGDBp is connected to the debugger
146 */
147 - (void)startDebugger
148 {
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 - (void)fetchChildProperties:(VariableNode*)node
210 {
211 NSArray* selection = [stackArrayController selectedObjects];
212 assert([selection count] == 1);
213 NSInteger depth = [[selection objectAtIndex:0] index];
214 NSInteger txn = [connection getChildrenOfProperty:node atDepth:depth];
215 [pendingProperties_ setObject:node forKey:[NSNumber numberWithInt:txn]];
216 }
217
218 /**
219 * NSTableView delegate method that informs the controller that the stack selection did change and that
220 * we should update the source viewer
221 */
222 - (void)tableViewSelectionDidChange:(NSNotification*)notif
223 {
224 [self updateSourceViewer];
225 [self expandVariables];
226 }
227
228 /**
229 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
230 */
231 - (void)outlineViewItemDidExpand:(NSNotification*)notif
232 {
233 NSTreeNode* node = [[notif userInfo] objectForKey:@"NSObject"];
234 [expandedVariables addObject:[[node representedObject] fullName]];
235 }
236
237 /**
238 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
239 */
240 - (void)outlineViewItemDidCollapse:(NSNotification*)notif
241 {
242 [expandedVariables removeObject:[[[[notif userInfo] objectForKey:@"NSObject"] representedObject] fullName]];
243 }
244
245 #pragma mark Private
246
247 /**
248 * Does the actual updating of the source viewer by reading in the file
249 */
250 - (void)updateSourceViewer
251 {
252 NSArray* selection = [stackArrayController selectedObjects];
253 if (!selection || [selection count] < 1)
254 return;
255 if ([selection count] > 1)
256 NSLog(@"INVALID SELECTION");
257 StackFrame* frame = [selection objectAtIndex:0];
258
259 if (!frame.loaded) {
260 [connection loadStackFrame:frame];
261 return;
262 }
263
264 // Get the filename.
265 NSString* filename = [[NSURL URLWithString:frame.filename] path];
266 if ([filename isEqualToString:@""])
267 return;
268
269 // Replace the source if necessary.
270 if (frame.source && ![sourceViewer.file isEqualToString:filename])
271 {
272 [sourceViewer setString:frame.source asFile:filename];
273
274 NSSet* breakpoints = [NSSet setWithArray:[[BreakpointManager sharedManager] breakpointsForFile:filename]];
275 [[sourceViewer numberView] setMarkers:breakpoints];
276 }
277
278 [sourceViewer setMarkedLine:frame.lineNumber];
279 [sourceViewer scrollToLine:frame.lineNumber];
280
281 [[sourceViewer textView] display];
282 }
283
284 /**
285 * Does some house keeping to the stack viewer
286 */
287 - (void)updateStackViewer
288 {
289 [stackArrayController rearrangeObjects];
290 [stackArrayController setSelectionIndex:0];
291 [self expandVariables];
292 }
293
294 /**
295 * Expands the variables based on the stored set
296 */
297 - (void)expandVariables
298 {
299 NSString* selection = [selectedVariable fullName];
300
301 for (int i = 0; i < [variablesOutlineView numberOfRows]; i++)
302 {
303 NSTreeNode* node = [variablesOutlineView itemAtRow:i];
304 NSString* fullName = [[node representedObject] fullName];
305
306 // see if it needs expanding
307 if ([expandedVariables containsObject:fullName])
308 [variablesOutlineView expandItem:node];
309
310 // select it if we had it selected before
311 if ([fullName isEqualToString:selection])
312 [variablesTreeController setSelectionIndexPath:[node indexPath]];
313 }
314 }
315
316 #pragma mark BSSourceView Delegate
317
318 /**
319 * The gutter was clicked, which indicates that a breakpoint needs to be changed
320 */
321 - (void)gutterClickedAtLine:(int)line forFile:(NSString*)file
322 {
323 BreakpointManager* mngr = [BreakpointManager sharedManager];
324
325 if ([mngr hasBreakpointAt:line inFile:file])
326 {
327 [mngr removeBreakpointAt:line inFile:file];
328 }
329 else
330 {
331 Breakpoint* bp = [[Breakpoint alloc] initWithLine:line inFile:file];
332 [mngr addBreakpoint:bp];
333 [bp release];
334 }
335
336 [[sourceViewer numberView] setMarkers:[NSSet setWithArray:[mngr breakpointsForFile:file]]];
337 [[sourceViewer numberView] setNeedsDisplay:YES];
338 }
339
340 #pragma mark GDBpConnectionDelegate
341
342 - (void)clobberStack
343 {
344 aboutToClobber_ = YES;
345 [pendingProperties_ removeAllObjects];
346 }
347
348 - (void)newStackFrame:(StackFrame*)frame
349 {
350 if (aboutToClobber_)
351 {
352 [stackController.stack removeAllObjects];
353 aboutToClobber_ = NO;
354 }
355 [stackController push:frame];
356 [self updateStackViewer];
357 [self updateSourceViewer];
358 }
359
360 - (void)sourceUpdated:(StackFrame*)frame
361 {
362 [self updateSourceViewer];
363 }
364
365 - (void)receivedProperties:(NSArray*)properties forTransaction:(NSInteger)transaction
366 {
367 NSNumber* key = [NSNumber numberWithInt:transaction];
368 VariableNode* node = [pendingProperties_ objectForKey:key];
369 if (node) {
370 [node setChildrenFromXMLChildren:properties];
371 [variablesTreeController rearrangeObjects];
372 [pendingProperties_ removeObjectForKey:key];
373 }
374 }
375
376 @end