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
];
43 connection
= [[DebuggerConnection alloc
] initWithPort
:[defaults integerForKey
:@
"Port"]];
44 connection.delegate
= self;
45 expandedVariables
= [[NSMutableSet alloc
] init
];
46 [[self window
] makeKeyAndOrderFront
:nil];
47 [[self window
] setDelegate
:self];
49 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:@
"InspectorWindowVisible"])
50 [inspector orderFront
:self];
61 [expandedVariables release
];
62 [stackController release
];
67 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
71 [[self window
] setExcludedFromWindowsMenu
:YES
];
72 [[self window
] setTitle
:[NSString stringWithFormat
:@
"GDBp @ %@:%d", [connection remoteHost
], [connection port
]]];
73 [sourceViewer setDelegate
:self];
74 [stackArrayController setSortDescriptors
:[NSArray arrayWithObject
:[[[NSSortDescriptor alloc
] initWithKey
:@
"index" ascending
:YES
] autorelease
]]];
78 * Validates the menu items for the "Debugger" menu
80 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
82 SEL action
= [anItem action
];
84 if (action
== @selector(stepOut
:))
85 return ([connection isConnected
] && [stackController.stack count
] > 1);
86 else if (action
== @selector(stepIn
:) || action
== @selector(stepOver
:) || action
== @selector(run
:))
87 return [connection isConnected
];
88 else if (action
== @selector(reconnect
:))
89 return ![connection isConnected
];
91 return [[self window
] validateUserInterfaceItem
:anItem
];
95 * Shows the inspector window
97 - (IBAction
)showInspectorWindow
:(id)sender
99 if (![inspector isVisible
])
100 [inspector makeKeyAndOrderFront
:sender
];
102 [inspector orderOut
:sender
];
106 * Resets all the displays to be empty
108 - (void)resetDisplays
110 [variablesTreeController setContent
:nil];
111 [stackController.stack removeAllObjects
];
112 [stackArrayController rearrangeObjects
];
113 [[sourceViewer textView
] setString
:@
""];
114 sourceViewer.file
= nil;
118 * Sets the status to be "Error" and then displays the error message
120 - (void)setError
:(NSString
*)anError
122 [errormsg setStringValue
:anError
];
123 [errormsg setHidden
:NO
];
127 * Handles a GDBpConnection error
129 - (void)errorEncountered
:(NSString
*)error
131 [self setError
:error
];
135 * Delegate function for GDBpConnection for when the debugger connects.
137 - (void)debuggerConnected
139 [self startDebugger
];
143 * Called once the socket accepts and MacGDBp is connected to the debugger
145 - (void)startDebugger
147 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:@
"BreakOnFirstLine"])
152 * Called once the debugger disconnects.
154 - (void)debuggerDisconnected
156 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:@
"AutoReconnect"])
157 [self reconnect
:self];
159 // Invalidate the marked line so we don't look like we're still running.
160 sourceViewer.markedLine
= -1;
161 [sourceViewer setNeedsDisplay
:YES
];
165 * Forwards the message to run script execution to the connection
167 - (IBAction
)run
:(id)sender
173 * Tells the connection to ask the server to reconnect
175 - (IBAction
)reconnect
:(id)sender
177 [connection reconnect
];
178 [self resetDisplays
];
182 * Forwards the message to "step in" to the connection
184 - (IBAction
)stepIn
:(id)sender
186 if ([[variablesTreeController selectedObjects
] count
] > 0)
187 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
193 * Forwards the message to "step out" to the connection
195 - (IBAction
)stepOut
:(id)sender
197 if ([[variablesTreeController selectedObjects
] count
] > 0)
198 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
200 [connection stepOut
];
204 * Forwards the message to "step over" to the connection
206 - (IBAction
)stepOver
:(id)sender
208 if ([[variablesTreeController selectedObjects
] count
] > 0)
209 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
211 [connection stepOver
];
215 * NSTableView delegate method that informs the controller that the stack selection did change and that
216 * we should update the source viewer
218 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
220 [self updateSourceViewer
];
221 [self expandVariables
];
225 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
227 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
229 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
230 [expandedVariables addObject
:[[node representedObject
] fullname
]];
234 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
236 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
238 [expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullname
]];
244 * Does the actual updating of the source viewer by reading in the file
246 - (void)updateSourceViewer
248 NSArray
* selection
= [stackArrayController selectedObjects
];
249 if (!selection ||
[selection count
] < 1)
251 if ([selection count
] > 1)
252 NSLog(@
"INVALID SELECTION");
253 StackFrame
* frame
= [selection objectAtIndex
:0];
256 NSString
* filename
= [[NSURL URLWithString
:frame.filename
] path
];
257 if ([filename isEqualToString
:@
""])
260 // Replace the source if necessary.
261 if (frame.source
&& ![sourceViewer.file isEqualToString
:filename
])
263 [sourceViewer setString
:frame.source asFile
:filename
];
265 NSSet
* breakpoints
= [NSSet setWithArray
:[[BreakpointManager sharedManager
] breakpointsForFile
:filename
]];
266 [[sourceViewer numberView
] setMarkers
:breakpoints
];
269 [sourceViewer setMarkedLine
:frame.lineNumber
];
270 [sourceViewer scrollToLine
:frame.lineNumber
];
272 [[sourceViewer textView
] display
];
276 * Does some house keeping to the stack viewer
278 - (void)updateStackViewer
280 [stackArrayController rearrangeObjects
];
281 [stackArrayController setSelectionIndex
:0];
282 [self expandVariables
];
286 * Expands the variables based on the stored set
288 - (void)expandVariables
290 NSString
* selection
= [selectedVariable fullname
];
292 for (int i
= 0; i
< [variablesOutlineView numberOfRows
]; i
++)
294 NSTreeNode
* node
= [variablesOutlineView itemAtRow
:i
];
295 NSString
* fullname
= [[node representedObject
] fullname
];
297 // see if it needs expanding
298 if ([expandedVariables containsObject
:fullname
])
299 [variablesOutlineView expandItem
:node
];
301 // select it if we had it selected before
302 if ([fullname isEqualToString
:selection
])
303 [variablesTreeController setSelectionIndexPath
:[node indexPath
]];
307 #pragma mark BSSourceView Delegate
310 * The gutter was clicked, which indicates that a breakpoint needs to be changed
312 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
314 BreakpointManager
* mngr
= [BreakpointManager sharedManager
];
316 if ([mngr hasBreakpointAt
:line inFile
:file
])
318 [mngr removeBreakpointAt
:line inFile
:file
];
322 Breakpoint
* bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
323 [mngr addBreakpoint
:bp
];
327 [[sourceViewer numberView
] setMarkers
:[NSSet setWithArray
:[mngr breakpointsForFile
:file
]]];
328 [[sourceViewer numberView
] setNeedsDisplay
:YES
];
331 #pragma mark GDBpConnectionDelegate
335 aboutToClobber_
= YES
;
338 - (void)newStackFrame
:(StackFrame
*)frame
342 [stackController.stack removeAllObjects
];
343 aboutToClobber_
= NO
;
345 [stackController push
:frame
];
346 [self updateStackViewer
];
347 [self updateSourceViewer
];
350 - (void)sourceUpdated
:(StackFrame
*)frame
352 [self updateSourceViewer
];