Rename GDBpConnection to DebuggerConnection. Also organize all the ivars and methods.
[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 - (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 = [[DebuggerConnection 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 * 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 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 function 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 }
180
181 /**
182 * Tells the connection to ask the server to reconnect
183 */
184 - (IBAction)reconnect:(id)sender
185 {
186 [connection reconnect];
187 [self resetDisplays];
188 }
189
190 /**
191 * Forwards the message to "step in" to the connection
192 */
193 - (IBAction)stepIn:(id)sender
194 {
195 if ([[variablesTreeController selectedObjects] count] > 0)
196 selectedVariable = [[variablesTreeController selectedObjects] objectAtIndex:0];
197
198 [connection stepIn];
199 }
200
201 /**
202 * Forwards the message to "step out" to the connection
203 */
204 - (IBAction)stepOut:(id)sender
205 {
206 if ([[variablesTreeController selectedObjects] count] > 0)
207 selectedVariable = [[variablesTreeController selectedObjects] objectAtIndex:0];
208
209 [connection stepOut];
210 }
211
212 /**
213 * Forwards the message to "step over" to the connection
214 */
215 - (IBAction)stepOver:(id)sender
216 {
217 if ([[variablesTreeController selectedObjects] count] > 0)
218 selectedVariable = [[variablesTreeController selectedObjects] objectAtIndex:0];
219
220 [connection stepOver];
221 }
222
223 /**
224 * NSTableView delegate method that informs the controller that the stack selection did change and that
225 * we should update the source viewer
226 */
227 - (void)tableViewSelectionDidChange:(NSNotification*)notif
228 {
229 [self updateSourceViewer];
230 [self expandVariables];
231 }
232
233 /**
234 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
235 */
236 - (void)outlineViewItemDidExpand:(NSNotification*)notif
237 {
238 NSTreeNode* node = [[notif userInfo] objectForKey:@"NSObject"];
239 [expandedVariables addObject:[[node representedObject] fullname]];
240 }
241
242 /**
243 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
244 */
245 - (void)outlineViewItemDidCollapse:(NSNotification*)notif
246 {
247 [expandedVariables removeObject:[[[[notif userInfo] objectForKey:@"NSObject"] representedObject] fullname]];
248 }
249
250 #pragma mark Private
251
252 /**
253 * Does the actual updating of the source viewer by reading in the file
254 */
255 - (void)updateSourceViewer
256 {
257 NSArray* selection = [stackArrayController selectedObjects];
258 if (!selection || [selection count] < 1)
259 return;
260 if ([selection count] > 1)
261 NSLog(@"INVALID SELECTION");
262 StackFrame* frame = [selection objectAtIndex:0];
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 /**
317 * This updates the entire stack. Xdebug is queried to get the stack, non-shifted
318 * frames are reused and new ones are fetched.
319 */
320 - (void)reloadStack
321 {
322 NSArray* stack = [connection getCurrentStack];
323 if (stack == nil)
324 return;
325
326 [stackController.stack removeAllObjects];
327 [stackController.stack addObjectsFromArray:stack];
328 [self updateStackViewer];
329 [self updateSourceViewer];
330 }
331
332 #pragma mark BSSourceView Delegate
333
334 /**
335 * The gutter was clicked, which indicates that a breakpoint needs to be changed
336 */
337 - (void)gutterClickedAtLine:(int)line forFile:(NSString*)file
338 {
339 BreakpointManager* mngr = [BreakpointManager sharedManager];
340
341 if ([mngr hasBreakpointAt:line inFile:file])
342 {
343 [mngr removeBreakpointAt:line inFile:file];
344 }
345 else
346 {
347 Breakpoint* bp = [[Breakpoint alloc] initWithLine:line inFile:file];
348 [mngr addBreakpoint:bp];
349 [bp release];
350 }
351
352 [[sourceViewer numberView] setMarkers:[NSSet setWithArray:[mngr breakpointsForFile:file]]];
353 [[sourceViewer numberView] setNeedsDisplay:YES];
354 }
355
356 #pragma mark GDBpConnectionDelegate
357
358 - (void)clobberStack
359 {
360 aboutToClobber_ = YES;
361 }
362
363 - (void)newStackFrame:(StackFrame*)frame
364 {
365 if (aboutToClobber_)
366 {
367 [stackController.stack removeAllObjects];
368 aboutToClobber_ = NO;
369 }
370 [stackController push:frame];
371 [self updateStackViewer];
372 [self updateSourceViewer];
373 }
374
375 - (void)sourceUpdated:(StackFrame*)frame
376 {
377 [self updateSourceViewer];
378 }
379
380 @end