Remove all references of the window controller from GDBpConnection
[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
100 return [[self window] validateUserInterfaceItem:anItem];
101 }
102
103 /**
104 * Resets all the displays to be empty
105 */
106 - (void)resetDisplays
107 {
108 [registerController setContent:nil];
109 [[sourceViewer textView] setString:@""];
110 }
111
112 /**
113 * Sets the status and clears any error message
114 */
115 - (void)setStatus:(NSString *)aStatus
116 {
117 [errormsg setHidden:YES];
118 [statusmsg setStringValue:aStatus];
119 [[self window] setTitle:[NSString stringWithFormat:@"GDBp @ %@:%d/%@", [connection remoteHost], [connection port], [connection session]]];
120
121 [stepInButton setEnabled:NO];
122 [stepOutButton setEnabled:NO];
123 [stepOverButton setEnabled:NO];
124 [runButton setEnabled:NO];
125 [reconnectButton setEnabled:NO];
126
127 if ([connection isConnected])
128 {
129 if ([aStatus isEqualToString:@"Starting"])
130 {
131 [stepInButton setEnabled:YES];
132 [runButton setEnabled:YES];
133 }
134 }
135 else
136 {
137 [reconnectButton setEnabled:YES];
138 }
139 }
140
141 /**
142 * Sets the status to be "Error" and then displays the error message
143 */
144 - (void)setError:(NSString *)anError
145 {
146 [errormsg setStringValue:anError];
147 [self setStatus:@"Error"];
148 [errormsg setHidden:NO];
149 }
150
151 /**
152 * Handles a GDBpConnection error
153 */
154 - (void)handleConnectionError:(NSNotification *)notif
155 {
156 [self setError:[[notif userInfo] valueForKey:@"NSString"]];
157 }
158
159 /**
160 * Sets the stack root element so that the NSOutlineView can display it
161 */
162 - (void)setRegister:(NSXMLDocument *)elm
163 {
164 // XXX: Doing anything short of this will cause bindings to crash spectacularly for no reason whatsoever, and
165 // in seemingly arbitrary places. The class that crashes is _NSKeyValueObservationInfoCreateByRemoving.
166 // http://boredzo.org/blog/archives/2006-01-29/have-you-seen-this-crash says that this means nothing is
167 // being observed, but I doubt that he was using an NSOutlineView which seems to be one f!cking piece of
168 // sh!t when used with NSTreeController. http://www.cocoadev.com/index.pl?NSTreeControllerBugOrDeveloperError
169 // was the inspiration for this fix (below) but the author says that inserting does not work too well, but
170 // that's okay for us as we just need to replace the entire thing.
171 [registerController setContent:nil];
172 [registerController setContent:[[elm rootElement] children]];
173
174 for (int i = 0; i < [registerView numberOfRows]; i++)
175 {
176 NSTreeNode *node = [registerView itemAtRow:i];
177 if ([expandedRegisters containsObject:[[node representedObject] fullname]])
178 {
179 [registerView expandItem:node];
180 }
181 }
182 }
183
184 /**
185 * Forwards the message to run script execution to the connection
186 */
187 - (IBAction)run:(id)sender
188 {
189 [connection run];
190 }
191
192 /**
193 * Tells the connection to ask the server to reconnect
194 */
195 - (IBAction)reconnect:(id)sender
196 {
197 [connection reconnect];
198 }
199
200 /**
201 * Forwards the message to "step in" to the connection
202 */
203 - (IBAction)stepIn:(id)sender
204 {
205 StackFrame *frame = [connection stepIn];
206 if ([frame isShiftedFrame:[stackController peek]])
207 [stackController pop];
208 [stackController push:frame];
209 [self updateStackViewer];
210 }
211
212 /**
213 * Forwards the message to "step out" to the connection
214 */
215 - (IBAction)stepOut:(id)sender
216 {
217 StackFrame *frame = [connection stepOut];
218 [stackController pop]; // frame we were out of
219 [stackController pop]; // frame we are returning to
220 [stackController push:frame];
221 [self updateStackViewer];
222 }
223
224 /**
225 * Forwards the message to "step over" to the connection
226 */
227 - (IBAction)stepOver:(id)sender
228 {
229 StackFrame *frame = [connection stepOver];
230 [stackController pop];
231 [stackController push:frame];
232 [self updateStackViewer];
233 }
234
235 /**
236 * NSTableView delegate method that informs the controller that the stack selection did change and that
237 * we should update the source viewer
238 */
239 - (void)tableViewSelectionDidChange:(NSNotification *)notif
240 {
241 [self updateSourceViewer];
242 }
243
244 /**
245 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
246 */
247 - (void)outlineViewItemDidExpand:(NSNotification *)notif
248 {
249 NSTreeNode *node = [[notif userInfo] objectForKey:@"NSObject"];
250 [expandedRegisters addObject:[[node representedObject] fullname]];
251 }
252
253 /**
254 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
255 */
256 - (void)outlineViewItemDidCollapse:(NSNotification *)notif
257 {
258 [expandedRegisters removeObject:[[[[notif userInfo] objectForKey:@"NSObject"] representedObject] fullname]];
259 }
260
261 #pragma mark Private
262
263 /**
264 * Does the actual updating of the source viewer by reading in the file
265 */
266 - (void)updateSourceViewer
267 {
268 id selection = [stackArrayController selection];
269 if ([selection valueForKey:@"filename"] == NSNoSelectionMarker)
270 {
271 [[sourceViewer textView] setString:@""];
272 return;
273 }
274
275 // get the filename and then set the text
276 NSString *filename = [selection valueForKey:@"filename"];
277 filename = [[NSURL URLWithString:filename] path];
278 if ([filename isEqualToString:@""])
279 {
280 return;
281 }
282
283 [sourceViewer setFile:filename];
284
285 int line = [[selection valueForKey:@"lineNumber"] intValue];
286 [sourceViewer setMarkedLine:line];
287 [sourceViewer scrollToLine:line];
288
289 // make sure the font stays Monaco
290 //[sourceViewer setFont:[NSFont fontWithName:@"Monaco" size:10.0]];
291 }
292
293 /**
294 * Does some house keeping to the stack viewer
295 */
296 - (void)updateStackViewer
297 {
298 [stackArrayController rearrangeObjects];
299 [stackArrayController setSelectionIndex:0];
300
301 [stepInButton setEnabled:YES];
302 [stepOverButton setEnabled:YES];
303 [runButton setEnabled:YES];
304 }
305
306 #pragma mark BSSourceView Delegate
307
308 /**
309 * The gutter was clicked, which indicates that a breakpoint needs to be changed
310 */
311 - (void)gutterClickedAtLine:(int)line forFile:(NSString *)file
312 {
313 BreakpointManager *mngr = [BreakpointManager sharedManager];
314
315 if ([mngr hasBreakpointAt:line inFile:file])
316 {
317 [mngr removeBreakpointAt:line inFile:file];
318 }
319 else
320 {
321 Breakpoint *bp = [[Breakpoint alloc] initWithLine:line inFile:file];
322 [mngr addBreakpoint:bp];
323 [bp release];
324 }
325
326 [[sourceViewer numberView] setMarkers:[NSSet setWithArray:[mngr breakpointsForFile:file]]];
327 [[sourceViewer numberView] setNeedsDisplay:YES];
328 }
329
330 @end