Remove DebuggerBackEndDelegate.
[macgdbp.git] / Source / DebuggerController.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2011, 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
19 #import "AppDelegate.h"
20 #import "BSSourceView.h"
21 #import "BreakpointManager.h"
22 #import "DebuggerBackEnd.h"
23 #import "DebuggerModel.h"
24 #import "EvalController.h"
25 #import "NSXMLElementAdditions.h"
26
27 @interface DebuggerController (Private)
28 - (void)updateSourceViewer;
29 - (void)expandVariables;
30 @end
31
32 @implementation DebuggerController
33
34 @synthesize connection, sourceViewer, inspector;
35
36 /**
37 * Initializes the window controller and sets the connection using preference
38 * values
39 */
40 - (id)init
41 {
42 if (self = [super initWithWindowNibName:@"Debugger"])
43 {
44 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
45
46 _model = [[DebuggerModel alloc] init];
47 [_model addObserver:self
48 forKeyPath:@"connected"
49 options:NSKeyValueObservingOptionNew
50 context:nil];
51
52 connection = [[DebuggerBackEnd alloc] initWithPort:[defaults integerForKey:@"Port"]];
53 connection.model = _model;
54 expandedVariables = [[NSMutableSet alloc] init];
55 [[self window] makeKeyAndOrderFront:nil];
56 [[self window] setDelegate:self];
57
58 if ([defaults boolForKey:@"InspectorWindowVisible"])
59 [inspector orderFront:self];
60 }
61 return self;
62 }
63
64 /**
65 * Dealloc
66 */
67 - (void)dealloc
68 {
69 [connection release];
70 [_model release];
71 [expandedVariables release];
72 [super dealloc];
73 }
74
75 /**
76 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
77 */
78 - (void)awakeFromNib
79 {
80 [[self window] setExcludedFromWindowsMenu:YES];
81 [[self window] setTitle:[NSString stringWithFormat:@"MacGDBp @ %d", [connection port]]];
82 [sourceViewer setDelegate:self];
83 [stackArrayController setSortDescriptors:@[ [[[NSSortDescriptor alloc] initWithKey:@"index" ascending:YES] autorelease] ]];
84 [stackArrayController addObserver:self
85 forKeyPath:@"selectedObjects"
86 options:NSKeyValueObservingOptionNew
87 context:nil];
88 [stackArrayController addObserver:self
89 forKeyPath:@"selection.source"
90 options:NSKeyValueObservingOptionNew
91 context:nil];
92 self.connection.autoAttach = [attachedCheckbox_ state] == NSOnState;
93 }
94
95 /**
96 * Key-value observation routine.
97 */
98 - (void)observeValueForKeyPath:(NSString*)keyPath
99 ofObject:(id)object
100 change:(NSDictionary<NSString*,id>*)change
101 context:(void*)context {
102 if (object == stackArrayController && [keyPath isEqualToString:@"selectedObjects"]) {
103 for (StackFrame* frame in stackArrayController.selectedObjects)
104 [connection loadStackFrame:frame];
105 } else if (object == stackArrayController && [keyPath isEqualToString:@"selection.source"]) {
106 [self updateSourceViewer];
107 } else if (object == _model && [keyPath isEqualToString:@"connected"]) {
108 if ([change[NSKeyValueChangeNewKey] boolValue])
109 [self debuggerConnected];
110 else
111 [self debuggerDisconnected];
112 } else {
113 [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
114 }
115 }
116
117 /**
118 * Validates the menu items for the "Debugger" menu
119 */
120 - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem
121 {
122 SEL action = [anItem action];
123
124 if (action == @selector(stepOut:)) {
125 return ([connection isConnected] && _model.stackDepth > 1);
126 } else if (action == @selector(stepIn:) ||
127 action == @selector(stepOver:) ||
128 action == @selector(run:) ||
129 action == @selector(stop:) ||
130 action == @selector(showEvalWindow:)) {
131 return [connection isConnected];
132 }
133 return [[self window] validateUserInterfaceItem:anItem];
134 }
135
136 /**
137 * Shows the inspector window
138 */
139 - (IBAction)showInspectorWindow:(id)sender
140 {
141 if (![inspector isVisible])
142 [inspector makeKeyAndOrderFront:sender];
143 else
144 [inspector orderOut:sender];
145 }
146
147 /**
148 * Runs the eval window sheet.
149 */
150 - (IBAction)showEvalWindow:(id)sender
151 {
152 // The |controller| will release itself on close.
153 EvalController* controller = [[EvalController alloc] initWithBackEnd:connection];
154 [controller runModalForWindow:[self window]];
155 }
156
157 /**
158 * Resets all the displays to be empty
159 */
160 - (void)resetDisplays
161 {
162 [variablesTreeController setContent:nil];
163 [stackArrayController rearrangeObjects];
164 [[sourceViewer textView] setString:@""];
165 sourceViewer.file = nil;
166 }
167
168 /**
169 * Sets the status to be "Error" and then displays the error message
170 */
171 - (void)setError:(NSString*)anError
172 {
173 [errormsg setStringValue:anError];
174 [errormsg setHidden:NO];
175 }
176
177 /**
178 * Delegate function for GDBpConnection for when the debugger connects.
179 */
180 - (void)debuggerConnected
181 {
182 [errormsg setHidden:YES];
183 if (!self.connection.autoAttach)
184 return;
185 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"BreakOnFirstLine"])
186 [self stepIn:self];
187 }
188
189 /**
190 * Called once the debugger disconnects.
191 */
192 - (void)debuggerDisconnected
193 {
194 // Invalidate the marked line so we don't look like we're still running.
195 sourceViewer.markedLine = -1;
196 [sourceViewer setNeedsDisplay:YES];
197 }
198
199 /**
200 * Forwards the message to run script execution to the connection
201 */
202 - (IBAction)run:(id)sender
203 {
204 [connection run];
205 }
206
207 - (IBAction)attachedToggled:(id)sender
208 {
209 connection.autoAttach = [sender state] == NSOnState;
210 }
211
212 /**
213 * Forwards the message to "step in" to the connection
214 */
215 - (IBAction)stepIn:(id)sender
216 {
217 if ([[variablesTreeController selectedObjects] count] > 0)
218 selectedVariable = [[variablesTreeController selectedObjects] objectAtIndex:0];
219
220 [connection stepIn];
221 }
222
223 /**
224 * Forwards the message to "step out" to the connection
225 */
226 - (IBAction)stepOut:(id)sender
227 {
228 if ([[variablesTreeController selectedObjects] count] > 0)
229 selectedVariable = [[variablesTreeController selectedObjects] objectAtIndex:0];
230
231 [connection stepOut];
232 }
233
234 /**
235 * Forwards the message to "step over" to the connection
236 */
237 - (IBAction)stepOver:(id)sender
238 {
239 if ([[variablesTreeController selectedObjects] count] > 0)
240 selectedVariable = [[variablesTreeController selectedObjects] objectAtIndex:0];
241
242 [connection stepOver];
243 }
244
245 /**
246 * Forwards the detach/"stop" message to the back end.
247 */
248 - (IBAction)stop:(id)sender
249 {
250 [connection stop];
251 }
252
253 /**
254 * NSTableView delegate method that informs the controller that the stack selection did change and that
255 * we should update the source viewer
256 */
257 - (void)tableViewSelectionDidChange:(NSNotification*)notif
258 {
259 // TODO: This is very, very hacky because it's nondeterministic. The issue
260 // is that calling |-[NSOutlineView expandItem:]| while the table is still
261 // doing its redraw will translate to a no-op. Instead, we need to restructure
262 // this controller so that when everything has been laid out we call
263 // |-expandVariables|; but NSOutlineView doesn't have a |-didFinishDoingCrap:|
264 // method. The other issue is that we need to call this method from
265 // selectionDidChange but ONLY when it was the result of a user-initiated
266 // action and not the stack viewer updating causing a selection change.
267 // If it happens in the latter, then we run into the same issue that causes
268 // this to no-op.
269 [self performSelector:@selector(expandVariables) withObject:nil afterDelay:0.05];
270 }
271
272 /**
273 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
274 */
275 - (void)outlineViewItemDidExpand:(NSNotification*)notif
276 {
277 NSTreeNode* node = [[notif userInfo] objectForKey:@"NSObject"];
278 [expandedVariables addObject:[[node representedObject] fullName]];
279
280 [connection loadVariableNode:[node representedObject]
281 forStackFrame:[[stackArrayController selectedObjects] lastObject]];
282 }
283
284 /**
285 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
286 */
287 - (void)outlineViewItemDidCollapse:(NSNotification*)notif
288 {
289 [expandedVariables removeObject:[[[[notif userInfo] objectForKey:@"NSObject"] representedObject] fullName]];
290 }
291
292 #pragma mark Private
293
294 /**
295 * Does the actual updating of the source viewer by reading in the file
296 */
297 - (void)updateSourceViewer
298 {
299 NSArray* selection = [stackArrayController selectedObjects];
300 if (!selection || [selection count] < 1)
301 return;
302 if ([selection count] > 1)
303 NSLog(@"INVALID SELECTION");
304 StackFrame* frame = [selection objectAtIndex:0];
305
306 if (!frame.loaded) {
307 [connection loadStackFrame:frame];
308 return;
309 }
310
311 // Get the filename.
312 NSString* filename = [[NSURL URLWithString:frame.filename] path];
313 if ([filename isEqualToString:@""])
314 return;
315
316 // Replace the source if necessary.
317 if (frame.source && ![sourceViewer.file isEqualToString:filename])
318 {
319 [sourceViewer setString:frame.source asFile:filename];
320
321 NSSet* breakpoints = [NSSet setWithArray:[[BreakpointManager sharedManager] breakpointsForFile:filename]];
322 [sourceViewer setMarkers:breakpoints];
323 }
324
325 [sourceViewer setMarkedLine:frame.lineNumber];
326 [sourceViewer scrollToLine:frame.lineNumber];
327
328 [[sourceViewer textView] display];
329 }
330
331 /**
332 * Expands the variables based on the stored set
333 */
334 - (void)expandVariables
335 {
336 NSString* selection = [selectedVariable fullName];
337
338 for (NSInteger i = 0; i < [variablesOutlineView numberOfRows]; i++) {
339 NSTreeNode* node = [variablesOutlineView itemAtRow:i];
340 NSString* fullName = [[node representedObject] fullName];
341
342 // see if it needs expanding
343 if ([expandedVariables containsObject:fullName])
344 [variablesOutlineView expandItem:node];
345
346 // select it if we had it selected before
347 if ([fullName isEqualToString:selection])
348 [variablesTreeController setSelectionIndexPath:[node indexPath]];
349 }
350 }
351
352 #pragma mark BSSourceView Delegate
353
354 /**
355 * The gutter was clicked, which indicates that a breakpoint needs to be changed
356 */
357 - (void)gutterClickedAtLine:(int)line forFile:(NSString*)file
358 {
359 BreakpointManager* mngr = [BreakpointManager sharedManager];
360
361 if ([mngr hasBreakpointAt:line inFile:file])
362 {
363 [mngr removeBreakpointAt:line inFile:file];
364 }
365 else
366 {
367 Breakpoint* bp = [[Breakpoint alloc] initWithLine:line inFile:file];
368 [mngr addBreakpoint:bp];
369 [bp release];
370 }
371
372 [sourceViewer setMarkers:[NSSet setWithArray:[mngr breakpointsForFile:file]]];
373 [sourceViewer setNeedsDisplay:YES];
374 }
375
376 @end