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 "StackFrame.h"
30 @interface DebuggerController (Private
)
31 - (void)updateSourceViewer
;
32 - (void)expandVariables
;
35 @implementation DebuggerController
{
36 DebuggerModel
* _model
;
38 DebuggerBackEnd
* _connection
;
40 BreakpointController
* _breakpointsController
;
41 EvalController
* _evalController
;
43 NSMutableSet
* _expandedVariables
;
44 VariableNode
* _selectedVariable
;
48 * Initializes the window controller and sets the connection using preference
53 if (self = [super initWithWindowNibName
:@
"Debugger"])
55 NSUserDefaults
* defaults
= [NSUserDefaults standardUserDefaults
];
57 _model
= [[DebuggerModel alloc
] init
];
58 [_model addObserver
:self
59 forKeyPath
:@
"connected"
60 options
:NSKeyValueObservingOptionNew
63 _connection
= [[DebuggerBackEnd alloc
] initWithModel
:_model
64 port
:[defaults integerForKey
:kPrefPort
]
65 autoAttach
:[defaults boolForKey
:kPrefDebuggerAttached
]];
66 _model.breakpointManager.connection
= _connection
;
68 [_model addObserver
:self
70 options
:NSKeyValueObservingOptionNew
73 _expandedVariables
= [[NSMutableSet alloc
] init
];
74 [[self window
] makeKeyAndOrderFront
:nil];
75 [[self window
] setDelegate
:self];
77 if ([defaults boolForKey
:kPrefInspectorWindowVisible
])
78 [_inspector orderFront
:self];
84 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
88 // Exclude from the Windows menu because there is an explicit entry.
89 [[self window
] setExcludedFromWindowsMenu
:YES
];
91 // Connect to XIB properties.
92 [_sourceViewer setDelegate
:self];
93 [_stackArrayController setSortDescriptors
:@
[ [[NSSortDescriptor alloc
] initWithKey
:@
"index" ascending
:YES
] ]];
94 [_stackArrayController addObserver
:self
95 forKeyPath
:@
"selectedObjects"
96 options
:NSKeyValueObservingOptionNew
98 [_stackArrayController addObserver
:self
99 forKeyPath
:@
"selection.source"
100 options
:NSKeyValueObservingOptionNew
102 [_variablesTreeController addObserver
:self
103 forKeyPath
:@
"arrangedObjects"
104 options
:NSKeyValueObservingOptionNew
106 self.connection.autoAttach
= [_attachedCheckbox state
] == NSOnState
;
108 // Load view controllers into the tab views.
109 _breakpointsController
= [[BreakpointController alloc
] initWithBreakpointManager
:_model.breakpointManager
110 sourceView
:_sourceViewer
];
111 [[self.tabView tabViewItemAtIndex
:1] setView
:_breakpointsController.view
];
113 _evalController
= [[EvalController alloc
] initWithBackEnd
:_connection
];
114 [[self.tabView tabViewItemAtIndex
:2] setView
:_evalController.view
];
116 // When the segment control's selection changes, update the tab view.
117 [[_segmentControl cell
] addObserver
:self
118 forKeyPath
:@
"selectedSegment"
121 // When the segment control's superview changes, recalculate the spacer
123 [[_segmentControl superview
] addObserver
:self
128 NSUInteger selectedSegment
=
129 [[[NSUserDefaults standardUserDefaults
] valueForKey
:kPrefSelectedDebuggerSegment
] intValue
];
130 [[_segmentControl cell
] setSelectedSegment
:selectedSegment
];
131 [self updateSegmentControl
];
135 * Key-value observation routine.
137 - (void)observeValueForKeyPath
:(NSString
*)keyPath
139 change
:(NSDictionary
<NSString
*,id>*)change
140 context
:(void*)context
{
141 if (object
== _stackArrayController
&& [keyPath isEqualToString
:@
"selectedObjects"]) {
142 for (StackFrame
* frame
in _stackArrayController.selectedObjects
)
143 [_connection loadStackFrame
:frame
];
144 } else if (object
== _stackArrayController
&& [keyPath isEqualToString
:@
"selection.source"]) {
145 [self updateSourceViewer
];
146 } else if (object
== _variablesTreeController
) {
147 [self expandVariables
];
148 } else if (object
== _model
) {
149 if ([keyPath isEqualToString
:@
"connected"]) {
150 if ([change
[NSKeyValueChangeNewKey
] boolValue
]) {
151 [self debuggerConnected
];
153 [self debuggerDisconnected
];
156 } else if (object
== _segmentControl.cell
) {
157 [[NSUserDefaults standardUserDefaults
] setValue
:@
(_segmentControl.selectedSegment
)
158 forKey
:kPrefSelectedDebuggerSegment
];
159 [_tabView selectTabViewItemAtIndex
:_segmentControl.selectedSegment
- 1];
160 } else if (object
== _segmentControl.superview
) {
161 [self updateSegmentControl
];
163 [super observeValueForKeyPath
:keyPath ofObject
:object change
:change context
:context
];
168 * Validates the menu items for the "Debugger" menu
170 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
172 SEL action
= [anItem action
];
174 if (action
== @selector(stepOut
:)) {
175 return _model.connected
&& _model.stackDepth
> 1;
176 } else if (action
== @selector(stepIn
:) ||
177 action
== @selector(stepOver
:) ||
178 action
== @selector(run
:) ||
179 action
== @selector(stop
:)) {
180 return _model.connected
;
182 return [[self window
] validateUserInterfaceItem
:anItem
];
186 * Shows the inspector window
188 - (IBAction
)showInspectorWindow
:(id)sender
190 if (![_inspector isVisible
])
191 [_inspector makeKeyAndOrderFront
:sender
];
193 [_inspector orderOut
:sender
];
197 * Runs the eval window sheet.
199 - (IBAction
)showEvalWindow
:(id)sender
201 [self.segmentControl setSelectedSegment
:3];
205 * Delegate function for GDBpConnection for when the debugger connects.
207 - (void)debuggerConnected
209 if (!self.connection.autoAttach
)
211 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:kPrefBreakOnFirstLine
])
213 // Do not cache the file between debugger executions.
214 _sourceViewer.file
= nil;
215 [_expandedVariables removeAllObjects
];
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];
249 [_connection stepIn
];
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 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
285 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
287 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
288 [_expandedVariables addObject
:[[node representedObject
] fullName
]];
290 [_connection loadVariableNode
:[node representedObject
]
291 forStackFrame
:[[_stackArrayController selectedObjects
] lastObject
]];
292 [self expandVariables
];
296 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
298 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
300 [_expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullName
]];
306 * Does the actual updating of the source viewer by reading in the file
308 - (void)updateSourceViewer
310 StackFrame
* frame
= _stackArrayController.selectedObjects.firstObject
;
314 if (!frame.loaded
&& self.model.connected
) {
315 [_connection loadStackFrame
:frame
];
320 NSString
* filename
= [[NSURL URLWithString
:frame.filename
] path
];
321 if ([filename isEqualToString
:@
""])
324 if (![_sourceViewer.file isEqualToString
:filename
]) {
325 // Replace the source if necessary.
327 [_sourceViewer setString
:frame.source asFile
:filename
];
329 [_sourceViewer setFile
:filename
];
332 NSSet
<NSNumber
*>* breakpoints
= [_model.breakpointManager breakpointsForFile
:filename
];
333 [_sourceViewer setMarkers
:breakpoints
];
336 [_sourceViewer setMarkedLine
:frame.lineNumber
];
337 [_sourceViewer scrollToLine
:frame.lineNumber
];
341 * Expands the variables based on the stored set
343 - (void)expandVariables
345 NSString
* selection
= [_selectedVariable fullName
];
347 for (NSInteger i
= 0; i
< [_variablesOutlineView numberOfRows
]; i
++) {
348 NSTreeNode
* node
= [_variablesOutlineView itemAtRow
:i
];
349 NSString
* fullName
= [[node representedObject
] fullName
];
351 // see if it needs expanding
352 if ([_expandedVariables containsObject
:fullName
])
353 [_variablesOutlineView expandItem
:node
];
355 // select it if we had it selected before
356 if ([fullName isEqualToString
:selection
])
357 [_variablesTreeController setSelectionIndexPath
:[node indexPath
]];
362 * Sets the widths of the segmented control.
364 - (void)updateSegmentControl
{
365 NSRect containerFrame
= [[_segmentControl superview
] frame
];
366 CGFloat containerWidth
= NSWidth(containerFrame
);
367 CGFloat segmentSizes
= 0;
368 for (NSInteger i
= 1; i
< [_segmentControl segmentCount
] - 1; ++i
) {
369 segmentSizes
+= [_segmentControl widthForSegment
:i
];
371 CGFloat spacerWidth
= (containerWidth
- segmentSizes
) / 2;
372 [_segmentControl setWidth
:spacerWidth forSegment
:0];
373 [_segmentControl setWidth
:spacerWidth forSegment
:[_segmentControl segmentCount
] - 1];
375 [_segmentControl setFrame
:NSMakeRect(-5, NSHeight(containerFrame
) - 27, containerWidth
+ 10, 30)];
378 #pragma mark BSSourceView Delegate
381 * The gutter was clicked, which indicates that a breakpoint needs to be changed
383 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
385 BreakpointManager
* manager
= _model.breakpointManager
;
386 Breakpoint
* breakpoint
= [Breakpoint breakpointAtLine
:line inFile
:file
];
388 if ([manager hasBreakpoint
:breakpoint
]) {
389 [manager removeBreakpoint
:breakpoint
];
391 [manager addBreakpoint
:breakpoint
];
394 [_sourceViewer setMarkers
:[manager breakpointsForFile
:file
]];
397 - (void)error
:(NSError
*)error whileHighlightingFile
:(NSString
*)file
400 if (error.code
== NSFileReadNoPermissionError
) {
401 [FileAccessController showFileAccessDialog
];
403 #endif // USE_APP_SANDBOX
406 #pragma mark NSSplitView Delegate
408 - (CGFloat
)splitView
:(NSSplitView
*)splitView
409 constrainSplitPosition
:(CGFloat
)proposedPosition
410 ofSubviewAt
:(NSInteger
)dividerIndex
412 const NSSize splitViewSize
= splitView.bounds.size
;
413 const CGFloat minimumSplit
= 150;
414 const CGFloat maximumSplit
= (splitView.isVertical ? splitViewSize.width
: splitViewSize.height
) - minimumSplit
;
416 if (proposedPosition
< minimumSplit
)
418 if (proposedPosition
> maximumSplit
)
420 return proposedPosition
;