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