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 "DebuggerModel.h"
23 #import "EvalController.h"
24 #import "NSXMLElementAdditions.h"
26 @interface DebuggerController (Private
)
27 - (void)updateSourceViewer
;
28 - (void)expandVariables
;
31 @implementation DebuggerController
33 @synthesize connection
, sourceViewer
, inspector
;
36 * Initializes the window controller and sets the connection using preference
41 if (self = [super initWithWindowNibName
:@
"Debugger"])
43 NSUserDefaults
* defaults
= [NSUserDefaults standardUserDefaults
];
45 _model
= [[DebuggerModel alloc
] init
];
47 connection
= [[DebuggerBackEnd alloc
] initWithPort
:[defaults integerForKey
:@
"Port"]];
48 connection.delegate
= self;
49 connection.model
= _model
;
50 expandedVariables
= [[NSMutableSet alloc
] init
];
51 [[self window
] makeKeyAndOrderFront
:nil];
52 [[self window
] setDelegate
:self];
54 if ([defaults boolForKey
:@
"InspectorWindowVisible"])
55 [inspector orderFront
:self];
67 [expandedVariables release
];
72 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
76 [[self window
] setExcludedFromWindowsMenu
:YES
];
77 [[self window
] setTitle
:[NSString stringWithFormat
:@
"MacGDBp @ %d", [connection port
]]];
78 [sourceViewer setDelegate
:self];
79 [stackArrayController setSortDescriptors
:@
[ [[[NSSortDescriptor alloc
] initWithKey
:@
"index" ascending
:YES
] autorelease
] ]];
80 [stackArrayController addObserver
:self
81 forKeyPath
:@
"selectedObjects"
82 options
:NSKeyValueObservingOptionNew
84 [stackArrayController addObserver
:self
85 forKeyPath
:@
"selection.source"
86 options
:NSKeyValueObservingOptionNew
88 self.connection.autoAttach
= [attachedCheckbox_ state
] == NSOnState
;
92 * Key-value observation routine.
94 - (void)observeValueForKeyPath
:(NSString
*)keyPath
96 change
:(NSDictionary
<NSString
*,id>*)change
97 context
:(void*)context
{
98 if (object
== stackArrayController
&& [keyPath isEqualToString
:@
"selectedObjects"]) {
99 for (StackFrame
* frame
in stackArrayController.selectedObjects
)
100 [connection loadStackFrame
:frame
];
101 } else if (object
== stackArrayController
&& [keyPath isEqualToString
:@
"selection.source"]) {
102 [self updateSourceViewer
];
104 [super observeValueForKeyPath
:keyPath ofObject
:object change
:change context
:context
];
109 * Validates the menu items for the "Debugger" menu
111 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
113 SEL action
= [anItem action
];
115 if (action
== @selector(stepOut
:)) {
116 return ([connection isConnected
] && _model.stackDepth
> 1);
117 } else if (action
== @selector(stepIn
:) ||
118 action
== @selector(stepOver
:) ||
119 action
== @selector(run
:) ||
120 action
== @selector(stop
:) ||
121 action
== @selector(showEvalWindow
:)) {
122 return [connection isConnected
];
124 return [[self window
] validateUserInterfaceItem
:anItem
];
128 * Shows the inspector window
130 - (IBAction
)showInspectorWindow
:(id)sender
132 if (![inspector isVisible
])
133 [inspector makeKeyAndOrderFront
:sender
];
135 [inspector orderOut
:sender
];
139 * Runs the eval window sheet.
141 - (IBAction
)showEvalWindow
:(id)sender
143 // The |controller| will release itself on close.
144 EvalController
* controller
= [[EvalController alloc
] initWithBackEnd
:connection
];
145 [controller runModalForWindow
:[self window
]];
149 * Resets all the displays to be empty
151 - (void)resetDisplays
153 [variablesTreeController setContent
:nil];
154 [stackArrayController rearrangeObjects
];
155 [[sourceViewer textView
] setString
:@
""];
156 sourceViewer.file
= nil;
160 * Sets the status to be "Error" and then displays the error message
162 - (void)setError
:(NSString
*)anError
164 [errormsg setStringValue
:anError
];
165 [errormsg setHidden
:NO
];
169 * Handles a GDBpConnection error
171 - (void)errorEncountered
:(NSString
*)error
173 [self setError
:error
];
177 * Delegate function for GDBpConnection for when the debugger connects.
179 - (void)debuggerConnected
181 [errormsg setHidden
:YES
];
182 if (!self.connection.autoAttach
)
184 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:@
"BreakOnFirstLine"])
189 * Called once the debugger disconnects.
191 - (void)debuggerDisconnected
193 // Invalidate the marked line so we don't look like we're still running.
194 sourceViewer.markedLine
= -1;
195 [sourceViewer setNeedsDisplay
:YES
];
199 * Forwards the message to run script execution to the connection
201 - (IBAction
)run
:(id)sender
206 - (IBAction
)attachedToggled
:(id)sender
208 connection.autoAttach
= [sender state
] == NSOnState
;
212 * Forwards the message to "step in" to the connection
214 - (IBAction
)stepIn
:(id)sender
216 if ([[variablesTreeController selectedObjects
] count
] > 0)
217 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
223 * Forwards the message to "step out" to the connection
225 - (IBAction
)stepOut
:(id)sender
227 if ([[variablesTreeController selectedObjects
] count
] > 0)
228 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
230 [connection stepOut
];
234 * Forwards the message to "step over" to the connection
236 - (IBAction
)stepOver
:(id)sender
238 if ([[variablesTreeController selectedObjects
] count
] > 0)
239 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
241 [connection stepOver
];
245 * Forwards the detach/"stop" message to the back end.
247 - (IBAction
)stop
:(id)sender
253 * NSTableView delegate method that informs the controller that the stack selection did change and that
254 * we should update the source viewer
256 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
258 // TODO: This is very, very hacky because it's nondeterministic. The issue
259 // is that calling |-[NSOutlineView expandItem:]| while the table is still
260 // doing its redraw will translate to a no-op. Instead, we need to restructure
261 // this controller so that when everything has been laid out we call
262 // |-expandVariables|; but NSOutlineView doesn't have a |-didFinishDoingCrap:|
263 // method. The other issue is that we need to call this method from
264 // selectionDidChange but ONLY when it was the result of a user-initiated
265 // action and not the stack viewer updating causing a selection change.
266 // If it happens in the latter, then we run into the same issue that causes
268 [self performSelector
:@selector(expandVariables
) withObject
:nil afterDelay
:0.05];
272 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
274 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
276 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
277 [expandedVariables addObject
:[[node representedObject
] fullName
]];
279 [connection loadVariableNode
:[node representedObject
]
280 forStackFrame
:[[stackArrayController selectedObjects
] lastObject
]];
284 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
286 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
288 [expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullName
]];
294 * Does the actual updating of the source viewer by reading in the file
296 - (void)updateSourceViewer
298 NSArray
* selection
= [stackArrayController selectedObjects
];
299 if (!selection ||
[selection count
] < 1)
301 if ([selection count
] > 1)
302 NSLog(@
"INVALID SELECTION");
303 StackFrame
* frame
= [selection objectAtIndex
:0];
306 [connection loadStackFrame
:frame
];
311 NSString
* filename
= [[NSURL URLWithString
:frame.filename
] path
];
312 if ([filename isEqualToString
:@
""])
315 // Replace the source if necessary.
316 if (frame.source
&& ![sourceViewer.file isEqualToString
:filename
])
318 [sourceViewer setString
:frame.source asFile
:filename
];
320 NSSet
* breakpoints
= [NSSet setWithArray
:[[BreakpointManager sharedManager
] breakpointsForFile
:filename
]];
321 [sourceViewer setMarkers
:breakpoints
];
324 [sourceViewer setMarkedLine
:frame.lineNumber
];
325 [sourceViewer scrollToLine
:frame.lineNumber
];
327 [[sourceViewer textView
] display
];
331 * Expands the variables based on the stored set
333 - (void)expandVariables
335 NSString
* selection
= [selectedVariable fullName
];
337 for (NSInteger i
= 0; i
< [variablesOutlineView numberOfRows
]; i
++) {
338 NSTreeNode
* node
= [variablesOutlineView itemAtRow
:i
];
339 NSString
* fullName
= [[node representedObject
] fullName
];
341 // see if it needs expanding
342 if ([expandedVariables containsObject
:fullName
])
343 [variablesOutlineView expandItem
:node
];
345 // select it if we had it selected before
346 if ([fullName isEqualToString
:selection
])
347 [variablesTreeController setSelectionIndexPath
:[node indexPath
]];
351 #pragma mark BSSourceView Delegate
354 * The gutter was clicked, which indicates that a breakpoint needs to be changed
356 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
358 BreakpointManager
* mngr
= [BreakpointManager sharedManager
];
360 if ([mngr hasBreakpointAt
:line inFile
:file
])
362 [mngr removeBreakpointAt
:line inFile
:file
];
366 Breakpoint
* bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
367 [mngr addBreakpoint
:bp
];
371 [sourceViewer setMarkers
:[NSSet setWithArray
:[mngr breakpointsForFile
:file
]]];
372 [sourceViewer setNeedsDisplay
:YES
];