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"])
192 * Called once the debugger disconnects.
194 - (void)debuggerDisconnected
196 // Invalidate the marked line so we don't look like we're still running.
197 sourceViewer.markedLine
= -1;
198 [sourceViewer setNeedsDisplay
:YES
];
202 * Forwards the message to run script execution to the connection
204 - (IBAction
)run
:(id)sender
209 - (IBAction
)attachedToggled
:(id)sender
211 connection.autoAttach
= [sender state
] == NSOnState
;
215 * Forwards the message to "step in" to the connection
217 - (IBAction
)stepIn
:(id)sender
219 if ([[variablesTreeController selectedObjects
] count
] > 0)
220 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
226 * Forwards the message to "step out" to the connection
228 - (IBAction
)stepOut
:(id)sender
230 if ([[variablesTreeController selectedObjects
] count
] > 0)
231 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
233 [connection stepOut
];
237 * Forwards the message to "step over" to the connection
239 - (IBAction
)stepOver
:(id)sender
241 if ([[variablesTreeController selectedObjects
] count
] > 0)
242 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
244 [connection stepOver
];
248 * Forwards the detach/"stop" message to the back end.
250 - (IBAction
)stop
:(id)sender
256 * NSTableView delegate method that informs the controller that the stack selection did change and that
257 * we should update the source viewer
259 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
261 // TODO: This is very, very hacky because it's nondeterministic. The issue
262 // is that calling |-[NSOutlineView expandItem:]| while the table is still
263 // doing its redraw will translate to a no-op. Instead, we need to restructure
264 // this controller so that when everything has been laid out we call
265 // |-expandVariables|; but NSOutlineView doesn't have a |-didFinishDoingCrap:|
266 // method. The other issue is that we need to call this method from
267 // selectionDidChange but ONLY when it was the result of a user-initiated
268 // action and not the stack viewer updating causing a selection change.
269 // If it happens in the latter, then we run into the same issue that causes
271 [self performSelector
:@selector(expandVariables
) withObject
:nil afterDelay
:0.05];
275 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
277 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
279 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
280 [expandedVariables addObject
:[[node representedObject
] fullName
]];
282 [connection loadVariableNode
:[node representedObject
]
283 forStackFrame
:[[stackArrayController selectedObjects
] lastObject
]];
287 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
289 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
291 [expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullName
]];
297 * Does the actual updating of the source viewer by reading in the file
299 - (void)updateSourceViewer
301 NSArray
* selection
= [stackArrayController selectedObjects
];
302 if (!selection ||
[selection count
] < 1)
304 if ([selection count
] > 1)
305 NSLog(@
"INVALID SELECTION");
306 StackFrame
* frame
= [selection objectAtIndex
:0];
309 [connection loadStackFrame
:frame
];
314 NSString
* filename
= [[NSURL URLWithString
:frame.filename
] path
];
315 if ([filename isEqualToString
:@
""])
318 // Replace the source if necessary.
319 if (frame.source
&& ![sourceViewer.file isEqualToString
:filename
])
321 [sourceViewer setString
:frame.source asFile
:filename
];
323 NSSet
* breakpoints
= [NSSet setWithArray
:[[BreakpointManager sharedManager
] breakpointsForFile
:filename
]];
324 [sourceViewer setMarkers
:breakpoints
];
327 [sourceViewer setMarkedLine
:frame.lineNumber
];
328 [sourceViewer scrollToLine
:frame.lineNumber
];
330 [[sourceViewer textView
] display
];
334 * Expands the variables based on the stored set
336 - (void)expandVariables
338 NSString
* selection
= [selectedVariable fullName
];
340 for (NSInteger i
= 0; i
< [variablesOutlineView numberOfRows
]; i
++) {
341 NSTreeNode
* node
= [variablesOutlineView itemAtRow
:i
];
342 NSString
* fullName
= [[node representedObject
] fullName
];
344 // see if it needs expanding
345 if ([expandedVariables containsObject
:fullName
])
346 [variablesOutlineView expandItem
:node
];
348 // select it if we had it selected before
349 if ([fullName isEqualToString
:selection
])
350 [variablesTreeController setSelectionIndexPath
:[node indexPath
]];
354 #pragma mark BSSourceView Delegate
357 * The gutter was clicked, which indicates that a breakpoint needs to be changed
359 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
361 BreakpointManager
* mngr
= [BreakpointManager sharedManager
];
363 if ([mngr hasBreakpointAt
:line inFile
:file
])
365 [mngr removeBreakpointAt
:line inFile
:file
];
369 Breakpoint
* bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
370 [mngr addBreakpoint
:bp
];
374 [sourceViewer setMarkers
:[NSSet setWithArray
:[mngr breakpointsForFile
:file
]]];
375 [sourceViewer setNeedsDisplay
:YES
];