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
];
42 NSUserDefaults
* defaults
= [NSUserDefaults standardUserDefaults
];
44 connection
= [[DebuggerProcessor alloc
] initWithPort
:[defaults integerForKey
:@
"Port"]];
45 connection.delegate
= self;
46 expandedVariables
= [[NSMutableSet alloc
] init
];
47 [[self window
] makeKeyAndOrderFront
:nil];
48 [[self window
] setDelegate
:self];
50 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:@
"InspectorWindowVisible"])
51 [inspector orderFront
:self];
62 [expandedVariables release
];
63 [stackController release
];
68 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
72 [[self window
] setExcludedFromWindowsMenu
:YES
];
73 [[self window
] setTitle
:[NSString stringWithFormat
:@
"GDBp @ %@:%d", [connection remoteHost
], [connection port
]]];
74 [sourceViewer setDelegate
:self];
75 [stackArrayController setSortDescriptors
:[NSArray arrayWithObject
:[[[NSSortDescriptor alloc
] initWithKey
:@
"index" ascending
:YES
] autorelease
]]];
79 * Validates the menu items for the "Debugger" menu
81 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
83 SEL action
= [anItem action
];
85 if (action
== @selector(stepOut
:))
86 return ([connection isConnected
] && [stackController.stack count
] > 1);
87 else if (action
== @selector(stepIn
:) || action
== @selector(stepOver
:) || action
== @selector(run
:))
88 return [connection isConnected
];
89 else if (action
== @selector(reconnect
:))
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 [self startDebugger
];
144 * Called once the socket accepts and MacGDBp is connected to the debugger
146 - (void)startDebugger
148 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:@
"BreakOnFirstLine"])
153 * Called once the debugger disconnects.
155 - (void)debuggerDisconnected
157 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:@
"AutoReconnect"])
158 [self reconnect
:self];
160 // Invalidate the marked line so we don't look like we're still running.
161 sourceViewer.markedLine
= -1;
162 [sourceViewer setNeedsDisplay
:YES
];
166 * Forwards the message to run script execution to the connection
168 - (IBAction
)run
:(id)sender
174 * Tells the connection to ask the server to reconnect
176 - (IBAction
)reconnect
:(id)sender
178 [connection reconnect
];
179 [self resetDisplays
];
183 * Forwards the message to "step in" to the connection
185 - (IBAction
)stepIn
:(id)sender
187 if ([[variablesTreeController selectedObjects
] count
] > 0)
188 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
194 * Forwards the message to "step out" to the connection
196 - (IBAction
)stepOut
:(id)sender
198 if ([[variablesTreeController selectedObjects
] count
] > 0)
199 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
201 [connection stepOut
];
205 * Forwards the message to "step over" to the connection
207 - (IBAction
)stepOver
:(id)sender
209 if ([[variablesTreeController selectedObjects
] count
] > 0)
210 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
212 [connection stepOver
];
216 * NSTableView delegate method that informs the controller that the stack selection did change and that
217 * we should update the source viewer
219 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
221 [self updateSourceViewer
];
222 [self expandVariables
];
226 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
228 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
230 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
231 [expandedVariables addObject
:[[node representedObject
] fullname
]];
235 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
237 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
239 [expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullname
]];
245 * Does the actual updating of the source viewer by reading in the file
247 - (void)updateSourceViewer
249 NSArray
* selection
= [stackArrayController selectedObjects
];
250 if (!selection ||
[selection count
] < 1)
252 if ([selection count
] > 1)
253 NSLog(@
"INVALID SELECTION");
254 StackFrame
* frame
= [selection objectAtIndex
:0];
257 NSString
* filename
= [[NSURL URLWithString
:frame.filename
] path
];
258 if ([filename isEqualToString
:@
""])
261 // Replace the source if necessary.
262 if (frame.source
&& ![sourceViewer.file isEqualToString
:filename
])
264 [sourceViewer setString
:frame.source asFile
:filename
];
266 NSSet
* breakpoints
= [NSSet setWithArray
:[[BreakpointManager sharedManager
] breakpointsForFile
:filename
]];
267 [[sourceViewer numberView
] setMarkers
:breakpoints
];
270 [sourceViewer setMarkedLine
:frame.lineNumber
];
271 [sourceViewer scrollToLine
:frame.lineNumber
];
273 [[sourceViewer textView
] display
];
277 * Does some house keeping to the stack viewer
279 - (void)updateStackViewer
281 [stackArrayController rearrangeObjects
];
282 [stackArrayController setSelectionIndex
:0];
283 [self expandVariables
];
287 * Expands the variables based on the stored set
289 - (void)expandVariables
291 NSString
* selection
= [selectedVariable fullname
];
293 for (int i
= 0; i
< [variablesOutlineView numberOfRows
]; i
++)
295 NSTreeNode
* node
= [variablesOutlineView itemAtRow
:i
];
296 NSString
* fullname
= [[node representedObject
] fullname
];
298 // see if it needs expanding
299 if ([expandedVariables containsObject
:fullname
])
300 [variablesOutlineView expandItem
:node
];
302 // select it if we had it selected before
303 if ([fullname isEqualToString
:selection
])
304 [variablesTreeController setSelectionIndexPath
:[node indexPath
]];
308 #pragma mark BSSourceView Delegate
311 * The gutter was clicked, which indicates that a breakpoint needs to be changed
313 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
315 BreakpointManager
* mngr
= [BreakpointManager sharedManager
];
317 if ([mngr hasBreakpointAt
:line inFile
:file
])
319 [mngr removeBreakpointAt
:line inFile
:file
];
323 Breakpoint
* bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
324 [mngr addBreakpoint
:bp
];
328 [[sourceViewer numberView
] setMarkers
:[NSSet setWithArray
:[mngr breakpointsForFile
:file
]]];
329 [[sourceViewer numberView
] setNeedsDisplay
:YES
];
332 #pragma mark GDBpConnectionDelegate
336 aboutToClobber_
= YES
;
339 - (void)newStackFrame
:(StackFrame
*)frame
343 [stackController.stack removeAllObjects
];
344 aboutToClobber_
= NO
;
346 [stackController push
:frame
];
347 [self updateStackViewer
];
348 [self updateSourceViewer
];
351 - (void)sourceUpdated
:(StackFrame
*)frame
353 [self updateSourceViewer
];