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 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 self.connection.autoAttach
= [_attachedCheckbox state
] == NSOnState
;
104 // Load view controllers into the tab views.
105 _breakpointsController
= [[BreakpointController alloc
] initWithBreakpointManager
:_model.breakpointManager
106 sourceView
:_sourceViewer
];
107 [[self.tabView tabViewItemAtIndex
:1] setView
:_breakpointsController.view
];
109 _evalController
= [[EvalController alloc
] initWithBackEnd
:_connection
];
110 [[self.tabView tabViewItemAtIndex
:2] setView
:_evalController.view
];
112 // When the segment control's selection changes, update the tab view.
113 [[_segmentControl cell
] addObserver
:self
114 forKeyPath
:@
"selectedSegment"
117 // When the segment control's superview changes, recalculate the spacer
119 [[_segmentControl superview
] addObserver
:self
124 NSUInteger selectedSegment
=
125 [[[NSUserDefaults standardUserDefaults
] valueForKey
:kPrefSelectedDebuggerSegment
] intValue
];
126 [[_segmentControl cell
] setSelectedSegment
:selectedSegment
];
127 [self updateSegmentControl
];
131 * Key-value observation routine.
133 - (void)observeValueForKeyPath
:(NSString
*)keyPath
135 change
:(NSDictionary
<NSString
*,id>*)change
136 context
:(void*)context
{
137 if (object
== _stackArrayController
&& [keyPath isEqualToString
:@
"selectedObjects"]) {
138 for (StackFrame
* frame
in _stackArrayController.selectedObjects
)
139 [_connection loadStackFrame
:frame
];
140 } else if (object
== _stackArrayController
&& [keyPath isEqualToString
:@
"selection.source"]) {
141 [self updateSourceViewer
];
142 } else if (object
== _model
) {
143 if ([keyPath isEqualToString
:@
"connected"]) {
144 if ([change
[NSKeyValueChangeNewKey
] boolValue
]) {
145 [self debuggerConnected
];
147 [self debuggerDisconnected
];
150 } else if (object
== _segmentControl.cell
) {
151 [[NSUserDefaults standardUserDefaults
] setValue
:@
(_segmentControl.selectedSegment
)
152 forKey
:kPrefSelectedDebuggerSegment
];
153 [_tabView selectTabViewItemAtIndex
:_segmentControl.selectedSegment
- 1];
154 } else if (object
== _segmentControl.superview
) {
155 [self updateSegmentControl
];
157 [super observeValueForKeyPath
:keyPath ofObject
:object change
:change context
:context
];
162 * Validates the menu items for the "Debugger" menu
164 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
166 SEL action
= [anItem action
];
168 if (action
== @selector(stepOut
:)) {
169 return _model.connected
&& _model.stackDepth
> 1;
170 } else if (action
== @selector(stepIn
:) ||
171 action
== @selector(stepOver
:) ||
172 action
== @selector(run
:) ||
173 action
== @selector(stop
:)) {
174 return _model.connected
;
176 return [[self window
] validateUserInterfaceItem
:anItem
];
180 * Shows the inspector window
182 - (IBAction
)showInspectorWindow
:(id)sender
184 if (![_inspector isVisible
])
185 [_inspector makeKeyAndOrderFront
:sender
];
187 [_inspector orderOut
:sender
];
191 * Runs the eval window sheet.
193 - (IBAction
)showEvalWindow
:(id)sender
195 [self.segmentControl setSelectedSegment
:3];
199 * Delegate function for GDBpConnection for when the debugger connects.
201 - (void)debuggerConnected
203 if (!self.connection.autoAttach
)
205 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:kPrefBreakOnFirstLine
])
207 // Do not cache the file between debugger executions.
208 _sourceViewer.file
= nil;
212 * Called once the debugger disconnects.
214 - (void)debuggerDisconnected
216 // Invalidate the marked line so we don't look like we're still running.
217 _sourceViewer.markedLine
= -1;
218 [_sourceViewer setNeedsDisplay
:YES
];
222 * Forwards the message to run script execution to the connection
224 - (IBAction
)run
:(id)sender
229 - (IBAction
)attachedToggled
:(id)sender
231 _connection.autoAttach
= [sender state
] == NSOnState
;
235 * Forwards the message to "step in" to the connection
237 - (IBAction
)stepIn
:(id)sender
239 if ([[_variablesTreeController selectedObjects
] count
] > 0)
240 _selectedVariable
= [[_variablesTreeController selectedObjects
] objectAtIndex
:0];
242 [_connection stepIn
];
246 * Forwards the message to "step out" to the connection
248 - (IBAction
)stepOut
:(id)sender
250 if ([[_variablesTreeController selectedObjects
] count
] > 0)
251 _selectedVariable
= [[_variablesTreeController selectedObjects
] objectAtIndex
:0];
253 [_connection stepOut
];
257 * Forwards the message to "step over" to the connection
259 - (IBAction
)stepOver
:(id)sender
261 if ([[_variablesTreeController selectedObjects
] count
] > 0)
262 _selectedVariable
= [[_variablesTreeController selectedObjects
] objectAtIndex
:0];
264 [_connection stepOver
];
268 * Forwards the detach/"stop" message to the back end.
270 - (IBAction
)stop
:(id)sender
276 * NSTableView delegate method that informs the controller that the stack selection did change and that
277 * we should update the source viewer
279 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
281 // TODO: This is very, very hacky because it's nondeterministic. The issue
282 // is that calling |-[NSOutlineView expandItem:]| while the table is still
283 // doing its redraw will translate to a no-op. Instead, we need to restructure
284 // this controller so that when everything has been laid out we call
285 // |-expandVariables|; but NSOutlineView doesn't have a |-didFinishDoingCrap:|
286 // method. The other issue is that we need to call this method from
287 // selectionDidChange but ONLY when it was the result of a user-initiated
288 // action and not the stack viewer updating causing a selection change.
289 // If it happens in the latter, then we run into the same issue that causes
291 [self performSelector
:@selector(expandVariables
) withObject
:nil afterDelay
:0.05];
295 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
297 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
299 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
300 [_expandedVariables addObject
:[[node representedObject
] fullName
]];
302 [_connection loadVariableNode
:[node representedObject
]
303 forStackFrame
:[[_stackArrayController selectedObjects
] lastObject
]];
307 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
309 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
311 [_expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullName
]];
317 * Does the actual updating of the source viewer by reading in the file
319 - (void)updateSourceViewer
321 NSArray
* selection
= [_stackArrayController selectedObjects
];
322 if (!selection ||
[selection count
] < 1)
324 if ([selection count
] > 1)
325 NSLog(@
"INVALID SELECTION");
326 StackFrame
* frame
= [selection objectAtIndex
:0];
329 [_connection loadStackFrame
:frame
];
334 NSString
* filename
= [[NSURL URLWithString
:frame.filename
] path
];
335 if ([filename isEqualToString
:@
""])
338 // Replace the source if necessary.
339 if (frame.source
&& ![_sourceViewer.file isEqualToString
:filename
])
341 [_sourceViewer setString
:frame.source asFile
:filename
];
343 NSSet
<NSNumber
*>* breakpoints
= [_model.breakpointManager breakpointsForFile
:filename
];
344 [_sourceViewer setMarkers
:breakpoints
];
347 [_sourceViewer setMarkedLine
:frame.lineNumber
];
348 [_sourceViewer scrollToLine
:frame.lineNumber
];
350 [[_sourceViewer textView
] setNeedsDisplay
:YES
];
354 * Expands the variables based on the stored set
356 - (void)expandVariables
358 NSString
* selection
= [_selectedVariable fullName
];
360 for (NSInteger i
= 0; i
< [_variablesOutlineView numberOfRows
]; i
++) {
361 NSTreeNode
* node
= [_variablesOutlineView itemAtRow
:i
];
362 NSString
* fullName
= [[node representedObject
] fullName
];
364 // see if it needs expanding
365 if ([_expandedVariables containsObject
:fullName
])
366 [_variablesOutlineView expandItem
:node
];
368 // select it if we had it selected before
369 if ([fullName isEqualToString
:selection
])
370 [_variablesTreeController setSelectionIndexPath
:[node indexPath
]];
375 * Sets the widths of the segmented control.
377 - (void)updateSegmentControl
{
378 NSRect containerFrame
= [[_segmentControl superview
] frame
];
379 CGFloat containerWidth
= NSWidth(containerFrame
);
380 CGFloat segmentSizes
= 0;
381 for (NSInteger i
= 1; i
< [_segmentControl segmentCount
] - 1; ++i
) {
382 segmentSizes
+= [_segmentControl widthForSegment
:i
];
384 CGFloat spacerWidth
= (containerWidth
- segmentSizes
) / 2;
385 [_segmentControl setWidth
:spacerWidth forSegment
:0];
386 [_segmentControl setWidth
:spacerWidth forSegment
:[_segmentControl segmentCount
] - 1];
388 [_segmentControl setFrame
:NSMakeRect(-5, NSHeight(containerFrame
) - 27, containerWidth
+ 10, 30)];
391 #pragma mark BSSourceView Delegate
394 * The gutter was clicked, which indicates that a breakpoint needs to be changed
396 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
398 BreakpointManager
* manager
= _model.breakpointManager
;
399 Breakpoint
* breakpoint
= [Breakpoint breakpointAtLine
:line inFile
:file
];
401 if ([manager hasBreakpoint
:breakpoint
]) {
402 [manager removeBreakpoint
:breakpoint
];
404 [manager addBreakpoint
:breakpoint
];
407 [_sourceViewer setMarkers
:[manager breakpointsForFile
:file
]];