Renaming DebuggerConnection to 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 @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 [[self window] center];
68 [sourceViewer setDelegate:self];
69 }
70
71 /**
72 * Called right before the window closes so that we can tell the socket to close down
73 */
74 - (void)windowWillClose:(NSNotification *)notif
75 {
76 [[connection socket] close];
77 }
78
79 /**
80 * Validates the menu items for the "Debugger" menu
81 */
82 - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem
83 {
84 SEL action = [anItem action];
85
86 if (action == @selector(stepOut:))
87 return ([connection isConnected] && [stack count] > 1);
88 else if (action == @selector(stepIn:) || action == @selector(stepOver:) || action == @selector(run:))
89 return [connection isConnected];
90
91 return [[self window] validateUserInterfaceItem:anItem];
92 }
93
94 /**
95 * Resets all the displays to be empty
96 */
97 - (void)resetDisplays
98 {
99 [registerController setContent:nil];
100 [stackController setContent:nil];
101 [[sourceViewer textView] setString:@""];
102 }
103
104 /**
105 * Sets the status and clears any error message
106 */
107 - (void)setStatus:(NSString *)aStatus
108 {
109 [errormsg setHidden:YES];
110 [statusmsg setStringValue:aStatus];
111 [[self window] setTitle:[NSString stringWithFormat:@"GDBp @ %@:%d/%@", [connection remoteHost], [connection port], [connection session]]];
112
113 [stepInButton setEnabled:NO];
114 [stepOutButton setEnabled:NO];
115 [stepOverButton setEnabled:NO];
116 [runButton setEnabled:NO];
117 [reconnectButton setEnabled:NO];
118
119 if ([connection isConnected])
120 {
121 if ([aStatus isEqualToString:@"Starting"])
122 {
123 [stepInButton setEnabled:YES];
124 [runButton setEnabled:YES];
125 }
126 }
127 else
128 {
129 [reconnectButton setEnabled:YES];
130 }
131 }
132
133 /**
134 * Sets the status to be "Error" and then displays the error message
135 */
136 - (void)setError:(NSString *)anError
137 {
138 [errormsg setStringValue:anError];
139 [self setStatus:@"Error"];
140 [errormsg setHidden:NO];
141 }
142
143 /**
144 * Sets the root node element of the stacktrace
145 */
146 - (void)setStack:(NSArray *)node
147 {
148 stack = node;
149
150 if ([stack count] > 1)
151 {
152 [stepOutButton setEnabled:YES];
153 }
154 [stepInButton setEnabled:YES];
155 [stepOverButton setEnabled:YES];
156 [runButton setEnabled:YES];
157
158 [self updateSourceViewer];
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 [connection stepIn];
208 }
209
210 /**
211 * Forwards the message to "step out" to the connection
212 */
213 - (IBAction)stepOut:(id)sender
214 {
215 [connection stepOut];
216 }
217
218 /**
219 * Forwards the message to "step over" to the connection
220 */
221 - (IBAction)stepOver:(id)sender
222 {
223 [connection stepOver];
224 }
225
226 /**
227 * NSTableView delegate method that informs the controller that the stack selection did change and that
228 * we should update the source viewer
229 */
230 - (void)tableViewSelectionDidChange:(NSNotification *)notif
231 {
232 [self updateSourceViewer];
233 }
234
235 /**
236 * Does the actual updating of the source viewer by reading in the file
237 */
238 - (void)updateSourceViewer
239 {
240 id selectedLevel = [[stackController selection] valueForKey:@"level"];
241 if (selectedLevel == NSNoSelectionMarker)
242 {
243 [[sourceViewer textView] setString:@""];
244 return;
245 }
246 int selection = [selectedLevel intValue];
247
248 if ([stack count] < 1)
249 {
250 NSLog(@"huh... we don't have a stack");
251 return;
252 }
253
254 // get the filename and then set the text
255 NSString *filename = [[stack objectAtIndex:selection] valueForKey:@"filename"];
256 filename = [[NSURL URLWithString:filename] path];
257 if ([filename isEqualToString:@""])
258 {
259 return;
260 }
261
262 [sourceViewer setFile:filename];
263
264 int line = [[[stack objectAtIndex:selection] valueForKey:@"lineno"] intValue];
265 [sourceViewer setMarkedLine:line];
266 [sourceViewer scrollToLine:line];
267
268 // make sure the font stays Monaco
269 //[sourceViewer setFont:[NSFont fontWithName:@"Monaco" size:10.0]];
270 }
271
272 /**
273 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
274 */
275 - (void)outlineViewItemDidExpand:(NSNotification *)notif
276 {
277 NSTreeNode *node = [[notif userInfo] objectForKey:@"NSObject"];
278 [expandedRegisters addObject:[[node representedObject] fullname]];
279 }
280
281 /**
282 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
283 */
284 - (void)outlineViewItemDidCollapse:(NSNotification *)notif
285 {
286 [expandedRegisters removeObject:[[[[notif userInfo] objectForKey:@"NSObject"] representedObject] fullname]];
287 }
288
289 #pragma mark BSSourceView Delegate
290
291 /**
292 * The gutter was clicked, which indicates that a breakpoint needs to be changed
293 */
294 - (void)gutterClickedAtLine:(int)line forFile:(NSString *)file
295 {
296 BreakpointManager *mngr = [BreakpointManager sharedManager];
297
298 if ([mngr hasBreakpointAt:line inFile:file])
299 {
300 [mngr removeBreakpointAt:line inFile:file];
301 }
302 else
303 {
304 Breakpoint *bp = [[Breakpoint alloc] initWithLine:line inFile:file];
305 [mngr addBreakpoint:bp];
306 }
307
308 [[sourceViewer numberView] setMarkers:[NSSet setWithArray:[mngr breakpointsForFile:file]]];
309 [[sourceViewer numberView] setNeedsDisplay:YES];
310 }
311
312 @end