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