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 BreakpointController
* _breakpointsController
;
37 EvalController
* _evalController
;
40 @synthesize connection
, sourceViewer
, inspector
;
43 * Initializes the window controller and sets the connection using preference
48 if (self = [super initWithWindowNibName
:@
"Debugger"])
50 NSUserDefaults
* defaults
= [NSUserDefaults standardUserDefaults
];
52 _model
= [[DebuggerModel alloc
] init
];
53 [_model addObserver
:self
54 forKeyPath
:@
"connected"
55 options
:NSKeyValueObservingOptionNew
58 connection
= [[DebuggerBackEnd alloc
] initWithPort
:[defaults integerForKey
:kPrefPort
]
59 autoAttach
:[defaults boolForKey
:kPrefDebuggerAttached
]];
60 connection.model
= _model
;
61 _model.breakpointManager.connection
= connection
;
63 expandedVariables
= [[NSMutableSet alloc
] init
];
64 [[self window
] makeKeyAndOrderFront
:nil];
65 [[self window
] setDelegate
:self];
67 if ([defaults boolForKey
:kPrefInspectorWindowVisible
])
68 [inspector orderFront
:self];
80 [_breakpointsController release
];
81 [_evalController release
];
82 [expandedVariables release
];
87 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
91 [[self window
] setExcludedFromWindowsMenu
:YES
];
92 [[self window
] setTitle
:[NSString stringWithFormat
:@
"MacGDBp @ %d", [connection port
]]];
93 [sourceViewer setDelegate
:self];
94 [stackArrayController setSortDescriptors
:@
[ [[[NSSortDescriptor alloc
] initWithKey
:@
"index" ascending
:YES
] autorelease
] ]];
95 [stackArrayController addObserver
:self
96 forKeyPath
:@
"selectedObjects"
97 options
:NSKeyValueObservingOptionNew
99 [stackArrayController addObserver
:self
100 forKeyPath
:@
"selection.source"
101 options
:NSKeyValueObservingOptionNew
103 self.connection.autoAttach
= [attachedCheckbox_ state
] == NSOnState
;
105 // Load view controllers into the tab views.
106 _breakpointsController
= [[BreakpointController alloc
] initWithBreakpointManager
:_model.breakpointManager
107 sourceView
:sourceViewer
];
108 [[self.tabView tabViewItemAtIndex
:1] setView
:_breakpointsController.view
];
110 _evalController
= [[EvalController alloc
] initWithBackEnd
:connection
];
111 [[self.tabView tabViewItemAtIndex
:2] setView
:_evalController.view
];
113 // When the segment control's selection changes, update the tab view.
114 [[_segmentControl cell
] addObserver
:self
115 forKeyPath
:@
"selectedSegment"
118 // When the segment control's superview changes, recalculate the spacer
120 [[_segmentControl superview
] addObserver
:self
125 NSUInteger selectedSegment
=
126 [[[NSUserDefaults standardUserDefaults
] valueForKey
:kPrefSelectedDebuggerSegment
] intValue
];
127 [[_segmentControl cell
] setSelectedSegment
:selectedSegment
];
128 [self updateSegmentControl
];
132 * Key-value observation routine.
134 - (void)observeValueForKeyPath
:(NSString
*)keyPath
136 change
:(NSDictionary
<NSString
*,id>*)change
137 context
:(void*)context
{
138 if (object
== stackArrayController
&& [keyPath isEqualToString
:@
"selectedObjects"]) {
139 for (StackFrame
* frame
in stackArrayController.selectedObjects
)
140 [connection loadStackFrame
:frame
];
141 } else if (object
== stackArrayController
&& [keyPath isEqualToString
:@
"selection.source"]) {
142 [self updateSourceViewer
];
143 } else if (object
== _model
&& [keyPath isEqualToString
:@
"connected"]) {
144 if ([change
[NSKeyValueChangeNewKey
] boolValue
])
145 [self debuggerConnected
];
147 [self debuggerDisconnected
];
148 } else if (object
== _segmentControl.cell
) {
149 [[NSUserDefaults standardUserDefaults
] setValue
:@
(_segmentControl.selectedSegment
)
150 forKey
:kPrefSelectedDebuggerSegment
];
151 [_tabView selectTabViewItemAtIndex
:_segmentControl.selectedSegment
- 1];
152 } else if (object
== _segmentControl.superview
) {
153 [self updateSegmentControl
];
155 [super observeValueForKeyPath
:keyPath ofObject
:object change
:change context
:context
];
160 * Validates the menu items for the "Debugger" menu
162 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
164 SEL action
= [anItem action
];
166 if (action
== @selector(stepOut
:)) {
167 return _model.connected
&& _model.stackDepth
> 1;
168 } else if (action
== @selector(stepIn
:) ||
169 action
== @selector(stepOver
:) ||
170 action
== @selector(run
:) ||
171 action
== @selector(stop
:) ||
172 action
== @selector(showEvalWindow
:)) {
173 return _model.connected
;
175 return [[self window
] validateUserInterfaceItem
:anItem
];
179 * Shows the inspector window
181 - (IBAction
)showInspectorWindow
:(id)sender
183 if (![inspector isVisible
])
184 [inspector makeKeyAndOrderFront
:sender
];
186 [inspector orderOut
:sender
];
190 * Runs the eval window sheet.
192 - (IBAction
)showEvalWindow
:(id)sender
194 [self.segmentControl setSelectedSegment
:3];
198 * Resets all the displays to be empty
200 - (void)resetDisplays
202 [variablesTreeController setContent
:nil];
203 [stackArrayController rearrangeObjects
];
204 [[sourceViewer textView
] setString
:@
""];
205 sourceViewer.file
= nil;
209 * Sets the status to be "Error" and then displays the error message
211 - (void)setError
:(NSString
*)anError
213 [errormsg setStringValue
:anError
];
214 [errormsg setHidden
:NO
];
218 * Delegate function for GDBpConnection for when the debugger connects.
220 - (void)debuggerConnected
222 [errormsg setHidden
:YES
];
223 if (!self.connection.autoAttach
)
225 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:kPrefBreakOnFirstLine
])
227 // Do not cache the file between debugger executions.
228 sourceViewer.file
= nil;
232 * Called once the debugger disconnects.
234 - (void)debuggerDisconnected
236 // Invalidate the marked line so we don't look like we're still running.
237 sourceViewer.markedLine
= -1;
238 [sourceViewer setNeedsDisplay
:YES
];
242 * Forwards the message to run script execution to the connection
244 - (IBAction
)run
:(id)sender
249 - (IBAction
)attachedToggled
:(id)sender
251 connection.autoAttach
= [sender state
] == NSOnState
;
255 * Forwards the message to "step in" to the connection
257 - (IBAction
)stepIn
:(id)sender
259 if ([[variablesTreeController selectedObjects
] count
] > 0)
260 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
266 * Forwards the message to "step out" to the connection
268 - (IBAction
)stepOut
:(id)sender
270 if ([[variablesTreeController selectedObjects
] count
] > 0)
271 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
273 [connection stepOut
];
277 * Forwards the message to "step over" to the connection
279 - (IBAction
)stepOver
:(id)sender
281 if ([[variablesTreeController selectedObjects
] count
] > 0)
282 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
284 [connection stepOver
];
288 * Forwards the detach/"stop" message to the back end.
290 - (IBAction
)stop
:(id)sender
296 * NSTableView delegate method that informs the controller that the stack selection did change and that
297 * we should update the source viewer
299 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
301 // TODO: This is very, very hacky because it's nondeterministic. The issue
302 // is that calling |-[NSOutlineView expandItem:]| while the table is still
303 // doing its redraw will translate to a no-op. Instead, we need to restructure
304 // this controller so that when everything has been laid out we call
305 // |-expandVariables|; but NSOutlineView doesn't have a |-didFinishDoingCrap:|
306 // method. The other issue is that we need to call this method from
307 // selectionDidChange but ONLY when it was the result of a user-initiated
308 // action and not the stack viewer updating causing a selection change.
309 // If it happens in the latter, then we run into the same issue that causes
311 [self performSelector
:@selector(expandVariables
) withObject
:nil afterDelay
:0.05];
315 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
317 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
319 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
320 [expandedVariables addObject
:[[node representedObject
] fullName
]];
322 [connection loadVariableNode
:[node representedObject
]
323 forStackFrame
:[[stackArrayController selectedObjects
] lastObject
]];
327 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
329 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
331 [expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullName
]];
337 * Does the actual updating of the source viewer by reading in the file
339 - (void)updateSourceViewer
341 NSArray
* selection
= [stackArrayController selectedObjects
];
342 if (!selection ||
[selection count
] < 1)
344 if ([selection count
] > 1)
345 NSLog(@
"INVALID SELECTION");
346 StackFrame
* frame
= [selection objectAtIndex
:0];
349 [connection loadStackFrame
:frame
];
354 NSString
* filename
= [[NSURL URLWithString
:frame.filename
] path
];
355 if ([filename isEqualToString
:@
""])
358 // Replace the source if necessary.
359 if (frame.source
&& ![sourceViewer.file isEqualToString
:filename
])
361 [sourceViewer setString
:frame.source asFile
:filename
];
363 NSSet
<NSNumber
*>* breakpoints
= [_model.breakpointManager breakpointsForFile
:filename
];
364 [sourceViewer setMarkers
:breakpoints
];
367 [sourceViewer setMarkedLine
:frame.lineNumber
];
368 [sourceViewer scrollToLine
:frame.lineNumber
];
370 [[sourceViewer textView
] setNeedsDisplay
:YES
];
374 * Expands the variables based on the stored set
376 - (void)expandVariables
378 NSString
* selection
= [selectedVariable fullName
];
380 for (NSInteger i
= 0; i
< [variablesOutlineView numberOfRows
]; i
++) {
381 NSTreeNode
* node
= [variablesOutlineView itemAtRow
:i
];
382 NSString
* fullName
= [[node representedObject
] fullName
];
384 // see if it needs expanding
385 if ([expandedVariables containsObject
:fullName
])
386 [variablesOutlineView expandItem
:node
];
388 // select it if we had it selected before
389 if ([fullName isEqualToString
:selection
])
390 [variablesTreeController setSelectionIndexPath
:[node indexPath
]];
395 * Sets the widths of the segmented control.
397 - (void)updateSegmentControl
{
398 NSRect containerFrame
= [[_segmentControl superview
] frame
];
399 CGFloat containerWidth
= NSWidth(containerFrame
);
400 CGFloat segmentSizes
= 0;
401 for (NSInteger i
= 1; i
< [_segmentControl segmentCount
] - 1; ++i
) {
402 segmentSizes
+= [_segmentControl widthForSegment
:i
];
404 CGFloat spacerWidth
= (containerWidth
- segmentSizes
) / 2;
405 [_segmentControl setWidth
:spacerWidth forSegment
:0];
406 [_segmentControl setWidth
:spacerWidth forSegment
:[_segmentControl segmentCount
] - 1];
408 [_segmentControl setFrame
:NSMakeRect(-1, NSHeight(containerFrame
) - 27, containerWidth
+ 2, 30)];
411 #pragma mark BSSourceView Delegate
414 * The gutter was clicked, which indicates that a breakpoint needs to be changed
416 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
418 BreakpointManager
* mngr
= _model.breakpointManager
;
420 if ([mngr hasBreakpointAt
:line inFile
:file
])
422 [mngr removeBreakpointAt
:line inFile
:file
];
426 Breakpoint
* bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
427 [mngr addBreakpoint
:bp
];
431 [sourceViewer setMarkers
:[mngr breakpointsForFile
:file
]];