The breakpoint manager now works -- breakpoints can be set from the main debugger...
[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 - (void)updateSourceViewer;
25 @end
26
27 @implementation DebuggerWindowController
28
29 @synthesize connection, sourceViewer;
30
31 /**
32 * Initializes the window controller and sets the connection
33 */
34 - (id)initWithPort:(int)aPort session:(NSString *)aSession
35 {
36 if (self = [super initWithWindowNibName:@"Debugger"])
37 {
38 connection = [[DebuggerConnection alloc] initWithWindowController:self port:aPort session:aSession];
39 expandedRegisters = [[NSMutableSet alloc] init];
40 [[self window] makeKeyAndOrderFront:nil];
41 }
42 return self;
43 }
44
45 /**
46 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
47 */
48 - (void)awakeFromNib
49 {
50 [self setStatus:@"Connecting"];
51 [[self window] setExcludedFromWindowsMenu:YES];
52 [sourceViewer setDelegate:self];
53 }
54
55 /**
56 * Called right before the window closes so that we can tell the socket to close down
57 */
58 - (void)windowWillClose:(NSNotification *)notif
59 {
60 [[connection socket] close];
61 }
62
63 /**
64 * Resets all the displays to be empty
65 */
66 - (void)resetDisplays
67 {
68 [registerController setContent:nil];
69 [stackController setContent:nil];
70 [[sourceViewer textView] setString:@""];
71 }
72
73 /**
74 * Sets the status and clears any error message
75 */
76 - (void)setStatus:(NSString *)aStatus
77 {
78 [errormsg setHidden:YES];
79 [statusmsg setStringValue:aStatus];
80 [[self window] setTitle:[NSString stringWithFormat:@"GDBp @ %@:%d/%@", [connection remoteHost], [connection port], [connection session]]];
81
82 [stepInButton setEnabled:NO];
83 [stepOutButton setEnabled:NO];
84 [stepOverButton setEnabled:NO];
85 [runButton setEnabled:NO];
86 [reconnectButton setEnabled:NO];
87
88 if ([connection isConnected])
89 {
90 if ([aStatus isEqualToString:@"Starting"])
91 {
92 [stepInButton setEnabled:YES];
93 [runButton setEnabled:YES];
94 }
95 }
96 else
97 {
98 [reconnectButton setEnabled:YES];
99 }
100 }
101
102 /**
103 * Sets the status to be "Error" and then displays the error message
104 */
105 - (void)setError:(NSString *)anError
106 {
107 [errormsg setStringValue:anError];
108 [self setStatus:@"Error"];
109 [errormsg setHidden:NO];
110 }
111
112 /**
113 * Sets the root node element of the stacktrace
114 */
115 - (void)setStack:(NSArray *)node
116 {
117 stack = node;
118
119 if ([stack count] > 1)
120 {
121 [stepOutButton setEnabled:YES];
122 }
123 [stepInButton setEnabled:YES];
124 [stepOverButton setEnabled:YES];
125 [runButton setEnabled:YES];
126
127 [self updateSourceViewer];
128 }
129
130 /**
131 * Sets the stack root element so that the NSOutlineView can display it
132 */
133 - (void)setRegister:(NSXMLDocument *)elm
134 {
135 // XXX: Doing anything short of this will cause bindings to crash spectacularly for no reason whatsoever, and
136 // in seemingly arbitrary places. The class that crashes is _NSKeyValueObservationInfoCreateByRemoving.
137 // http://boredzo.org/blog/archives/2006-01-29/have-you-seen-this-crash says that this means nothing is
138 // being observed, but I doubt that he was using an NSOutlineView which seems to be one f!cking piece of
139 // sh!t when used with NSTreeController. http://www.cocoadev.com/index.pl?NSTreeControllerBugOrDeveloperError
140 // was the inspiration for this fix (below) but the author says that inserting does not work too well, but
141 // that's okay for us as we just need to replace the entire thing.
142 [registerController setContent:nil];
143 [registerController setContent:[[elm rootElement] children]];
144
145 for (int i = 0; i < [registerView numberOfRows]; i++)
146 {
147 NSTreeNode *node = [registerView itemAtRow:i];
148 if ([expandedRegisters containsObject:[[node representedObject] fullname]])
149 {
150 [registerView expandItem:node];
151 }
152 }
153 }
154
155 /**
156 * Forwards the message to run script execution to the connection
157 */
158 - (IBAction)run:(id)sender
159 {
160 [connection run];
161 }
162
163 /**
164 * Tells the connection to ask the server to reconnect
165 */
166 - (IBAction)reconnect:(id)sender
167 {
168 [connection reconnect];
169 }
170
171 /**
172 * Forwards the message to "step in" to the connection
173 */
174 - (IBAction)stepIn:(id)sender
175 {
176 [connection stepIn];
177 }
178
179 /**
180 * Forwards the message to "step out" to the connection
181 */
182 - (IBAction)stepOut:(id)sender
183 {
184 [connection stepOut];
185 }
186
187 /**
188 * Forwards the message to "step over" to the connection
189 */
190 - (IBAction)stepOver:(id)sender
191 {
192 [connection stepOver];
193 }
194
195 /**
196 * NSTableView delegate method that informs the controller that the stack selection did change and that
197 * we should update the source viewer
198 */
199 - (void)tableViewSelectionDidChange:(NSNotification *)notif
200 {
201 [self updateSourceViewer];
202 }
203
204 /**
205 * Does the actual updating of the source viewer by reading in the file
206 */
207 - (void)updateSourceViewer
208 {
209 id selectedLevel = [[stackController selection] valueForKey:@"level"];
210 if (selectedLevel == NSNoSelectionMarker)
211 {
212 [[sourceViewer textView] setString:@""];
213 return;
214 }
215 int selection = [selectedLevel intValue];
216
217 if ([stack count] < 1)
218 {
219 NSLog(@"huh... we don't have a stack");
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 if ([filename isEqualToString:@""])
227 {
228 return;
229 }
230
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 [expandedRegisters addObject:[[node representedObject] fullname]];
248 }
249
250 /**
251 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
252 */
253 - (void)outlineViewItemDidCollapse:(NSNotification *)notif
254 {
255 [expandedRegisters removeObject:[[[[notif userInfo] objectForKey:@"NSObject"] representedObject] fullname]];
256 }
257
258 #pragma mark BSSourceView Delegate
259
260 /**
261 * The gutter was clicked, which indicates that a breakpoint needs to be changed
262 */
263 - (void)gutterClickedAtLine:(int)line forFile:(NSString *)file
264 {
265 BreakpointManager *mngr = [BreakpointManager sharedManager];
266
267 if ([mngr hasBreakpointAt:line inFile:file])
268 {
269 [mngr removeBreakpointAt:line inFile:file];
270 }
271 else
272 {
273 Breakpoint *bp = [[Breakpoint alloc] initWithLine:line inFile:file];
274 [mngr addBreakpoint:bp];
275 }
276
277 [[sourceViewer numberView] setMarkers:[NSSet setWithArray:[mngr breakpointsForFile:file]]];
278 [[sourceViewer numberView] setNeedsDisplay:YES];
279 }
280
281 @end