Remove -[DebuggerBackEndDelegate errorEncountered:].
[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 * Delegate function for GDBpConnection for when the debugger connects.
170 */
171 - (void)debuggerConnected
172 {
173 [errormsg setHidden:YES];
174 if (!self.connection.autoAttach)
175 return;
176 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"BreakOnFirstLine"])
177 [self stepIn:self];
178 }
179
180 /**
181 * Called once the debugger disconnects.
182 */
183 - (void)debuggerDisconnected
184 {
185 // Invalidate the marked line so we don't look like we're still running.
186 sourceViewer.markedLine = -1;
187 [sourceViewer setNeedsDisplay:YES];
188 }
189
190 /**
191 * Forwards the message to run script execution to the connection
192 */
193 - (IBAction)run:(id)sender
194 {
195 [connection run];
196 }
197
198 - (IBAction)attachedToggled:(id)sender
199 {
200 connection.autoAttach = [sender state] == NSOnState;
201 }
202
203 /**
204 * Forwards the message to "step in" to the connection
205 */
206 - (IBAction)stepIn:(id)sender
207 {
208 if ([[variablesTreeController selectedObjects] count] > 0)
209 selectedVariable = [[variablesTreeController selectedObjects] objectAtIndex:0];
210
211 [connection stepIn];
212 }
213
214 /**
215 * Forwards the message to "step out" to the connection
216 */
217 - (IBAction)stepOut:(id)sender
218 {
219 if ([[variablesTreeController selectedObjects] count] > 0)
220 selectedVariable = [[variablesTreeController selectedObjects] objectAtIndex:0];
221
222 [connection stepOut];
223 }
224
225 /**
226 * Forwards the message to "step over" to the connection
227 */
228 - (IBAction)stepOver:(id)sender
229 {
230 if ([[variablesTreeController selectedObjects] count] > 0)
231 selectedVariable = [[variablesTreeController selectedObjects] objectAtIndex:0];
232
233 [connection stepOver];
234 }
235
236 /**
237 * Forwards the detach/"stop" message to the back end.
238 */
239 - (IBAction)stop:(id)sender
240 {
241 [connection stop];
242 }
243
244 /**
245 * NSTableView delegate method that informs the controller that the stack selection did change and that
246 * we should update the source viewer
247 */
248 - (void)tableViewSelectionDidChange:(NSNotification*)notif
249 {
250 // TODO: This is very, very hacky because it's nondeterministic. The issue
251 // is that calling |-[NSOutlineView expandItem:]| while the table is still
252 // doing its redraw will translate to a no-op. Instead, we need to restructure
253 // this controller so that when everything has been laid out we call
254 // |-expandVariables|; but NSOutlineView doesn't have a |-didFinishDoingCrap:|
255 // method. The other issue is that we need to call this method from
256 // selectionDidChange but ONLY when it was the result of a user-initiated
257 // action and not the stack viewer updating causing a selection change.
258 // If it happens in the latter, then we run into the same issue that causes
259 // this to no-op.
260 [self performSelector:@selector(expandVariables) withObject:nil afterDelay:0.05];
261 }
262
263 /**
264 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
265 */
266 - (void)outlineViewItemDidExpand:(NSNotification*)notif
267 {
268 NSTreeNode* node = [[notif userInfo] objectForKey:@"NSObject"];
269 [expandedVariables addObject:[[node representedObject] fullName]];
270
271 [connection loadVariableNode:[node representedObject]
272 forStackFrame:[[stackArrayController selectedObjects] lastObject]];
273 }
274
275 /**
276 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
277 */
278 - (void)outlineViewItemDidCollapse:(NSNotification*)notif
279 {
280 [expandedVariables removeObject:[[[[notif userInfo] objectForKey:@"NSObject"] representedObject] fullName]];
281 }
282
283 #pragma mark Private
284
285 /**
286 * Does the actual updating of the source viewer by reading in the file
287 */
288 - (void)updateSourceViewer
289 {
290 NSArray* selection = [stackArrayController selectedObjects];
291 if (!selection || [selection count] < 1)
292 return;
293 if ([selection count] > 1)
294 NSLog(@"INVALID SELECTION");
295 StackFrame* frame = [selection objectAtIndex:0];
296
297 if (!frame.loaded) {
298 [connection loadStackFrame:frame];
299 return;
300 }
301
302 // Get the filename.
303 NSString* filename = [[NSURL URLWithString:frame.filename] path];
304 if ([filename isEqualToString:@""])
305 return;
306
307 // Replace the source if necessary.
308 if (frame.source && ![sourceViewer.file isEqualToString:filename])
309 {
310 [sourceViewer setString:frame.source asFile:filename];
311
312 NSSet* breakpoints = [NSSet setWithArray:[[BreakpointManager sharedManager] breakpointsForFile:filename]];
313 [sourceViewer setMarkers:breakpoints];
314 }
315
316 [sourceViewer setMarkedLine:frame.lineNumber];
317 [sourceViewer scrollToLine:frame.lineNumber];
318
319 [[sourceViewer textView] display];
320 }
321
322 /**
323 * Expands the variables based on the stored set
324 */
325 - (void)expandVariables
326 {
327 NSString* selection = [selectedVariable fullName];
328
329 for (NSInteger i = 0; i < [variablesOutlineView numberOfRows]; i++) {
330 NSTreeNode* node = [variablesOutlineView itemAtRow:i];
331 NSString* fullName = [[node representedObject] fullName];
332
333 // see if it needs expanding
334 if ([expandedVariables containsObject:fullName])
335 [variablesOutlineView expandItem:node];
336
337 // select it if we had it selected before
338 if ([fullName isEqualToString:selection])
339 [variablesTreeController setSelectionIndexPath:[node indexPath]];
340 }
341 }
342
343 #pragma mark BSSourceView Delegate
344
345 /**
346 * The gutter was clicked, which indicates that a breakpoint needs to be changed
347 */
348 - (void)gutterClickedAtLine:(int)line forFile:(NSString*)file
349 {
350 BreakpointManager* mngr = [BreakpointManager sharedManager];
351
352 if ([mngr hasBreakpointAt:line inFile:file])
353 {
354 [mngr removeBreakpointAt:line inFile:file];
355 }
356 else
357 {
358 Breakpoint* bp = [[Breakpoint alloc] initWithLine:line inFile:file];
359 [mngr addBreakpoint:bp];
360 [bp release];
361 }
362
363 [sourceViewer setMarkers:[NSSet setWithArray:[mngr breakpointsForFile:file]]];
364 [sourceViewer setNeedsDisplay:YES];
365 }
366
367 @end