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
:@
[ [[[NSSortDescriptor alloc
] initWithKey
:@
"index" ascending
:YES
] autorelease
] ]];
80 [stackArrayController addObserver
:self
81 forKeyPath
:@
"selectedObjects"
82 options
:NSKeyValueObservingOptionNew
84 self.connection.autoAttach
= [attachedCheckbox_ state
] == NSOnState
;
88 * Key-value observation routine.
90 - (void)observeValueForKeyPath
:(NSString
*)keyPath
92 change
:(NSDictionary
<NSString
*,id>*)change
93 context
:(void*)context
{
94 if (object
== stackArrayController
&& [keyPath isEqualToString
:@
"selectedObjects"]) {
95 for (StackFrame
* frame
in stackArrayController.selectedObjects
)
96 [connection loadStackFrame
:frame
];
98 [super observeValueForKeyPath
:keyPath ofObject
:object change
:change context
:context
];
103 * Validates the menu items for the "Debugger" menu
105 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
107 SEL action
= [anItem action
];
109 if (action
== @selector(stepOut
:)) {
110 return ([connection isConnected
] && _model.stackDepth
> 1);
111 } else if (action
== @selector(stepIn
:) ||
112 action
== @selector(stepOver
:) ||
113 action
== @selector(run
:) ||
114 action
== @selector(stop
:) ||
115 action
== @selector(showEvalWindow
:)) {
116 return [connection isConnected
];
118 return [[self window
] validateUserInterfaceItem
:anItem
];
122 * Shows the inspector window
124 - (IBAction
)showInspectorWindow
:(id)sender
126 if (![inspector isVisible
])
127 [inspector makeKeyAndOrderFront
:sender
];
129 [inspector orderOut
:sender
];
133 * Runs the eval window sheet.
135 - (IBAction
)showEvalWindow
:(id)sender
137 // The |controller| will release itself on close.
138 EvalController
* controller
= [[EvalController alloc
] initWithBackEnd
:connection
];
139 [controller runModalForWindow
:[self window
]];
143 * Resets all the displays to be empty
145 - (void)resetDisplays
147 [variablesTreeController setContent
:nil];
148 [stackArrayController rearrangeObjects
];
149 [[sourceViewer textView
] setString
:@
""];
150 sourceViewer.file
= nil;
154 * Sets the status to be "Error" and then displays the error message
156 - (void)setError
:(NSString
*)anError
158 [errormsg setStringValue
:anError
];
159 [errormsg setHidden
:NO
];
163 * Handles a GDBpConnection error
165 - (void)errorEncountered
:(NSString
*)error
167 [self setError
:error
];
171 * Delegate function for GDBpConnection for when the debugger connects.
173 - (void)debuggerConnected
175 [errormsg setHidden
:YES
];
176 if (!self.connection.autoAttach
)
178 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:@
"BreakOnFirstLine"])
183 * Called once the debugger disconnects.
185 - (void)debuggerDisconnected
187 // Invalidate the marked line so we don't look like we're still running.
188 sourceViewer.markedLine
= -1;
189 [sourceViewer setNeedsDisplay
:YES
];
193 * Forwards the message to run script execution to the connection
195 - (IBAction
)run
:(id)sender
200 - (IBAction
)attachedToggled
:(id)sender
202 connection.autoAttach
= [sender state
] == NSOnState
;
206 * Forwards the message to "step in" to the connection
208 - (IBAction
)stepIn
:(id)sender
210 if ([[variablesTreeController selectedObjects
] count
] > 0)
211 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
217 * Forwards the message to "step out" to the connection
219 - (IBAction
)stepOut
:(id)sender
221 if ([[variablesTreeController selectedObjects
] count
] > 0)
222 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
224 [connection stepOut
];
228 * Forwards the message to "step over" to the connection
230 - (IBAction
)stepOver
:(id)sender
232 if ([[variablesTreeController selectedObjects
] count
] > 0)
233 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
235 [connection stepOver
];
239 * Forwards the detach/"stop" message to the back end.
241 - (IBAction
)stop
:(id)sender
246 - (void)fetchChildProperties
:(VariableNode
*)node
248 NSArray
* selection
= [stackArrayController selectedObjects
];
249 if (![selection count
])
251 assert([selection count
] == 1);
252 NSInteger depth
= [[selection objectAtIndex
:0] index
];
253 [connection getChildrenOfProperty
:node atDepth
:depth callback
:^
(NSArray
* properties
) {
254 [node setChildrenFromXMLChildren
:properties
];
255 [variablesTreeController rearrangeObjects
];
260 * NSTableView delegate method that informs the controller that the stack selection did change and that
261 * we should update the source viewer
263 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
265 [self updateSourceViewer
];
266 // TODO: This is very, very hacky because it's nondeterministic. The issue
267 // is that calling |-[NSOutlineView expandItem:]| while the table is still
268 // doing its redraw will translate to a no-op. Instead, we need to restructure
269 // this controller so that when everything has been laid out we call
270 // |-expandVariables|; but NSOutlineView doesn't have a |-didFinishDoingCrap:|
271 // method. The other issue is that we need to call this method from
272 // selectionDidChange but ONLY when it was the result of a user-initiated
273 // action and not the stack viewer updating causing a selection change.
274 // If it happens in the latter, then we run into the same issue that causes
276 [self performSelector
:@selector(expandVariables
) withObject
:nil afterDelay
:0.05];
280 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
282 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
284 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
285 [expandedVariables addObject
:[[node representedObject
] fullName
]];
289 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
291 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
293 [expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullName
]];
299 * Does the actual updating of the source viewer by reading in the file
301 - (void)updateSourceViewer
303 NSArray
* selection
= [stackArrayController selectedObjects
];
304 if (!selection ||
[selection count
] < 1)
306 if ([selection count
] > 1)
307 NSLog(@
"INVALID SELECTION");
308 StackFrame
* frame
= [selection objectAtIndex
:0];
311 [connection loadStackFrame
:frame
];
316 NSString
* filename
= [[NSURL URLWithString
:frame.filename
] path
];
317 if ([filename isEqualToString
:@
""])
320 // Replace the source if necessary.
321 if (frame.source
&& ![sourceViewer.file isEqualToString
:filename
])
323 [sourceViewer setString
:frame.source asFile
:filename
];
325 NSSet
* breakpoints
= [NSSet setWithArray
:[[BreakpointManager sharedManager
] breakpointsForFile
:filename
]];
326 [sourceViewer setMarkers
:breakpoints
];
329 [sourceViewer setMarkedLine
:frame.lineNumber
];
330 [sourceViewer scrollToLine
:frame.lineNumber
];
332 [[sourceViewer textView
] display
];
336 * Expands the variables based on the stored set
338 - (void)expandVariables
340 NSString
* selection
= [selectedVariable fullName
];
342 for (NSInteger i
= 0; i
< [variablesOutlineView numberOfRows
]; i
++) {
343 NSTreeNode
* node
= [variablesOutlineView itemAtRow
:i
];
344 NSString
* fullName
= [[node representedObject
] fullName
];
346 // see if it needs expanding
347 if ([expandedVariables containsObject
:fullName
])
348 [variablesOutlineView expandItem
:node
];
350 // select it if we had it selected before
351 if ([fullName isEqualToString
:selection
])
352 [variablesTreeController setSelectionIndexPath
:[node indexPath
]];
356 #pragma mark BSSourceView Delegate
359 * The gutter was clicked, which indicates that a breakpoint needs to be changed
361 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
363 BreakpointManager
* mngr
= [BreakpointManager sharedManager
];
365 if ([mngr hasBreakpointAt
:line inFile
:file
])
367 [mngr removeBreakpointAt
:line inFile
:file
];
371 Breakpoint
* bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
372 [mngr addBreakpoint
:bp
];
376 [sourceViewer setMarkers
:[NSSet setWithArray
:[mngr breakpointsForFile
:file
]]];
377 [sourceViewer setNeedsDisplay
:YES
];
380 #pragma mark GDBpConnectionDelegate
382 - (void)sourceUpdated
:(StackFrame
*)frame
384 [self updateSourceViewer
];