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