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
] initWithPort
:[defaults integerForKey
:kPrefPort
]
64 autoAttach
:[defaults boolForKey
:kPrefDebuggerAttached
]];
65 _connection.model
= _model
;
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];
88 [_connection release
];
90 [_breakpointsController release
];
91 [_evalController release
];
92 [_expandedVariables release
];
94 [_sourceViewer release
];
97 [_attachedCheckbox release
];
98 [_stackArrayController release
];
99 [_variablesTreeController release
];
100 [_variablesOutlineView release
];
102 [_segmentControl release
];
109 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
113 [[self window
] setExcludedFromWindowsMenu
:YES
];
114 [[self window
] setTitle
:[NSString stringWithFormat
:@
"MacGDBp @ %d", [_connection port
]]];
115 [_sourceViewer setDelegate
:self];
116 [_stackArrayController setSortDescriptors
:@
[ [[[NSSortDescriptor alloc
] initWithKey
:@
"index" ascending
:YES
] autorelease
] ]];
117 [_stackArrayController addObserver
:self
118 forKeyPath
:@
"selectedObjects"
119 options
:NSKeyValueObservingOptionNew
121 [_stackArrayController addObserver
:self
122 forKeyPath
:@
"selection.source"
123 options
:NSKeyValueObservingOptionNew
125 self.connection.autoAttach
= [_attachedCheckbox state
] == NSOnState
;
127 // Load view controllers into the tab views.
128 _breakpointsController
= [[BreakpointController alloc
] initWithBreakpointManager
:_model.breakpointManager
129 sourceView
:_sourceViewer
];
130 [[self.tabView tabViewItemAtIndex
:1] setView
:_breakpointsController.view
];
132 _evalController
= [[EvalController alloc
] initWithBackEnd
:_connection
];
133 [[self.tabView tabViewItemAtIndex
:2] setView
:_evalController.view
];
135 // When the segment control's selection changes, update the tab view.
136 [[_segmentControl cell
] addObserver
:self
137 forKeyPath
:@
"selectedSegment"
140 // When the segment control's superview changes, recalculate the spacer
142 [[_segmentControl superview
] addObserver
:self
147 NSUInteger selectedSegment
=
148 [[[NSUserDefaults standardUserDefaults
] valueForKey
:kPrefSelectedDebuggerSegment
] intValue
];
149 [[_segmentControl cell
] setSelectedSegment
:selectedSegment
];
150 [self updateSegmentControl
];
154 * Key-value observation routine.
156 - (void)observeValueForKeyPath
:(NSString
*)keyPath
158 change
:(NSDictionary
<NSString
*,id>*)change
159 context
:(void*)context
{
160 if (object
== _stackArrayController
&& [keyPath isEqualToString
:@
"selectedObjects"]) {
161 for (StackFrame
* frame
in _stackArrayController.selectedObjects
)
162 [_connection loadStackFrame
:frame
];
163 } else if (object
== _stackArrayController
&& [keyPath isEqualToString
:@
"selection.source"]) {
164 [self updateSourceViewer
];
165 } else if (object
== _model
) {
166 if ([keyPath isEqualToString
:@
"connected"]) {
167 if ([change
[NSKeyValueChangeNewKey
] boolValue
]) {
168 [self debuggerConnected
];
170 [self debuggerDisconnected
];
173 } else if (object
== _segmentControl.cell
) {
174 [[NSUserDefaults standardUserDefaults
] setValue
:@
(_segmentControl.selectedSegment
)
175 forKey
:kPrefSelectedDebuggerSegment
];
176 [_tabView selectTabViewItemAtIndex
:_segmentControl.selectedSegment
- 1];
177 } else if (object
== _segmentControl.superview
) {
178 [self updateSegmentControl
];
180 [super observeValueForKeyPath
:keyPath ofObject
:object change
:change context
:context
];
185 * Validates the menu items for the "Debugger" menu
187 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
189 SEL action
= [anItem action
];
191 if (action
== @selector(stepOut
:)) {
192 return _model.connected
&& _model.stackDepth
> 1;
193 } else if (action
== @selector(stepIn
:) ||
194 action
== @selector(stepOver
:) ||
195 action
== @selector(run
:) ||
196 action
== @selector(stop
:)) {
197 return _model.connected
;
199 return [[self window
] validateUserInterfaceItem
:anItem
];
203 * Shows the inspector window
205 - (IBAction
)showInspectorWindow
:(id)sender
207 if (![_inspector isVisible
])
208 [_inspector makeKeyAndOrderFront
:sender
];
210 [_inspector orderOut
:sender
];
214 * Runs the eval window sheet.
216 - (IBAction
)showEvalWindow
:(id)sender
218 [self.segmentControl setSelectedSegment
:3];
222 * Delegate function for GDBpConnection for when the debugger connects.
224 - (void)debuggerConnected
226 if (!self.connection.autoAttach
)
228 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:kPrefBreakOnFirstLine
])
230 // Do not cache the file between debugger executions.
231 _sourceViewer.file
= nil;
235 * Called once the debugger disconnects.
237 - (void)debuggerDisconnected
239 // Invalidate the marked line so we don't look like we're still running.
240 _sourceViewer.markedLine
= -1;
241 [_sourceViewer setNeedsDisplay
:YES
];
245 * Forwards the message to run script execution to the connection
247 - (IBAction
)run
:(id)sender
252 - (IBAction
)attachedToggled
:(id)sender
254 _connection.autoAttach
= [sender state
] == NSOnState
;
258 * Forwards the message to "step in" to the connection
260 - (IBAction
)stepIn
:(id)sender
262 if ([[_variablesTreeController selectedObjects
] count
] > 0)
263 _selectedVariable
= [[_variablesTreeController selectedObjects
] objectAtIndex
:0];
265 [_connection stepIn
];
269 * Forwards the message to "step out" to the connection
271 - (IBAction
)stepOut
:(id)sender
273 if ([[_variablesTreeController selectedObjects
] count
] > 0)
274 _selectedVariable
= [[_variablesTreeController selectedObjects
] objectAtIndex
:0];
276 [_connection stepOut
];
280 * Forwards the message to "step over" to the connection
282 - (IBAction
)stepOver
:(id)sender
284 if ([[_variablesTreeController selectedObjects
] count
] > 0)
285 _selectedVariable
= [[_variablesTreeController selectedObjects
] objectAtIndex
:0];
287 [_connection stepOver
];
291 * Forwards the detach/"stop" message to the back end.
293 - (IBAction
)stop
:(id)sender
299 * NSTableView delegate method that informs the controller that the stack selection did change and that
300 * we should update the source viewer
302 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
304 // TODO: This is very, very hacky because it's nondeterministic. The issue
305 // is that calling |-[NSOutlineView expandItem:]| while the table is still
306 // doing its redraw will translate to a no-op. Instead, we need to restructure
307 // this controller so that when everything has been laid out we call
308 // |-expandVariables|; but NSOutlineView doesn't have a |-didFinishDoingCrap:|
309 // method. The other issue is that we need to call this method from
310 // selectionDidChange but ONLY when it was the result of a user-initiated
311 // action and not the stack viewer updating causing a selection change.
312 // If it happens in the latter, then we run into the same issue that causes
314 [self performSelector
:@selector(expandVariables
) withObject
:nil afterDelay
:0.05];
318 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
320 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
322 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
323 [_expandedVariables addObject
:[[node representedObject
] fullName
]];
325 [_connection loadVariableNode
:[node representedObject
]
326 forStackFrame
:[[_stackArrayController selectedObjects
] lastObject
]];
330 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
332 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
334 [_expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullName
]];
340 * Does the actual updating of the source viewer by reading in the file
342 - (void)updateSourceViewer
344 NSArray
* selection
= [_stackArrayController selectedObjects
];
345 if (!selection ||
[selection count
] < 1)
347 if ([selection count
] > 1)
348 NSLog(@
"INVALID SELECTION");
349 StackFrame
* frame
= [selection objectAtIndex
:0];
352 [_connection loadStackFrame
:frame
];
357 NSString
* filename
= [[NSURL URLWithString
:frame.filename
] path
];
358 if ([filename isEqualToString
:@
""])
361 // Replace the source if necessary.
362 if (frame.source
&& ![_sourceViewer.file isEqualToString
:filename
])
364 [_sourceViewer setString
:frame.source asFile
:filename
];
366 NSSet
<NSNumber
*>* breakpoints
= [_model.breakpointManager breakpointsForFile
:filename
];
367 [_sourceViewer setMarkers
:breakpoints
];
370 [_sourceViewer setMarkedLine
:frame.lineNumber
];
371 [_sourceViewer scrollToLine
:frame.lineNumber
];
373 [[_sourceViewer textView
] setNeedsDisplay
:YES
];
377 * Expands the variables based on the stored set
379 - (void)expandVariables
381 NSString
* selection
= [_selectedVariable fullName
];
383 for (NSInteger i
= 0; i
< [_variablesOutlineView numberOfRows
]; i
++) {
384 NSTreeNode
* node
= [_variablesOutlineView itemAtRow
:i
];
385 NSString
* fullName
= [[node representedObject
] fullName
];
387 // see if it needs expanding
388 if ([_expandedVariables containsObject
:fullName
])
389 [_variablesOutlineView expandItem
:node
];
391 // select it if we had it selected before
392 if ([fullName isEqualToString
:selection
])
393 [_variablesTreeController setSelectionIndexPath
:[node indexPath
]];
398 * Sets the widths of the segmented control.
400 - (void)updateSegmentControl
{
401 NSRect containerFrame
= [[_segmentControl superview
] frame
];
402 CGFloat containerWidth
= NSWidth(containerFrame
);
403 CGFloat segmentSizes
= 0;
404 for (NSInteger i
= 1; i
< [_segmentControl segmentCount
] - 1; ++i
) {
405 segmentSizes
+= [_segmentControl widthForSegment
:i
];
407 CGFloat spacerWidth
= (containerWidth
- segmentSizes
) / 2;
408 [_segmentControl setWidth
:spacerWidth forSegment
:0];
409 [_segmentControl setWidth
:spacerWidth forSegment
:[_segmentControl segmentCount
] - 1];
411 [_segmentControl setFrame
:NSMakeRect(-5, NSHeight(containerFrame
) - 27, containerWidth
+ 10, 30)];
414 #pragma mark BSSourceView Delegate
417 * The gutter was clicked, which indicates that a breakpoint needs to be changed
419 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
421 BreakpointManager
* manager
= _model.breakpointManager
;
422 Breakpoint
* breakpoint
= [Breakpoint breakpointAtLine
:line inFile
:file
];
424 if ([manager hasBreakpoint
:breakpoint
]) {
425 [manager removeBreakpoint
:breakpoint
];
427 [manager addBreakpoint
:breakpoint
];
430 [_sourceViewer setMarkers
:[manager breakpointsForFile
:file
]];