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