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 self.connection.autoAttach
= [attachedCheckbox_ state
] == NSOnState
;
88 * Key-value observation routine.
90 - (void)observeValueForKeyPath
:(NSString
*)keyPath
92 change
:(NSDictionary
<NSString
*,id>*)change
93 context
:(void*)context
{
94 if (object
== stackArrayController
&& [keyPath isEqualToString
:@
"selectedObjects"]) {
95 for (StackFrame
* frame
in stackArrayController.selectedObjects
)
96 [connection loadStackFrame
:frame
];
98 [super observeValueForKeyPath
:keyPath ofObject
:object change
:change context
:context
];
103 * Validates the menu items for the "Debugger" menu
105 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
107 SEL action
= [anItem action
];
109 if (action
== @selector(stepOut
:)) {
110 return ([connection isConnected
] && _model.stackDepth
> 1);
111 } else if (action
== @selector(stepIn
:) ||
112 action
== @selector(stepOver
:) ||
113 action
== @selector(run
:) ||
114 action
== @selector(stop
:) ||
115 action
== @selector(showEvalWindow
:)) {
116 return [connection isConnected
];
118 return [[self window
] validateUserInterfaceItem
:anItem
];
122 * Shows the inspector window
124 - (IBAction
)showInspectorWindow
:(id)sender
126 if (![inspector isVisible
])
127 [inspector makeKeyAndOrderFront
:sender
];
129 [inspector orderOut
:sender
];
133 * Runs the eval window sheet.
135 - (IBAction
)showEvalWindow
:(id)sender
137 // The |controller| will release itself on close.
138 EvalController
* controller
= [[EvalController alloc
] initWithBackEnd
:connection
];
139 [controller runModalForWindow
:[self window
]];
143 * Resets all the displays to be empty
145 - (void)resetDisplays
147 [variablesTreeController setContent
:nil];
148 [stackArrayController rearrangeObjects
];
149 [[sourceViewer textView
] setString
:@
""];
150 sourceViewer.file
= nil;
154 * Sets the status to be "Error" and then displays the error message
156 - (void)setError
:(NSString
*)anError
158 [errormsg setStringValue
:anError
];
159 [errormsg setHidden
:NO
];
163 * Handles a GDBpConnection error
165 - (void)errorEncountered
:(NSString
*)error
167 [self setError
:error
];
171 * Delegate function for GDBpConnection for when the debugger connects.
173 - (void)debuggerConnected
175 [errormsg setHidden
:YES
];
176 if (!self.connection.autoAttach
)
178 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:@
"BreakOnFirstLine"])
183 * Called once the debugger disconnects.
185 - (void)debuggerDisconnected
187 // Invalidate the marked line so we don't look like we're still running.
188 sourceViewer.markedLine
= -1;
189 [sourceViewer setNeedsDisplay
:YES
];
193 * Forwards the message to run script execution to the connection
195 - (IBAction
)run
:(id)sender
200 - (IBAction
)attachedToggled
:(id)sender
202 connection.autoAttach
= [sender state
] == NSOnState
;
206 * Forwards the message to "step in" to the connection
208 - (IBAction
)stepIn
:(id)sender
210 if ([[variablesTreeController selectedObjects
] count
] > 0)
211 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
217 * Forwards the message to "step out" to the connection
219 - (IBAction
)stepOut
:(id)sender
221 if ([[variablesTreeController selectedObjects
] count
] > 0)
222 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
224 [connection stepOut
];
228 * Forwards the message to "step over" to the connection
230 - (IBAction
)stepOver
:(id)sender
232 if ([[variablesTreeController selectedObjects
] count
] > 0)
233 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
235 [connection stepOver
];
239 * Forwards the detach/"stop" message to the back end.
241 - (IBAction
)stop
:(id)sender
247 * NSTableView delegate method that informs the controller that the stack selection did change and that
248 * we should update the source viewer
250 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
252 [self updateSourceViewer
];
253 // TODO: This is very, very hacky because it's nondeterministic. The issue
254 // is that calling |-[NSOutlineView expandItem:]| while the table is still
255 // doing its redraw will translate to a no-op. Instead, we need to restructure
256 // this controller so that when everything has been laid out we call
257 // |-expandVariables|; but NSOutlineView doesn't have a |-didFinishDoingCrap:|
258 // method. The other issue is that we need to call this method from
259 // selectionDidChange but ONLY when it was the result of a user-initiated
260 // action and not the stack viewer updating causing a selection change.
261 // If it happens in the latter, then we run into the same issue that causes
263 [self performSelector
:@selector(expandVariables
) withObject
:nil afterDelay
:0.05];
267 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
269 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
271 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
272 [expandedVariables addObject
:[[node representedObject
] fullName
]];
274 [connection loadVariableNode
:[node representedObject
]
275 forStackFrame
:[[stackArrayController selectedObjects
] lastObject
]];
279 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
281 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
283 [expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullName
]];
289 * Does the actual updating of the source viewer by reading in the file
291 - (void)updateSourceViewer
293 NSArray
* selection
= [stackArrayController selectedObjects
];
294 if (!selection ||
[selection count
] < 1)
296 if ([selection count
] > 1)
297 NSLog(@
"INVALID SELECTION");
298 StackFrame
* frame
= [selection objectAtIndex
:0];
301 [connection loadStackFrame
:frame
];
306 NSString
* filename
= [[NSURL URLWithString
:frame.filename
] path
];
307 if ([filename isEqualToString
:@
""])
310 // Replace the source if necessary.
311 if (frame.source
&& ![sourceViewer.file isEqualToString
:filename
])
313 [sourceViewer setString
:frame.source asFile
:filename
];
315 NSSet
* breakpoints
= [NSSet setWithArray
:[[BreakpointManager sharedManager
] breakpointsForFile
:filename
]];
316 [sourceViewer setMarkers
:breakpoints
];
319 [sourceViewer setMarkedLine
:frame.lineNumber
];
320 [sourceViewer scrollToLine
:frame.lineNumber
];
322 [[sourceViewer textView
] display
];
326 * Expands the variables based on the stored set
328 - (void)expandVariables
330 NSString
* selection
= [selectedVariable fullName
];
332 for (NSInteger i
= 0; i
< [variablesOutlineView numberOfRows
]; i
++) {
333 NSTreeNode
* node
= [variablesOutlineView itemAtRow
:i
];
334 NSString
* fullName
= [[node representedObject
] fullName
];
336 // see if it needs expanding
337 if ([expandedVariables containsObject
:fullName
])
338 [variablesOutlineView expandItem
:node
];
340 // select it if we had it selected before
341 if ([fullName isEqualToString
:selection
])
342 [variablesTreeController setSelectionIndexPath
:[node indexPath
]];
346 #pragma mark BSSourceView Delegate
349 * The gutter was clicked, which indicates that a breakpoint needs to be changed
351 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
353 BreakpointManager
* mngr
= [BreakpointManager sharedManager
];
355 if ([mngr hasBreakpointAt
:line inFile
:file
])
357 [mngr removeBreakpointAt
:line inFile
:file
];
361 Breakpoint
* bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
362 [mngr addBreakpoint
:bp
];
366 [sourceViewer setMarkers
:[NSSet setWithArray
:[mngr breakpointsForFile
:file
]]];
367 [sourceViewer setNeedsDisplay
:YES
];
370 #pragma mark GDBpConnectionDelegate
372 - (void)sourceUpdated
:(StackFrame
*)frame
374 [self updateSourceViewer
];