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