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 "BreakpointManager.h"
22 #import "DebuggerBackEnd.h"
23 #import "DebuggerModel.h"
24 #import "EvalController.h"
25 #import "PreferenceNames.h"
26 #import "NSXMLElementAdditions.h"
27 #import "StackFrame.h"
29 @interface DebuggerController (Private
)
30 - (void)updateSourceViewer
;
31 - (void)expandVariables
;
34 @implementation DebuggerController
36 @synthesize connection
, sourceViewer
, inspector
;
39 * Initializes the window controller and sets the connection using preference
44 if (self = [super initWithWindowNibName
:@
"Debugger"])
46 NSUserDefaults
* defaults
= [NSUserDefaults standardUserDefaults
];
48 _model
= [[DebuggerModel alloc
] init
];
49 [_model addObserver
:self
50 forKeyPath
:@
"connected"
51 options
:NSKeyValueObservingOptionNew
54 connection
= [[DebuggerBackEnd alloc
] initWithPort
:[defaults integerForKey
:kPrefPort
]
55 autoAttach
:[defaults boolForKey
:kPrefDebuggerAttached
]];
56 connection.model
= _model
;
57 expandedVariables
= [[NSMutableSet alloc
] init
];
58 [[self window
] makeKeyAndOrderFront
:nil];
59 [[self window
] setDelegate
:self];
61 if ([defaults boolForKey
:kPrefInspectorWindowVisible
])
62 [inspector orderFront
:self];
74 [expandedVariables release
];
79 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
83 [[self window
] setExcludedFromWindowsMenu
:YES
];
84 [[self window
] setTitle
:[NSString stringWithFormat
:@
"MacGDBp @ %d", [connection port
]]];
85 [sourceViewer setDelegate
:self];
86 [stackArrayController setSortDescriptors
:@
[ [[[NSSortDescriptor alloc
] initWithKey
:@
"index" ascending
:YES
] autorelease
] ]];
87 [stackArrayController addObserver
:self
88 forKeyPath
:@
"selectedObjects"
89 options
:NSKeyValueObservingOptionNew
91 [stackArrayController addObserver
:self
92 forKeyPath
:@
"selection.source"
93 options
:NSKeyValueObservingOptionNew
95 [[_segmentControl cell
] addObserver
:self
96 forKeyPath
:@
"selectedSegment"
97 options
:NSKeyValueObservingOptionNew
99 self.connection.autoAttach
= [attachedCheckbox_ state
] == NSOnState
;
101 [self updateSegmentControl
];
106 * Key-value observation routine.
108 - (void)observeValueForKeyPath
:(NSString
*)keyPath
110 change
:(NSDictionary
<NSString
*,id>*)change
111 context
:(void*)context
{
112 if (object
== stackArrayController
&& [keyPath isEqualToString
:@
"selectedObjects"]) {
113 for (StackFrame
* frame
in stackArrayController.selectedObjects
)
114 [connection loadStackFrame
:frame
];
115 } else if (object
== stackArrayController
&& [keyPath isEqualToString
:@
"selection.source"]) {
116 [self updateSourceViewer
];
117 } else if (object
== _model
&& [keyPath isEqualToString
:@
"connected"]) {
118 if ([change
[NSKeyValueChangeNewKey
] boolValue
])
119 [self debuggerConnected
];
121 [self debuggerDisconnected
];
122 } else if (object
== self.segmentControl.cell
) {
123 [self.tabView selectTabViewItemAtIndex
:self.segmentControl.selectedSegment
- 1];
125 [super observeValueForKeyPath
:keyPath ofObject
:object change
:change context
:context
];
130 * Validates the menu items for the "Debugger" menu
132 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
134 SEL action
= [anItem action
];
136 if (action
== @selector(stepOut
:)) {
137 return _model.connected
&& _model.stackDepth
> 1;
138 } else if (action
== @selector(stepIn
:) ||
139 action
== @selector(stepOver
:) ||
140 action
== @selector(run
:) ||
141 action
== @selector(stop
:) ||
142 action
== @selector(showEvalWindow
:)) {
143 return _model.connected
;
145 return [[self window
] validateUserInterfaceItem
:anItem
];
149 * Shows the inspector window
151 - (IBAction
)showInspectorWindow
:(id)sender
153 if (![inspector isVisible
])
154 [inspector makeKeyAndOrderFront
:sender
];
156 [inspector orderOut
:sender
];
160 * Runs the eval window sheet.
162 - (IBAction
)showEvalWindow
:(id)sender
164 // The |controller| will release itself on close.
165 EvalController
* controller
= [[EvalController alloc
] initWithBackEnd
:connection
];
166 [controller runModalForWindow
:[self window
]];
170 * Resets all the displays to be empty
172 - (void)resetDisplays
174 [variablesTreeController setContent
:nil];
175 [stackArrayController rearrangeObjects
];
176 [[sourceViewer textView
] setString
:@
""];
177 sourceViewer.file
= nil;
181 * Sets the status to be "Error" and then displays the error message
183 - (void)setError
:(NSString
*)anError
185 [errormsg setStringValue
:anError
];
186 [errormsg setHidden
:NO
];
190 * Delegate function for GDBpConnection for when the debugger connects.
192 - (void)debuggerConnected
194 [errormsg setHidden
:YES
];
195 if (!self.connection.autoAttach
)
197 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:kPrefBreakOnFirstLine
])
199 // Do not cache the file between debugger executions.
200 sourceViewer.file
= nil;
204 * Called once the debugger disconnects.
206 - (void)debuggerDisconnected
208 // Invalidate the marked line so we don't look like we're still running.
209 sourceViewer.markedLine
= -1;
210 [sourceViewer setNeedsDisplay
:YES
];
214 * Forwards the message to run script execution to the connection
216 - (IBAction
)run
:(id)sender
221 - (IBAction
)attachedToggled
:(id)sender
223 connection.autoAttach
= [sender state
] == NSOnState
;
227 * Forwards the message to "step in" to the connection
229 - (IBAction
)stepIn
:(id)sender
231 if ([[variablesTreeController selectedObjects
] count
] > 0)
232 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
238 * Forwards the message to "step out" to the connection
240 - (IBAction
)stepOut
:(id)sender
242 if ([[variablesTreeController selectedObjects
] count
] > 0)
243 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
245 [connection stepOut
];
249 * Forwards the message to "step over" to the connection
251 - (IBAction
)stepOver
:(id)sender
253 if ([[variablesTreeController selectedObjects
] count
] > 0)
254 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
256 [connection stepOver
];
260 * Forwards the detach/"stop" message to the back end.
262 - (IBAction
)stop
:(id)sender
268 * NSTableView delegate method that informs the controller that the stack selection did change and that
269 * we should update the source viewer
271 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
273 // TODO: This is very, very hacky because it's nondeterministic. The issue
274 // is that calling |-[NSOutlineView expandItem:]| while the table is still
275 // doing its redraw will translate to a no-op. Instead, we need to restructure
276 // this controller so that when everything has been laid out we call
277 // |-expandVariables|; but NSOutlineView doesn't have a |-didFinishDoingCrap:|
278 // method. The other issue is that we need to call this method from
279 // selectionDidChange but ONLY when it was the result of a user-initiated
280 // action and not the stack viewer updating causing a selection change.
281 // If it happens in the latter, then we run into the same issue that causes
283 [self performSelector
:@selector(expandVariables
) withObject
:nil afterDelay
:0.05];
287 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
289 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
291 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
292 [expandedVariables addObject
:[[node representedObject
] fullName
]];
294 [connection loadVariableNode
:[node representedObject
]
295 forStackFrame
:[[stackArrayController selectedObjects
] lastObject
]];
299 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
301 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
303 [expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullName
]];
309 * Does the actual updating of the source viewer by reading in the file
311 - (void)updateSourceViewer
313 NSArray
* selection
= [stackArrayController selectedObjects
];
314 if (!selection ||
[selection count
] < 1)
316 if ([selection count
] > 1)
317 NSLog(@
"INVALID SELECTION");
318 StackFrame
* frame
= [selection objectAtIndex
:0];
321 [connection loadStackFrame
:frame
];
326 NSString
* filename
= [[NSURL URLWithString
:frame.filename
] path
];
327 if ([filename isEqualToString
:@
""])
330 // Replace the source if necessary.
331 if (frame.source
&& ![sourceViewer.file isEqualToString
:filename
])
333 [sourceViewer setString
:frame.source asFile
:filename
];
335 NSSet
* breakpoints
= [NSSet setWithArray
:[[BreakpointManager sharedManager
] breakpointsForFile
:filename
]];
336 [sourceViewer setMarkers
:breakpoints
];
339 [sourceViewer setMarkedLine
:frame.lineNumber
];
340 [sourceViewer scrollToLine
:frame.lineNumber
];
342 [[sourceViewer textView
] setNeedsDisplay
:YES
];
346 * Expands the variables based on the stored set
348 - (void)expandVariables
350 NSString
* selection
= [selectedVariable fullName
];
352 for (NSInteger i
= 0; i
< [variablesOutlineView numberOfRows
]; i
++) {
353 NSTreeNode
* node
= [variablesOutlineView itemAtRow
:i
];
354 NSString
* fullName
= [[node representedObject
] fullName
];
356 // see if it needs expanding
357 if ([expandedVariables containsObject
:fullName
])
358 [variablesOutlineView expandItem
:node
];
360 // select it if we had it selected before
361 if ([fullName isEqualToString
:selection
])
362 [variablesTreeController setSelectionIndexPath
:[node indexPath
]];
367 * Sets the widths of the segmented control.
369 - (void)updateSegmentControl
{
370 NSRect containerFrame
= [[_segmentControl superview
] frame
];
371 CGFloat containerWidth
= NSWidth(containerFrame
);
372 CGFloat segmentSizes
= 0;
373 for (NSInteger i
= 1; i
< [_segmentControl segmentCount
] - 1; ++i
) {
374 segmentSizes
+= [_segmentControl widthForSegment
:i
];
376 CGFloat spacerWidth
= (containerWidth
- segmentSizes
) / 2;
377 [_segmentControl setWidth
:spacerWidth forSegment
:0];
378 [_segmentControl setWidth
:spacerWidth forSegment
:[_segmentControl segmentCount
] - 1];
380 [_segmentControl setFrame
:NSMakeRect(-1, NSHeight(containerFrame
) - 27, containerWidth
+ 2, 30)];
383 #pragma mark BSSourceView Delegate
386 * The gutter was clicked, which indicates that a breakpoint needs to be changed
388 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
390 BreakpointManager
* mngr
= [BreakpointManager sharedManager
];
392 if ([mngr hasBreakpointAt
:line inFile
:file
])
394 [mngr removeBreakpointAt
:line inFile
:file
];
398 Breakpoint
* bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
399 [mngr addBreakpoint
:bp
];
403 [sourceViewer setMarkers
:[NSSet setWithArray
:[mngr breakpointsForFile
:file
]]];
404 [sourceViewer setNeedsDisplay
:YES
];