Update the bindings for the stack viewer to use our new StackController system
[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 [stackArrayController setSortDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"index" ascending:YES] autorelease]]];
72 }
73
74 /**
75 * Called right before the window closes so that we can tell the socket to close down
76 */
77 - (void)windowWillClose:(NSNotification *)notif
78 {
79 [[connection socket] close];
80 }
81
82 /**
83 * Validates the menu items for the "Debugger" menu
84 */
85 - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem
86 {
87 SEL action = [anItem action];
88
89 if (action == @selector(stepOut:))
90 return ([connection isConnected] && [stack count] > 1);
91 else if (action == @selector(stepIn:) || action == @selector(stepOver:) || action == @selector(run:))
92 return [connection isConnected];
93
94 return [[self window] validateUserInterfaceItem:anItem];
95 }
96
97 /**
98 * Resets all the displays to be empty
99 */
100 - (void)resetDisplays
101 {
102 [registerController setContent:nil];
103 [stackController2 setContent:nil];
104 [[sourceViewer textView] setString:@""];
105 }
106
107 /**
108 * Sets the status and clears any error message
109 */
110 - (void)setStatus:(NSString *)aStatus
111 {
112 [errormsg setHidden:YES];
113 [statusmsg setStringValue:aStatus];
114 [[self window] setTitle:[NSString stringWithFormat:@"GDBp @ %@:%d/%@", [connection remoteHost], [connection port], [connection session]]];
115
116 [stepInButton setEnabled:NO];
117 [stepOutButton setEnabled:NO];
118 [stepOverButton setEnabled:NO];
119 [runButton setEnabled:NO];
120 [reconnectButton setEnabled:NO];
121
122 if ([connection isConnected])
123 {
124 if ([aStatus isEqualToString:@"Starting"])
125 {
126 [stepInButton setEnabled:YES];
127 [runButton setEnabled:YES];
128 }
129 }
130 else
131 {
132 [reconnectButton setEnabled:YES];
133 }
134 }
135
136 /**
137 * Sets the status to be "Error" and then displays the error message
138 */
139 - (void)setError:(NSString *)anError
140 {
141 [errormsg setStringValue:anError];
142 [self setStatus:@"Error"];
143 [errormsg setHidden:NO];
144 }
145
146 /**
147 * Sets the root node element of the stacktrace
148 */
149 - (void)setStack:(NSArray *)node
150 {
151 stack = node;
152
153 if ([stack count] > 1)
154 {
155 [stepOutButton setEnabled:YES];
156 }
157 [stepInButton setEnabled:YES];
158 [stepOverButton setEnabled:YES];
159 [runButton setEnabled:YES];
160
161 [self updateSourceViewer];
162 }
163
164 /**
165 * Sets the stack root element so that the NSOutlineView can display it
166 */
167 - (void)setRegister:(NSXMLDocument *)elm
168 {
169 // XXX: Doing anything short of this will cause bindings to crash spectacularly for no reason whatsoever, and
170 // in seemingly arbitrary places. The class that crashes is _NSKeyValueObservationInfoCreateByRemoving.
171 // http://boredzo.org/blog/archives/2006-01-29/have-you-seen-this-crash says that this means nothing is
172 // being observed, but I doubt that he was using an NSOutlineView which seems to be one f!cking piece of
173 // sh!t when used with NSTreeController. http://www.cocoadev.com/index.pl?NSTreeControllerBugOrDeveloperError
174 // was the inspiration for this fix (below) but the author says that inserting does not work too well, but
175 // that's okay for us as we just need to replace the entire thing.
176 [registerController setContent:nil];
177 [registerController setContent:[[elm rootElement] children]];
178
179 for (int i = 0; i < [registerView numberOfRows]; i++)
180 {
181 NSTreeNode *node = [registerView itemAtRow:i];
182 if ([expandedRegisters containsObject:[[node representedObject] fullname]])
183 {
184 [registerView expandItem:node];
185 }
186 }
187 }
188
189 /**
190 * Forwards the message to run script execution to the connection
191 */
192 - (IBAction)run:(id)sender
193 {
194 [connection run];
195 }
196
197 /**
198 * Tells the connection to ask the server to reconnect
199 */
200 - (IBAction)reconnect:(id)sender
201 {
202 [connection reconnect];
203 }
204
205 /**
206 * Forwards the message to "step in" to the connection
207 */
208 - (IBAction)stepIn:(id)sender
209 {
210 StackFrame *frame = [connection stepIn];
211 if ([frame isShiftedFrame:[stackController peek]])
212 [stackController pop];
213 [stackController push:frame];
214 [stackArrayController rearrangeObjects];
215 NSLog(@"stack = %@", stackController.stack);
216 }
217
218 /**
219 * Forwards the message to "step out" to the connection
220 */
221 - (IBAction)stepOut:(id)sender
222 {
223 StackFrame *frame = [connection stepOut];
224 [stackController pop]; // frame we were out of
225 [stackController pop]; // frame we are returning to
226 [stackController push:frame];
227 [stackArrayController rearrangeObjects];
228 NSLog(@"stack = %@", stackController.stack);
229 }
230
231 /**
232 * Forwards the message to "step over" to the connection
233 */
234 - (IBAction)stepOver:(id)sender
235 {
236 StackFrame *frame = [connection stepOver];
237
238 [stackController pop];
239 [stackController push:frame];
240 [stackArrayController rearrangeObjects];
241 NSLog(@"stack = %@", stackController.stack);
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 [self updateSourceViewer];
251 }
252
253 /**
254 * Does the actual updating of the source viewer by reading in the file
255 */
256 - (void)updateSourceViewer
257 {
258 id selectedLevel = [[stackController2 selection] valueForKey:@"level"];
259 if (selectedLevel == NSNoSelectionMarker)
260 {
261 [[sourceViewer textView] setString:@""];
262 return;
263 }
264 int selection = [selectedLevel intValue];
265
266 if ([stack count] < 1)
267 {
268 NSLog(@"huh... we don't have a stack");
269 return;
270 }
271
272 // get the filename and then set the text
273 NSString *filename = [[stack objectAtIndex:selection] valueForKey:@"filename"];
274 filename = [[NSURL URLWithString:filename] path];
275 if ([filename isEqualToString:@""])
276 {
277 return;
278 }
279
280 [sourceViewer setFile:filename];
281
282 int line = [[[stack objectAtIndex:selection] valueForKey:@"lineno"] intValue];
283 [sourceViewer setMarkedLine:line];
284 [sourceViewer scrollToLine:line];
285
286 // make sure the font stays Monaco
287 //[sourceViewer setFont:[NSFont fontWithName:@"Monaco" size:10.0]];
288 }
289
290 /**
291 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
292 */
293 - (void)outlineViewItemDidExpand:(NSNotification *)notif
294 {
295 NSTreeNode *node = [[notif userInfo] objectForKey:@"NSObject"];
296 [expandedRegisters addObject:[[node representedObject] fullname]];
297 }
298
299 /**
300 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
301 */
302 - (void)outlineViewItemDidCollapse:(NSNotification *)notif
303 {
304 [expandedRegisters removeObject:[[[[notif userInfo] objectForKey:@"NSObject"] representedObject] fullname]];
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 @end