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 "NSXMLElementAdditions.h"
26 #import "StackFrame.h"
28 @interface DebuggerController (Private
)
29 - (void)updateSourceViewer
;
30 - (void)expandVariables
;
33 @implementation DebuggerController
35 @synthesize connection
, sourceViewer
, inspector
;
38 * Initializes the window controller and sets the connection using preference
43 if (self = [super initWithWindowNibName
:@
"Debugger"])
45 NSUserDefaults
* defaults
= [NSUserDefaults standardUserDefaults
];
47 _model
= [[DebuggerModel alloc
] init
];
48 [_model addObserver
:self
49 forKeyPath
:@
"connected"
50 options
:NSKeyValueObservingOptionNew
53 connection
= [[DebuggerBackEnd alloc
] initWithPort
:[defaults integerForKey
:@
"Port"]
54 autoAttach
:[defaults boolForKey
:@
"DebuggerAttached"]];
55 connection.model
= _model
;
56 expandedVariables
= [[NSMutableSet alloc
] init
];
57 [[self window
] makeKeyAndOrderFront
:nil];
58 [[self window
] setDelegate
:self];
60 if ([defaults boolForKey
:@
"InspectorWindowVisible"])
61 [inspector orderFront
:self];
73 [expandedVariables release
];
78 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
82 [[self window
] setExcludedFromWindowsMenu
:YES
];
83 [[self window
] setTitle
:[NSString stringWithFormat
:@
"MacGDBp @ %d", [connection port
]]];
84 [sourceViewer setDelegate
:self];
85 [stackArrayController setSortDescriptors
:@
[ [[[NSSortDescriptor alloc
] initWithKey
:@
"index" ascending
:YES
] autorelease
] ]];
86 [stackArrayController addObserver
:self
87 forKeyPath
:@
"selectedObjects"
88 options
:NSKeyValueObservingOptionNew
90 [stackArrayController addObserver
:self
91 forKeyPath
:@
"selection.source"
92 options
:NSKeyValueObservingOptionNew
94 self.connection.autoAttach
= [attachedCheckbox_ state
] == NSOnState
;
98 * Key-value observation routine.
100 - (void)observeValueForKeyPath
:(NSString
*)keyPath
102 change
:(NSDictionary
<NSString
*,id>*)change
103 context
:(void*)context
{
104 if (object
== stackArrayController
&& [keyPath isEqualToString
:@
"selectedObjects"]) {
105 for (StackFrame
* frame
in stackArrayController.selectedObjects
)
106 [connection loadStackFrame
:frame
];
107 } else if (object
== stackArrayController
&& [keyPath isEqualToString
:@
"selection.source"]) {
108 [self updateSourceViewer
];
109 } else if (object
== _model
&& [keyPath isEqualToString
:@
"connected"]) {
110 if ([change
[NSKeyValueChangeNewKey
] boolValue
])
111 [self debuggerConnected
];
113 [self debuggerDisconnected
];
115 [super observeValueForKeyPath
:keyPath ofObject
:object change
:change context
:context
];
120 * Validates the menu items for the "Debugger" menu
122 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
124 SEL action
= [anItem action
];
126 if (action
== @selector(stepOut
:)) {
127 return _model.connected
&& _model.stackDepth
> 1;
128 } else if (action
== @selector(stepIn
:) ||
129 action
== @selector(stepOver
:) ||
130 action
== @selector(run
:) ||
131 action
== @selector(stop
:) ||
132 action
== @selector(showEvalWindow
:)) {
133 return _model.connected
;
135 return [[self window
] validateUserInterfaceItem
:anItem
];
139 * Shows the inspector window
141 - (IBAction
)showInspectorWindow
:(id)sender
143 if (![inspector isVisible
])
144 [inspector makeKeyAndOrderFront
:sender
];
146 [inspector orderOut
:sender
];
150 * Runs the eval window sheet.
152 - (IBAction
)showEvalWindow
:(id)sender
154 // The |controller| will release itself on close.
155 EvalController
* controller
= [[EvalController alloc
] initWithBackEnd
:connection
];
156 [controller runModalForWindow
:[self window
]];
160 * Resets all the displays to be empty
162 - (void)resetDisplays
164 [variablesTreeController setContent
:nil];
165 [stackArrayController rearrangeObjects
];
166 [[sourceViewer textView
] setString
:@
""];
167 sourceViewer.file
= nil;
171 * Sets the status to be "Error" and then displays the error message
173 - (void)setError
:(NSString
*)anError
175 [errormsg setStringValue
:anError
];
176 [errormsg setHidden
:NO
];
180 * Delegate function for GDBpConnection for when the debugger connects.
182 - (void)debuggerConnected
184 [errormsg setHidden
:YES
];
185 if (!self.connection.autoAttach
)
187 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:@
"BreakOnFirstLine"])
189 // Do not cache the file between debugger executions.
190 sourceViewer.file
= nil;
194 * Called once the debugger disconnects.
196 - (void)debuggerDisconnected
198 // Invalidate the marked line so we don't look like we're still running.
199 sourceViewer.markedLine
= -1;
200 [sourceViewer setNeedsDisplay
:YES
];
204 * Forwards the message to run script execution to the connection
206 - (IBAction
)run
:(id)sender
211 - (IBAction
)attachedToggled
:(id)sender
213 connection.autoAttach
= [sender state
] == NSOnState
;
217 * Forwards the message to "step in" to the connection
219 - (IBAction
)stepIn
:(id)sender
221 if ([[variablesTreeController selectedObjects
] count
] > 0)
222 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
228 * Forwards the message to "step out" to the connection
230 - (IBAction
)stepOut
:(id)sender
232 if ([[variablesTreeController selectedObjects
] count
] > 0)
233 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
235 [connection stepOut
];
239 * Forwards the message to "step over" to the connection
241 - (IBAction
)stepOver
:(id)sender
243 if ([[variablesTreeController selectedObjects
] count
] > 0)
244 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
246 [connection stepOver
];
250 * Forwards the detach/"stop" message to the back end.
252 - (IBAction
)stop
:(id)sender
258 * NSTableView delegate method that informs the controller that the stack selection did change and that
259 * we should update the source viewer
261 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
263 // TODO: This is very, very hacky because it's nondeterministic. The issue
264 // is that calling |-[NSOutlineView expandItem:]| while the table is still
265 // doing its redraw will translate to a no-op. Instead, we need to restructure
266 // this controller so that when everything has been laid out we call
267 // |-expandVariables|; but NSOutlineView doesn't have a |-didFinishDoingCrap:|
268 // method. The other issue is that we need to call this method from
269 // selectionDidChange but ONLY when it was the result of a user-initiated
270 // action and not the stack viewer updating causing a selection change.
271 // If it happens in the latter, then we run into the same issue that causes
273 [self performSelector
:@selector(expandVariables
) withObject
:nil afterDelay
:0.05];
277 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
279 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
281 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
282 [expandedVariables addObject
:[[node representedObject
] fullName
]];
284 [connection loadVariableNode
:[node representedObject
]
285 forStackFrame
:[[stackArrayController selectedObjects
] lastObject
]];
289 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
291 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
293 [expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullName
]];
299 * Does the actual updating of the source viewer by reading in the file
301 - (void)updateSourceViewer
303 NSArray
* selection
= [stackArrayController selectedObjects
];
304 if (!selection ||
[selection count
] < 1)
306 if ([selection count
] > 1)
307 NSLog(@
"INVALID SELECTION");
308 StackFrame
* frame
= [selection objectAtIndex
:0];
311 [connection loadStackFrame
:frame
];
316 NSString
* filename
= [[NSURL URLWithString
:frame.filename
] path
];
317 if ([filename isEqualToString
:@
""])
320 // Replace the source if necessary.
321 if (frame.source
&& ![sourceViewer.file isEqualToString
:filename
])
323 [sourceViewer setString
:frame.source asFile
:filename
];
325 NSSet
* breakpoints
= [NSSet setWithArray
:[[BreakpointManager sharedManager
] breakpointsForFile
:filename
]];
326 [sourceViewer setMarkers
:breakpoints
];
329 [sourceViewer setMarkedLine
:frame.lineNumber
];
330 [sourceViewer scrollToLine
:frame.lineNumber
];
332 [[sourceViewer textView
] display
];
336 * Expands the variables based on the stored set
338 - (void)expandVariables
340 NSString
* selection
= [selectedVariable fullName
];
342 for (NSInteger i
= 0; i
< [variablesOutlineView numberOfRows
]; i
++) {
343 NSTreeNode
* node
= [variablesOutlineView itemAtRow
:i
];
344 NSString
* fullName
= [[node representedObject
] fullName
];
346 // see if it needs expanding
347 if ([expandedVariables containsObject
:fullName
])
348 [variablesOutlineView expandItem
:node
];
350 // select it if we had it selected before
351 if ([fullName isEqualToString
:selection
])
352 [variablesTreeController setSelectionIndexPath
:[node indexPath
]];
356 #pragma mark BSSourceView Delegate
359 * The gutter was clicked, which indicates that a breakpoint needs to be changed
361 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
363 BreakpointManager
* mngr
= [BreakpointManager sharedManager
];
365 if ([mngr hasBreakpointAt
:line inFile
:file
])
367 [mngr removeBreakpointAt
:line inFile
:file
];
371 Breakpoint
* bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
372 [mngr addBreakpoint
:bp
];
376 [sourceViewer setMarkers
:[NSSet setWithArray
:[mngr breakpointsForFile
:file
]]];
377 [sourceViewer setNeedsDisplay
:YES
];