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
];
98 [_segmentControl release
];
99 [_statusField release
];
106 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
110 [[self window
] setExcludedFromWindowsMenu
:YES
];
111 [[self window
] setTitle
:[NSString stringWithFormat
:@
"MacGDBp @ %d", [_connection port
]]];
112 [_sourceViewer setDelegate
:self];
113 [_stackArrayController setSortDescriptors
:@
[ [[[NSSortDescriptor alloc
] initWithKey
:@
"index" ascending
:YES
] autorelease
] ]];
114 [_stackArrayController addObserver
:self
115 forKeyPath
:@
"selectedObjects"
116 options
:NSKeyValueObservingOptionNew
118 [_stackArrayController addObserver
:self
119 forKeyPath
:@
"selection.source"
120 options
:NSKeyValueObservingOptionNew
122 self.connection.autoAttach
= [_attachedCheckbox state
] == NSOnState
;
124 // Load view controllers into the tab views.
125 _breakpointsController
= [[BreakpointController alloc
] initWithBreakpointManager
:_model.breakpointManager
126 sourceView
:_sourceViewer
];
127 [[self.tabView tabViewItemAtIndex
:1] setView
:_breakpointsController.view
];
129 _evalController
= [[EvalController alloc
] initWithBackEnd
:_connection
];
130 [[self.tabView tabViewItemAtIndex
:2] setView
:_evalController.view
];
132 // When the segment control's selection changes, update the tab view.
133 [[_segmentControl cell
] addObserver
:self
134 forKeyPath
:@
"selectedSegment"
137 // When the segment control's superview changes, recalculate the spacer
139 [[_segmentControl superview
] addObserver
:self
144 NSUInteger selectedSegment
=
145 [[[NSUserDefaults standardUserDefaults
] valueForKey
:kPrefSelectedDebuggerSegment
] intValue
];
146 [[_segmentControl cell
] setSelectedSegment
:selectedSegment
];
147 [self updateSegmentControl
];
151 * Key-value observation routine.
153 - (void)observeValueForKeyPath
:(NSString
*)keyPath
155 change
:(NSDictionary
<NSString
*,id>*)change
156 context
:(void*)context
{
157 if (object
== _stackArrayController
&& [keyPath isEqualToString
:@
"selectedObjects"]) {
158 for (StackFrame
* frame
in _stackArrayController.selectedObjects
)
159 [_connection loadStackFrame
:frame
];
160 } else if (object
== _stackArrayController
&& [keyPath isEqualToString
:@
"selection.source"]) {
161 [self updateSourceViewer
];
162 } else if (object
== _model
) {
163 if ([keyPath isEqualToString
:@
"connected"]) {
164 if ([change
[NSKeyValueChangeNewKey
] boolValue
]) {
165 [self debuggerConnected
];
167 [self debuggerDisconnected
];
169 } else if ([keyPath isEqualToString
:@
"status"]) {
170 self.statusField.textColor
= self.model.lastError ?
[NSColor redColor
] : [NSColor textColor
];
172 } else if (object
== _segmentControl.cell
) {
173 [[NSUserDefaults standardUserDefaults
] setValue
:@
(_segmentControl.selectedSegment
)
174 forKey
:kPrefSelectedDebuggerSegment
];
175 [_tabView selectTabViewItemAtIndex
:_segmentControl.selectedSegment
- 1];
176 } else if (object
== _segmentControl.superview
) {
177 [self updateSegmentControl
];
179 [super observeValueForKeyPath
:keyPath ofObject
:object change
:change context
:context
];
184 * Validates the menu items for the "Debugger" menu
186 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
188 SEL action
= [anItem action
];
190 if (action
== @selector(stepOut
:)) {
191 return _model.connected
&& _model.stackDepth
> 1;
192 } else if (action
== @selector(stepIn
:) ||
193 action
== @selector(stepOver
:) ||
194 action
== @selector(run
:) ||
195 action
== @selector(stop
:)) {
196 return _model.connected
;
198 return [[self window
] validateUserInterfaceItem
:anItem
];
202 * Shows the inspector window
204 - (IBAction
)showInspectorWindow
:(id)sender
206 if (![_inspector isVisible
])
207 [_inspector makeKeyAndOrderFront
:sender
];
209 [_inspector orderOut
:sender
];
213 * Runs the eval window sheet.
215 - (IBAction
)showEvalWindow
:(id)sender
217 [self.segmentControl setSelectedSegment
:3];
221 * Delegate function for GDBpConnection for when the debugger connects.
223 - (void)debuggerConnected
225 if (!self.connection.autoAttach
)
227 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:kPrefBreakOnFirstLine
])
229 // Do not cache the file between debugger executions.
230 _sourceViewer.file
= nil;
234 * Called once the debugger disconnects.
236 - (void)debuggerDisconnected
238 // Invalidate the marked line so we don't look like we're still running.
239 _sourceViewer.markedLine
= -1;
240 [_sourceViewer setNeedsDisplay
:YES
];
244 * Forwards the message to run script execution to the connection
246 - (IBAction
)run
:(id)sender
251 - (IBAction
)attachedToggled
:(id)sender
253 _connection.autoAttach
= [sender state
] == NSOnState
;
257 * Forwards the message to "step in" to the connection
259 - (IBAction
)stepIn
:(id)sender
261 if ([[_variablesTreeController selectedObjects
] count
] > 0)
262 _selectedVariable
= [[_variablesTreeController selectedObjects
] objectAtIndex
:0];
264 [_connection stepIn
];
268 * Forwards the message to "step out" to the connection
270 - (IBAction
)stepOut
:(id)sender
272 if ([[_variablesTreeController selectedObjects
] count
] > 0)
273 _selectedVariable
= [[_variablesTreeController selectedObjects
] objectAtIndex
:0];
275 [_connection stepOut
];
279 * Forwards the message to "step over" to the connection
281 - (IBAction
)stepOver
:(id)sender
283 if ([[_variablesTreeController selectedObjects
] count
] > 0)
284 _selectedVariable
= [[_variablesTreeController selectedObjects
] objectAtIndex
:0];
286 [_connection stepOver
];
290 * Forwards the detach/"stop" message to the back end.
292 - (IBAction
)stop
:(id)sender
298 * NSTableView delegate method that informs the controller that the stack selection did change and that
299 * we should update the source viewer
301 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
303 // TODO: This is very, very hacky because it's nondeterministic. The issue
304 // is that calling |-[NSOutlineView expandItem:]| while the table is still
305 // doing its redraw will translate to a no-op. Instead, we need to restructure
306 // this controller so that when everything has been laid out we call
307 // |-expandVariables|; but NSOutlineView doesn't have a |-didFinishDoingCrap:|
308 // method. The other issue is that we need to call this method from
309 // selectionDidChange but ONLY when it was the result of a user-initiated
310 // action and not the stack viewer updating causing a selection change.
311 // If it happens in the latter, then we run into the same issue that causes
313 [self performSelector
:@selector(expandVariables
) withObject
:nil afterDelay
:0.05];
317 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
319 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
321 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
322 [_expandedVariables addObject
:[[node representedObject
] fullName
]];
324 [_connection loadVariableNode
:[node representedObject
]
325 forStackFrame
:[[_stackArrayController selectedObjects
] lastObject
]];
329 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
331 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
333 [_expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullName
]];
339 * Does the actual updating of the source viewer by reading in the file
341 - (void)updateSourceViewer
343 NSArray
* selection
= [_stackArrayController selectedObjects
];
344 if (!selection ||
[selection count
] < 1)
346 if ([selection count
] > 1)
347 NSLog(@
"INVALID SELECTION");
348 StackFrame
* frame
= [selection objectAtIndex
:0];
351 [_connection loadStackFrame
:frame
];
356 NSString
* filename
= [[NSURL URLWithString
:frame.filename
] path
];
357 if ([filename isEqualToString
:@
""])
360 // Replace the source if necessary.
361 if (frame.source
&& ![_sourceViewer.file isEqualToString
:filename
])
363 [_sourceViewer setString
:frame.source asFile
:filename
];
365 NSSet
<NSNumber
*>* breakpoints
= [_model.breakpointManager breakpointsForFile
:filename
];
366 [_sourceViewer setMarkers
:breakpoints
];
369 [_sourceViewer setMarkedLine
:frame.lineNumber
];
370 [_sourceViewer scrollToLine
:frame.lineNumber
];
372 [[_sourceViewer textView
] setNeedsDisplay
:YES
];
376 * Expands the variables based on the stored set
378 - (void)expandVariables
380 NSString
* selection
= [_selectedVariable fullName
];
382 for (NSInteger i
= 0; i
< [_variablesOutlineView numberOfRows
]; i
++) {
383 NSTreeNode
* node
= [_variablesOutlineView itemAtRow
:i
];
384 NSString
* fullName
= [[node representedObject
] fullName
];
386 // see if it needs expanding
387 if ([_expandedVariables containsObject
:fullName
])
388 [_variablesOutlineView expandItem
:node
];
390 // select it if we had it selected before
391 if ([fullName isEqualToString
:selection
])
392 [_variablesTreeController setSelectionIndexPath
:[node indexPath
]];
397 * Sets the widths of the segmented control.
399 - (void)updateSegmentControl
{
400 NSRect containerFrame
= [[_segmentControl superview
] frame
];
401 CGFloat containerWidth
= NSWidth(containerFrame
);
402 CGFloat segmentSizes
= 0;
403 for (NSInteger i
= 1; i
< [_segmentControl segmentCount
] - 1; ++i
) {
404 segmentSizes
+= [_segmentControl widthForSegment
:i
];
406 CGFloat spacerWidth
= (containerWidth
- segmentSizes
) / 2;
407 [_segmentControl setWidth
:spacerWidth forSegment
:0];
408 [_segmentControl setWidth
:spacerWidth forSegment
:[_segmentControl segmentCount
] - 1];
410 [_segmentControl setFrame
:NSMakeRect(-1, NSHeight(containerFrame
) - 27, containerWidth
+ 2, 30)];
413 #pragma mark BSSourceView Delegate
416 * The gutter was clicked, which indicates that a breakpoint needs to be changed
418 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
420 BreakpointManager
* manager
= _model.breakpointManager
;
421 Breakpoint
* breakpoint
= [Breakpoint breakpointAtLine
:line inFile
:file
];
423 if ([manager hasBreakpoint
:breakpoint
]) {
424 [manager removeBreakpoint
:breakpoint
];
426 [manager addBreakpoint
:breakpoint
];
429 [_sourceViewer setMarkers
:[manager breakpointsForFile
:file
]];