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