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 "EvalController.h"
23 #import "NSXMLElementAdditions.h"
25 @interface DebuggerController (Private
)
26 - (void)updateSourceViewer
;
27 - (void)updateStackViewer
;
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 stackController
= [[StackController alloc
] init
];
44 pendingProperties_
= [[NSMutableDictionary alloc
] init
];
46 NSUserDefaults
* defaults
= [NSUserDefaults standardUserDefaults
];
48 connection
= [[DebuggerBackEnd alloc
] initWithPort
:[defaults integerForKey
:@
"Port"]];
49 connection.delegate
= self;
50 expandedVariables
= [[NSMutableSet alloc
] init
];
51 [[self window
] makeKeyAndOrderFront
:nil];
52 [[self window
] setDelegate
:self];
54 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:@
"InspectorWindowVisible"])
55 [inspector orderFront
:self];
66 [expandedVariables release
];
67 [stackController release
];
68 [pendingProperties_ release
];
73 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
77 [[self window
] setExcludedFromWindowsMenu
:YES
];
78 [[self window
] setTitle
:[NSString stringWithFormat
:@
"MacGDBp @ %d", [connection port
]]];
79 [sourceViewer setDelegate
:self];
80 [stackArrayController setSortDescriptors
:[NSArray arrayWithObject
:[[[NSSortDescriptor alloc
] initWithKey
:@
"index" ascending
:YES
] autorelease
]]];
81 self.connection.attached
= [attachedCheckbox_ state
] == NSOnState
;
85 * Validates the menu items for the "Debugger" menu
87 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
89 SEL action
= [anItem action
];
91 if (action
== @selector(stepOut
:)) {
92 return ([connection isConnected
] && [stackController.stack count
] > 1);
93 } else if (action
== @selector(stepIn
:) ||
94 action
== @selector(stepOver
:) ||
95 action
== @selector(run
:) ||
96 action
== @selector(stop
:)) {
97 return [connection isConnected
];
99 return [[self window
] validateUserInterfaceItem
:anItem
];
103 * Shows the inspector window
105 - (IBAction
)showInspectorWindow
:(id)sender
107 if (![inspector isVisible
])
108 [inspector makeKeyAndOrderFront
:sender
];
110 [inspector orderOut
:sender
];
114 * Runs the eval window sheet.
116 - (IBAction
)showEvalWindow
:(id)sender
118 // The |controller| will release itself on close.
119 EvalController
* controller
= [[EvalController alloc
] initWithBackEnd
:connection
];
120 [controller runModalForWindow
:[self window
]];
124 * Resets all the displays to be empty
126 - (void)resetDisplays
128 [variablesTreeController setContent
:nil];
129 [stackController.stack removeAllObjects
];
130 [stackArrayController rearrangeObjects
];
131 [[sourceViewer textView
] setString
:@
""];
132 sourceViewer.file
= nil;
136 * Sets the status to be "Error" and then displays the error message
138 - (void)setError
:(NSString
*)anError
140 [errormsg setStringValue
:anError
];
141 [errormsg setHidden
:NO
];
145 * Handles a GDBpConnection error
147 - (void)errorEncountered
:(NSString
*)error
149 [self setError
:error
];
153 * Delegate function for GDBpConnection for when the debugger connects.
155 - (void)debuggerConnected
157 [errormsg setHidden
:YES
];
158 if (!self.connection.attached
)
160 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:@
"BreakOnFirstLine"])
165 * Called once the debugger disconnects.
167 - (void)debuggerDisconnected
169 // Invalidate the marked line so we don't look like we're still running.
170 sourceViewer.markedLine
= -1;
171 [sourceViewer setNeedsDisplay
:YES
];
175 * Forwards the message to run script execution to the connection
177 - (IBAction
)run
:(id)sender
182 - (IBAction
)attachedToggled
:(id)sender
184 connection.attached
= [sender state
] == NSOnState
;
188 * Forwards the message to "step in" to the connection
190 - (IBAction
)stepIn
:(id)sender
192 if ([[variablesTreeController selectedObjects
] count
] > 0)
193 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
199 * Forwards the message to "step out" to the connection
201 - (IBAction
)stepOut
:(id)sender
203 if ([[variablesTreeController selectedObjects
] count
] > 0)
204 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
206 [connection stepOut
];
210 * Forwards the message to "step over" to the connection
212 - (IBAction
)stepOver
:(id)sender
214 if ([[variablesTreeController selectedObjects
] count
] > 0)
215 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
217 [connection stepOver
];
221 * Forwards the detach/"stop" message to the back end.
223 - (IBAction
)stop
:(id)sender
228 - (void)fetchChildProperties
:(VariableNode
*)node
230 NSArray
* selection
= [stackArrayController selectedObjects
];
231 if (![selection count
])
233 assert([selection count
] == 1);
234 NSInteger depth
= [[selection objectAtIndex
:0] index
];
235 NSInteger txn
= [connection getChildrenOfProperty
:node atDepth
:depth
];
236 [pendingProperties_ setObject
:node forKey
:[NSNumber numberWithInt
:txn
]];
240 * NSTableView delegate method that informs the controller that the stack selection did change and that
241 * we should update the source viewer
243 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
245 [self updateSourceViewer
];
246 // TODO: This is very, very hacky because it's nondeterministic. The issue
247 // is that calling |-[NSOutlineView expandItem:]| while the table is still
248 // doing its redraw will translate to a no-op. Instead, we need to restructure
249 // this controller so that when everything has been laid out we call
250 // |-expandVariables|; but NSOutlineView doesn't have a |-didFinishDoingCrap:|
251 // method. The other issue is that we need to call this method from
252 // selectionDidChange but ONLY when it was the result of a user-initiated
253 // action and not the stack viewer updating causing a selection change.
254 // If it happens in the latter, then we run into the same issue that causes
256 [self performSelector
:@selector(expandVariables
) withObject
:nil afterDelay
:0.05];
260 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
262 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
264 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
265 [expandedVariables addObject
:[[node representedObject
] fullName
]];
269 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
271 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
273 [expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullName
]];
279 * Does the actual updating of the source viewer by reading in the file
281 - (void)updateSourceViewer
283 NSArray
* selection
= [stackArrayController selectedObjects
];
284 if (!selection ||
[selection count
] < 1)
286 if ([selection count
] > 1)
287 NSLog(@
"INVALID SELECTION");
288 StackFrame
* frame
= [selection objectAtIndex
:0];
291 [connection loadStackFrame
:frame
];
296 NSString
* filename
= [[NSURL URLWithString
:frame.filename
] path
];
297 if ([filename isEqualToString
:@
""])
300 // Replace the source if necessary.
301 if (frame.source
&& ![sourceViewer.file isEqualToString
:filename
])
303 [sourceViewer setString
:frame.source asFile
:filename
];
305 NSSet
* breakpoints
= [NSSet setWithArray
:[[BreakpointManager sharedManager
] breakpointsForFile
:filename
]];
306 [sourceViewer setMarkers
:breakpoints
];
309 [sourceViewer setMarkedLine
:frame.lineNumber
];
310 [sourceViewer scrollToLine
:frame.lineNumber
];
312 [[sourceViewer textView
] display
];
316 * Does some house keeping to the stack viewer
318 - (void)updateStackViewer
320 [stackArrayController rearrangeObjects
];
321 [stackArrayController setSelectionIndex
:0];
325 * Expands the variables based on the stored set
327 - (void)expandVariables
329 NSString
* selection
= [selectedVariable fullName
];
331 for (NSInteger i
= 0; i
< [variablesOutlineView numberOfRows
]; i
++) {
332 NSTreeNode
* node
= [variablesOutlineView itemAtRow
:i
];
333 NSString
* fullName
= [[node representedObject
] fullName
];
335 // see if it needs expanding
336 if ([expandedVariables containsObject
:fullName
])
337 [variablesOutlineView expandItem
:node
];
339 // select it if we had it selected before
340 if ([fullName isEqualToString
:selection
])
341 [variablesTreeController setSelectionIndexPath
:[node indexPath
]];
345 #pragma mark BSSourceView Delegate
348 * The gutter was clicked, which indicates that a breakpoint needs to be changed
350 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
352 BreakpointManager
* mngr
= [BreakpointManager sharedManager
];
354 if ([mngr hasBreakpointAt
:line inFile
:file
])
356 [mngr removeBreakpointAt
:line inFile
:file
];
360 Breakpoint
* bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
361 [mngr addBreakpoint
:bp
];
365 [sourceViewer setMarkers
:[NSSet setWithArray
:[mngr breakpointsForFile
:file
]]];
366 [sourceViewer setNeedsDisplay
:YES
];
369 #pragma mark GDBpConnectionDelegate
373 aboutToClobber_
= YES
;
374 [pendingProperties_ removeAllObjects
];
377 - (void)newStackFrame
:(StackFrame
*)frame
381 [stackController.stack removeAllObjects
];
382 aboutToClobber_
= NO
;
384 [stackController push
:frame
];
385 [self updateStackViewer
];
386 [self updateSourceViewer
];
389 - (void)sourceUpdated
:(StackFrame
*)frame
391 [self updateSourceViewer
];
394 - (void)receivedProperties
:(NSArray
*)properties forTransaction
:(NSInteger
)transaction
396 NSNumber
* key
= [NSNumber numberWithInt
:transaction
];
397 VariableNode
* node
= [pendingProperties_ objectForKey
:key
];
399 [node setChildrenFromXMLChildren
:properties
];
400 [variablesTreeController rearrangeObjects
];
401 [pendingProperties_ removeObjectForKey
:key
];
405 - (void)scriptWasEvaluatedWithResult
:(NSString
*)result
407 [EvalController scriptWasEvaluatedWithResult
:result
];