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