We need to pop the stack twice when stepOut is called
[macgdbp.git] / Source / DebuggerController.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2008, 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 "GDBpConnection.h"
19 #import "NSXMLElementAdditions.h"
20 #import "AppDelegate.h"
21 #import "BreakpointManager.h"
22
23 @interface DebuggerController (Private)
24 - (void)updateSourceViewer;
25 @end
26
27 @implementation DebuggerController
28
29 @synthesize connection, sourceViewer;
30
31 /**
32 * Initializes the window controller and sets the connection using preference
33 * values
34 */
35 - (id)init
36 {
37 if (self = [super initWithWindowNibName:@"Debugger"])
38 {
39 stackController = [[StackController alloc] init];
40
41 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
42 connection = [[GDBpConnection alloc] initWithWindowController:self
43 port:[defaults integerForKey:@"Port"]
44 session:[defaults stringForKey:@"IDEKey"]];
45 expandedRegisters = [[NSMutableSet alloc] init];
46 [[self window] makeKeyAndOrderFront:nil];
47 [[self window] setDelegate:self];
48 }
49 return self;
50 }
51
52 /**
53 * Dealloc
54 */
55 - (void)dealloc
56 {
57 [connection release];
58 [expandedRegisters release];
59 [stackController release];
60 [super dealloc];
61 }
62
63 /**
64 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
65 */
66 - (void)awakeFromNib
67 {
68 [self setStatus:@"Connecting"];
69 [[self window] setExcludedFromWindowsMenu:YES];
70 [sourceViewer setDelegate:self];
71 }
72
73 /**
74 * Called right before the window closes so that we can tell the socket to close down
75 */
76 - (void)windowWillClose:(NSNotification *)notif
77 {
78 [[connection socket] close];
79 }
80
81 /**
82 * Validates the menu items for the "Debugger" menu
83 */
84 - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem
85 {
86 SEL action = [anItem action];
87
88 if (action == @selector(stepOut:))
89 return ([connection isConnected] && [stack count] > 1);
90 else if (action == @selector(stepIn:) || action == @selector(stepOver:) || action == @selector(run:))
91 return [connection isConnected];
92
93 return [[self window] validateUserInterfaceItem:anItem];
94 }
95
96 /**
97 * Resets all the displays to be empty
98 */
99 - (void)resetDisplays
100 {
101 [registerController setContent:nil];
102 [stackController2 setContent:nil];
103 [[sourceViewer textView] setString:@""];
104 }
105
106 /**
107 * Sets the status and clears any error message
108 */
109 - (void)setStatus:(NSString *)aStatus
110 {
111 [errormsg setHidden:YES];
112 [statusmsg setStringValue:aStatus];
113 [[self window] setTitle:[NSString stringWithFormat:@"GDBp @ %@:%d/%@", [connection remoteHost], [connection port], [connection session]]];
114
115 [stepInButton setEnabled:NO];
116 [stepOutButton setEnabled:NO];
117 [stepOverButton setEnabled:NO];
118 [runButton setEnabled:NO];
119 [reconnectButton setEnabled:NO];
120
121 if ([connection isConnected])
122 {
123 if ([aStatus isEqualToString:@"Starting"])
124 {
125 [stepInButton setEnabled:YES];
126 [runButton setEnabled:YES];
127 }
128 }
129 else
130 {
131 [reconnectButton setEnabled:YES];
132 }
133 }
134
135 /**
136 * Sets the status to be "Error" and then displays the error message
137 */
138 - (void)setError:(NSString *)anError
139 {
140 [errormsg setStringValue:anError];
141 [self setStatus:@"Error"];
142 [errormsg setHidden:NO];
143 }
144
145 /**
146 * Sets the root node element of the stacktrace
147 */
148 - (void)setStack:(NSArray *)node
149 {
150 stack = node;
151
152 if ([stack count] > 1)
153 {
154 [stepOutButton setEnabled:YES];
155 }
156 [stepInButton setEnabled:YES];
157 [stepOverButton setEnabled:YES];
158 [runButton setEnabled:YES];
159
160 [self updateSourceViewer];
161 }
162
163 /**
164 * Sets the stack root element so that the NSOutlineView can display it
165 */
166 - (void)setRegister:(NSXMLDocument *)elm
167 {
168 // XXX: Doing anything short of this will cause bindings to crash spectacularly for no reason whatsoever, and
169 // in seemingly arbitrary places. The class that crashes is _NSKeyValueObservationInfoCreateByRemoving.
170 // http://boredzo.org/blog/archives/2006-01-29/have-you-seen-this-crash says that this means nothing is
171 // being observed, but I doubt that he was using an NSOutlineView which seems to be one f!cking piece of
172 // sh!t when used with NSTreeController. http://www.cocoadev.com/index.pl?NSTreeControllerBugOrDeveloperError
173 // was the inspiration for this fix (below) but the author says that inserting does not work too well, but
174 // that's okay for us as we just need to replace the entire thing.
175 [registerController setContent:nil];
176 [registerController setContent:[[elm rootElement] children]];
177
178 for (int i = 0; i < [registerView numberOfRows]; i++)
179 {
180 NSTreeNode *node = [registerView itemAtRow:i];
181 if ([expandedRegisters containsObject:[[node representedObject] fullname]])
182 {
183 [registerView expandItem:node];
184 }
185 }
186 }
187
188 /**
189 * Forwards the message to run script execution to the connection
190 */
191 - (IBAction)run:(id)sender
192 {
193 [connection run];
194 }
195
196 /**
197 * Tells the connection to ask the server to reconnect
198 */
199 - (IBAction)reconnect:(id)sender
200 {
201 [connection reconnect];
202 }
203
204 /**
205 * Forwards the message to "step in" to the connection
206 */
207 - (IBAction)stepIn:(id)sender
208 {
209 StackFrame *frame = [connection stepIn];
210 if ([frame isShiftedFrame:[stackController peek]])
211 [stackController pop];
212 [stackController push:frame];
213 NSLog(@"stack = %@", stackController.stack);
214 }
215
216 /**
217 * Forwards the message to "step out" to the connection
218 */
219 - (IBAction)stepOut:(id)sender
220 {
221 StackFrame *frame = [connection stepOut];
222 [stackController pop]; // frame we were out of
223 [stackController pop]; // frame we are returning to
224 [stackController push:frame];
225 NSLog(@"stack = %@", stackController.stack);
226 }
227
228 /**
229 * Forwards the message to "step over" to the connection
230 */
231 - (IBAction)stepOver:(id)sender
232 {
233 StackFrame *frame = [connection stepOver];
234
235 [stackController pop];
236 [stackController push:frame];
237
238 NSLog(@"stack = %@", stackController.stack);
239 }
240
241 /**
242 * NSTableView delegate method that informs the controller that the stack selection did change and that
243 * we should update the source viewer
244 */
245 - (void)tableViewSelectionDidChange:(NSNotification *)notif
246 {
247 [self updateSourceViewer];
248 }
249
250 /**
251 * Does the actual updating of the source viewer by reading in the file
252 */
253 - (void)updateSourceViewer
254 {
255 id selectedLevel = [[stackController2 selection] valueForKey:@"level"];
256 if (selectedLevel == NSNoSelectionMarker)
257 {
258 [[sourceViewer textView] setString:@""];
259 return;
260 }
261 int selection = [selectedLevel intValue];
262
263 if ([stack count] < 1)
264 {
265 NSLog(@"huh... we don't have a stack");
266 return;
267 }
268
269 // get the filename and then set the text
270 NSString *filename = [[stack objectAtIndex:selection] valueForKey:@"filename"];
271 filename = [[NSURL URLWithString:filename] path];
272 if ([filename isEqualToString:@""])
273 {
274 return;
275 }
276
277 [sourceViewer setFile:filename];
278
279 int line = [[[stack objectAtIndex:selection] valueForKey:@"lineno"] intValue];
280 [sourceViewer setMarkedLine:line];
281 [sourceViewer scrollToLine:line];
282
283 // make sure the font stays Monaco
284 //[sourceViewer setFont:[NSFont fontWithName:@"Monaco" size:10.0]];
285 }
286
287 /**
288 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
289 */
290 - (void)outlineViewItemDidExpand:(NSNotification *)notif
291 {
292 NSTreeNode *node = [[notif userInfo] objectForKey:@"NSObject"];
293 [expandedRegisters addObject:[[node representedObject] fullname]];
294 }
295
296 /**
297 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
298 */
299 - (void)outlineViewItemDidCollapse:(NSNotification *)notif
300 {
301 [expandedRegisters removeObject:[[[[notif userInfo] objectForKey:@"NSObject"] representedObject] fullname]];
302 }
303
304 #pragma mark BSSourceView Delegate
305
306 /**
307 * The gutter was clicked, which indicates that a breakpoint needs to be changed
308 */
309 - (void)gutterClickedAtLine:(int)line forFile:(NSString *)file
310 {
311 BreakpointManager *mngr = [BreakpointManager sharedManager];
312
313 if ([mngr hasBreakpointAt:line inFile:file])
314 {
315 [mngr removeBreakpointAt:line inFile:file];
316 }
317 else
318 {
319 Breakpoint *bp = [[Breakpoint alloc] initWithLine:line inFile:file];
320 [mngr addBreakpoint:bp];
321 [bp release];
322 }
323
324 [[sourceViewer numberView] setMarkers:[NSSet setWithArray:[mngr breakpointsForFile:file]]];
325 [[sourceViewer numberView] setNeedsDisplay:YES];
326 }
327
328 @end