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 "PreferenceNames.h"
26 #import "NSXMLElementAdditions.h"
27 #import "StackFrame.h"
29 @interface DebuggerController (Private
)
30 - (void)updateSourceViewer
;
31 - (void)expandVariables
;
34 @implementation DebuggerController
36 @synthesize connection
, sourceViewer
, inspector
;
39 * Initializes the window controller and sets the connection using preference
44 if (self = [super initWithWindowNibName
:@
"Debugger"])
46 NSUserDefaults
* defaults
= [NSUserDefaults standardUserDefaults
];
48 _model
= [[DebuggerModel alloc
] init
];
49 [_model addObserver
:self
50 forKeyPath
:@
"connected"
51 options
:NSKeyValueObservingOptionNew
54 connection
= [[DebuggerBackEnd alloc
] initWithPort
:[defaults integerForKey
:kPrefPort
]
55 autoAttach
:[defaults boolForKey
:kPrefDebuggerAttached
]];
56 connection.model
= _model
;
57 expandedVariables
= [[NSMutableSet alloc
] init
];
58 [[self window
] makeKeyAndOrderFront
:nil];
59 [[self window
] setDelegate
:self];
61 if ([defaults boolForKey
:kPrefInspectorWindowVisible
])
62 [inspector orderFront
:self];
74 [expandedVariables release
];
79 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
83 [[self window
] setExcludedFromWindowsMenu
:YES
];
84 [[self window
] setTitle
:[NSString stringWithFormat
:@
"MacGDBp @ %d", [connection port
]]];
85 [sourceViewer setDelegate
:self];
86 [stackArrayController setSortDescriptors
:@
[ [[[NSSortDescriptor alloc
] initWithKey
:@
"index" ascending
:YES
] autorelease
] ]];
87 [stackArrayController addObserver
:self
88 forKeyPath
:@
"selectedObjects"
89 options
:NSKeyValueObservingOptionNew
91 [stackArrayController addObserver
:self
92 forKeyPath
:@
"selection.source"
93 options
:NSKeyValueObservingOptionNew
95 self.connection.autoAttach
= [attachedCheckbox_ state
] == NSOnState
;
99 * Key-value observation routine.
101 - (void)observeValueForKeyPath
:(NSString
*)keyPath
103 change
:(NSDictionary
<NSString
*,id>*)change
104 context
:(void*)context
{
105 if (object
== stackArrayController
&& [keyPath isEqualToString
:@
"selectedObjects"]) {
106 for (StackFrame
* frame
in stackArrayController.selectedObjects
)
107 [connection loadStackFrame
:frame
];
108 } else if (object
== stackArrayController
&& [keyPath isEqualToString
:@
"selection.source"]) {
109 [self updateSourceViewer
];
110 } else if (object
== _model
&& [keyPath isEqualToString
:@
"connected"]) {
111 if ([change
[NSKeyValueChangeNewKey
] boolValue
])
112 [self debuggerConnected
];
114 [self debuggerDisconnected
];
116 [super observeValueForKeyPath
:keyPath ofObject
:object change
:change context
:context
];
121 * Validates the menu items for the "Debugger" menu
123 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
125 SEL action
= [anItem action
];
127 if (action
== @selector(stepOut
:)) {
128 return _model.connected
&& _model.stackDepth
> 1;
129 } else if (action
== @selector(stepIn
:) ||
130 action
== @selector(stepOver
:) ||
131 action
== @selector(run
:) ||
132 action
== @selector(stop
:) ||
133 action
== @selector(showEvalWindow
:)) {
134 return _model.connected
;
136 return [[self window
] validateUserInterfaceItem
:anItem
];
140 * Shows the inspector window
142 - (IBAction
)showInspectorWindow
:(id)sender
144 if (![inspector isVisible
])
145 [inspector makeKeyAndOrderFront
:sender
];
147 [inspector orderOut
:sender
];
151 * Runs the eval window sheet.
153 - (IBAction
)showEvalWindow
:(id)sender
155 // The |controller| will release itself on close.
156 EvalController
* controller
= [[EvalController alloc
] initWithBackEnd
:connection
];
157 [controller runModalForWindow
:[self window
]];
161 * Resets all the displays to be empty
163 - (void)resetDisplays
165 [variablesTreeController setContent
:nil];
166 [stackArrayController rearrangeObjects
];
167 [[sourceViewer textView
] setString
:@
""];
168 sourceViewer.file
= nil;
172 * Sets the status to be "Error" and then displays the error message
174 - (void)setError
:(NSString
*)anError
176 [errormsg setStringValue
:anError
];
177 [errormsg setHidden
:NO
];
181 * Delegate function for GDBpConnection for when the debugger connects.
183 - (void)debuggerConnected
185 [errormsg setHidden
:YES
];
186 if (!self.connection.autoAttach
)
188 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:kPrefBreakOnFirstLine
])
190 // Do not cache the file between debugger executions.
191 sourceViewer.file
= nil;
195 * Called once the debugger disconnects.
197 - (void)debuggerDisconnected
199 // Invalidate the marked line so we don't look like we're still running.
200 sourceViewer.markedLine
= -1;
201 [sourceViewer setNeedsDisplay
:YES
];
205 * Forwards the message to run script execution to the connection
207 - (IBAction
)run
:(id)sender
212 - (IBAction
)attachedToggled
:(id)sender
214 connection.autoAttach
= [sender state
] == NSOnState
;
218 * Forwards the message to "step in" to the connection
220 - (IBAction
)stepIn
:(id)sender
222 if ([[variablesTreeController selectedObjects
] count
] > 0)
223 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
229 * Forwards the message to "step out" to the connection
231 - (IBAction
)stepOut
:(id)sender
233 if ([[variablesTreeController selectedObjects
] count
] > 0)
234 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
236 [connection stepOut
];
240 * Forwards the message to "step over" to the connection
242 - (IBAction
)stepOver
:(id)sender
244 if ([[variablesTreeController selectedObjects
] count
] > 0)
245 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
247 [connection stepOver
];
251 * Forwards the detach/"stop" message to the back end.
253 - (IBAction
)stop
:(id)sender
259 * NSTableView delegate method that informs the controller that the stack selection did change and that
260 * we should update the source viewer
262 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
264 // TODO: This is very, very hacky because it's nondeterministic. The issue
265 // is that calling |-[NSOutlineView expandItem:]| while the table is still
266 // doing its redraw will translate to a no-op. Instead, we need to restructure
267 // this controller so that when everything has been laid out we call
268 // |-expandVariables|; but NSOutlineView doesn't have a |-didFinishDoingCrap:|
269 // method. The other issue is that we need to call this method from
270 // selectionDidChange but ONLY when it was the result of a user-initiated
271 // action and not the stack viewer updating causing a selection change.
272 // If it happens in the latter, then we run into the same issue that causes
274 [self performSelector
:@selector(expandVariables
) withObject
:nil afterDelay
:0.05];
278 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
280 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
282 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
283 [expandedVariables addObject
:[[node representedObject
] fullName
]];
285 [connection loadVariableNode
:[node representedObject
]
286 forStackFrame
:[[stackArrayController selectedObjects
] lastObject
]];
290 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
292 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
294 [expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullName
]];
300 * Does the actual updating of the source viewer by reading in the file
302 - (void)updateSourceViewer
304 NSArray
* selection
= [stackArrayController selectedObjects
];
305 if (!selection ||
[selection count
] < 1)
307 if ([selection count
] > 1)
308 NSLog(@
"INVALID SELECTION");
309 StackFrame
* frame
= [selection objectAtIndex
:0];
312 [connection loadStackFrame
:frame
];
317 NSString
* filename
= [[NSURL URLWithString
:frame.filename
] path
];
318 if ([filename isEqualToString
:@
""])
321 // Replace the source if necessary.
322 if (frame.source
&& ![sourceViewer.file isEqualToString
:filename
])
324 [sourceViewer setString
:frame.source asFile
:filename
];
326 NSSet
* breakpoints
= [NSSet setWithArray
:[[BreakpointManager sharedManager
] breakpointsForFile
:filename
]];
327 [sourceViewer setMarkers
:breakpoints
];
330 [sourceViewer setMarkedLine
:frame.lineNumber
];
331 [sourceViewer scrollToLine
:frame.lineNumber
];
333 [[sourceViewer textView
] setNeedsDisplay
:YES
];
337 * Expands the variables based on the stored set
339 - (void)expandVariables
341 NSString
* selection
= [selectedVariable fullName
];
343 for (NSInteger i
= 0; i
< [variablesOutlineView numberOfRows
]; i
++) {
344 NSTreeNode
* node
= [variablesOutlineView itemAtRow
:i
];
345 NSString
* fullName
= [[node representedObject
] fullName
];
347 // see if it needs expanding
348 if ([expandedVariables containsObject
:fullName
])
349 [variablesOutlineView expandItem
:node
];
351 // select it if we had it selected before
352 if ([fullName isEqualToString
:selection
])
353 [variablesTreeController setSelectionIndexPath
:[node indexPath
]];
357 #pragma mark BSSourceView Delegate
360 * The gutter was clicked, which indicates that a breakpoint needs to be changed
362 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
364 BreakpointManager
* mngr
= [BreakpointManager sharedManager
];
366 if ([mngr hasBreakpointAt
:line inFile
:file
])
368 [mngr removeBreakpointAt
:line inFile
:file
];
372 Breakpoint
* bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
373 [mngr addBreakpoint
:bp
];
377 [sourceViewer setMarkers
:[NSSet setWithArray
:[mngr breakpointsForFile
:file
]]];
378 [sourceViewer setNeedsDisplay
:YES
];