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"
27 @interface DebuggerController (Private
)
28 - (void)updateSourceViewer
;
29 - (void)expandVariables
;
32 @implementation DebuggerController
34 @synthesize connection
, sourceViewer
, inspector
;
37 * Initializes the window controller and sets the connection using preference
42 if (self = [super initWithWindowNibName
:@
"Debugger"])
44 NSUserDefaults
* defaults
= [NSUserDefaults standardUserDefaults
];
46 _model
= [[DebuggerModel alloc
] init
];
47 [_model addObserver
:self
48 forKeyPath
:@
"connected"
49 options
:NSKeyValueObservingOptionNew
52 connection
= [[DebuggerBackEnd alloc
] initWithPort
:[defaults integerForKey
:@
"Port"]];
53 connection.model
= _model
;
54 expandedVariables
= [[NSMutableSet alloc
] init
];
55 [[self window
] makeKeyAndOrderFront
:nil];
56 [[self window
] setDelegate
:self];
58 if ([defaults boolForKey
:@
"InspectorWindowVisible"])
59 [inspector orderFront
:self];
71 [expandedVariables release
];
76 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
80 [[self window
] setExcludedFromWindowsMenu
:YES
];
81 [[self window
] setTitle
:[NSString stringWithFormat
:@
"MacGDBp @ %d", [connection port
]]];
82 [sourceViewer setDelegate
:self];
83 [stackArrayController setSortDescriptors
:@
[ [[[NSSortDescriptor alloc
] initWithKey
:@
"index" ascending
:YES
] autorelease
] ]];
84 [stackArrayController addObserver
:self
85 forKeyPath
:@
"selectedObjects"
86 options
:NSKeyValueObservingOptionNew
88 [stackArrayController addObserver
:self
89 forKeyPath
:@
"selection.source"
90 options
:NSKeyValueObservingOptionNew
92 self.connection.autoAttach
= [attachedCheckbox_ state
] == NSOnState
;
96 * Key-value observation routine.
98 - (void)observeValueForKeyPath
:(NSString
*)keyPath
100 change
:(NSDictionary
<NSString
*,id>*)change
101 context
:(void*)context
{
102 if (object
== stackArrayController
&& [keyPath isEqualToString
:@
"selectedObjects"]) {
103 for (StackFrame
* frame
in stackArrayController.selectedObjects
)
104 [connection loadStackFrame
:frame
];
105 } else if (object
== stackArrayController
&& [keyPath isEqualToString
:@
"selection.source"]) {
106 [self updateSourceViewer
];
107 } else if (object
== _model
&& [keyPath isEqualToString
:@
"connected"]) {
108 if ([change
[NSKeyValueChangeNewKey
] boolValue
])
109 [self debuggerConnected
];
111 [self debuggerDisconnected
];
113 [super observeValueForKeyPath
:keyPath ofObject
:object change
:change context
:context
];
118 * Validates the menu items for the "Debugger" menu
120 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
122 SEL action
= [anItem action
];
124 if (action
== @selector(stepOut
:)) {
125 return ([connection isConnected
] && _model.stackDepth
> 1);
126 } else if (action
== @selector(stepIn
:) ||
127 action
== @selector(stepOver
:) ||
128 action
== @selector(run
:) ||
129 action
== @selector(stop
:) ||
130 action
== @selector(showEvalWindow
:)) {
131 return [connection isConnected
];
133 return [[self window
] validateUserInterfaceItem
:anItem
];
137 * Shows the inspector window
139 - (IBAction
)showInspectorWindow
:(id)sender
141 if (![inspector isVisible
])
142 [inspector makeKeyAndOrderFront
:sender
];
144 [inspector orderOut
:sender
];
148 * Runs the eval window sheet.
150 - (IBAction
)showEvalWindow
:(id)sender
152 // The |controller| will release itself on close.
153 EvalController
* controller
= [[EvalController alloc
] initWithBackEnd
:connection
];
154 [controller runModalForWindow
:[self window
]];
158 * Resets all the displays to be empty
160 - (void)resetDisplays
162 [variablesTreeController setContent
:nil];
163 [stackArrayController rearrangeObjects
];
164 [[sourceViewer textView
] setString
:@
""];
165 sourceViewer.file
= nil;
169 * Sets the status to be "Error" and then displays the error message
171 - (void)setError
:(NSString
*)anError
173 [errormsg setStringValue
:anError
];
174 [errormsg setHidden
:NO
];
178 * Delegate function for GDBpConnection for when the debugger connects.
180 - (void)debuggerConnected
182 [errormsg setHidden
:YES
];
183 if (!self.connection.autoAttach
)
185 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:@
"BreakOnFirstLine"])
190 * Called once the debugger disconnects.
192 - (void)debuggerDisconnected
194 // Invalidate the marked line so we don't look like we're still running.
195 sourceViewer.markedLine
= -1;
196 [sourceViewer setNeedsDisplay
:YES
];
200 * Forwards the message to run script execution to the connection
202 - (IBAction
)run
:(id)sender
207 - (IBAction
)attachedToggled
:(id)sender
209 connection.autoAttach
= [sender state
] == NSOnState
;
213 * Forwards the message to "step in" to the connection
215 - (IBAction
)stepIn
:(id)sender
217 if ([[variablesTreeController selectedObjects
] count
] > 0)
218 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
224 * Forwards the message to "step out" to the connection
226 - (IBAction
)stepOut
:(id)sender
228 if ([[variablesTreeController selectedObjects
] count
] > 0)
229 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
231 [connection stepOut
];
235 * Forwards the message to "step over" to the connection
237 - (IBAction
)stepOver
:(id)sender
239 if ([[variablesTreeController selectedObjects
] count
] > 0)
240 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
242 [connection stepOver
];
246 * Forwards the detach/"stop" message to the back end.
248 - (IBAction
)stop
:(id)sender
254 * NSTableView delegate method that informs the controller that the stack selection did change and that
255 * we should update the source viewer
257 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
259 // TODO: This is very, very hacky because it's nondeterministic. The issue
260 // is that calling |-[NSOutlineView expandItem:]| while the table is still
261 // doing its redraw will translate to a no-op. Instead, we need to restructure
262 // this controller so that when everything has been laid out we call
263 // |-expandVariables|; but NSOutlineView doesn't have a |-didFinishDoingCrap:|
264 // method. The other issue is that we need to call this method from
265 // selectionDidChange but ONLY when it was the result of a user-initiated
266 // action and not the stack viewer updating causing a selection change.
267 // If it happens in the latter, then we run into the same issue that causes
269 [self performSelector
:@selector(expandVariables
) withObject
:nil afterDelay
:0.05];
273 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
275 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
277 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
278 [expandedVariables addObject
:[[node representedObject
] fullName
]];
280 [connection loadVariableNode
:[node representedObject
]
281 forStackFrame
:[[stackArrayController selectedObjects
] lastObject
]];
285 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
287 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
289 [expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullName
]];
295 * Does the actual updating of the source viewer by reading in the file
297 - (void)updateSourceViewer
299 NSArray
* selection
= [stackArrayController selectedObjects
];
300 if (!selection ||
[selection count
] < 1)
302 if ([selection count
] > 1)
303 NSLog(@
"INVALID SELECTION");
304 StackFrame
* frame
= [selection objectAtIndex
:0];
307 [connection loadStackFrame
:frame
];
312 NSString
* filename
= [[NSURL URLWithString
:frame.filename
] path
];
313 if ([filename isEqualToString
:@
""])
316 // Replace the source if necessary.
317 if (frame.source
&& ![sourceViewer.file isEqualToString
:filename
])
319 [sourceViewer setString
:frame.source asFile
:filename
];
321 NSSet
* breakpoints
= [NSSet setWithArray
:[[BreakpointManager sharedManager
] breakpointsForFile
:filename
]];
322 [sourceViewer setMarkers
:breakpoints
];
325 [sourceViewer setMarkedLine
:frame.lineNumber
];
326 [sourceViewer scrollToLine
:frame.lineNumber
];
328 [[sourceViewer textView
] display
];
332 * Expands the variables based on the stored set
334 - (void)expandVariables
336 NSString
* selection
= [selectedVariable fullName
];
338 for (NSInteger i
= 0; i
< [variablesOutlineView numberOfRows
]; i
++) {
339 NSTreeNode
* node
= [variablesOutlineView itemAtRow
:i
];
340 NSString
* fullName
= [[node representedObject
] fullName
];
342 // see if it needs expanding
343 if ([expandedVariables containsObject
:fullName
])
344 [variablesOutlineView expandItem
:node
];
346 // select it if we had it selected before
347 if ([fullName isEqualToString
:selection
])
348 [variablesTreeController setSelectionIndexPath
:[node indexPath
]];
352 #pragma mark BSSourceView Delegate
355 * The gutter was clicked, which indicates that a breakpoint needs to be changed
357 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
359 BreakpointManager
* mngr
= [BreakpointManager sharedManager
];
361 if ([mngr hasBreakpointAt
:line inFile
:file
])
363 [mngr removeBreakpointAt
:line inFile
:file
];
367 Breakpoint
* bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
368 [mngr addBreakpoint
:bp
];
372 [sourceViewer setMarkers
:[NSSet setWithArray
:[mngr breakpointsForFile
:file
]]];
373 [sourceViewer setNeedsDisplay
:YES
];