]> src.bluestatic.org Git - macgdbp.git/blob - Source/DebuggerController.m
In |-debuggerDisconnected|, unmark the program counter line so it doesn't look like...
[macgdbp.git] / Source / DebuggerController.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2009, 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 - (void)reloadStack;
27 @end
28
29 @implementation DebuggerController
30
31 @synthesize connection, sourceViewer, inspector;
32
33 /**
34 * Initializes the window controller and sets the connection using preference
35 * values
36 */
37 - (id)init
38 {
39 if (self = [super initWithWindowNibName:@"Debugger"])
40 {
41 stackController = [[StackController alloc] init];
42
43 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
44 connection = [[GDBpConnection alloc] initWithPort:[defaults integerForKey:@"Port"] session:[defaults stringForKey:@"IDEKey"]];
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], [connection session]]];
74 [sourceViewer setDelegate:self];
75 [stackArrayController setSortDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"index" ascending:YES] autorelease]]];
76 }
77
78 /**
79 * Called right before the window closes so that we can tell the socket to close down
80 */
81 - (void)windowWillClose:(NSNotification*)notif
82 {
83 [[connection socket] close];
84 }
85
86 /**
87 * Validates the menu items for the "Debugger" menu
88 */
89 - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem
90 {
91 SEL action = [anItem action];
92
93 if (action == @selector(stepOut:))
94 return ([connection isConnected] && [stackController.stack count] > 1);
95 else if (action == @selector(stepIn:) || action == @selector(stepOver:) || action == @selector(run:))
96 return [connection isConnected];
97 else if (action == @selector(reconnect:))
98 return ![connection isConnected];
99
100 return [[self window] validateUserInterfaceItem:anItem];
101 }
102
103 /**
104 * Shows the inspector window
105 */
106 - (IBAction)showInspectorWindow:(id)sender
107 {
108 if (![inspector isVisible])
109 [inspector makeKeyAndOrderFront:sender];
110 else
111 [inspector orderOut:sender];
112 }
113
114 /**
115 * Resets all the displays to be empty
116 */
117 - (void)resetDisplays
118 {
119 [variablesTreeController setContent:nil];
120 [stackController.stack removeAllObjects];
121 [stackArrayController rearrangeObjects];
122 [[sourceViewer textView] setString:@""];
123 sourceViewer.file = nil;
124 }
125
126 /**
127 * Sets the status to be "Error" and then displays the error message
128 */
129 - (void)setError:(NSString*)anError
130 {
131 [errormsg setStringValue:anError];
132 [errormsg setHidden:NO];
133 }
134
135 /**
136 * Handles a GDBpConnection error
137 */
138 - (void)errorEncountered:(NSString*)error
139 {
140 [self setError:error];
141 }
142
143 /**
144 * Delegate functioni for GDBpConnection for when the debugger connects.
145 */
146 - (void)debuggerConnected
147 {
148 [self startDebugger];
149 }
150
151 /**
152 * Called once the socket accepts and MacGDBp is connected to the debugger
153 */
154 - (void)startDebugger
155 {
156 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"BreakOnFirstLine"])
157 [self stepIn:self];
158 }
159
160 /**
161 * Called once the debugger disconnects.
162 */
163 - (void)debuggerDisconnected
164 {
165 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"AutoReconnect"])
166 [self reconnect:self];
167
168 // Invalidate the marked line so we don't look like we're still running.
169 sourceViewer.markedLine = -1;
170 [sourceViewer setNeedsDisplay:YES];
171 }
172
173 /**
174 * Forwards the message to run script execution to the connection
175 */
176 - (IBAction)run:(id)sender
177 {
178 [connection run];
179 if ([connection isConnected])
180 [self reloadStack];
181 }
182
183 /**
184 * Tells the connection to ask the server to reconnect
185 */
186 - (IBAction)reconnect:(id)sender
187 {
188 [connection reconnect];
189 [self resetDisplays];
190 }
191
192 /**
193 * Forwards the message to "step in" to the connection
194 */
195 - (IBAction)stepIn:(id)sender
196 {
197 if ([[variablesTreeController selectedObjects] count] > 0)
198 selectedVariable = [[variablesTreeController selectedObjects] objectAtIndex:0];
199
200 [connection stepIn];
201 if ([connection isConnected])
202 [self reloadStack];
203 }
204
205 /**
206 * Forwards the message to "step out" to the connection
207 */
208 - (IBAction)stepOut:(id)sender
209 {
210 if ([[variablesTreeController selectedObjects] count] > 0)
211 selectedVariable = [[variablesTreeController selectedObjects] objectAtIndex:0];
212
213 [connection stepOut];
214 if ([connection isConnected])
215 [self reloadStack];
216 }
217
218 /**
219 * Forwards the message to "step over" to the connection
220 */
221 - (IBAction)stepOver:(id)sender
222 {
223 if ([[variablesTreeController selectedObjects] count] > 0)
224 selectedVariable = [[variablesTreeController selectedObjects] objectAtIndex:0];
225
226 [connection stepOver];
227 if ([connection isConnected])
228 [self reloadStack];
229 }
230
231 /**
232 * NSTableView delegate method that informs the controller that the stack selection did change and that
233 * we should update the source viewer
234 */
235 - (void)tableViewSelectionDidChange:(NSNotification*)notif
236 {
237 [self updateSourceViewer];
238 [self expandVariables];
239 }
240
241 /**
242 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
243 */
244 - (void)outlineViewItemDidExpand:(NSNotification*)notif
245 {
246 NSTreeNode* node = [[notif userInfo] objectForKey:@"NSObject"];
247 [expandedVariables addObject:[[node representedObject] fullname]];
248 }
249
250 /**
251 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
252 */
253 - (void)outlineViewItemDidCollapse:(NSNotification*)notif
254 {
255 [expandedVariables removeObject:[[[[notif userInfo] objectForKey:@"NSObject"] representedObject] fullname]];
256 }
257
258 #pragma mark Private
259
260 /**
261 * Does the actual updating of the source viewer by reading in the file
262 */
263 - (void)updateSourceViewer
264 {
265 id selection = [stackArrayController selection];
266 if ([selection valueForKey:@"filename"] == NSNoSelectionMarker)
267 return;
268
269 // get the filename
270 NSString* filename = [selection valueForKey:@"filename"];
271 filename = [[NSURL URLWithString:filename] path];
272 if ([filename isEqualToString:@""])
273 return;
274
275 // replace the source if necessary
276 if (![sourceViewer.file isEqualToString:filename])
277 {
278 NSString* source = [selection valueForKey:@"source"];
279 [sourceViewer setString:source asFile:filename];
280
281 NSSet* breakpoints = [NSSet setWithArray:[[BreakpointManager sharedManager] breakpointsForFile:filename]];
282 [[sourceViewer numberView] setMarkers:breakpoints];
283 }
284
285 int line = [[selection valueForKey:@"lineNumber"] intValue];
286 [sourceViewer setMarkedLine:line];
287 [sourceViewer scrollToLine:line];
288
289 [[sourceViewer textView] display];
290 }
291
292 /**
293 * Does some house keeping to the stack viewer
294 */
295 - (void)updateStackViewer
296 {
297 [stackArrayController rearrangeObjects];
298 [stackArrayController setSelectionIndex:0];
299 [self expandVariables];
300 }
301
302 /**
303 * Expands the variables based on the stored set
304 */
305 - (void)expandVariables
306 {
307 NSString* selection = [selectedVariable fullname];
308
309 for (int i = 0; i < [variablesOutlineView numberOfRows]; i++)
310 {
311 NSTreeNode* node = [variablesOutlineView itemAtRow:i];
312 NSString* fullname = [[node representedObject] fullname];
313
314 // see if it needs expanding
315 if ([expandedVariables containsObject:fullname])
316 [variablesOutlineView expandItem:node];
317
318 // select it if we had it selected before
319 if ([fullname isEqualToString:selection])
320 [variablesTreeController setSelectionIndexPath:[node indexPath]];
321 }
322 }
323
324 /**
325 * This updates the entire stack. Xdebug is queried to get the stack, non-shifted
326 * frames are reused and new ones are fetched.
327 */
328 - (void)reloadStack
329 {
330 NSArray* stack = [connection getCurrentStack];
331 if (stack == nil)
332 return;
333
334 [stackController.stack removeAllObjects];
335 [stackController.stack addObjectsFromArray:stack];
336 [self updateStackViewer];
337 [self updateSourceViewer];
338 }
339
340 #pragma mark BSSourceView Delegate
341
342 /**
343 * The gutter was clicked, which indicates that a breakpoint needs to be changed
344 */
345 - (void)gutterClickedAtLine:(int)line forFile:(NSString*)file
346 {
347 BreakpointManager* mngr = [BreakpointManager sharedManager];
348
349 if ([mngr hasBreakpointAt:line inFile:file])
350 {
351 [mngr removeBreakpointAt:line inFile:file];
352 }
353 else
354 {
355 Breakpoint* bp = [[Breakpoint alloc] initWithLine:line inFile:file];
356 [mngr addBreakpoint:bp];
357 [bp release];
358 }
359
360 [[sourceViewer numberView] setMarkers:[NSSet setWithArray:[mngr breakpointsForFile:file]]];
361 [[sourceViewer numberView] setNeedsDisplay:YES];
362 }
363
364 @end