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
];
103 [_statusField release
];
110 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
114 [[self window
] setExcludedFromWindowsMenu
:YES
];
115 [[self window
] setTitle
:[NSString stringWithFormat
:@
"MacGDBp @ %d", [_connection port
]]];
116 [_sourceViewer setDelegate
:self];
117 [_stackArrayController setSortDescriptors
:@
[ [[[NSSortDescriptor alloc
] initWithKey
:@
"index" ascending
:YES
] autorelease
] ]];
118 [_stackArrayController addObserver
:self
119 forKeyPath
:@
"selectedObjects"
120 options
:NSKeyValueObservingOptionNew
122 [_stackArrayController addObserver
:self
123 forKeyPath
:@
"selection.source"
124 options
:NSKeyValueObservingOptionNew
126 self.connection.autoAttach
= [_attachedCheckbox state
] == NSOnState
;
128 // Load view controllers into the tab views.
129 _breakpointsController
= [[BreakpointController alloc
] initWithBreakpointManager
:_model.breakpointManager
130 sourceView
:_sourceViewer
];
131 [[self.tabView tabViewItemAtIndex
:1] setView
:_breakpointsController.view
];
133 _evalController
= [[EvalController alloc
] initWithBackEnd
:_connection
];
134 [[self.tabView tabViewItemAtIndex
:2] setView
:_evalController.view
];
136 // When the segment control's selection changes, update the tab view.
137 [[_segmentControl cell
] addObserver
:self
138 forKeyPath
:@
"selectedSegment"
141 // When the segment control's superview changes, recalculate the spacer
143 [[_segmentControl superview
] addObserver
:self
148 NSUInteger selectedSegment
=
149 [[[NSUserDefaults standardUserDefaults
] valueForKey
:kPrefSelectedDebuggerSegment
] intValue
];
150 [[_segmentControl cell
] setSelectedSegment
:selectedSegment
];
151 [self updateSegmentControl
];
155 * Key-value observation routine.
157 - (void)observeValueForKeyPath
:(NSString
*)keyPath
159 change
:(NSDictionary
<NSString
*,id>*)change
160 context
:(void*)context
{
161 if (object
== _stackArrayController
&& [keyPath isEqualToString
:@
"selectedObjects"]) {
162 for (StackFrame
* frame
in _stackArrayController.selectedObjects
)
163 [_connection loadStackFrame
:frame
];
164 } else if (object
== _stackArrayController
&& [keyPath isEqualToString
:@
"selection.source"]) {
165 [self updateSourceViewer
];
166 } else if (object
== _model
) {
167 if ([keyPath isEqualToString
:@
"connected"]) {
168 if ([change
[NSKeyValueChangeNewKey
] boolValue
]) {
169 [self debuggerConnected
];
171 [self debuggerDisconnected
];
173 } else if ([keyPath isEqualToString
:@
"status"]) {
174 self.statusField.textColor
= self.model.lastError ?
[NSColor redColor
] : [NSColor textColor
];
176 } else if (object
== _segmentControl.cell
) {
177 [[NSUserDefaults standardUserDefaults
] setValue
:@
(_segmentControl.selectedSegment
)
178 forKey
:kPrefSelectedDebuggerSegment
];
179 [_tabView selectTabViewItemAtIndex
:_segmentControl.selectedSegment
- 1];
180 } else if (object
== _segmentControl.superview
) {
181 [self updateSegmentControl
];
183 [super observeValueForKeyPath
:keyPath ofObject
:object change
:change context
:context
];
188 * Validates the menu items for the "Debugger" menu
190 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
192 SEL action
= [anItem action
];
194 if (action
== @selector(stepOut
:)) {
195 return _model.connected
&& _model.stackDepth
> 1;
196 } else if (action
== @selector(stepIn
:) ||
197 action
== @selector(stepOver
:) ||
198 action
== @selector(run
:) ||
199 action
== @selector(stop
:)) {
200 return _model.connected
;
202 return [[self window
] validateUserInterfaceItem
:anItem
];
206 * Shows the inspector window
208 - (IBAction
)showInspectorWindow
:(id)sender
210 if (![_inspector isVisible
])
211 [_inspector makeKeyAndOrderFront
:sender
];
213 [_inspector orderOut
:sender
];
217 * Runs the eval window sheet.
219 - (IBAction
)showEvalWindow
:(id)sender
221 [self.segmentControl setSelectedSegment
:3];
225 * Delegate function for GDBpConnection for when the debugger connects.
227 - (void)debuggerConnected
229 if (!self.connection.autoAttach
)
231 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:kPrefBreakOnFirstLine
])
233 // Do not cache the file between debugger executions.
234 _sourceViewer.file
= nil;
238 * Called once the debugger disconnects.
240 - (void)debuggerDisconnected
242 // Invalidate the marked line so we don't look like we're still running.
243 _sourceViewer.markedLine
= -1;
244 [_sourceViewer setNeedsDisplay
:YES
];
248 * Forwards the message to run script execution to the connection
250 - (IBAction
)run
:(id)sender
255 - (IBAction
)attachedToggled
:(id)sender
257 _connection.autoAttach
= [sender state
] == NSOnState
;
261 * Forwards the message to "step in" to the connection
263 - (IBAction
)stepIn
:(id)sender
265 if ([[_variablesTreeController selectedObjects
] count
] > 0)
266 _selectedVariable
= [[_variablesTreeController selectedObjects
] objectAtIndex
:0];
268 [_connection stepIn
];
272 * Forwards the message to "step out" to the connection
274 - (IBAction
)stepOut
:(id)sender
276 if ([[_variablesTreeController selectedObjects
] count
] > 0)
277 _selectedVariable
= [[_variablesTreeController selectedObjects
] objectAtIndex
:0];
279 [_connection stepOut
];
283 * Forwards the message to "step over" to the connection
285 - (IBAction
)stepOver
:(id)sender
287 if ([[_variablesTreeController selectedObjects
] count
] > 0)
288 _selectedVariable
= [[_variablesTreeController selectedObjects
] objectAtIndex
:0];
290 [_connection stepOver
];
294 * Forwards the detach/"stop" message to the back end.
296 - (IBAction
)stop
:(id)sender
302 * NSTableView delegate method that informs the controller that the stack selection did change and that
303 * we should update the source viewer
305 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
307 // TODO: This is very, very hacky because it's nondeterministic. The issue
308 // is that calling |-[NSOutlineView expandItem:]| while the table is still
309 // doing its redraw will translate to a no-op. Instead, we need to restructure
310 // this controller so that when everything has been laid out we call
311 // |-expandVariables|; but NSOutlineView doesn't have a |-didFinishDoingCrap:|
312 // method. The other issue is that we need to call this method from
313 // selectionDidChange but ONLY when it was the result of a user-initiated
314 // action and not the stack viewer updating causing a selection change.
315 // If it happens in the latter, then we run into the same issue that causes
317 [self performSelector
:@selector(expandVariables
) withObject
:nil afterDelay
:0.05];
321 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
323 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
325 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
326 [_expandedVariables addObject
:[[node representedObject
] fullName
]];
328 [_connection loadVariableNode
:[node representedObject
]
329 forStackFrame
:[[_stackArrayController selectedObjects
] lastObject
]];
333 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
335 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
337 [_expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullName
]];
343 * Does the actual updating of the source viewer by reading in the file
345 - (void)updateSourceViewer
347 NSArray
* selection
= [_stackArrayController selectedObjects
];
348 if (!selection ||
[selection count
] < 1)
350 if ([selection count
] > 1)
351 NSLog(@
"INVALID SELECTION");
352 StackFrame
* frame
= [selection objectAtIndex
:0];
355 [_connection loadStackFrame
:frame
];
360 NSString
* filename
= [[NSURL URLWithString
:frame.filename
] path
];
361 if ([filename isEqualToString
:@
""])
364 // Replace the source if necessary.
365 if (frame.source
&& ![_sourceViewer.file isEqualToString
:filename
])
367 [_sourceViewer setString
:frame.source asFile
:filename
];
369 NSSet
<NSNumber
*>* breakpoints
= [_model.breakpointManager breakpointsForFile
:filename
];
370 [_sourceViewer setMarkers
:breakpoints
];
373 [_sourceViewer setMarkedLine
:frame.lineNumber
];
374 [_sourceViewer scrollToLine
:frame.lineNumber
];
376 [[_sourceViewer textView
] setNeedsDisplay
:YES
];
380 * Expands the variables based on the stored set
382 - (void)expandVariables
384 NSString
* selection
= [_selectedVariable fullName
];
386 for (NSInteger i
= 0; i
< [_variablesOutlineView numberOfRows
]; i
++) {
387 NSTreeNode
* node
= [_variablesOutlineView itemAtRow
:i
];
388 NSString
* fullName
= [[node representedObject
] fullName
];
390 // see if it needs expanding
391 if ([_expandedVariables containsObject
:fullName
])
392 [_variablesOutlineView expandItem
:node
];
394 // select it if we had it selected before
395 if ([fullName isEqualToString
:selection
])
396 [_variablesTreeController setSelectionIndexPath
:[node indexPath
]];
401 * Sets the widths of the segmented control.
403 - (void)updateSegmentControl
{
404 NSRect containerFrame
= [[_segmentControl superview
] frame
];
405 CGFloat containerWidth
= NSWidth(containerFrame
);
406 CGFloat segmentSizes
= 0;
407 for (NSInteger i
= 1; i
< [_segmentControl segmentCount
] - 1; ++i
) {
408 segmentSizes
+= [_segmentControl widthForSegment
:i
];
410 CGFloat spacerWidth
= (containerWidth
- segmentSizes
) / 2;
411 [_segmentControl setWidth
:spacerWidth forSegment
:0];
412 [_segmentControl setWidth
:spacerWidth forSegment
:[_segmentControl segmentCount
] - 1];
414 [_segmentControl setFrame
:NSMakeRect(-1, NSHeight(containerFrame
) - 27, containerWidth
+ 2, 30)];
417 #pragma mark BSSourceView Delegate
420 * The gutter was clicked, which indicates that a breakpoint needs to be changed
422 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
424 BreakpointManager
* manager
= _model.breakpointManager
;
425 Breakpoint
* breakpoint
= [Breakpoint breakpointAtLine
:line inFile
:file
];
427 if ([manager hasBreakpoint
:breakpoint
]) {
428 [manager removeBreakpoint
:breakpoint
];
430 [manager addBreakpoint
:breakpoint
];
433 [_sourceViewer setMarkers
:[manager breakpointsForFile
:file
]];