Tabs to spaces.
[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
42 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
43
44 connection = [[DebuggerProcessor alloc] initWithPort:[defaults integerForKey:@"Port"]];
45 connection.delegate = self;
46 expandedVariables = [[NSMutableSet alloc] init];
47 [[self window] makeKeyAndOrderFront:nil];
48 [[self window] setDelegate:self];
49
50 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"InspectorWindowVisible"])
51 [inspector orderFront:self];
52 }
53 return self;
54 }
55
56 /**
57 * Dealloc
58 */
59 - (void)dealloc
60 {
61 [connection release];
62 [expandedVariables release];
63 [stackController release];
64 [super dealloc];
65 }
66
67 /**
68 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
69 */
70 - (void)awakeFromNib
71 {
72 [[self window] setExcludedFromWindowsMenu:YES];
73 [[self window] setTitle:[NSString stringWithFormat:@"GDBp @ %@:%d", [connection remoteHost], [connection port]]];
74 [sourceViewer setDelegate:self];
75 [stackArrayController setSortDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"index" ascending:YES] autorelease]]];
76 }
77
78 /**
79 * Validates the menu items for the "Debugger" menu
80 */
81 - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem
82 {
83 SEL action = [anItem action];
84
85 if (action == @selector(stepOut:))
86 return ([connection isConnected] && [stackController.stack count] > 1);
87 else if (action == @selector(stepIn:) || action == @selector(stepOver:) || action == @selector(run:))
88 return [connection isConnected];
89 else if (action == @selector(reconnect:))
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 [self startDebugger];
141 }
142
143 /**
144 * Called once the socket accepts and MacGDBp is connected to the debugger
145 */
146 - (void)startDebugger
147 {
148 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"BreakOnFirstLine"])
149 [self stepIn:self];
150 }
151
152 /**
153 * Called once the debugger disconnects.
154 */
155 - (void)debuggerDisconnected
156 {
157 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"AutoReconnect"])
158 [self reconnect:self];
159
160 // Invalidate the marked line so we don't look like we're still running.
161 sourceViewer.markedLine = -1;
162 [sourceViewer setNeedsDisplay:YES];
163 }
164
165 /**
166 * Forwards the message to run script execution to the connection
167 */
168 - (IBAction)run:(id)sender
169 {
170 [connection run];
171 }
172
173 /**
174 * Tells the connection to ask the server to reconnect
175 */
176 - (IBAction)reconnect:(id)sender
177 {
178 [connection reconnect];
179 [self resetDisplays];
180 }
181
182 /**
183 * Forwards the message to "step in" to the connection
184 */
185 - (IBAction)stepIn:(id)sender
186 {
187 if ([[variablesTreeController selectedObjects] count] > 0)
188 selectedVariable = [[variablesTreeController selectedObjects] objectAtIndex:0];
189
190 [connection stepIn];
191 }
192
193 /**
194 * Forwards the message to "step out" to the connection
195 */
196 - (IBAction)stepOut:(id)sender
197 {
198 if ([[variablesTreeController selectedObjects] count] > 0)
199 selectedVariable = [[variablesTreeController selectedObjects] objectAtIndex:0];
200
201 [connection stepOut];
202 }
203
204 /**
205 * Forwards the message to "step over" to the connection
206 */
207 - (IBAction)stepOver:(id)sender
208 {
209 if ([[variablesTreeController selectedObjects] count] > 0)
210 selectedVariable = [[variablesTreeController selectedObjects] objectAtIndex:0];
211
212 [connection stepOver];
213 }
214
215 /**
216 * NSTableView delegate method that informs the controller that the stack selection did change and that
217 * we should update the source viewer
218 */
219 - (void)tableViewSelectionDidChange:(NSNotification*)notif
220 {
221 [self updateSourceViewer];
222 [self expandVariables];
223 }
224
225 /**
226 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
227 */
228 - (void)outlineViewItemDidExpand:(NSNotification*)notif
229 {
230 NSTreeNode* node = [[notif userInfo] objectForKey:@"NSObject"];
231 [expandedVariables addObject:[[node representedObject] fullname]];
232 }
233
234 /**
235 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
236 */
237 - (void)outlineViewItemDidCollapse:(NSNotification*)notif
238 {
239 [expandedVariables removeObject:[[[[notif userInfo] objectForKey:@"NSObject"] representedObject] fullname]];
240 }
241
242 #pragma mark Private
243
244 /**
245 * Does the actual updating of the source viewer by reading in the file
246 */
247 - (void)updateSourceViewer
248 {
249 NSArray* selection = [stackArrayController selectedObjects];
250 if (!selection || [selection count] < 1)
251 return;
252 if ([selection count] > 1)
253 NSLog(@"INVALID SELECTION");
254 StackFrame* frame = [selection objectAtIndex:0];
255
256 if (!frame.loaded) {
257 [connection loadStackFrame:frame];
258 return;
259 }
260
261 // Get the filename.
262 NSString* filename = [[NSURL URLWithString:frame.filename] path];
263 if ([filename isEqualToString:@""])
264 return;
265
266 // Replace the source if necessary.
267 if (frame.source && ![sourceViewer.file isEqualToString:filename])
268 {
269 [sourceViewer setString:frame.source asFile:filename];
270
271 NSSet* breakpoints = [NSSet setWithArray:[[BreakpointManager sharedManager] breakpointsForFile:filename]];
272 [[sourceViewer numberView] setMarkers:breakpoints];
273 }
274
275 [sourceViewer setMarkedLine:frame.lineNumber];
276 [sourceViewer scrollToLine:frame.lineNumber];
277
278 [[sourceViewer textView] display];
279 }
280
281 /**
282 * Does some house keeping to the stack viewer
283 */
284 - (void)updateStackViewer
285 {
286 [stackArrayController rearrangeObjects];
287 [stackArrayController setSelectionIndex:0];
288 [self expandVariables];
289 }
290
291 /**
292 * Expands the variables based on the stored set
293 */
294 - (void)expandVariables
295 {
296 NSString* selection = [selectedVariable fullname];
297
298 for (int i = 0; i < [variablesOutlineView numberOfRows]; i++)
299 {
300 NSTreeNode* node = [variablesOutlineView itemAtRow:i];
301 NSString* fullname = [[node representedObject] fullname];
302
303 // see if it needs expanding
304 if ([expandedVariables containsObject:fullname])
305 [variablesOutlineView expandItem:node];
306
307 // select it if we had it selected before
308 if ([fullname isEqualToString:selection])
309 [variablesTreeController setSelectionIndexPath:[node indexPath]];
310 }
311 }
312
313 #pragma mark BSSourceView Delegate
314
315 /**
316 * The gutter was clicked, which indicates that a breakpoint needs to be changed
317 */
318 - (void)gutterClickedAtLine:(int)line forFile:(NSString*)file
319 {
320 BreakpointManager* mngr = [BreakpointManager sharedManager];
321
322 if ([mngr hasBreakpointAt:line inFile:file])
323 {
324 [mngr removeBreakpointAt:line inFile:file];
325 }
326 else
327 {
328 Breakpoint* bp = [[Breakpoint alloc] initWithLine:line inFile:file];
329 [mngr addBreakpoint:bp];
330 [bp release];
331 }
332
333 [[sourceViewer numberView] setMarkers:[NSSet setWithArray:[mngr breakpointsForFile:file]]];
334 [[sourceViewer numberView] setNeedsDisplay:YES];
335 }
336
337 #pragma mark GDBpConnectionDelegate
338
339 - (void)clobberStack
340 {
341 aboutToClobber_ = YES;
342 }
343
344 - (void)newStackFrame:(StackFrame*)frame
345 {
346 if (aboutToClobber_)
347 {
348 [stackController.stack removeAllObjects];
349 aboutToClobber_ = NO;
350 }
351 [stackController push:frame];
352 [self updateStackViewer];
353 [self updateSourceViewer];
354 }
355
356 - (void)sourceUpdated:(StackFrame*)frame
357 {
358 [self updateSourceViewer];
359 }
360
361 @end