Rename DebuggerConnection to DebuggerProcessor in preparation for the separation...
[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 // Get the filename.
257 NSString* filename = [[NSURL URLWithString:frame.filename] path];
258 if ([filename isEqualToString:@""])
259 return;
260
261 // Replace the source if necessary.
262 if (frame.source && ![sourceViewer.file isEqualToString:filename])
263 {
264 [sourceViewer setString:frame.source asFile:filename];
265
266 NSSet* breakpoints = [NSSet setWithArray:[[BreakpointManager sharedManager] breakpointsForFile:filename]];
267 [[sourceViewer numberView] setMarkers:breakpoints];
268 }
269
270 [sourceViewer setMarkedLine:frame.lineNumber];
271 [sourceViewer scrollToLine:frame.lineNumber];
272
273 [[sourceViewer textView] display];
274 }
275
276 /**
277 * Does some house keeping to the stack viewer
278 */
279 - (void)updateStackViewer
280 {
281 [stackArrayController rearrangeObjects];
282 [stackArrayController setSelectionIndex:0];
283 [self expandVariables];
284 }
285
286 /**
287 * Expands the variables based on the stored set
288 */
289 - (void)expandVariables
290 {
291 NSString* selection = [selectedVariable fullname];
292
293 for (int i = 0; i < [variablesOutlineView numberOfRows]; i++)
294 {
295 NSTreeNode* node = [variablesOutlineView itemAtRow:i];
296 NSString* fullname = [[node representedObject] fullname];
297
298 // see if it needs expanding
299 if ([expandedVariables containsObject:fullname])
300 [variablesOutlineView expandItem:node];
301
302 // select it if we had it selected before
303 if ([fullname isEqualToString:selection])
304 [variablesTreeController setSelectionIndexPath:[node indexPath]];
305 }
306 }
307
308 #pragma mark BSSourceView Delegate
309
310 /**
311 * The gutter was clicked, which indicates that a breakpoint needs to be changed
312 */
313 - (void)gutterClickedAtLine:(int)line forFile:(NSString*)file
314 {
315 BreakpointManager* mngr = [BreakpointManager sharedManager];
316
317 if ([mngr hasBreakpointAt:line inFile:file])
318 {
319 [mngr removeBreakpointAt:line inFile:file];
320 }
321 else
322 {
323 Breakpoint* bp = [[Breakpoint alloc] initWithLine:line inFile:file];
324 [mngr addBreakpoint:bp];
325 [bp release];
326 }
327
328 [[sourceViewer numberView] setMarkers:[NSSet setWithArray:[mngr breakpointsForFile:file]]];
329 [[sourceViewer numberView] setNeedsDisplay:YES];
330 }
331
332 #pragma mark GDBpConnectionDelegate
333
334 - (void)clobberStack
335 {
336 aboutToClobber_ = YES;
337 }
338
339 - (void)newStackFrame:(StackFrame*)frame
340 {
341 if (aboutToClobber_)
342 {
343 [stackController.stack removeAllObjects];
344 aboutToClobber_ = NO;
345 }
346 [stackController push:frame];
347 [self updateStackViewer];
348 [self updateSourceViewer];
349 }
350
351 - (void)sourceUpdated:(StackFrame*)frame
352 {
353 [self updateSourceViewer];
354 }
355
356 @end