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 action
== @selector(showEvalWindow
:)) {
98 return [connection isConnected
];
100 return [[self window
] validateUserInterfaceItem
:anItem
];
104 * Shows the inspector window
106 - (IBAction
)showInspectorWindow
:(id)sender
108 if (![inspector isVisible
])
109 [inspector makeKeyAndOrderFront
:sender
];
111 [inspector orderOut
:sender
];
115 * Runs the eval window sheet.
117 - (IBAction
)showEvalWindow
:(id)sender
119 // The |controller| will release itself on close.
120 EvalController
* controller
= [[EvalController alloc
] initWithBackEnd
:connection
];
121 [controller runModalForWindow
:[self window
]];
125 * Resets all the displays to be empty
127 - (void)resetDisplays
129 [variablesTreeController setContent
:nil];
130 [stackController.stack removeAllObjects
];
131 [stackArrayController rearrangeObjects
];
132 [[sourceViewer textView
] setString
:@
""];
133 sourceViewer.file
= nil;
137 * Sets the status to be "Error" and then displays the error message
139 - (void)setError
:(NSString
*)anError
141 [errormsg setStringValue
:anError
];
142 [errormsg setHidden
:NO
];
146 * Handles a GDBpConnection error
148 - (void)errorEncountered
:(NSString
*)error
150 [self setError
:error
];
154 * Delegate function for GDBpConnection for when the debugger connects.
156 - (void)debuggerConnected
158 [errormsg setHidden
:YES
];
159 if (!self.connection.attached
)
161 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:@
"BreakOnFirstLine"])
166 * Called once the debugger disconnects.
168 - (void)debuggerDisconnected
170 // Invalidate the marked line so we don't look like we're still running.
171 sourceViewer.markedLine
= -1;
172 [sourceViewer setNeedsDisplay
:YES
];
176 * Forwards the message to run script execution to the connection
178 - (IBAction
)run
:(id)sender
183 - (IBAction
)attachedToggled
:(id)sender
185 connection.attached
= [sender state
] == NSOnState
;
189 * Forwards the message to "step in" to the connection
191 - (IBAction
)stepIn
:(id)sender
193 if ([[variablesTreeController selectedObjects
] count
] > 0)
194 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
200 * Forwards the message to "step out" to the connection
202 - (IBAction
)stepOut
:(id)sender
204 if ([[variablesTreeController selectedObjects
] count
] > 0)
205 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
207 [connection stepOut
];
211 * Forwards the message to "step over" to the connection
213 - (IBAction
)stepOver
:(id)sender
215 if ([[variablesTreeController selectedObjects
] count
] > 0)
216 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
218 [connection stepOver
];
222 * Forwards the detach/"stop" message to the back end.
224 - (IBAction
)stop
:(id)sender
229 - (void)fetchChildProperties
:(VariableNode
*)node
231 NSArray
* selection
= [stackArrayController selectedObjects
];
232 if (![selection count
])
234 assert([selection count
] == 1);
235 NSInteger depth
= [[selection objectAtIndex
:0] index
];
236 NSInteger txn
= [connection getChildrenOfProperty
:node atDepth
:depth
];
237 [pendingProperties_ setObject
:node forKey
:[NSNumber numberWithInt
:txn
]];
241 * NSTableView delegate method that informs the controller that the stack selection did change and that
242 * we should update the source viewer
244 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
246 [self updateSourceViewer
];
247 // TODO: This is very, very hacky because it's nondeterministic. The issue
248 // is that calling |-[NSOutlineView expandItem:]| while the table is still
249 // doing its redraw will translate to a no-op. Instead, we need to restructure
250 // this controller so that when everything has been laid out we call
251 // |-expandVariables|; but NSOutlineView doesn't have a |-didFinishDoingCrap:|
252 // method. The other issue is that we need to call this method from
253 // selectionDidChange but ONLY when it was the result of a user-initiated
254 // action and not the stack viewer updating causing a selection change.
255 // If it happens in the latter, then we run into the same issue that causes
257 [self performSelector
:@selector(expandVariables
) withObject
:nil afterDelay
:0.05];
261 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
263 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
265 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
266 [expandedVariables addObject
:[[node representedObject
] fullName
]];
270 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
272 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
274 [expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullName
]];
280 * Does the actual updating of the source viewer by reading in the file
282 - (void)updateSourceViewer
284 NSArray
* selection
= [stackArrayController selectedObjects
];
285 if (!selection ||
[selection count
] < 1)
287 if ([selection count
] > 1)
288 NSLog(@
"INVALID SELECTION");
289 StackFrame
* frame
= [selection objectAtIndex
:0];
292 [connection loadStackFrame
:frame
];
297 NSString
* filename
= [[NSURL URLWithString
:frame.filename
] path
];
298 if ([filename isEqualToString
:@
""])
301 // Replace the source if necessary.
302 if (frame.source
&& ![sourceViewer.file isEqualToString
:filename
])
304 [sourceViewer setString
:frame.source asFile
:filename
];
306 NSSet
* breakpoints
= [NSSet setWithArray
:[[BreakpointManager sharedManager
] breakpointsForFile
:filename
]];
307 [sourceViewer setMarkers
:breakpoints
];
310 [sourceViewer setMarkedLine
:frame.lineNumber
];
311 [sourceViewer scrollToLine
:frame.lineNumber
];
313 [[sourceViewer textView
] display
];
317 * Does some house keeping to the stack viewer
319 - (void)updateStackViewer
321 [stackArrayController rearrangeObjects
];
322 [stackArrayController setSelectionIndex
:0];
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
374 aboutToClobber_
= YES
;
375 [pendingProperties_ removeAllObjects
];
378 - (void)newStackFrame
:(StackFrame
*)frame
382 [stackController.stack removeAllObjects
];
383 aboutToClobber_
= NO
;
385 [stackController push
:frame
];
386 [self updateStackViewer
];
387 [self updateSourceViewer
];
390 - (void)sourceUpdated
:(StackFrame
*)frame
392 [self updateSourceViewer
];
395 - (void)receivedProperties
:(NSArray
*)properties forTransaction
:(NSInteger
)transaction
397 NSNumber
* key
= [NSNumber numberWithInt
:transaction
];
398 VariableNode
* node
= [pendingProperties_ objectForKey
:key
];
400 [node setChildrenFromXMLChildren
:properties
];
401 [variablesTreeController rearrangeObjects
];
402 [pendingProperties_ removeObjectForKey
:key
];
406 - (void)scriptWasEvaluatedWithResult
:(NSString
*)result
408 [EvalController scriptWasEvaluatedWithResult
:result
];