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
];
91 else if (action
== @selector(reconnect
:))
92 return ![connection isConnected
];
94 return [[self window
] validateUserInterfaceItem
:anItem
];
98 * Shows the inspector window
100 - (IBAction
)showInspectorWindow
:(id)sender
102 if (![inspector isVisible
])
103 [inspector makeKeyAndOrderFront
:sender
];
105 [inspector orderOut
:sender
];
109 * Resets all the displays to be empty
111 - (void)resetDisplays
113 [variablesTreeController setContent
:nil];
114 [stackController.stack removeAllObjects
];
115 [stackArrayController rearrangeObjects
];
116 [[sourceViewer textView
] setString
:@
""];
117 sourceViewer.file
= nil;
121 * Sets the status to be "Error" and then displays the error message
123 - (void)setError
:(NSString
*)anError
125 [errormsg setStringValue
:anError
];
126 [errormsg setHidden
:NO
];
130 * Handles a GDBpConnection error
132 - (void)errorEncountered
:(NSString
*)error
134 [self setError
:error
];
138 * Delegate function for GDBpConnection for when the debugger connects.
140 - (void)debuggerConnected
142 [errormsg setHidden
:YES
];
143 [self startDebugger
];
147 * Called once the socket accepts and MacGDBp is connected to the debugger
149 - (void)startDebugger
151 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:@
"BreakOnFirstLine"])
156 * Called once the debugger disconnects.
158 - (void)debuggerDisconnected
160 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:@
"AutoReconnect"])
161 [self reconnect
:self];
163 // Invalidate the marked line so we don't look like we're still running.
164 sourceViewer.markedLine
= -1;
165 [sourceViewer setNeedsDisplay
:YES
];
169 * Forwards the message to run script execution to the connection
171 - (IBAction
)run
:(id)sender
177 * Tells the connection to ask the server to reconnect
179 - (IBAction
)reconnect
:(id)sender
181 [connection reconnect
];
182 [self resetDisplays
];
186 * Forwards the message to "step in" to the connection
188 - (IBAction
)stepIn
:(id)sender
190 if ([[variablesTreeController selectedObjects
] count
] > 0)
191 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
197 * Forwards the message to "step out" to the connection
199 - (IBAction
)stepOut
:(id)sender
201 if ([[variablesTreeController selectedObjects
] count
] > 0)
202 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
204 [connection stepOut
];
208 * Forwards the message to "step over" to the connection
210 - (IBAction
)stepOver
:(id)sender
212 if ([[variablesTreeController selectedObjects
] count
] > 0)
213 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
215 [connection stepOver
];
218 - (void)fetchProperty
:(NSString
*)property forNode
:(VariableNode
*)node
220 NSInteger txn
= [connection getProperty
:property
];
221 [pendingProperties_ setObject
:node forKey
:[NSNumber numberWithInt
:txn
]];
225 * NSTableView delegate method that informs the controller that the stack selection did change and that
226 * we should update the source viewer
228 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
230 [self updateSourceViewer
];
231 [self expandVariables
];
235 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
237 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
239 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
240 [expandedVariables addObject
:[[node representedObject
] fullName
]];
244 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
246 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
248 [expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullName
]];
254 * Does the actual updating of the source viewer by reading in the file
256 - (void)updateSourceViewer
258 NSArray
* selection
= [stackArrayController selectedObjects
];
259 if (!selection ||
[selection count
] < 1)
261 if ([selection count
] > 1)
262 NSLog(@
"INVALID SELECTION");
263 StackFrame
* frame
= [selection objectAtIndex
:0];
266 [connection loadStackFrame
:frame
];
271 NSString
* filename
= [[NSURL URLWithString
:frame.filename
] path
];
272 if ([filename isEqualToString
:@
""])
275 // Replace the source if necessary.
276 if (frame.source
&& ![sourceViewer.file isEqualToString
:filename
])
278 [sourceViewer setString
:frame.source asFile
:filename
];
280 NSSet
* breakpoints
= [NSSet setWithArray
:[[BreakpointManager sharedManager
] breakpointsForFile
:filename
]];
281 [[sourceViewer numberView
] setMarkers
:breakpoints
];
284 [sourceViewer setMarkedLine
:frame.lineNumber
];
285 [sourceViewer scrollToLine
:frame.lineNumber
];
287 [[sourceViewer textView
] display
];
291 * Does some house keeping to the stack viewer
293 - (void)updateStackViewer
295 [stackArrayController rearrangeObjects
];
296 [stackArrayController setSelectionIndex
:0];
297 [self expandVariables
];
301 * Expands the variables based on the stored set
303 - (void)expandVariables
305 NSString
* selection
= [selectedVariable fullName
];
307 for (int i
= 0; i
< [variablesOutlineView numberOfRows
]; i
++)
309 NSTreeNode
* node
= [variablesOutlineView itemAtRow
:i
];
310 NSString
* fullName
= [[node representedObject
] fullName
];
312 // see if it needs expanding
313 if ([expandedVariables containsObject
:fullName
])
314 [variablesOutlineView expandItem
:node
];
316 // select it if we had it selected before
317 if ([fullName isEqualToString
:selection
])
318 [variablesTreeController setSelectionIndexPath
:[node indexPath
]];
322 #pragma mark BSSourceView Delegate
325 * The gutter was clicked, which indicates that a breakpoint needs to be changed
327 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
329 BreakpointManager
* mngr
= [BreakpointManager sharedManager
];
331 if ([mngr hasBreakpointAt
:line inFile
:file
])
333 [mngr removeBreakpointAt
:line inFile
:file
];
337 Breakpoint
* bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
338 [mngr addBreakpoint
:bp
];
342 [[sourceViewer numberView
] setMarkers
:[NSSet setWithArray
:[mngr breakpointsForFile
:file
]]];
343 [[sourceViewer numberView
] setNeedsDisplay
:YES
];
346 #pragma mark GDBpConnectionDelegate
350 aboutToClobber_
= YES
;
351 [pendingProperties_ removeAllObjects
];
354 - (void)newStackFrame
:(StackFrame
*)frame
358 [stackController.stack removeAllObjects
];
359 aboutToClobber_
= NO
;
361 [stackController push
:frame
];
362 [self updateStackViewer
];
363 [self updateSourceViewer
];
366 - (void)sourceUpdated
:(StackFrame
*)frame
368 [self updateSourceViewer
];
371 - (void)receivedProperties
:(NSArray
*)properties forTransaction
:(NSInteger
)transaction
373 NSNumber
* key
= [NSNumber numberWithInt
:transaction
];
374 VariableNode
* node
= [pendingProperties_ objectForKey
:key
];
376 [node setChildrenFromXMLChildren
:properties
];
377 [variablesTreeController rearrangeObjects
];
378 [pendingProperties_ removeObjectForKey
:key
];