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 "NSXMLElementAdditions.h"
24 @interface DebuggerController (Private
)
25 - (void)updateSourceViewer
;
26 - (void)updateStackViewer
;
27 - (void)expandVariables
;
30 @implementation DebuggerController
32 @synthesize connection
, sourceViewer
, inspector
;
35 * Initializes the window controller and sets the connection using preference
40 if (self = [super initWithWindowNibName
:@
"Debugger"])
42 stackController
= [[StackController alloc
] init
];
43 pendingProperties_
= [[NSMutableDictionary alloc
] init
];
45 NSUserDefaults
* defaults
= [NSUserDefaults standardUserDefaults
];
47 connection
= [[DebuggerBackEnd alloc
] initWithPort
:[defaults integerForKey
:@
"Port"]];
48 connection.delegate
= self;
49 expandedVariables
= [[NSMutableSet alloc
] init
];
50 [[self window
] makeKeyAndOrderFront
:nil];
51 [[self window
] setDelegate
:self];
53 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:@
"InspectorWindowVisible"])
54 [inspector orderFront
:self];
65 [expandedVariables release
];
66 [stackController release
];
67 [pendingProperties_ release
];
72 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
76 [[self window
] setExcludedFromWindowsMenu
:YES
];
77 [[self window
] setTitle
:[NSString stringWithFormat
:@
"MacGDBp @ %d", [connection port
]]];
78 [sourceViewer setDelegate
:self];
79 [stackArrayController setSortDescriptors
:[NSArray arrayWithObject
:[[[NSSortDescriptor alloc
] initWithKey
:@
"index" ascending
:YES
] autorelease
]]];
80 self.connection.attached
= [attachedCheckbox_ state
] == NSOnState
;
84 * Validates the menu items for the "Debugger" menu
86 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
88 SEL action
= [anItem action
];
90 if (action
== @selector(stepOut
:)) {
91 return ([connection isConnected
] && [stackController.stack count
] > 1);
92 } else if (action
== @selector(stepIn
:) ||
93 action
== @selector(stepOver
:) ||
94 action
== @selector(run
:) ||
95 action
== @selector(stop
:)) {
96 return [connection isConnected
];
98 return [[self window
] validateUserInterfaceItem
:anItem
];
102 * Shows the inspector window
104 - (IBAction
)showInspectorWindow
:(id)sender
106 if (![inspector isVisible
])
107 [inspector makeKeyAndOrderFront
:sender
];
109 [inspector orderOut
:sender
];
113 * Resets all the displays to be empty
115 - (void)resetDisplays
117 [variablesTreeController setContent
:nil];
118 [stackController.stack removeAllObjects
];
119 [stackArrayController rearrangeObjects
];
120 [[sourceViewer textView
] setString
:@
""];
121 sourceViewer.file
= nil;
125 * Sets the status to be "Error" and then displays the error message
127 - (void)setError
:(NSString
*)anError
129 [errormsg setStringValue
:anError
];
130 [errormsg setHidden
:NO
];
134 * Handles a GDBpConnection error
136 - (void)errorEncountered
:(NSString
*)error
138 [self setError
:error
];
142 * Delegate function for GDBpConnection for when the debugger connects.
144 - (void)debuggerConnected
146 [errormsg setHidden
:YES
];
147 if (!self.connection.attached
)
149 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:@
"BreakOnFirstLine"])
154 * Called once the debugger disconnects.
156 - (void)debuggerDisconnected
158 // Invalidate the marked line so we don't look like we're still running.
159 sourceViewer.markedLine
= -1;
160 [sourceViewer setNeedsDisplay
:YES
];
164 * Forwards the message to run script execution to the connection
166 - (IBAction
)run
:(id)sender
171 - (IBAction
)attachedToggled
:(id)sender
173 connection.attached
= [sender state
] == NSOnState
;
177 * Forwards the message to "step in" to the connection
179 - (IBAction
)stepIn
:(id)sender
181 if ([[variablesTreeController selectedObjects
] count
] > 0)
182 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
188 * Forwards the message to "step out" to the connection
190 - (IBAction
)stepOut
:(id)sender
192 if ([[variablesTreeController selectedObjects
] count
] > 0)
193 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
195 [connection stepOut
];
199 * Forwards the message to "step over" to the connection
201 - (IBAction
)stepOver
:(id)sender
203 if ([[variablesTreeController selectedObjects
] count
] > 0)
204 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
206 [connection stepOver
];
210 * Forwards the detach/"stop" message to the back end.
212 - (IBAction
)stop
:(id)sender
217 - (void)fetchChildProperties
:(VariableNode
*)node
219 NSArray
* selection
= [stackArrayController selectedObjects
];
220 if (![selection count
])
222 assert([selection count
] == 1);
223 NSInteger depth
= [[selection objectAtIndex
:0] index
];
224 NSInteger txn
= [connection getChildrenOfProperty
:node atDepth
:depth
];
225 [pendingProperties_ setObject
:node forKey
:[NSNumber numberWithInt
:txn
]];
229 * NSTableView delegate method that informs the controller that the stack selection did change and that
230 * we should update the source viewer
232 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
234 [self updateSourceViewer
];
235 // TODO: This is very, very hacky because it's nondeterministic. The issue
236 // is that calling |-[NSOutlineView expandItem:]| while the table is still
237 // doing its redraw will translate to a no-op. Instead, we need to restructure
238 // this controller so that when everything has been laid out we call
239 // |-expandVariables|; but NSOutlineView doesn't have a |-didFinishDoingCrap:|
240 // method. The other issue is that we need to call this method from
241 // selectionDidChange but ONLY when it was the result of a user-initiated
242 // action and not the stack viewer updating causing a selection change.
243 // If it happens in the latter, then we run into the same issue that causes
245 [self performSelector
:@selector(expandVariables
) withObject
:nil afterDelay
:0.05];
249 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
251 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
253 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
254 [expandedVariables addObject
:[[node representedObject
] fullName
]];
258 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
260 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
262 [expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullName
]];
268 * Does the actual updating of the source viewer by reading in the file
270 - (void)updateSourceViewer
272 NSArray
* selection
= [stackArrayController selectedObjects
];
273 if (!selection ||
[selection count
] < 1)
275 if ([selection count
] > 1)
276 NSLog(@
"INVALID SELECTION");
277 StackFrame
* frame
= [selection objectAtIndex
:0];
280 [connection loadStackFrame
:frame
];
285 NSString
* filename
= [[NSURL URLWithString
:frame.filename
] path
];
286 if ([filename isEqualToString
:@
""])
289 // Replace the source if necessary.
290 if (frame.source
&& ![sourceViewer.file isEqualToString
:filename
])
292 [sourceViewer setString
:frame.source asFile
:filename
];
294 NSSet
* breakpoints
= [NSSet setWithArray
:[[BreakpointManager sharedManager
] breakpointsForFile
:filename
]];
295 [sourceViewer setMarkers
:breakpoints
];
298 [sourceViewer setMarkedLine
:frame.lineNumber
];
299 [sourceViewer scrollToLine
:frame.lineNumber
];
301 [[sourceViewer textView
] display
];
305 * Does some house keeping to the stack viewer
307 - (void)updateStackViewer
309 [stackArrayController rearrangeObjects
];
310 [stackArrayController setSelectionIndex
:0];
314 * Expands the variables based on the stored set
316 - (void)expandVariables
318 NSString
* selection
= [selectedVariable fullName
];
320 for (int i
= 0; i
< [variablesOutlineView numberOfRows
]; i
++) {
321 NSTreeNode
* node
= [variablesOutlineView itemAtRow
:i
];
322 NSString
* fullName
= [[node representedObject
] fullName
];
324 // see if it needs expanding
325 if ([expandedVariables containsObject
:fullName
])
326 [variablesOutlineView expandItem
:node
];
328 // select it if we had it selected before
329 if ([fullName isEqualToString
:selection
])
330 [variablesTreeController setSelectionIndexPath
:[node indexPath
]];
334 #pragma mark BSSourceView Delegate
337 * The gutter was clicked, which indicates that a breakpoint needs to be changed
339 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
341 BreakpointManager
* mngr
= [BreakpointManager sharedManager
];
343 if ([mngr hasBreakpointAt
:line inFile
:file
])
345 [mngr removeBreakpointAt
:line inFile
:file
];
349 Breakpoint
* bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
350 [mngr addBreakpoint
:bp
];
354 [sourceViewer setMarkers
:[NSSet setWithArray
:[mngr breakpointsForFile
:file
]]];
355 [sourceViewer setNeedsDisplay
:YES
];
358 #pragma mark GDBpConnectionDelegate
362 aboutToClobber_
= YES
;
363 [pendingProperties_ removeAllObjects
];
366 - (void)newStackFrame
:(StackFrame
*)frame
370 [stackController.stack removeAllObjects
];
371 aboutToClobber_
= NO
;
373 [stackController push
:frame
];
374 [self updateStackViewer
];
375 [self updateSourceViewer
];
378 - (void)sourceUpdated
:(StackFrame
*)frame
380 [self updateSourceViewer
];
383 - (void)receivedProperties
:(NSArray
*)properties forTransaction
:(NSInteger
)transaction
385 NSNumber
* key
= [NSNumber numberWithInt
:transaction
];
386 VariableNode
* node
= [pendingProperties_ objectForKey
:key
];
388 [node setChildrenFromXMLChildren
:properties
];
389 [variablesTreeController rearrangeObjects
];
390 [pendingProperties_ removeObjectForKey
:key
];