We were previously using, for the stack table, the index of the row to determine...
[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
22 @interface DebuggerWindowController (Private)
23
24 - (void)updateSourceViewer;
25
26 @end
27
28 @implementation DebuggerWindowController
29
30 @synthesize connection;
31
32 /**
33 * Initializes the window controller and sets the connection
34 */
35 - (id)initWithPort:(int)aPort session:(NSString *)aSession
36 {
37 if (self = [super initWithWindowNibName:@"Debugger"])
38 {
39 connection = [[DebuggerConnection alloc] initWithWindowController:self port:aPort session:aSession];
40 expandedRegisters = [[NSMutableSet alloc] init];
41 [[self window] makeKeyAndOrderFront:nil];
42 }
43 return self;
44 }
45
46 /**
47 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
48 */
49 - (void)awakeFromNib
50 {
51 // set up the scroller for the source viewer
52 [sourceViewer setEditable:NO];
53 [sourceViewer setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
54 [[sourceViewer textContainer] setContainerSize:NSMakeSize(FLT_MAX, FLT_MAX)];
55 [[sourceViewer textContainer] setWidthTracksTextView:NO];
56 [sourceViewer setHorizontallyResizable:YES];
57 [sourceViewerScroller setHasHorizontalScroller:YES];
58 [sourceViewerScroller display];
59
60 [self setStatus:@"Connecting"];
61 }
62
63 /**
64 * Called right before the window closes so that we can tell the socket to close down
65 */
66 - (void)windowWillClose:(NSNotification *)notif
67 {
68 [[connection socket] close];
69 }
70
71 /**
72 * Resets all the displays to be empty
73 */
74 - (void)resetDisplays
75 {
76 [registerController setContent:nil];
77 [stackController setContent:nil];
78 [sourceViewer setString:@""];
79 }
80
81 /**
82 * Sets the status and clears any error message
83 */
84 - (void)setStatus:(NSString *)aStatus
85 {
86 [errormsg setHidden:YES];
87 [statusmsg setStringValue:aStatus];
88 [[self window] setTitle:[NSString stringWithFormat:@"GDBp @ %@:%d/%@", [connection remoteHost], [connection port], [connection session]]];
89
90 [stepInButton setEnabled:NO];
91 [stepOutButton setEnabled:NO];
92 [stepOverButton setEnabled:NO];
93 [runButton setEnabled:NO];
94 [reconnectButton setEnabled:NO];
95
96 if ([connection isConnected])
97 {
98 if ([aStatus isEqualToString:@"Starting"])
99 {
100 [stepInButton setEnabled:YES];
101 [runButton setEnabled:YES];
102 }
103 }
104 else
105 {
106 [reconnectButton setEnabled:YES];
107 }
108 }
109
110 /**
111 * Sets the status to be "Error" and then displays the error message
112 */
113 - (void)setError:(NSString *)anError
114 {
115 [errormsg setStringValue:anError];
116 [self setStatus:@"Error"];
117 [errormsg setHidden:NO];
118 }
119
120 /**
121 * Sets the root node element of the stacktrace
122 */
123 - (void)setStack:(NSArray *)node
124 {
125 stack = node;
126
127 if ([stack count] > 1)
128 {
129 [stepOutButton setEnabled:YES];
130 }
131 [stepInButton setEnabled:YES];
132 [stepOverButton setEnabled:YES];
133 [runButton setEnabled:YES];
134
135 [self updateSourceViewer];
136 }
137
138 /**
139 * Sets the stack root element so that the NSOutlineView can display it
140 */
141 - (void)setRegister:(NSXMLDocument *)elm
142 {
143 // XXX: Doing anything short of this will cause bindings to crash spectacularly for no reason whatsoever, and
144 // in seemingly arbitrary places. The class that crashes is _NSKeyValueObservationInfoCreateByRemoving.
145 // http://boredzo.org/blog/archives/2006-01-29/have-you-seen-this-crash says that this means nothing is
146 // being observed, but I doubt that he was using an NSOutlineView which seems to be one f!cking piece of
147 // sh!t when used with NSTreeController. http://www.cocoadev.com/index.pl?NSTreeControllerBugOrDeveloperError
148 // was the inspiration for this fix (below) but the author says that inserting does not work too well, but
149 // that's okay for us as we just need to replace the entire thing.
150 [registerController setContent:nil];
151 [registerController setContent:[[elm rootElement] children]];
152
153 for (int i = 0; i < [registerView numberOfRows]; i++)
154 {
155 NSTreeNode *node = [registerView itemAtRow:i];
156 if ([expandedRegisters containsObject:[[node representedObject] fullname]])
157 {
158 [registerView expandItem:node];
159 }
160 }
161 }
162
163 /**
164 * Forwards the message to run script execution to the connection
165 */
166 - (IBAction)run:(id)sender
167 {
168 [connection run];
169 }
170
171 /**
172 * Tells the connection to ask the server to reconnect
173 */
174 - (IBAction)reconnect:(id)sender
175 {
176 [connection reconnect];
177 }
178
179 /**
180 * Forwards the message to "step in" to the connection
181 */
182 - (IBAction)stepIn:(id)sender
183 {
184 [connection stepIn];
185 }
186
187 /**
188 * Forwards the message to "step out" to the connection
189 */
190 - (IBAction)stepOut:(id)sender
191 {
192 [connection stepOut];
193 }
194
195 /**
196 * Forwards the message to "step over" to the connection
197 */
198 - (IBAction)stepOver:(id)sender
199 {
200 [connection stepOver];
201 }
202
203 /**
204 * NSTableView delegate method that informs the controller that the stack selection did change and that
205 * we should update the source viewer
206 */
207 - (void)tableViewSelectionDidChange:(NSNotification *)notif
208 {
209 [self updateSourceViewer];
210 }
211
212 /**
213 * Does the actual updating of the source viewer by reading in the file
214 */
215 - (void)updateSourceViewer
216 {
217 id selectedLevel = [[stackController selection] valueForKey:@"level"];
218 if (selectedLevel == NSNoSelectionMarker)
219 {
220 [sourceViewer setString:@""];
221 return;
222 }
223 int selection = [selectedLevel intValue];
224
225 // get the filename and then set the text
226 NSString *filename = [[stack objectAtIndex:selection] valueForKey:@"filename"];
227 filename = [[NSURL URLWithString:filename] path];
228 NSString *text = [NSString stringWithContentsOfFile:filename];
229 [sourceViewer setString:text];
230
231 // go through the document until we find the NSRange for the line we want
232 int destination = [[[stack objectAtIndex:selection] valueForKey:@"lineno"] intValue];
233 int rangeIndex = 0;
234 for (int line = 0; line < destination; line++)
235 {
236 rangeIndex = NSMaxRange([text lineRangeForRange:NSMakeRange(rangeIndex, 0)]);
237 }
238
239 // now get the true start/end markers for it
240 unsigned lineStart, lineEnd;
241 [text getLineStart:&lineStart end:NULL contentsEnd:&lineEnd forRange:NSMakeRange(rangeIndex - 1, 0)];
242 NSRange lineRange = NSMakeRange(lineStart, lineEnd - lineStart);
243
244 // colorize it so the user knows which line we're on in the stack
245 [[sourceViewer textStorage] setAttributes:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSColor redColor], [NSColor yellowColor], nil]
246 forKeys:[NSArray arrayWithObjects:NSForegroundColorAttributeName, NSBackgroundColorAttributeName, nil]]
247 range:lineRange];
248 [sourceViewer scrollRangeToVisible:[text lineRangeForRange:NSMakeRange(lineStart, lineEnd - lineStart)]];
249
250 // make sure the font stays Monaco
251 [sourceViewer setFont:[NSFont fontWithName:@"Monaco" size:10.0]];
252 }
253
254 /**
255 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
256 */
257 - (void)outlineViewItemDidExpand:(NSNotification *)notif
258 {
259 NSTreeNode *node = [[notif userInfo] objectForKey:@"NSObject"];
260
261 // we're not a leaf but have no children. this must be beyond our depth, so go make us deeper
262 if (![node isLeaf] && [[node childNodes] count] < 1)
263 {
264 [connection getProperty:[[node representedObject] fullname] forNode:node];
265 }
266
267 [expandedRegisters addObject:[[node representedObject] fullname]];
268 }
269
270 /**
271 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
272 */
273 - (void)outlineViewItemDidCollapse:(NSNotification *)notif
274 {
275 [expandedRegisters removeObject:[[[[notif userInfo] objectForKey:@"NSObject"] representedObject] fullname]];
276 }
277
278 /**
279 * Updates the register view by reinserting a given node back into the outline view
280 */
281 - (void)addChildren:(NSArray *)children toNode:(NSTreeNode *)node
282 {
283 NSXMLElement *parent = [node representedObject];
284 for (NSXMLNode *child in children)
285 {
286 [parent addChild:child];
287 }
288
289 [registerController rearrangeObjects];
290 }
291
292 @end