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"
18 #import "NSXMLElementAdditions.h"
19 #import "AppDelegate.h"
20 #import "BreakpointManager.h"
22 @interface DebuggerController (Private
)
23 - (void)updateSourceViewer
;
24 - (void)updateStackViewer
;
25 - (void)expandVariables
;
28 @implementation DebuggerController
30 @synthesize connection
, sourceViewer
, inspector
;
33 * Initializes the window controller and sets the connection using preference
38 if (self = [super initWithWindowNibName
:@
"Debugger"])
40 stackController
= [[StackController alloc
] init
];
41 pendingProperties_
= [[NSMutableDictionary alloc
] init
];
43 NSUserDefaults
* defaults
= [NSUserDefaults standardUserDefaults
];
45 connection
= [[DebuggerBackEnd alloc
] initWithPort
:[defaults integerForKey
:@
"Port"]];
46 connection.delegate
= self;
47 expandedVariables
= [[NSMutableSet alloc
] init
];
48 [[self window
] makeKeyAndOrderFront
:nil];
49 [[self window
] setDelegate
:self];
51 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:@
"InspectorWindowVisible"])
52 [inspector orderFront
:self];
63 [expandedVariables release
];
64 [stackController release
];
65 [pendingProperties_ release
];
70 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
74 [[self window
] setExcludedFromWindowsMenu
:YES
];
75 [[self window
] setTitle
:[NSString stringWithFormat
:@
"MacGDBp @ %d", [connection port
]]];
76 [sourceViewer setDelegate
:self];
77 [stackArrayController setSortDescriptors
:[NSArray arrayWithObject
:[[[NSSortDescriptor alloc
] initWithKey
:@
"index" ascending
:YES
] autorelease
]]];
78 self.connection.attached
= [attachedCheckbox_ state
] == NSOnState
;
82 * Validates the menu items for the "Debugger" menu
84 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
86 SEL action
= [anItem action
];
88 if (action
== @selector(stepOut
:)) {
89 return ([connection isConnected
] && [stackController.stack count
] > 1);
90 } else if (action
== @selector(stepIn
:) ||
91 action
== @selector(stepOver
:) ||
92 action
== @selector(run
:) ||
93 action
== @selector(stop
:)) {
94 return [connection isConnected
];
96 return [[self window
] validateUserInterfaceItem
:anItem
];
100 * Shows the inspector window
102 - (IBAction
)showInspectorWindow
:(id)sender
104 if (![inspector isVisible
])
105 [inspector makeKeyAndOrderFront
:sender
];
107 [inspector orderOut
:sender
];
111 * Resets all the displays to be empty
113 - (void)resetDisplays
115 [variablesTreeController setContent
:nil];
116 [stackController.stack removeAllObjects
];
117 [stackArrayController rearrangeObjects
];
118 [[sourceViewer textView
] setString
:@
""];
119 sourceViewer.file
= nil;
123 * Sets the status to be "Error" and then displays the error message
125 - (void)setError
:(NSString
*)anError
127 [errormsg setStringValue
:anError
];
128 [errormsg setHidden
:NO
];
132 * Handles a GDBpConnection error
134 - (void)errorEncountered
:(NSString
*)error
136 [self setError
:error
];
140 * Delegate function for GDBpConnection for when the debugger connects.
142 - (void)debuggerConnected
144 [errormsg setHidden
:YES
];
145 if (!self.connection.attached
)
147 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:@
"BreakOnFirstLine"])
152 * Called once the debugger disconnects.
154 - (void)debuggerDisconnected
156 // Invalidate the marked line so we don't look like we're still running.
157 sourceViewer.markedLine
= -1;
158 [sourceViewer setNeedsDisplay
:YES
];
162 * Forwards the message to run script execution to the connection
164 - (IBAction
)run
:(id)sender
169 - (IBAction
)attachedToggled
:(id)sender
171 connection.attached
= [sender state
] == NSOnState
;
175 * Forwards the message to "step in" to the connection
177 - (IBAction
)stepIn
:(id)sender
179 if ([[variablesTreeController selectedObjects
] count
] > 0)
180 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
186 * Forwards the message to "step out" to the connection
188 - (IBAction
)stepOut
:(id)sender
190 if ([[variablesTreeController selectedObjects
] count
] > 0)
191 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
193 [connection stepOut
];
197 * Forwards the message to "step over" to the connection
199 - (IBAction
)stepOver
:(id)sender
201 if ([[variablesTreeController selectedObjects
] count
] > 0)
202 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
204 [connection stepOver
];
208 * Forwards the detach/"stop" message to the back end.
210 - (IBAction
)stop
:(id)sender
215 - (void)fetchChildProperties
:(VariableNode
*)node
217 NSArray
* selection
= [stackArrayController selectedObjects
];
218 if (![selection count
])
220 assert([selection count
] == 1);
221 NSInteger depth
= [[selection objectAtIndex
:0] index
];
222 NSInteger txn
= [connection getChildrenOfProperty
:node atDepth
:depth
];
223 [pendingProperties_ setObject
:node forKey
:[NSNumber numberWithInt
:txn
]];
227 * NSTableView delegate method that informs the controller that the stack selection did change and that
228 * we should update the source viewer
230 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
232 [self updateSourceViewer
];
233 // TODO: This is very, very hacky because it's nondeterministic. The issue
234 // is that calling |-[NSOutlineView expandItem:]| while the table is still
235 // doing its redraw will translate to a no-op. Instead, we need to restructure
236 // this controller so that when everything has been laid out we call
237 // |-expandVariables|; but NSOutlineView doesn't have a |-didFinishDoingCrap:|
238 // method. The other issue is that we need to call this method from
239 // selectionDidChange but ONLY when it was the result of a user-initiated
240 // action and not the stack viewer updating causing a selection change.
241 // If it happens in the latter, then we run into the same issue that causes
243 [self performSelector
:@selector(expandVariables
) withObject
:nil afterDelay
:0.05];
247 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
249 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
251 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
252 [expandedVariables addObject
:[[node representedObject
] fullName
]];
256 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
258 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
260 [expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullName
]];
266 * Does the actual updating of the source viewer by reading in the file
268 - (void)updateSourceViewer
270 NSArray
* selection
= [stackArrayController selectedObjects
];
271 if (!selection ||
[selection count
] < 1)
273 if ([selection count
] > 1)
274 NSLog(@
"INVALID SELECTION");
275 StackFrame
* frame
= [selection objectAtIndex
:0];
278 [connection loadStackFrame
:frame
];
283 NSString
* filename
= [[NSURL URLWithString
:frame.filename
] path
];
284 if ([filename isEqualToString
:@
""])
287 // Replace the source if necessary.
288 if (frame.source
&& ![sourceViewer.file isEqualToString
:filename
])
290 [sourceViewer setString
:frame.source asFile
:filename
];
292 NSSet
* breakpoints
= [NSSet setWithArray
:[[BreakpointManager sharedManager
] breakpointsForFile
:filename
]];
293 [[sourceViewer numberView
] setMarkers
:breakpoints
];
296 [sourceViewer setMarkedLine
:frame.lineNumber
];
297 [sourceViewer scrollToLine
:frame.lineNumber
];
299 [[sourceViewer textView
] display
];
303 * Does some house keeping to the stack viewer
305 - (void)updateStackViewer
307 [stackArrayController rearrangeObjects
];
308 [stackArrayController setSelectionIndex
:0];
312 * Expands the variables based on the stored set
314 - (void)expandVariables
316 NSString
* selection
= [selectedVariable fullName
];
318 for (int i
= 0; i
< [variablesOutlineView numberOfRows
]; i
++) {
319 NSTreeNode
* node
= [variablesOutlineView itemAtRow
:i
];
320 NSString
* fullName
= [[node representedObject
] fullName
];
322 // see if it needs expanding
323 if ([expandedVariables containsObject
:fullName
])
324 [variablesOutlineView expandItem
:node
];
326 // select it if we had it selected before
327 if ([fullName isEqualToString
:selection
])
328 [variablesTreeController setSelectionIndexPath
:[node indexPath
]];
332 #pragma mark BSSourceView Delegate
335 * The gutter was clicked, which indicates that a breakpoint needs to be changed
337 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
339 BreakpointManager
* mngr
= [BreakpointManager sharedManager
];
341 if ([mngr hasBreakpointAt
:line inFile
:file
])
343 [mngr removeBreakpointAt
:line inFile
:file
];
347 Breakpoint
* bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
348 [mngr addBreakpoint
:bp
];
352 [[sourceViewer numberView
] setMarkers
:[NSSet setWithArray
:[mngr breakpointsForFile
:file
]]];
353 [[sourceViewer numberView
] setNeedsDisplay
:YES
];
356 #pragma mark GDBpConnectionDelegate
360 aboutToClobber_
= YES
;
361 [pendingProperties_ removeAllObjects
];
364 - (void)newStackFrame
:(StackFrame
*)frame
368 [stackController.stack removeAllObjects
];
369 aboutToClobber_
= NO
;
371 [stackController push
:frame
];
372 [self updateStackViewer
];
373 [self updateSourceViewer
];
376 - (void)sourceUpdated
:(StackFrame
*)frame
378 [self updateSourceViewer
];
381 - (void)receivedProperties
:(NSArray
*)properties forTransaction
:(NSInteger
)transaction
383 NSNumber
* key
= [NSNumber numberWithInt
:transaction
];
384 VariableNode
* node
= [pendingProperties_ objectForKey
:key
];
386 [node setChildrenFromXMLChildren
:properties
];
387 [variablesTreeController rearrangeObjects
];
388 [pendingProperties_ removeObjectForKey
:key
];