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
;
47 @synthesize connection
= _connection
;
48 @synthesize sourceViewer
= _sourceViewer
;
49 @synthesize inspector
= _inspector
;
52 * Initializes the window controller and sets the connection using preference
57 if (self = [super initWithWindowNibName
:@
"Debugger"])
59 NSUserDefaults
* defaults
= [NSUserDefaults standardUserDefaults
];
61 _model
= [[DebuggerModel alloc
] init
];
62 [_model addObserver
:self
63 forKeyPath
:@
"connected"
64 options
:NSKeyValueObservingOptionNew
67 _connection
= [[DebuggerBackEnd alloc
] initWithPort
:[defaults integerForKey
:kPrefPort
]
68 autoAttach
:[defaults boolForKey
:kPrefDebuggerAttached
]];
69 _connection.model
= _model
;
70 _model.breakpointManager.connection
= _connection
;
72 [_model addObserver
:self
74 options
:NSKeyValueObservingOptionNew
77 _expandedVariables
= [[NSMutableSet alloc
] init
];
78 [[self window
] makeKeyAndOrderFront
:nil];
79 [[self window
] setDelegate
:self];
81 if ([defaults boolForKey
:kPrefInspectorWindowVisible
])
82 [_inspector orderFront
:self];
92 [_connection release
];
94 [_breakpointsController release
];
95 [_evalController release
];
96 [_expandedVariables release
];
101 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
105 [[self window
] setExcludedFromWindowsMenu
:YES
];
106 [[self window
] setTitle
:[NSString stringWithFormat
:@
"MacGDBp @ %d", [_connection port
]]];
107 [_sourceViewer setDelegate
:self];
108 [_stackArrayController setSortDescriptors
:@
[ [[[NSSortDescriptor alloc
] initWithKey
:@
"index" ascending
:YES
] autorelease
] ]];
109 [_stackArrayController addObserver
:self
110 forKeyPath
:@
"selectedObjects"
111 options
:NSKeyValueObservingOptionNew
113 [_stackArrayController addObserver
:self
114 forKeyPath
:@
"selection.source"
115 options
:NSKeyValueObservingOptionNew
117 self.connection.autoAttach
= [_attachedCheckbox state
] == NSOnState
;
119 // Load view controllers into the tab views.
120 _breakpointsController
= [[BreakpointController alloc
] initWithBreakpointManager
:_model.breakpointManager
121 sourceView
:_sourceViewer
];
122 [[self.tabView tabViewItemAtIndex
:1] setView
:_breakpointsController.view
];
124 _evalController
= [[EvalController alloc
] initWithBackEnd
:_connection
];
125 [[self.tabView tabViewItemAtIndex
:2] setView
:_evalController.view
];
127 // When the segment control's selection changes, update the tab view.
128 [[_segmentControl cell
] addObserver
:self
129 forKeyPath
:@
"selectedSegment"
132 // When the segment control's superview changes, recalculate the spacer
134 [[_segmentControl superview
] addObserver
:self
139 NSUInteger selectedSegment
=
140 [[[NSUserDefaults standardUserDefaults
] valueForKey
:kPrefSelectedDebuggerSegment
] intValue
];
141 [[_segmentControl cell
] setSelectedSegment
:selectedSegment
];
142 [self updateSegmentControl
];
146 * Key-value observation routine.
148 - (void)observeValueForKeyPath
:(NSString
*)keyPath
150 change
:(NSDictionary
<NSString
*,id>*)change
151 context
:(void*)context
{
152 if (object
== _stackArrayController
&& [keyPath isEqualToString
:@
"selectedObjects"]) {
153 for (StackFrame
* frame
in _stackArrayController.selectedObjects
)
154 [_connection loadStackFrame
:frame
];
155 } else if (object
== _stackArrayController
&& [keyPath isEqualToString
:@
"selection.source"]) {
156 [self updateSourceViewer
];
157 } else if (object
== _model
) {
158 if ([keyPath isEqualToString
:@
"connected"]) {
159 if ([change
[NSKeyValueChangeNewKey
] boolValue
]) {
160 [self debuggerConnected
];
162 [self debuggerDisconnected
];
164 } else if ([keyPath isEqualToString
:@
"status"]) {
165 self.statusField.textColor
= self.model.lastError ?
[NSColor redColor
] : [NSColor textColor
];
167 } else if (object
== _segmentControl.cell
) {
168 [[NSUserDefaults standardUserDefaults
] setValue
:@
(_segmentControl.selectedSegment
)
169 forKey
:kPrefSelectedDebuggerSegment
];
170 [_tabView selectTabViewItemAtIndex
:_segmentControl.selectedSegment
- 1];
171 } else if (object
== _segmentControl.superview
) {
172 [self updateSegmentControl
];
174 [super observeValueForKeyPath
:keyPath ofObject
:object change
:change context
:context
];
179 * Validates the menu items for the "Debugger" menu
181 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
183 SEL action
= [anItem action
];
185 if (action
== @selector(stepOut
:)) {
186 return _model.connected
&& _model.stackDepth
> 1;
187 } else if (action
== @selector(stepIn
:) ||
188 action
== @selector(stepOver
:) ||
189 action
== @selector(run
:) ||
190 action
== @selector(stop
:)) {
191 return _model.connected
;
193 return [[self window
] validateUserInterfaceItem
:anItem
];
197 * Shows the inspector window
199 - (IBAction
)showInspectorWindow
:(id)sender
201 if (![_inspector isVisible
])
202 [_inspector makeKeyAndOrderFront
:sender
];
204 [_inspector orderOut
:sender
];
208 * Runs the eval window sheet.
210 - (IBAction
)showEvalWindow
:(id)sender
212 [self.segmentControl setSelectedSegment
:3];
216 * Delegate function for GDBpConnection for when the debugger connects.
218 - (void)debuggerConnected
220 if (!self.connection.autoAttach
)
222 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:kPrefBreakOnFirstLine
])
224 // Do not cache the file between debugger executions.
225 _sourceViewer.file
= nil;
229 * Called once the debugger disconnects.
231 - (void)debuggerDisconnected
233 // Invalidate the marked line so we don't look like we're still running.
234 _sourceViewer.markedLine
= -1;
235 [_sourceViewer setNeedsDisplay
:YES
];
239 * Forwards the message to run script execution to the connection
241 - (IBAction
)run
:(id)sender
246 - (IBAction
)attachedToggled
:(id)sender
248 _connection.autoAttach
= [sender state
] == NSOnState
;
252 * Forwards the message to "step in" to the connection
254 - (IBAction
)stepIn
:(id)sender
256 if ([[_variablesTreeController selectedObjects
] count
] > 0)
257 _selectedVariable
= [[_variablesTreeController selectedObjects
] objectAtIndex
:0];
259 [_connection stepIn
];
263 * Forwards the message to "step out" to the connection
265 - (IBAction
)stepOut
:(id)sender
267 if ([[_variablesTreeController selectedObjects
] count
] > 0)
268 _selectedVariable
= [[_variablesTreeController selectedObjects
] objectAtIndex
:0];
270 [_connection stepOut
];
274 * Forwards the message to "step over" to the connection
276 - (IBAction
)stepOver
:(id)sender
278 if ([[_variablesTreeController selectedObjects
] count
] > 0)
279 _selectedVariable
= [[_variablesTreeController selectedObjects
] objectAtIndex
:0];
281 [_connection stepOver
];
285 * Forwards the detach/"stop" message to the back end.
287 - (IBAction
)stop
:(id)sender
293 * NSTableView delegate method that informs the controller that the stack selection did change and that
294 * we should update the source viewer
296 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
298 // TODO: This is very, very hacky because it's nondeterministic. The issue
299 // is that calling |-[NSOutlineView expandItem:]| while the table is still
300 // doing its redraw will translate to a no-op. Instead, we need to restructure
301 // this controller so that when everything has been laid out we call
302 // |-expandVariables|; but NSOutlineView doesn't have a |-didFinishDoingCrap:|
303 // method. The other issue is that we need to call this method from
304 // selectionDidChange but ONLY when it was the result of a user-initiated
305 // action and not the stack viewer updating causing a selection change.
306 // If it happens in the latter, then we run into the same issue that causes
308 [self performSelector
:@selector(expandVariables
) withObject
:nil afterDelay
:0.05];
312 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
314 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
316 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
317 [_expandedVariables addObject
:[[node representedObject
] fullName
]];
319 [_connection loadVariableNode
:[node representedObject
]
320 forStackFrame
:[[_stackArrayController selectedObjects
] lastObject
]];
324 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
326 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
328 [_expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullName
]];
334 * Does the actual updating of the source viewer by reading in the file
336 - (void)updateSourceViewer
338 NSArray
* selection
= [_stackArrayController selectedObjects
];
339 if (!selection ||
[selection count
] < 1)
341 if ([selection count
] > 1)
342 NSLog(@
"INVALID SELECTION");
343 StackFrame
* frame
= [selection objectAtIndex
:0];
346 [_connection loadStackFrame
:frame
];
351 NSString
* filename
= [[NSURL URLWithString
:frame.filename
] path
];
352 if ([filename isEqualToString
:@
""])
355 // Replace the source if necessary.
356 if (frame.source
&& ![_sourceViewer.file isEqualToString
:filename
])
358 [_sourceViewer setString
:frame.source asFile
:filename
];
360 NSSet
<NSNumber
*>* breakpoints
= [_model.breakpointManager breakpointsForFile
:filename
];
361 [_sourceViewer setMarkers
:breakpoints
];
364 [_sourceViewer setMarkedLine
:frame.lineNumber
];
365 [_sourceViewer scrollToLine
:frame.lineNumber
];
367 [[_sourceViewer textView
] setNeedsDisplay
:YES
];
371 * Expands the variables based on the stored set
373 - (void)expandVariables
375 NSString
* selection
= [_selectedVariable fullName
];
377 for (NSInteger i
= 0; i
< [_variablesOutlineView numberOfRows
]; i
++) {
378 NSTreeNode
* node
= [_variablesOutlineView itemAtRow
:i
];
379 NSString
* fullName
= [[node representedObject
] fullName
];
381 // see if it needs expanding
382 if ([_expandedVariables containsObject
:fullName
])
383 [_variablesOutlineView expandItem
:node
];
385 // select it if we had it selected before
386 if ([fullName isEqualToString
:selection
])
387 [_variablesTreeController setSelectionIndexPath
:[node indexPath
]];
392 * Sets the widths of the segmented control.
394 - (void)updateSegmentControl
{
395 NSRect containerFrame
= [[_segmentControl superview
] frame
];
396 CGFloat containerWidth
= NSWidth(containerFrame
);
397 CGFloat segmentSizes
= 0;
398 for (NSInteger i
= 1; i
< [_segmentControl segmentCount
] - 1; ++i
) {
399 segmentSizes
+= [_segmentControl widthForSegment
:i
];
401 CGFloat spacerWidth
= (containerWidth
- segmentSizes
) / 2;
402 [_segmentControl setWidth
:spacerWidth forSegment
:0];
403 [_segmentControl setWidth
:spacerWidth forSegment
:[_segmentControl segmentCount
] - 1];
405 [_segmentControl setFrame
:NSMakeRect(-1, NSHeight(containerFrame
) - 27, containerWidth
+ 2, 30)];
408 #pragma mark BSSourceView Delegate
411 * The gutter was clicked, which indicates that a breakpoint needs to be changed
413 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
415 BreakpointManager
* manager
= _model.breakpointManager
;
416 Breakpoint
* breakpoint
= [Breakpoint breakpointAtLine
:line inFile
:file
];
418 if ([manager hasBreakpoint
:breakpoint
]) {
419 [manager removeBreakpoint
:breakpoint
];
421 [manager addBreakpoint
:breakpoint
];
424 [_sourceViewer setMarkers
:[manager breakpointsForFile
:file
]];