3 * Copyright (c) 2007 - 2010, 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
= [[DebuggerProcessor 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
:@
"GDBp @ %@:%d", [connection remoteHost
], [connection port
]]];
76 [sourceViewer setDelegate
:self];
77 [stackArrayController setSortDescriptors
:[NSArray arrayWithObject
:[[[NSSortDescriptor alloc
] initWithKey
:@
"index" ascending
:YES
] autorelease
]]];
81 * Validates the menu items for the "Debugger" menu
83 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
85 SEL action
= [anItem action
];
87 if (action
== @selector(stepOut
:))
88 return ([connection isConnected
] && [stackController.stack count
] > 1);
89 else if (action
== @selector(stepIn
:) || action
== @selector(stepOver
:) || action
== @selector(run
:))
90 return [connection isConnected
];
92 return [[self window
] validateUserInterfaceItem
:anItem
];
96 * Shows the inspector window
98 - (IBAction
)showInspectorWindow
:(id)sender
100 if (![inspector isVisible
])
101 [inspector makeKeyAndOrderFront
:sender
];
103 [inspector orderOut
:sender
];
107 * Resets all the displays to be empty
109 - (void)resetDisplays
111 [variablesTreeController setContent
:nil];
112 [stackController.stack removeAllObjects
];
113 [stackArrayController rearrangeObjects
];
114 [[sourceViewer textView
] setString
:@
""];
115 sourceViewer.file
= nil;
119 * Sets the status to be "Error" and then displays the error message
121 - (void)setError
:(NSString
*)anError
123 [errormsg setStringValue
:anError
];
124 [errormsg setHidden
:NO
];
128 * Handles a GDBpConnection error
130 - (void)errorEncountered
:(NSString
*)error
132 [self setError
:error
];
136 * Delegate function for GDBpConnection for when the debugger connects.
138 - (void)debuggerConnected
140 [errormsg setHidden
:YES
];
141 [self startDebugger
];
145 * Called once the socket accepts and MacGDBp is connected to the debugger
147 - (void)startDebugger
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
];
209 - (void)fetchChildProperties
:(VariableNode
*)node
211 NSArray
* selection
= [stackArrayController selectedObjects
];
212 assert([selection count
] == 1);
213 NSInteger depth
= [[selection objectAtIndex
:0] index
];
214 NSInteger txn
= [connection getChildrenOfProperty
:node atDepth
:depth
];
215 [pendingProperties_ setObject
:node forKey
:[NSNumber numberWithInt
:txn
]];
219 * NSTableView delegate method that informs the controller that the stack selection did change and that
220 * we should update the source viewer
222 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
224 [self updateSourceViewer
];
225 [self expandVariables
];
229 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
231 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
233 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
234 [expandedVariables addObject
:[[node representedObject
] fullName
]];
238 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
240 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
242 [expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullName
]];
248 * Does the actual updating of the source viewer by reading in the file
250 - (void)updateSourceViewer
252 NSArray
* selection
= [stackArrayController selectedObjects
];
253 if (!selection ||
[selection count
] < 1)
255 if ([selection count
] > 1)
256 NSLog(@
"INVALID SELECTION");
257 StackFrame
* frame
= [selection objectAtIndex
:0];
260 [connection loadStackFrame
:frame
];
265 NSString
* filename
= [[NSURL URLWithString
:frame.filename
] path
];
266 if ([filename isEqualToString
:@
""])
269 // Replace the source if necessary.
270 if (frame.source
&& ![sourceViewer.file isEqualToString
:filename
])
272 [sourceViewer setString
:frame.source asFile
:filename
];
274 NSSet
* breakpoints
= [NSSet setWithArray
:[[BreakpointManager sharedManager
] breakpointsForFile
:filename
]];
275 [[sourceViewer numberView
] setMarkers
:breakpoints
];
278 [sourceViewer setMarkedLine
:frame.lineNumber
];
279 [sourceViewer scrollToLine
:frame.lineNumber
];
281 [[sourceViewer textView
] display
];
285 * Does some house keeping to the stack viewer
287 - (void)updateStackViewer
289 [stackArrayController rearrangeObjects
];
290 [stackArrayController setSelectionIndex
:0];
291 [self expandVariables
];
295 * Expands the variables based on the stored set
297 - (void)expandVariables
299 NSString
* selection
= [selectedVariable fullName
];
301 for (int i
= 0; i
< [variablesOutlineView numberOfRows
]; i
++)
303 NSTreeNode
* node
= [variablesOutlineView itemAtRow
:i
];
304 NSString
* fullName
= [[node representedObject
] fullName
];
306 // see if it needs expanding
307 if ([expandedVariables containsObject
:fullName
])
308 [variablesOutlineView expandItem
:node
];
310 // select it if we had it selected before
311 if ([fullName isEqualToString
:selection
])
312 [variablesTreeController setSelectionIndexPath
:[node indexPath
]];
316 #pragma mark BSSourceView Delegate
319 * The gutter was clicked, which indicates that a breakpoint needs to be changed
321 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
323 BreakpointManager
* mngr
= [BreakpointManager sharedManager
];
325 if ([mngr hasBreakpointAt
:line inFile
:file
])
327 [mngr removeBreakpointAt
:line inFile
:file
];
331 Breakpoint
* bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
332 [mngr addBreakpoint
:bp
];
336 [[sourceViewer numberView
] setMarkers
:[NSSet setWithArray
:[mngr breakpointsForFile
:file
]]];
337 [[sourceViewer numberView
] setNeedsDisplay
:YES
];
340 #pragma mark GDBpConnectionDelegate
344 aboutToClobber_
= YES
;
345 [pendingProperties_ removeAllObjects
];
348 - (void)newStackFrame
:(StackFrame
*)frame
352 [stackController.stack removeAllObjects
];
353 aboutToClobber_
= NO
;
355 [stackController push
:frame
];
356 [self updateStackViewer
];
357 [self updateSourceViewer
];
360 - (void)sourceUpdated
:(StackFrame
*)frame
362 [self updateSourceViewer
];
365 - (void)receivedProperties
:(NSArray
*)properties forTransaction
:(NSInteger
)transaction
367 NSNumber
* key
= [NSNumber numberWithInt
:transaction
];
368 VariableNode
* node
= [pendingProperties_ objectForKey
:key
];
370 [node setChildrenFromXMLChildren
:properties
];
371 [variablesTreeController rearrangeObjects
];
372 [pendingProperties_ removeObjectForKey
:key
];