Add an item to the Window menu to allow the inspector to be shown/hidden
[macgdbp.git] / Source / DebuggerController.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2009, 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 "DebuggerController.h"
18 #import "GDBpConnection.h"
19 #import "NSXMLElementAdditions.h"
20 #import "AppDelegate.h"
21 #import "BreakpointManager.h"
22
23 @interface DebuggerController (Private)
24 - (void)updateSourceViewer;
25 - (void)updateStackViewer;
26 - (void)expandVariables;
27 @end
28
29 @implementation DebuggerController
30
31 @synthesize connection, sourceViewer, inspector;
32
33 /**
34 * Initializes the window controller and sets the connection using preference
35 * values
36 */
37 - (id)init
38 {
39 if (self = [super initWithWindowNibName:@"Debugger"])
40 {
41 stackController = [[StackController alloc] init];
42
43 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
44 connection = [[GDBpConnection alloc] initWithPort:[defaults integerForKey:@"Port"] session:[defaults stringForKey:@"IDEKey"]];
45 expandedVariables = [[NSMutableSet alloc] init];
46 [[self window] makeKeyAndOrderFront:nil];
47 [[self window] setDelegate:self];
48
49 [[NSNotificationCenter defaultCenter]
50 addObserver:self
51 selector:@selector(handleConnectionError:)
52 name:kErrorOccurredNotif
53 object:connection
54 ];
55 }
56 return self;
57 }
58
59 /**
60 * Dealloc
61 */
62 - (void)dealloc
63 {
64 [connection release];
65 [expandedVariables release];
66 [stackController release];
67 [super dealloc];
68 }
69
70 /**
71 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
72 */
73 - (void)awakeFromNib
74 {
75 [[self window] setExcludedFromWindowsMenu:YES];
76 [[self window] setTitle:[NSString stringWithFormat:@"GDBp @ %@:%d/%@", [connection remoteHost], [connection port], [connection session]]];
77 [sourceViewer setDelegate:self];
78 [stackArrayController setSortDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"index" ascending:YES] autorelease]]];
79 }
80
81 /**
82 * Called right before the window closes so that we can tell the socket to close down
83 */
84 - (void)windowWillClose:(NSNotification *)notif
85 {
86 [[connection socket] close];
87 }
88
89 /**
90 * Validates the menu items for the "Debugger" menu
91 */
92 - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem
93 {
94 SEL action = [anItem action];
95
96 if (action == @selector(stepOut:))
97 return ([connection isConnected] && [stackController.stack count] > 1);
98 else if (action == @selector(stepIn:) || action == @selector(stepOver:) || action == @selector(run:))
99 return [connection isConnected];
100 else if (action == @selector(reconnect:))
101 return ![connection isConnected];
102
103 return [[self window] validateUserInterfaceItem:anItem];
104 }
105
106 /**
107 * Resets all the displays to be empty
108 */
109 - (void)resetDisplays
110 {
111 [variablesTreeController setContent:nil];
112 [stackController.stack removeAllObjects];
113 [[sourceViewer textView] setString:@""];
114 }
115
116 /**
117 * Sets the status to be "Error" and then displays the error message
118 */
119 - (void)setError:(NSString *)anError
120 {
121 [errormsg setStringValue:anError];
122 [errormsg setHidden:NO];
123 }
124
125 /**
126 * Handles a GDBpConnection error
127 */
128 - (void)handleConnectionError:(NSNotification *)notif
129 {
130 [self setError:[[notif userInfo] valueForKey:@"NSString"]];
131 }
132
133 /**
134 * Called once the socket accepts and MacGDBp is connected to the debugger
135 */
136 - (void)startDebugger
137 {
138 [self stepIn:self];
139 }
140
141 /**
142 * Forwards the message to run script execution to the connection
143 */
144 - (IBAction)run:(id)sender
145 {
146 StackFrame *frame = [connection run];
147 [stackController pop];
148
149 if ([connection isConnected] && frame != nil)
150 {
151 [stackController push:frame];
152 [self updateStackViewer];
153 }
154 }
155
156 /**
157 * Tells the connection to ask the server to reconnect
158 */
159 - (IBAction)reconnect:(id)sender
160 {
161 [connection reconnect];
162 [self resetDisplays];
163 }
164
165 /**
166 * Forwards the message to "step in" to the connection
167 */
168 - (IBAction)stepIn:(id)sender
169 {
170 StackFrame *frame = [connection stepIn];
171 if ([frame isShiftedFrame:[stackController peek]])
172 [stackController pop];
173 [stackController push:frame];
174 [self updateStackViewer];
175 }
176
177 /**
178 * Forwards the message to "step out" to the connection
179 */
180 - (IBAction)stepOut:(id)sender
181 {
182 StackFrame *frame = [connection stepOut];
183 [stackController pop]; // frame we were out of
184 [stackController pop]; // frame we are returning to
185 [stackController push:frame];
186 [self updateStackViewer];
187 }
188
189 /**
190 * Forwards the message to "step over" to the connection
191 */
192 - (IBAction)stepOver:(id)sender
193 {
194 StackFrame *frame = [connection stepOver];
195 [stackController pop];
196 [stackController push:frame];
197 [self updateStackViewer];
198 }
199
200 /**
201 * NSTableView delegate method that informs the controller that the stack selection did change and that
202 * we should update the source viewer
203 */
204 - (void)tableViewSelectionDidChange:(NSNotification *)notif
205 {
206 [self updateSourceViewer];
207 [self expandVariables];
208 }
209
210 /**
211 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
212 */
213 - (void)outlineViewItemDidExpand:(NSNotification *)notif
214 {
215 NSTreeNode *node = [[notif userInfo] objectForKey:@"NSObject"];
216 [expandedVariables addObject:[[node representedObject] fullname]];
217 }
218
219 /**
220 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
221 */
222 - (void)outlineViewItemDidCollapse:(NSNotification *)notif
223 {
224 [expandedVariables removeObject:[[[[notif userInfo] objectForKey:@"NSObject"] representedObject] fullname]];
225 }
226
227 #pragma mark Private
228
229 /**
230 * Does the actual updating of the source viewer by reading in the file
231 */
232 - (void)updateSourceViewer
233 {
234 id selection = [stackArrayController selection];
235 if ([selection valueForKey:@"filename"] == NSNoSelectionMarker)
236 return;
237
238 // get the filename
239 NSString *filename = [selection valueForKey:@"filename"];
240 filename = [[NSURL URLWithString:filename] path];
241 if ([filename isEqualToString:@""])
242 return;
243
244 // replace the source if necessary
245 if (![sourceViewer.file isEqualToString:filename])
246 {
247 NSString *source = [selection valueForKey:@"source"];
248 [sourceViewer setString:source asFile:filename];
249 }
250
251 int line = [[selection valueForKey:@"lineNumber"] intValue];
252 [sourceViewer setMarkedLine:line];
253 [sourceViewer scrollToLine:line];
254
255 [[sourceViewer textView] display];
256 }
257
258 /**
259 * Does some house keeping to the stack viewer
260 */
261 - (void)updateStackViewer
262 {
263 [stackArrayController rearrangeObjects];
264 [stackArrayController setSelectionIndex:0];
265 [self expandVariables];
266 }
267
268 /**
269 * Expands the variables based on the stored set
270 */
271 - (void)expandVariables
272 {
273 for (int i = 0; i < [variablesOutlineView numberOfRows]; i++)
274 {
275 NSTreeNode *node = [variablesOutlineView itemAtRow:i];
276 if ([expandedVariables containsObject:[[node representedObject] fullname]])
277 {
278 [variablesOutlineView expandItem:node];
279 }
280 }
281 }
282
283 #pragma mark BSSourceView Delegate
284
285 /**
286 * The gutter was clicked, which indicates that a breakpoint needs to be changed
287 */
288 - (void)gutterClickedAtLine:(int)line forFile:(NSString *)file
289 {
290 BreakpointManager *mngr = [BreakpointManager sharedManager];
291
292 if ([mngr hasBreakpointAt:line inFile:file])
293 {
294 [mngr removeBreakpointAt:line inFile:file];
295 }
296 else
297 {
298 Breakpoint *bp = [[Breakpoint alloc] initWithLine:line inFile:file];
299 [mngr addBreakpoint:bp];
300 [bp release];
301 }
302
303 [[sourceViewer numberView] setMarkers:[NSSet setWithArray:[mngr breakpointsForFile:file]]];
304 [[sourceViewer numberView] setNeedsDisplay:YES];
305 }
306
307 @end