Fix warnings about GDBpConnectionDelegate by including the header in DebuggerControll...
[macgdbp.git] / Source / DebuggerController.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2009, 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 - (void)reloadStack;
27 @end
28
29 @implementation DebuggerController
30
31 @synthesize connection, sourceViewer, inspector;
32
33 /**
34 * Initializes the window controller and sets the connection using preference
35 * values
36 */
37 - (id)init
38 {
39 if (self = [super initWithWindowNibName:@"Debugger"])
40 {
41 stackController = [[StackController alloc] init];
42
43 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
44 connection = [[GDBpConnection alloc] initWithPort:[defaults integerForKey:@"Port"] session:[defaults stringForKey:@"IDEKey"]];
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], [connection session]]];
74 [sourceViewer setDelegate:self];
75 [stackArrayController setSortDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"index" ascending:YES] autorelease]]];
76 }
77
78 /**
79 * Called right before the window closes so that we can tell the socket to close down
80 */
81 - (void)windowWillClose:(NSNotification*)notif
82 {
83 [[connection socket] close];
84 }
85
86 /**
87 * Validates the menu items for the "Debugger" menu
88 */
89 - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem
90 {
91 SEL action = [anItem action];
92
93 if (action == @selector(stepOut:))
94 return ([connection isConnected] && [stackController.stack count] > 1);
95 else if (action == @selector(stepIn:) || action == @selector(stepOver:) || action == @selector(run:))
96 return [connection isConnected];
97 else if (action == @selector(reconnect:))
98 return ![connection isConnected];
99
100 return [[self window] validateUserInterfaceItem:anItem];
101 }
102
103 /**
104 * Shows the inspector window
105 */
106 - (IBAction)showInspectorWindow:(id)sender
107 {
108 if (![inspector isVisible])
109 [inspector makeKeyAndOrderFront:sender];
110 else
111 [inspector orderOut:sender];
112 }
113
114 /**
115 * Resets all the displays to be empty
116 */
117 - (void)resetDisplays
118 {
119 [variablesTreeController setContent:nil];
120 [stackController.stack removeAllObjects];
121 [stackArrayController rearrangeObjects];
122 [[sourceViewer textView] setString:@""];
123 sourceViewer.file = nil;
124 }
125
126 /**
127 * Sets the status to be "Error" and then displays the error message
128 */
129 - (void)setError:(NSString*)anError
130 {
131 [errormsg setStringValue:anError];
132 [errormsg setHidden:NO];
133 }
134
135 /**
136 * Handles a GDBpConnection error
137 */
138 - (void)errorEncountered:(NSString*)error
139 {
140 [self setError:error];
141 }
142
143 /**
144 * Delegate functioni for GDBpConnection for when the debugger connects.
145 */
146 - (void)debuggerConnected
147 {
148 [self startDebugger];
149 }
150
151 /**
152 * Called once the socket accepts and MacGDBp is connected to the debugger
153 */
154 - (void)startDebugger
155 {
156 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"BreakOnFirstLine"])
157 [self stepIn:self];
158 }
159
160 /**
161 * Called once the debugger disconnects.
162 */
163 - (void)debuggerDisconnected
164 {
165 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"AutoReconnect"])
166 [self reconnect:self];
167 }
168
169 /**
170 * Forwards the message to run script execution to the connection
171 */
172 - (IBAction)run:(id)sender
173 {
174 [connection run];
175 if ([connection isConnected])
176 [self reloadStack];
177 }
178
179 /**
180 * Tells the connection to ask the server to reconnect
181 */
182 - (IBAction)reconnect:(id)sender
183 {
184 [connection reconnect];
185 [self resetDisplays];
186 }
187
188 /**
189 * Forwards the message to "step in" to the connection
190 */
191 - (IBAction)stepIn:(id)sender
192 {
193 if ([[variablesTreeController selectedObjects] count] > 0)
194 selectedVariable = [[variablesTreeController selectedObjects] objectAtIndex:0];
195
196 [connection stepIn];
197 if ([connection isConnected])
198 [self reloadStack];
199 }
200
201 /**
202 * Forwards the message to "step out" to the connection
203 */
204 - (IBAction)stepOut:(id)sender
205 {
206 if ([[variablesTreeController selectedObjects] count] > 0)
207 selectedVariable = [[variablesTreeController selectedObjects] objectAtIndex:0];
208
209 [connection stepOut];
210 if ([connection isConnected])
211 [self reloadStack];
212 }
213
214 /**
215 * Forwards the message to "step over" to the connection
216 */
217 - (IBAction)stepOver:(id)sender
218 {
219 if ([[variablesTreeController selectedObjects] count] > 0)
220 selectedVariable = [[variablesTreeController selectedObjects] objectAtIndex:0];
221
222 [connection stepOver];
223 if ([connection isConnected])
224 [self reloadStack];
225 }
226
227 /**
228 * NSTableView delegate method that informs the controller that the stack selection did change and that
229 * we should update the source viewer
230 */
231 - (void)tableViewSelectionDidChange:(NSNotification*)notif
232 {
233 [self updateSourceViewer];
234 [self expandVariables];
235 }
236
237 /**
238 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
239 */
240 - (void)outlineViewItemDidExpand:(NSNotification*)notif
241 {
242 NSTreeNode* node = [[notif userInfo] objectForKey:@"NSObject"];
243 [expandedVariables addObject:[[node representedObject] fullname]];
244 }
245
246 /**
247 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
248 */
249 - (void)outlineViewItemDidCollapse:(NSNotification*)notif
250 {
251 [expandedVariables removeObject:[[[[notif userInfo] objectForKey:@"NSObject"] representedObject] fullname]];
252 }
253
254 #pragma mark Private
255
256 /**
257 * Does the actual updating of the source viewer by reading in the file
258 */
259 - (void)updateSourceViewer
260 {
261 id selection = [stackArrayController selection];
262 if ([selection valueForKey:@"filename"] == NSNoSelectionMarker)
263 return;
264
265 // get the filename
266 NSString* filename = [selection valueForKey:@"filename"];
267 filename = [[NSURL URLWithString:filename] path];
268 if ([filename isEqualToString:@""])
269 return;
270
271 // replace the source if necessary
272 if (![sourceViewer.file isEqualToString:filename])
273 {
274 NSString* source = [selection valueForKey:@"source"];
275 [sourceViewer setString:source asFile:filename];
276
277 NSSet* breakpoints = [NSSet setWithArray:[[BreakpointManager sharedManager] breakpointsForFile:filename]];
278 [[sourceViewer numberView] setMarkers:breakpoints];
279 }
280
281 int line = [[selection valueForKey:@"lineNumber"] intValue];
282 [sourceViewer setMarkedLine:line];
283 [sourceViewer scrollToLine:line];
284
285 [[sourceViewer textView] display];
286 }
287
288 /**
289 * Does some house keeping to the stack viewer
290 */
291 - (void)updateStackViewer
292 {
293 [stackArrayController rearrangeObjects];
294 [stackArrayController setSelectionIndex:0];
295 [self expandVariables];
296 }
297
298 /**
299 * Expands the variables based on the stored set
300 */
301 - (void)expandVariables
302 {
303 NSString* selection = [selectedVariable fullname];
304
305 for (int i = 0; i < [variablesOutlineView numberOfRows]; i++)
306 {
307 NSTreeNode* node = [variablesOutlineView itemAtRow:i];
308 NSString* fullname = [[node representedObject] fullname];
309
310 // see if it needs expanding
311 if ([expandedVariables containsObject:fullname])
312 [variablesOutlineView expandItem:node];
313
314 // select it if we had it selected before
315 if ([fullname isEqualToString:selection])
316 [variablesTreeController setSelectionIndexPath:[node indexPath]];
317 }
318 }
319
320 /**
321 * This updates the entire stack. Xdebug is queried to get the stack, non-shifted
322 * frames are reused and new ones are fetched.
323 */
324 - (void)reloadStack
325 {
326 NSArray* stack = [connection getCurrentStack];
327 if (stack == nil)
328 return;
329
330 [stackController.stack removeAllObjects];
331 [stackController.stack addObjectsFromArray:stack];
332 [self updateStackViewer];
333 [self updateSourceViewer];
334 }
335
336 #pragma mark BSSourceView Delegate
337
338 /**
339 * The gutter was clicked, which indicates that a breakpoint needs to be changed
340 */
341 - (void)gutterClickedAtLine:(int)line forFile:(NSString*)file
342 {
343 BreakpointManager* mngr = [BreakpointManager sharedManager];
344
345 if ([mngr hasBreakpointAt:line inFile:file])
346 {
347 [mngr removeBreakpointAt:line inFile:file];
348 }
349 else
350 {
351 Breakpoint* bp = [[Breakpoint alloc] initWithLine:line inFile:file];
352 [mngr addBreakpoint:bp];
353 [bp release];
354 }
355
356 [[sourceViewer numberView] setMarkers:[NSSet setWithArray:[mngr breakpointsForFile:file]]];
357 [[sourceViewer numberView] setNeedsDisplay:YES];
358 }
359
360 @end