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