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