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 "BreakpointController.h"
22 #import "BreakpointManager.h"
23 #import "DebuggerBackEnd.h"
24 #import "DebuggerModel.h"
25 #import "EvalController.h"
26 #import "PreferenceNames.h"
27 #import "NSXMLElementAdditions.h"
28 #import "StackFrame.h"
30 @interface DebuggerController (Private
)
31 - (void)updateSourceViewer
;
32 - (void)expandVariables
;
35 @implementation DebuggerController
{
36 BreakpointController
* _breakpointsController
;
39 @synthesize connection
, sourceViewer
, inspector
;
42 * Initializes the window controller and sets the connection using preference
47 if (self = [super initWithWindowNibName
:@
"Debugger"])
49 NSUserDefaults
* defaults
= [NSUserDefaults standardUserDefaults
];
51 _model
= [[DebuggerModel alloc
] init
];
52 [_model addObserver
:self
53 forKeyPath
:@
"connected"
54 options
:NSKeyValueObservingOptionNew
57 connection
= [[DebuggerBackEnd alloc
] initWithPort
:[defaults integerForKey
:kPrefPort
]
58 autoAttach
:[defaults boolForKey
:kPrefDebuggerAttached
]];
59 connection.model
= _model
;
60 expandedVariables
= [[NSMutableSet alloc
] init
];
61 [[self window
] makeKeyAndOrderFront
:nil];
62 [[self window
] setDelegate
:self];
64 if ([defaults boolForKey
:kPrefInspectorWindowVisible
])
65 [inspector orderFront
:self];
77 [_breakpointsController release
];
78 [expandedVariables release
];
83 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
87 [[self window
] setExcludedFromWindowsMenu
:YES
];
88 [[self window
] setTitle
:[NSString stringWithFormat
:@
"MacGDBp @ %d", [connection port
]]];
89 [sourceViewer setDelegate
:self];
90 [stackArrayController setSortDescriptors
:@
[ [[[NSSortDescriptor alloc
] initWithKey
:@
"index" ascending
:YES
] autorelease
] ]];
91 [stackArrayController addObserver
:self
92 forKeyPath
:@
"selectedObjects"
93 options
:NSKeyValueObservingOptionNew
95 [stackArrayController addObserver
:self
96 forKeyPath
:@
"selection.source"
97 options
:NSKeyValueObservingOptionNew
99 self.connection.autoAttach
= [attachedCheckbox_ state
] == NSOnState
;
101 _breakpointsController
= [[BreakpointController alloc
] init
];
102 [[self.tabView tabViewItemAtIndex
:1] setView
:_breakpointsController.view
];
104 // When the segment control's selection changes, update the tab view.
105 [[_segmentControl cell
] addObserver
:self
106 forKeyPath
:@
"selectedSegment"
109 // When the segment control's superview changes, recalculate the spacer
111 [[_segmentControl superview
] addObserver
:self
115 [self updateSegmentControl
];
119 * Key-value observation routine.
121 - (void)observeValueForKeyPath
:(NSString
*)keyPath
123 change
:(NSDictionary
<NSString
*,id>*)change
124 context
:(void*)context
{
125 if (object
== stackArrayController
&& [keyPath isEqualToString
:@
"selectedObjects"]) {
126 for (StackFrame
* frame
in stackArrayController.selectedObjects
)
127 [connection loadStackFrame
:frame
];
128 } else if (object
== stackArrayController
&& [keyPath isEqualToString
:@
"selection.source"]) {
129 [self updateSourceViewer
];
130 } else if (object
== _model
&& [keyPath isEqualToString
:@
"connected"]) {
131 if ([change
[NSKeyValueChangeNewKey
] boolValue
])
132 [self debuggerConnected
];
134 [self debuggerDisconnected
];
135 } else if (object
== _segmentControl.cell
) {
136 [_tabView selectTabViewItemAtIndex
:_segmentControl.selectedSegment
- 1];
137 } else if (object
== _segmentControl.superview
) {
138 [self updateSegmentControl
];
140 [super observeValueForKeyPath
:keyPath ofObject
:object change
:change context
:context
];
145 * Validates the menu items for the "Debugger" menu
147 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
149 SEL action
= [anItem action
];
151 if (action
== @selector(stepOut
:)) {
152 return _model.connected
&& _model.stackDepth
> 1;
153 } else if (action
== @selector(stepIn
:) ||
154 action
== @selector(stepOver
:) ||
155 action
== @selector(run
:) ||
156 action
== @selector(stop
:) ||
157 action
== @selector(showEvalWindow
:)) {
158 return _model.connected
;
160 return [[self window
] validateUserInterfaceItem
:anItem
];
164 * Shows the inspector window
166 - (IBAction
)showInspectorWindow
:(id)sender
168 if (![inspector isVisible
])
169 [inspector makeKeyAndOrderFront
:sender
];
171 [inspector orderOut
:sender
];
175 * Runs the eval window sheet.
177 - (IBAction
)showEvalWindow
:(id)sender
179 // The |controller| will release itself on close.
180 EvalController
* controller
= [[EvalController alloc
] initWithBackEnd
:connection
];
181 [controller runModalForWindow
:[self window
]];
185 * Resets all the displays to be empty
187 - (void)resetDisplays
189 [variablesTreeController setContent
:nil];
190 [stackArrayController rearrangeObjects
];
191 [[sourceViewer textView
] setString
:@
""];
192 sourceViewer.file
= nil;
196 * Sets the status to be "Error" and then displays the error message
198 - (void)setError
:(NSString
*)anError
200 [errormsg setStringValue
:anError
];
201 [errormsg setHidden
:NO
];
205 * Delegate function for GDBpConnection for when the debugger connects.
207 - (void)debuggerConnected
209 [errormsg setHidden
:YES
];
210 if (!self.connection.autoAttach
)
212 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:kPrefBreakOnFirstLine
])
214 // Do not cache the file between debugger executions.
215 sourceViewer.file
= nil;
219 * Called once the debugger disconnects.
221 - (void)debuggerDisconnected
223 // Invalidate the marked line so we don't look like we're still running.
224 sourceViewer.markedLine
= -1;
225 [sourceViewer setNeedsDisplay
:YES
];
229 * Forwards the message to run script execution to the connection
231 - (IBAction
)run
:(id)sender
236 - (IBAction
)attachedToggled
:(id)sender
238 connection.autoAttach
= [sender state
] == NSOnState
;
242 * Forwards the message to "step in" to the connection
244 - (IBAction
)stepIn
:(id)sender
246 if ([[variablesTreeController selectedObjects
] count
] > 0)
247 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
253 * Forwards the message to "step out" to the connection
255 - (IBAction
)stepOut
:(id)sender
257 if ([[variablesTreeController selectedObjects
] count
] > 0)
258 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
260 [connection stepOut
];
264 * Forwards the message to "step over" to the connection
266 - (IBAction
)stepOver
:(id)sender
268 if ([[variablesTreeController selectedObjects
] count
] > 0)
269 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
271 [connection stepOver
];
275 * Forwards the detach/"stop" message to the back end.
277 - (IBAction
)stop
:(id)sender
283 * NSTableView delegate method that informs the controller that the stack selection did change and that
284 * we should update the source viewer
286 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
288 // TODO: This is very, very hacky because it's nondeterministic. The issue
289 // is that calling |-[NSOutlineView expandItem:]| while the table is still
290 // doing its redraw will translate to a no-op. Instead, we need to restructure
291 // this controller so that when everything has been laid out we call
292 // |-expandVariables|; but NSOutlineView doesn't have a |-didFinishDoingCrap:|
293 // method. The other issue is that we need to call this method from
294 // selectionDidChange but ONLY when it was the result of a user-initiated
295 // action and not the stack viewer updating causing a selection change.
296 // If it happens in the latter, then we run into the same issue that causes
298 [self performSelector
:@selector(expandVariables
) withObject
:nil afterDelay
:0.05];
302 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
304 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
306 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
307 [expandedVariables addObject
:[[node representedObject
] fullName
]];
309 [connection loadVariableNode
:[node representedObject
]
310 forStackFrame
:[[stackArrayController selectedObjects
] lastObject
]];
314 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
316 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
318 [expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullName
]];
324 * Does the actual updating of the source viewer by reading in the file
326 - (void)updateSourceViewer
328 NSArray
* selection
= [stackArrayController selectedObjects
];
329 if (!selection ||
[selection count
] < 1)
331 if ([selection count
] > 1)
332 NSLog(@
"INVALID SELECTION");
333 StackFrame
* frame
= [selection objectAtIndex
:0];
336 [connection loadStackFrame
:frame
];
341 NSString
* filename
= [[NSURL URLWithString
:frame.filename
] path
];
342 if ([filename isEqualToString
:@
""])
345 // Replace the source if necessary.
346 if (frame.source
&& ![sourceViewer.file isEqualToString
:filename
])
348 [sourceViewer setString
:frame.source asFile
:filename
];
350 NSSet
* breakpoints
= [NSSet setWithArray
:[[BreakpointManager sharedManager
] breakpointsForFile
:filename
]];
351 [sourceViewer setMarkers
:breakpoints
];
354 [sourceViewer setMarkedLine
:frame.lineNumber
];
355 [sourceViewer scrollToLine
:frame.lineNumber
];
357 [[sourceViewer textView
] setNeedsDisplay
:YES
];
361 * Expands the variables based on the stored set
363 - (void)expandVariables
365 NSString
* selection
= [selectedVariable fullName
];
367 for (NSInteger i
= 0; i
< [variablesOutlineView numberOfRows
]; i
++) {
368 NSTreeNode
* node
= [variablesOutlineView itemAtRow
:i
];
369 NSString
* fullName
= [[node representedObject
] fullName
];
371 // see if it needs expanding
372 if ([expandedVariables containsObject
:fullName
])
373 [variablesOutlineView expandItem
:node
];
375 // select it if we had it selected before
376 if ([fullName isEqualToString
:selection
])
377 [variablesTreeController setSelectionIndexPath
:[node indexPath
]];
382 * Sets the widths of the segmented control.
384 - (void)updateSegmentControl
{
385 NSRect containerFrame
= [[_segmentControl superview
] frame
];
386 CGFloat containerWidth
= NSWidth(containerFrame
);
387 CGFloat segmentSizes
= 0;
388 for (NSInteger i
= 1; i
< [_segmentControl segmentCount
] - 1; ++i
) {
389 segmentSizes
+= [_segmentControl widthForSegment
:i
];
391 CGFloat spacerWidth
= (containerWidth
- segmentSizes
) / 2;
392 [_segmentControl setWidth
:spacerWidth forSegment
:0];
393 [_segmentControl setWidth
:spacerWidth forSegment
:[_segmentControl segmentCount
] - 1];
395 [_segmentControl setFrame
:NSMakeRect(-1, NSHeight(containerFrame
) - 27, containerWidth
+ 2, 30)];
398 #pragma mark BSSourceView Delegate
401 * The gutter was clicked, which indicates that a breakpoint needs to be changed
403 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
405 BreakpointManager
* mngr
= [BreakpointManager sharedManager
];
407 if ([mngr hasBreakpointAt
:line inFile
:file
])
409 [mngr removeBreakpointAt
:line inFile
:file
];
413 Breakpoint
* bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
414 [mngr addBreakpoint
:bp
];
418 [sourceViewer setMarkers
:[NSSet setWithArray
:[mngr breakpointsForFile
:file
]]];
419 [sourceViewer setNeedsDisplay
:YES
];