Remove two obsolete methods: |-[DebuggerConnection getCurrentStack]| and |-[DebuggerC...
[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 connection = [[DebuggerConnection alloc] initWithPort:[defaults integerForKey:@"Port"]];
44 connection.delegate = self;
45 expandedVariables = [[NSMutableSet alloc] init];
46 [[self window] makeKeyAndOrderFront:nil];
47 [[self window] setDelegate:self];
48
49 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"InspectorWindowVisible"])
50 [inspector orderFront:self];
51 }
52 return self;
53 }
54
55 /**
56 * Dealloc
57 */
58 - (void)dealloc
59 {
60 [connection release];
61 [expandedVariables release];
62 [stackController release];
63 [super dealloc];
64 }
65
66 /**
67 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
68 */
69 - (void)awakeFromNib
70 {
71 [[self window] setExcludedFromWindowsMenu:YES];
72 [[self window] setTitle:[NSString stringWithFormat:@"GDBp @ %@:%d", [connection remoteHost], [connection port]]];
73 [sourceViewer setDelegate:self];
74 [stackArrayController setSortDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"index" ascending:YES] autorelease]]];
75 }
76
77 /**
78 * Validates the menu items for the "Debugger" menu
79 */
80 - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem
81 {
82 SEL action = [anItem action];
83
84 if (action == @selector(stepOut:))
85 return ([connection isConnected] && [stackController.stack count] > 1);
86 else if (action == @selector(stepIn:) || action == @selector(stepOver:) || action == @selector(run:))
87 return [connection isConnected];
88 else if (action == @selector(reconnect:))
89 return ![connection isConnected];
90
91 return [[self window] validateUserInterfaceItem:anItem];
92 }
93
94 /**
95 * Shows the inspector window
96 */
97 - (IBAction)showInspectorWindow:(id)sender
98 {
99 if (![inspector isVisible])
100 [inspector makeKeyAndOrderFront:sender];
101 else
102 [inspector orderOut:sender];
103 }
104
105 /**
106 * Resets all the displays to be empty
107 */
108 - (void)resetDisplays
109 {
110 [variablesTreeController setContent:nil];
111 [stackController.stack removeAllObjects];
112 [stackArrayController rearrangeObjects];
113 [[sourceViewer textView] setString:@""];
114 sourceViewer.file = nil;
115 }
116
117 /**
118 * Sets the status to be "Error" and then displays the error message
119 */
120 - (void)setError:(NSString*)anError
121 {
122 [errormsg setStringValue:anError];
123 [errormsg setHidden:NO];
124 }
125
126 /**
127 * Handles a GDBpConnection error
128 */
129 - (void)errorEncountered:(NSString*)error
130 {
131 [self setError:error];
132 }
133
134 /**
135 * Delegate function for GDBpConnection for when the debugger connects.
136 */
137 - (void)debuggerConnected
138 {
139 [self startDebugger];
140 }
141
142 /**
143 * Called once the socket accepts and MacGDBp is connected to the debugger
144 */
145 - (void)startDebugger
146 {
147 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"BreakOnFirstLine"])
148 [self stepIn:self];
149 }
150
151 /**
152 * Called once the debugger disconnects.
153 */
154 - (void)debuggerDisconnected
155 {
156 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"AutoReconnect"])
157 [self reconnect:self];
158
159 // Invalidate the marked line so we don't look like we're still running.
160 sourceViewer.markedLine = -1;
161 [sourceViewer setNeedsDisplay:YES];
162 }
163
164 /**
165 * Forwards the message to run script execution to the connection
166 */
167 - (IBAction)run:(id)sender
168 {
169 [connection run];
170 }
171
172 /**
173 * Tells the connection to ask the server to reconnect
174 */
175 - (IBAction)reconnect:(id)sender
176 {
177 [connection reconnect];
178 [self resetDisplays];
179 }
180
181 /**
182 * Forwards the message to "step in" to the connection
183 */
184 - (IBAction)stepIn:(id)sender
185 {
186 if ([[variablesTreeController selectedObjects] count] > 0)
187 selectedVariable = [[variablesTreeController selectedObjects] objectAtIndex:0];
188
189 [connection stepIn];
190 }
191
192 /**
193 * Forwards the message to "step out" to the connection
194 */
195 - (IBAction)stepOut:(id)sender
196 {
197 if ([[variablesTreeController selectedObjects] count] > 0)
198 selectedVariable = [[variablesTreeController selectedObjects] objectAtIndex:0];
199
200 [connection stepOut];
201 }
202
203 /**
204 * Forwards the message to "step over" to the connection
205 */
206 - (IBAction)stepOver:(id)sender
207 {
208 if ([[variablesTreeController selectedObjects] count] > 0)
209 selectedVariable = [[variablesTreeController selectedObjects] objectAtIndex:0];
210
211 [connection stepOver];
212 }
213
214 /**
215 * NSTableView delegate method that informs the controller that the stack selection did change and that
216 * we should update the source viewer
217 */
218 - (void)tableViewSelectionDidChange:(NSNotification*)notif
219 {
220 [self updateSourceViewer];
221 [self expandVariables];
222 }
223
224 /**
225 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
226 */
227 - (void)outlineViewItemDidExpand:(NSNotification*)notif
228 {
229 NSTreeNode* node = [[notif userInfo] objectForKey:@"NSObject"];
230 [expandedVariables addObject:[[node representedObject] fullname]];
231 }
232
233 /**
234 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
235 */
236 - (void)outlineViewItemDidCollapse:(NSNotification*)notif
237 {
238 [expandedVariables removeObject:[[[[notif userInfo] objectForKey:@"NSObject"] representedObject] fullname]];
239 }
240
241 #pragma mark Private
242
243 /**
244 * Does the actual updating of the source viewer by reading in the file
245 */
246 - (void)updateSourceViewer
247 {
248 NSArray* selection = [stackArrayController selectedObjects];
249 if (!selection || [selection count] < 1)
250 return;
251 if ([selection count] > 1)
252 NSLog(@"INVALID SELECTION");
253 StackFrame* frame = [selection objectAtIndex:0];
254
255 // Get the filename.
256 NSString* filename = [[NSURL URLWithString:frame.filename] path];
257 if ([filename isEqualToString:@""])
258 return;
259
260 // Replace the source if necessary.
261 if (frame.source && ![sourceViewer.file isEqualToString:filename])
262 {
263 [sourceViewer setString:frame.source asFile:filename];
264
265 NSSet* breakpoints = [NSSet setWithArray:[[BreakpointManager sharedManager] breakpointsForFile:filename]];
266 [[sourceViewer numberView] setMarkers:breakpoints];
267 }
268
269 [sourceViewer setMarkedLine:frame.lineNumber];
270 [sourceViewer scrollToLine:frame.lineNumber];
271
272 [[sourceViewer textView] display];
273 }
274
275 /**
276 * Does some house keeping to the stack viewer
277 */
278 - (void)updateStackViewer
279 {
280 [stackArrayController rearrangeObjects];
281 [stackArrayController setSelectionIndex:0];
282 [self expandVariables];
283 }
284
285 /**
286 * Expands the variables based on the stored set
287 */
288 - (void)expandVariables
289 {
290 NSString* selection = [selectedVariable fullname];
291
292 for (int i = 0; i < [variablesOutlineView numberOfRows]; i++)
293 {
294 NSTreeNode* node = [variablesOutlineView itemAtRow:i];
295 NSString* fullname = [[node representedObject] fullname];
296
297 // see if it needs expanding
298 if ([expandedVariables containsObject:fullname])
299 [variablesOutlineView expandItem:node];
300
301 // select it if we had it selected before
302 if ([fullname isEqualToString:selection])
303 [variablesTreeController setSelectionIndexPath:[node indexPath]];
304 }
305 }
306
307 #pragma mark BSSourceView Delegate
308
309 /**
310 * The gutter was clicked, which indicates that a breakpoint needs to be changed
311 */
312 - (void)gutterClickedAtLine:(int)line forFile:(NSString*)file
313 {
314 BreakpointManager* mngr = [BreakpointManager sharedManager];
315
316 if ([mngr hasBreakpointAt:line inFile:file])
317 {
318 [mngr removeBreakpointAt:line inFile:file];
319 }
320 else
321 {
322 Breakpoint* bp = [[Breakpoint alloc] initWithLine:line inFile:file];
323 [mngr addBreakpoint:bp];
324 [bp release];
325 }
326
327 [[sourceViewer numberView] setMarkers:[NSSet setWithArray:[mngr breakpointsForFile:file]]];
328 [[sourceViewer numberView] setNeedsDisplay:YES];
329 }
330
331 #pragma mark GDBpConnectionDelegate
332
333 - (void)clobberStack
334 {
335 aboutToClobber_ = YES;
336 }
337
338 - (void)newStackFrame:(StackFrame*)frame
339 {
340 if (aboutToClobber_)
341 {
342 [stackController.stack removeAllObjects];
343 aboutToClobber_ = NO;
344 }
345 [stackController push:frame];
346 [self updateStackViewer];
347 [self updateSourceViewer];
348 }
349
350 - (void)sourceUpdated:(StackFrame*)frame
351 {
352 [self updateSourceViewer];
353 }
354
355 @end