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 "FileAccessController.h"
27 #import "PreferenceNames.h"
28 #import "NSXMLElementAdditions.h"
29 #import "StackFrame.h"
31 @interface DebuggerController (Private
)
32 - (void)updateSourceViewer
;
33 - (void)expandVariables
;
36 @implementation DebuggerController
{
37 DebuggerModel
* _model
;
39 DebuggerBackEnd
* _connection
;
41 BreakpointController
* _breakpointsController
;
42 EvalController
* _evalController
;
44 NSMutableSet
* _expandedVariables
;
45 VariableNode
* _selectedVariable
;
49 * Initializes the window controller and sets the connection using preference
54 if (self = [super initWithWindowNibName
:@
"Debugger"])
56 NSUserDefaults
* defaults
= [NSUserDefaults standardUserDefaults
];
58 _model
= [[DebuggerModel alloc
] init
];
59 [_model addObserver
:self
60 forKeyPath
:@
"connected"
61 options
:NSKeyValueObservingOptionNew
64 _connection
= [[DebuggerBackEnd alloc
] initWithModel
:_model
65 port
:[defaults integerForKey
:kPrefPort
]
66 autoAttach
:[defaults boolForKey
:kPrefDebuggerAttached
]];
67 _model.breakpointManager.connection
= _connection
;
69 [_model addObserver
:self
71 options
:NSKeyValueObservingOptionNew
74 _expandedVariables
= [[NSMutableSet alloc
] init
];
75 [[self window
] makeKeyAndOrderFront
:nil];
76 [[self window
] setDelegate
:self];
78 if ([defaults boolForKey
:kPrefInspectorWindowVisible
])
79 [_inspector orderFront
:self];
85 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
89 // Exclude from the Windows menu because there is an explicit entry.
90 [[self window
] setExcludedFromWindowsMenu
:YES
];
92 // Connect to XIB properties.
93 [_sourceViewer setDelegate
:self];
94 [_stackArrayController setSortDescriptors
:@
[ [[NSSortDescriptor alloc
] initWithKey
:@
"index" ascending
:YES
] ]];
95 [_stackArrayController addObserver
:self
96 forKeyPath
:@
"selectedObjects"
97 options
:NSKeyValueObservingOptionNew
99 [_stackArrayController addObserver
:self
100 forKeyPath
:@
"selection.source"
101 options
:NSKeyValueObservingOptionNew
103 self.connection.autoAttach
= [_attachedCheckbox state
] == NSOnState
;
105 // Load view controllers into the tab views.
106 _breakpointsController
= [[BreakpointController alloc
] initWithBreakpointManager
:_model.breakpointManager
107 sourceView
:_sourceViewer
];
108 [[self.tabView tabViewItemAtIndex
:1] setView
:_breakpointsController.view
];
110 _evalController
= [[EvalController alloc
] initWithBackEnd
:_connection
];
111 [[self.tabView tabViewItemAtIndex
:2] setView
:_evalController.view
];
113 // When the segment control's selection changes, update the tab view.
114 [[_segmentControl cell
] addObserver
:self
115 forKeyPath
:@
"selectedSegment"
118 // When the segment control's superview changes, recalculate the spacer
120 [[_segmentControl superview
] addObserver
:self
125 NSUInteger selectedSegment
=
126 [[[NSUserDefaults standardUserDefaults
] valueForKey
:kPrefSelectedDebuggerSegment
] intValue
];
127 [[_segmentControl cell
] setSelectedSegment
:selectedSegment
];
128 [self updateSegmentControl
];
132 * Key-value observation routine.
134 - (void)observeValueForKeyPath
:(NSString
*)keyPath
136 change
:(NSDictionary
<NSString
*,id>*)change
137 context
:(void*)context
{
138 if (object
== _stackArrayController
&& [keyPath isEqualToString
:@
"selectedObjects"]) {
139 for (StackFrame
* frame
in _stackArrayController.selectedObjects
)
140 [_connection loadStackFrame
:frame
];
141 } else if (object
== _stackArrayController
&& [keyPath isEqualToString
:@
"selection.source"]) {
142 [self updateSourceViewer
];
143 } else if (object
== _model
) {
144 if ([keyPath isEqualToString
:@
"connected"]) {
145 if ([change
[NSKeyValueChangeNewKey
] boolValue
]) {
146 [self debuggerConnected
];
148 [self debuggerDisconnected
];
151 } else if (object
== _segmentControl.cell
) {
152 [[NSUserDefaults standardUserDefaults
] setValue
:@
(_segmentControl.selectedSegment
)
153 forKey
:kPrefSelectedDebuggerSegment
];
154 [_tabView selectTabViewItemAtIndex
:_segmentControl.selectedSegment
- 1];
155 } else if (object
== _segmentControl.superview
) {
156 [self updateSegmentControl
];
158 [super observeValueForKeyPath
:keyPath ofObject
:object change
:change context
:context
];
163 * Validates the menu items for the "Debugger" menu
165 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
167 SEL action
= [anItem action
];
169 if (action
== @selector(stepOut
:)) {
170 return _model.connected
&& _model.stackDepth
> 1;
171 } else if (action
== @selector(stepIn
:) ||
172 action
== @selector(stepOver
:) ||
173 action
== @selector(run
:) ||
174 action
== @selector(stop
:)) {
175 return _model.connected
;
177 return [[self window
] validateUserInterfaceItem
:anItem
];
181 * Shows the inspector window
183 - (IBAction
)showInspectorWindow
:(id)sender
185 if (![_inspector isVisible
])
186 [_inspector makeKeyAndOrderFront
:sender
];
188 [_inspector orderOut
:sender
];
192 * Runs the eval window sheet.
194 - (IBAction
)showEvalWindow
:(id)sender
196 [self.segmentControl setSelectedSegment
:3];
200 * Delegate function for GDBpConnection for when the debugger connects.
202 - (void)debuggerConnected
204 if (!self.connection.autoAttach
)
206 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:kPrefBreakOnFirstLine
])
208 // Do not cache the file between debugger executions.
209 _sourceViewer.file
= nil;
213 * Called once the debugger disconnects.
215 - (void)debuggerDisconnected
217 // Invalidate the marked line so we don't look like we're still running.
218 _sourceViewer.markedLine
= -1;
219 [_sourceViewer setNeedsDisplay
:YES
];
223 * Forwards the message to run script execution to the connection
225 - (IBAction
)run
:(id)sender
230 - (IBAction
)attachedToggled
:(id)sender
232 _connection.autoAttach
= [sender state
] == NSOnState
;
236 * Forwards the message to "step in" to the connection
238 - (IBAction
)stepIn
:(id)sender
240 if ([[_variablesTreeController selectedObjects
] count
] > 0)
241 _selectedVariable
= [[_variablesTreeController selectedObjects
] objectAtIndex
:0];
243 [_connection stepIn
];
247 * Forwards the message to "step out" to the connection
249 - (IBAction
)stepOut
:(id)sender
251 if ([[_variablesTreeController selectedObjects
] count
] > 0)
252 _selectedVariable
= [[_variablesTreeController selectedObjects
] objectAtIndex
:0];
254 [_connection stepOut
];
258 * Forwards the message to "step over" to the connection
260 - (IBAction
)stepOver
:(id)sender
262 if ([[_variablesTreeController selectedObjects
] count
] > 0)
263 _selectedVariable
= [[_variablesTreeController selectedObjects
] objectAtIndex
:0];
265 [_connection stepOver
];
269 * Forwards the detach/"stop" message to the back end.
271 - (IBAction
)stop
:(id)sender
277 * NSTableView delegate method that informs the controller that the stack selection did change and that
278 * we should update the source viewer
280 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
282 // TODO: This is very, very hacky because it's nondeterministic. The issue
283 // is that calling |-[NSOutlineView expandItem:]| while the table is still
284 // doing its redraw will translate to a no-op. Instead, we need to restructure
285 // this controller so that when everything has been laid out we call
286 // |-expandVariables|; but NSOutlineView doesn't have a |-didFinishDoingCrap:|
287 // method. The other issue is that we need to call this method from
288 // selectionDidChange but ONLY when it was the result of a user-initiated
289 // action and not the stack viewer updating causing a selection change.
290 // If it happens in the latter, then we run into the same issue that causes
292 [self performSelector
:@selector(expandVariables
) withObject
:nil afterDelay
:0.05];
296 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
298 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
300 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
301 [_expandedVariables addObject
:[[node representedObject
] fullName
]];
303 [_connection loadVariableNode
:[node representedObject
]
304 forStackFrame
:[[_stackArrayController selectedObjects
] lastObject
]];
308 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
310 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
312 [_expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullName
]];
318 * Does the actual updating of the source viewer by reading in the file
320 - (void)updateSourceViewer
322 NSArray
* selection
= [_stackArrayController selectedObjects
];
323 if (!selection ||
[selection count
] < 1)
325 if ([selection count
] > 1)
326 NSLog(@
"INVALID SELECTION");
327 StackFrame
* frame
= [selection objectAtIndex
:0];
330 [_connection loadStackFrame
:frame
];
335 NSString
* filename
= [[NSURL URLWithString
:frame.filename
] path
];
336 if ([filename isEqualToString
:@
""])
339 // Replace the source if necessary.
340 if (frame.source
&& ![_sourceViewer.file isEqualToString
:filename
])
342 [_sourceViewer setString
:frame.source asFile
:filename
];
344 NSSet
<NSNumber
*>* breakpoints
= [_model.breakpointManager breakpointsForFile
:filename
];
345 [_sourceViewer setMarkers
:breakpoints
];
348 [_sourceViewer setMarkedLine
:frame.lineNumber
];
349 [_sourceViewer scrollToLine
:frame.lineNumber
];
351 [[_sourceViewer textView
] setNeedsDisplay
:YES
];
355 * Expands the variables based on the stored set
357 - (void)expandVariables
359 NSString
* selection
= [_selectedVariable fullName
];
361 for (NSInteger i
= 0; i
< [_variablesOutlineView numberOfRows
]; i
++) {
362 NSTreeNode
* node
= [_variablesOutlineView itemAtRow
:i
];
363 NSString
* fullName
= [[node representedObject
] fullName
];
365 // see if it needs expanding
366 if ([_expandedVariables containsObject
:fullName
])
367 [_variablesOutlineView expandItem
:node
];
369 // select it if we had it selected before
370 if ([fullName isEqualToString
:selection
])
371 [_variablesTreeController setSelectionIndexPath
:[node indexPath
]];
376 * Sets the widths of the segmented control.
378 - (void)updateSegmentControl
{
379 NSRect containerFrame
= [[_segmentControl superview
] frame
];
380 CGFloat containerWidth
= NSWidth(containerFrame
);
381 CGFloat segmentSizes
= 0;
382 for (NSInteger i
= 1; i
< [_segmentControl segmentCount
] - 1; ++i
) {
383 segmentSizes
+= [_segmentControl widthForSegment
:i
];
385 CGFloat spacerWidth
= (containerWidth
- segmentSizes
) / 2;
386 [_segmentControl setWidth
:spacerWidth forSegment
:0];
387 [_segmentControl setWidth
:spacerWidth forSegment
:[_segmentControl segmentCount
] - 1];
389 [_segmentControl setFrame
:NSMakeRect(-5, NSHeight(containerFrame
) - 27, containerWidth
+ 10, 30)];
392 #pragma mark BSSourceView Delegate
395 * The gutter was clicked, which indicates that a breakpoint needs to be changed
397 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
399 BreakpointManager
* manager
= _model.breakpointManager
;
400 Breakpoint
* breakpoint
= [Breakpoint breakpointAtLine
:line inFile
:file
];
402 if ([manager hasBreakpoint
:breakpoint
]) {
403 [manager removeBreakpoint
:breakpoint
];
405 [manager addBreakpoint
:breakpoint
];
408 [_sourceViewer setMarkers
:[manager breakpointsForFile
:file
]];
411 - (void)error
:(NSError
*)error whileHighlightingFile
:(NSString
*)file
414 if (error.code
== NSFileReadNoPermissionError
) {
415 [FileAccessController showFileAccessDialog
];
417 #endif // USE_APP_SANDBOX