]> src.bluestatic.org Git - macgdbp.git/blob - Source/DebuggerController.m
Start using our new StackFrame and StackController classes as struts
[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];
223 [stackController push:frame];
224 NSLog(@"stack = %@", stackController.stack);
225 }
226
227 /**
228 * Forwards the message to "step over" to the connection
229 */
230 - (IBAction)stepOver:(id)sender
231 {
232 StackFrame *frame = [connection stepOver];
233
234 [stackController pop];
235 [stackController push:frame];
236
237 NSLog(@"stack = %@", stackController.stack);
238 }
239
240 /**
241 * NSTableView delegate method that informs the controller that the stack selection did change and that
242 * we should update the source viewer
243 */
244 - (void)tableViewSelectionDidChange:(NSNotification *)notif
245 {
246 [self updateSourceViewer];
247 }
248
249 /**
250 * Does the actual updating of the source viewer by reading in the file
251 */
252 - (void)updateSourceViewer
253 {
254 id selectedLevel = [[stackController2 selection] valueForKey:@"level"];
255 if (selectedLevel == NSNoSelectionMarker)
256 {
257 [[sourceViewer textView] setString:@""];
258 return;
259 }
260 int selection = [selectedLevel intValue];
261
262 if ([stack count] < 1)
263 {
264 NSLog(@"huh... we don't have a stack");
265 return;
266 }
267
268 // get the filename and then set the text
269 NSString *filename = [[stack objectAtIndex:selection] valueForKey:@"filename"];
270 filename = [[NSURL URLWithString:filename] path];
271 if ([filename isEqualToString:@""])
272 {
273 return;
274 }
275
276 [sourceViewer setFile:filename];
277
278 int line = [[[stack objectAtIndex:selection] valueForKey:@"lineno"] intValue];
279 [sourceViewer setMarkedLine:line];
280 [sourceViewer scrollToLine:line];
281
282 // make sure the font stays Monaco
283 //[sourceViewer setFont:[NSFont fontWithName:@"Monaco" size:10.0]];
284 }
285
286 /**
287 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
288 */
289 - (void)outlineViewItemDidExpand:(NSNotification *)notif
290 {
291 NSTreeNode *node = [[notif userInfo] objectForKey:@"NSObject"];
292 [expandedRegisters addObject:[[node representedObject] fullname]];
293 }
294
295 /**
296 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
297 */
298 - (void)outlineViewItemDidCollapse:(NSNotification *)notif
299 {
300 [expandedRegisters removeObject:[[[[notif userInfo] objectForKey:@"NSObject"] representedObject] fullname]];
301 }
302
303 #pragma mark BSSourceView Delegate
304
305 /**
306 * The gutter was clicked, which indicates that a breakpoint needs to be changed
307 */
308 - (void)gutterClickedAtLine:(int)line forFile:(NSString *)file
309 {
310 BreakpointManager *mngr = [BreakpointManager sharedManager];
311
312 if ([mngr hasBreakpointAt:line inFile:file])
313 {
314 [mngr removeBreakpointAt:line inFile:file];
315 }
316 else
317 {
318 Breakpoint *bp = [[Breakpoint alloc] initWithLine:line inFile:file];
319 [mngr addBreakpoint:bp];
320 [bp release];
321 }
322
323 [[sourceViewer numberView] setMarkers:[NSSet setWithArray:[mngr breakpointsForFile:file]]];
324 [[sourceViewer numberView] setNeedsDisplay:YES];
325 }
326
327 @end