3 * Copyright (c) 2007 - 2011, Blue Static <http://www.bluestatic.org>
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.
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.
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
17 #import "DebuggerController.h"
19 #import "AppDelegate.h"
20 #import "BSSourceView.h"
21 #import "BreakpointManager.h"
22 #import "DebuggerModel.h"
23 #import "EvalController.h"
24 #import "NSXMLElementAdditions.h"
26 @interface DebuggerController (Private
)
27 - (void)updateSourceViewer
;
28 - (void)expandVariables
;
31 @implementation DebuggerController
33 @synthesize connection
, sourceViewer
, inspector
;
36 * Initializes the window controller and sets the connection using preference
41 if (self = [super initWithWindowNibName
:@
"Debugger"])
43 NSUserDefaults
* defaults
= [NSUserDefaults standardUserDefaults
];
45 _model
= [[DebuggerModel alloc
] init
];
47 connection
= [[DebuggerBackEnd alloc
] initWithPort
:[defaults integerForKey
:@
"Port"]];
48 connection.delegate
= self;
49 connection.model
= _model
;
50 expandedVariables
= [[NSMutableSet alloc
] init
];
51 [[self window
] makeKeyAndOrderFront
:nil];
52 [[self window
] setDelegate
:self];
54 if ([defaults boolForKey
:@
"InspectorWindowVisible"])
55 [inspector orderFront
:self];
67 [expandedVariables release
];
72 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
76 [[self window
] setExcludedFromWindowsMenu
:YES
];
77 [[self window
] setTitle
:[NSString stringWithFormat
:@
"MacGDBp @ %d", [connection port
]]];
78 [sourceViewer setDelegate
:self];
79 [stackArrayController setSortDescriptors
:[NSArray arrayWithObject
:[[[NSSortDescriptor alloc
] initWithKey
:@
"index" ascending
:YES
] autorelease
]]];
80 self.connection.autoAttach
= [attachedCheckbox_ state
] == NSOnState
;
84 * Validates the menu items for the "Debugger" menu
86 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
88 SEL action
= [anItem action
];
90 if (action
== @selector(stepOut
:)) {
91 return ([connection isConnected
] && _model.stackDepth
> 1);
92 } else if (action
== @selector(stepIn
:) ||
93 action
== @selector(stepOver
:) ||
94 action
== @selector(run
:) ||
95 action
== @selector(stop
:) ||
96 action
== @selector(showEvalWindow
:)) {
97 return [connection isConnected
];
99 return [[self window
] validateUserInterfaceItem
:anItem
];
103 * Shows the inspector window
105 - (IBAction
)showInspectorWindow
:(id)sender
107 if (![inspector isVisible
])
108 [inspector makeKeyAndOrderFront
:sender
];
110 [inspector orderOut
:sender
];
114 * Runs the eval window sheet.
116 - (IBAction
)showEvalWindow
:(id)sender
118 // The |controller| will release itself on close.
119 EvalController
* controller
= [[EvalController alloc
] initWithBackEnd
:connection
];
120 [controller runModalForWindow
:[self window
]];
124 * Resets all the displays to be empty
126 - (void)resetDisplays
128 [variablesTreeController setContent
:nil];
129 [stackArrayController rearrangeObjects
];
130 [[sourceViewer textView
] setString
:@
""];
131 sourceViewer.file
= nil;
135 * Sets the status to be "Error" and then displays the error message
137 - (void)setError
:(NSString
*)anError
139 [errormsg setStringValue
:anError
];
140 [errormsg setHidden
:NO
];
144 * Handles a GDBpConnection error
146 - (void)errorEncountered
:(NSString
*)error
148 [self setError
:error
];
152 * Delegate function for GDBpConnection for when the debugger connects.
154 - (void)debuggerConnected
156 [errormsg setHidden
:YES
];
157 if (!self.connection.autoAttach
)
159 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:@
"BreakOnFirstLine"])
164 * Called once the debugger disconnects.
166 - (void)debuggerDisconnected
168 // Invalidate the marked line so we don't look like we're still running.
169 sourceViewer.markedLine
= -1;
170 [sourceViewer setNeedsDisplay
:YES
];
174 * Forwards the message to run script execution to the connection
176 - (IBAction
)run
:(id)sender
181 - (IBAction
)attachedToggled
:(id)sender
183 connection.autoAttach
= [sender state
] == NSOnState
;
187 * Forwards the message to "step in" to the connection
189 - (IBAction
)stepIn
:(id)sender
191 if ([[variablesTreeController selectedObjects
] count
] > 0)
192 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
198 * Forwards the message to "step out" to the connection
200 - (IBAction
)stepOut
:(id)sender
202 if ([[variablesTreeController selectedObjects
] count
] > 0)
203 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
205 [connection stepOut
];
209 * Forwards the message to "step over" to the connection
211 - (IBAction
)stepOver
:(id)sender
213 if ([[variablesTreeController selectedObjects
] count
] > 0)
214 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
216 [connection stepOver
];
220 * Forwards the detach/"stop" message to the back end.
222 - (IBAction
)stop
:(id)sender
227 - (void)fetchChildProperties
:(VariableNode
*)node
229 NSArray
* selection
= [stackArrayController selectedObjects
];
230 if (![selection count
])
232 assert([selection count
] == 1);
233 NSInteger depth
= [[selection objectAtIndex
:0] index
];
234 [connection getChildrenOfProperty
:node atDepth
:depth callback
:^
(NSArray
* properties
) {
235 [node setChildrenFromXMLChildren
:properties
];
236 [variablesTreeController rearrangeObjects
];
241 * NSTableView delegate method that informs the controller that the stack selection did change and that
242 * we should update the source viewer
244 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
246 [self updateSourceViewer
];
247 // TODO: This is very, very hacky because it's nondeterministic. The issue
248 // is that calling |-[NSOutlineView expandItem:]| while the table is still
249 // doing its redraw will translate to a no-op. Instead, we need to restructure
250 // this controller so that when everything has been laid out we call
251 // |-expandVariables|; but NSOutlineView doesn't have a |-didFinishDoingCrap:|
252 // method. The other issue is that we need to call this method from
253 // selectionDidChange but ONLY when it was the result of a user-initiated
254 // action and not the stack viewer updating causing a selection change.
255 // If it happens in the latter, then we run into the same issue that causes
257 [self performSelector
:@selector(expandVariables
) withObject
:nil afterDelay
:0.05];
261 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
263 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
265 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
266 [expandedVariables addObject
:[[node representedObject
] fullName
]];
270 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
272 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
274 [expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullName
]];
280 * Does the actual updating of the source viewer by reading in the file
282 - (void)updateSourceViewer
284 NSArray
* selection
= [stackArrayController selectedObjects
];
285 if (!selection ||
[selection count
] < 1)
287 if ([selection count
] > 1)
288 NSLog(@
"INVALID SELECTION");
289 StackFrame
* frame
= [selection objectAtIndex
:0];
292 [connection loadStackFrame
:frame
];
297 NSString
* filename
= [[NSURL URLWithString
:frame.filename
] path
];
298 if ([filename isEqualToString
:@
""])
301 // Replace the source if necessary.
302 if (frame.source
&& ![sourceViewer.file isEqualToString
:filename
])
304 [sourceViewer setString
:frame.source asFile
:filename
];
306 NSSet
* breakpoints
= [NSSet setWithArray
:[[BreakpointManager sharedManager
] breakpointsForFile
:filename
]];
307 [sourceViewer setMarkers
:breakpoints
];
310 [sourceViewer setMarkedLine
:frame.lineNumber
];
311 [sourceViewer scrollToLine
:frame.lineNumber
];
313 [[sourceViewer textView
] display
];
317 * Expands the variables based on the stored set
319 - (void)expandVariables
321 NSString
* selection
= [selectedVariable fullName
];
323 for (NSInteger i
= 0; i
< [variablesOutlineView numberOfRows
]; i
++) {
324 NSTreeNode
* node
= [variablesOutlineView itemAtRow
:i
];
325 NSString
* fullName
= [[node representedObject
] fullName
];
327 // see if it needs expanding
328 if ([expandedVariables containsObject
:fullName
])
329 [variablesOutlineView expandItem
:node
];
331 // select it if we had it selected before
332 if ([fullName isEqualToString
:selection
])
333 [variablesTreeController setSelectionIndexPath
:[node indexPath
]];
337 #pragma mark BSSourceView Delegate
340 * The gutter was clicked, which indicates that a breakpoint needs to be changed
342 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
344 BreakpointManager
* mngr
= [BreakpointManager sharedManager
];
346 if ([mngr hasBreakpointAt
:line inFile
:file
])
348 [mngr removeBreakpointAt
:line inFile
:file
];
352 Breakpoint
* bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
353 [mngr addBreakpoint
:bp
];
357 [sourceViewer setMarkers
:[NSSet setWithArray
:[mngr breakpointsForFile
:file
]]];
358 [sourceViewer setNeedsDisplay
:YES
];
361 #pragma mark GDBpConnectionDelegate
363 - (void)sourceUpdated
:(StackFrame
*)frame
365 [self updateSourceViewer
];