Merge branch 'master' into no-gc
[macgdbp.git] / Source / DebuggerWindowController.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 "DebuggerWindowController.h"
18 #import "DebuggerConnection.h"
19 #import "NSXMLElementAdditions.h"
20 #import "AppDelegate.h"
21 #import "BreakpointManager.h"
22
23 @interface DebuggerWindowController (Private)
24
25 - (void)updateSourceViewer;
26
27 @end
28
29 @implementation DebuggerWindowController
30
31 @synthesize connection;
32
33 /**
34 * Initializes the window controller and sets the connection
35 */
36 - (id)initWithPort:(int)aPort session:(NSString *)aSession
37 {
38 if (self = [super initWithWindowNibName:@"Debugger"])
39 {
40 connection = [[DebuggerConnection alloc] initWithWindowController:self port:aPort session:aSession];
41 expandedRegisters = [[NSMutableSet alloc] init];
42 [[self window] makeKeyAndOrderFront:nil];
43 }
44 return self;
45 }
46
47 /**
48 * Dealloc
49 */
50 - (void)dealloc
51 {
52 [connection release];
53 [expandedRegisters release];
54 [super dealloc];
55 }
56
57 /**
58 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
59 */
60 - (void)awakeFromNib
61 {
62 [self setStatus:@"Connecting"];
63 [[self window] setExcludedFromWindowsMenu:YES];
64 [sourceViewer setDelegate:self];
65 }
66
67 /**
68 * Called right before the window closes so that we can tell the socket to close down
69 */
70 - (void)windowWillClose:(NSNotification *)notif
71 {
72 [[connection socket] close];
73 }
74
75 /**
76 * Resets all the displays to be empty
77 */
78 - (void)resetDisplays
79 {
80 [registerController setContent:nil];
81 [stackController setContent:nil];
82 [[sourceViewer textView] setString:@""];
83 }
84
85 /**
86 * Sets the status and clears any error message
87 */
88 - (void)setStatus:(NSString *)aStatus
89 {
90 [errormsg setHidden:YES];
91 [statusmsg setStringValue:aStatus];
92 [[self window] setTitle:[NSString stringWithFormat:@"GDBp @ %@:%d/%@", [connection remoteHost], [connection port], [connection session]]];
93
94 [stepInButton setEnabled:NO];
95 [stepOutButton setEnabled:NO];
96 [stepOverButton setEnabled:NO];
97 [runButton setEnabled:NO];
98 [reconnectButton setEnabled:NO];
99
100 if ([connection isConnected])
101 {
102 if ([aStatus isEqualToString:@"Starting"])
103 {
104 [stepInButton setEnabled:YES];
105 [runButton setEnabled:YES];
106 }
107 }
108 else
109 {
110 [reconnectButton setEnabled:YES];
111 }
112 }
113
114 /**
115 * Sets the status to be "Error" and then displays the error message
116 */
117 - (void)setError:(NSString *)anError
118 {
119 [errormsg setStringValue:anError];
120 [self setStatus:@"Error"];
121 [errormsg setHidden:NO];
122 }
123
124 /**
125 * Sets the root node element of the stacktrace
126 */
127 - (void)setStack:(NSArray *)node
128 {
129 stack = node;
130
131 if ([stack count] > 1)
132 {
133 [stepOutButton setEnabled:YES];
134 }
135 [stepInButton setEnabled:YES];
136 [stepOverButton setEnabled:YES];
137 [runButton setEnabled:YES];
138
139 [self updateSourceViewer];
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 }
182
183 /**
184 * Forwards the message to "step in" to the connection
185 */
186 - (IBAction)stepIn:(id)sender
187 {
188 [connection stepIn];
189 }
190
191 /**
192 * Forwards the message to "step out" to the connection
193 */
194 - (IBAction)stepOut:(id)sender
195 {
196 [connection stepOut];
197 }
198
199 /**
200 * Forwards the message to "step over" to the connection
201 */
202 - (IBAction)stepOver:(id)sender
203 {
204 [connection stepOver];
205 }
206
207 /**
208 * NSTableView delegate method that informs the controller that the stack selection did change and that
209 * we should update the source viewer
210 */
211 - (void)tableViewSelectionDidChange:(NSNotification *)notif
212 {
213 [self updateSourceViewer];
214 }
215
216 /**
217 * Does the actual updating of the source viewer by reading in the file
218 */
219 - (void)updateSourceViewer
220 {
221 id selectedLevel = [[stackController selection] valueForKey:@"level"];
222 if (selectedLevel == NSNoSelectionMarker)
223 {
224 [[sourceViewer textView] setString:@""];
225 return;
226 }
227 int selection = [selectedLevel intValue];
228
229 if ([stack count] < 1)
230 {
231 NSLog(@"huh... we don't have a stack");
232 return;
233 }
234
235 // get the filename and then set the text
236 NSString *filename = [[stack objectAtIndex:selection] valueForKey:@"filename"];
237 filename = [[NSURL URLWithString:filename] path];
238 if ([filename isEqualToString:@""])
239 {
240 return;
241 }
242
243 [sourceViewer setFile:filename];
244
245 int line = [[[stack objectAtIndex:selection] valueForKey:@"lineno"] intValue];
246 [sourceViewer setMarkedLine:line];
247 [sourceViewer scrollToLine:line];
248
249 // make sure the font stays Monaco
250 //[sourceViewer setFont:[NSFont fontWithName:@"Monaco" size:10.0]];
251 }
252
253 /**
254 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
255 */
256 - (void)outlineViewItemDidExpand:(NSNotification *)notif
257 {
258 NSTreeNode *node = [[notif userInfo] objectForKey:@"NSObject"];
259
260 // we're not a leaf but have no children. this must be beyond our depth, so go make us deeper
261 if (![node isLeaf] && [[node childNodes] count] < 1)
262 {
263 [connection getProperty:[[node representedObject] fullname] forNode:node];
264 }
265
266 [expandedRegisters addObject:[[node representedObject] fullname]];
267 }
268
269 /**
270 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
271 */
272 - (void)outlineViewItemDidCollapse:(NSNotification *)notif
273 {
274 [expandedRegisters removeObject:[[[[notif userInfo] objectForKey:@"NSObject"] representedObject] fullname]];
275 }
276
277 /**
278 * Updates the register view by reinserting a given node back into the outline view
279 */
280 - (void)addChildren:(NSArray *)children toNode:(NSTreeNode *)node
281 {
282 NSXMLElement *parent = [node representedObject];
283 for (NSXMLNode *child in children)
284 {
285 [parent addChild:child];
286 }
287
288 [registerController rearrangeObjects];
289 }
290
291 #pragma mark BSSourceView Delegate
292
293 /**
294 * The gutter was clicked, which indicates that a breakpoint needs to be changed
295 */
296 - (void)gutterClickedAtLine:(int)line forFile:(NSString *)file
297 {
298 BreakpointManager *mngr = [BreakpointManager sharedManager];
299
300 if ([mngr hasBreakpointAt:line inFile:file])
301 {
302 [connection removeBreakpoint:[mngr removeBreakpointAt:line inFile:file]];
303 }
304 else
305 {
306 Breakpoint *bp = [[Breakpoint alloc] initWithLine:line inFile:file];
307 [mngr addBreakpoint:bp];
308 [connection addBreakpoint:bp];
309 }
310
311 [[sourceViewer numberView] setMarkers:[mngr breakpointsForFile:file]];
312 [[sourceViewer numberView] setNeedsDisplay:YES];
313 }
314
315 @end