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
]]];
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
:) || action
== @selector(stepOver
:) || action
== @selector(run
:))
91 return [connection isConnected
];
93 return [[self window
] validateUserInterfaceItem
:anItem
];
97 * Shows the inspector window
99 - (IBAction
)showInspectorWindow
:(id)sender
101 if (![inspector isVisible
])
102 [inspector makeKeyAndOrderFront
:sender
];
104 [inspector orderOut
:sender
];
108 * Resets all the displays to be empty
110 - (void)resetDisplays
112 [variablesTreeController setContent
:nil];
113 [stackController.stack removeAllObjects
];
114 [stackArrayController rearrangeObjects
];
115 [[sourceViewer textView
] setString
:@
""];
116 sourceViewer.file
= nil;
120 * Sets the status to be "Error" and then displays the error message
122 - (void)setError
:(NSString
*)anError
124 [errormsg setStringValue
:anError
];
125 [errormsg setHidden
:NO
];
129 * Handles a GDBpConnection error
131 - (void)errorEncountered
:(NSString
*)error
133 [self setError
:error
];
137 * Delegate function for GDBpConnection for when the debugger connects.
139 - (void)debuggerConnected
141 [errormsg setHidden
:YES
];
142 if (!self.connection.attached
)
144 [self startDebugger
];
148 * Called once the socket accepts and MacGDBp is connected to the debugger
150 - (void)startDebugger
152 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:@
"BreakOnFirstLine"])
157 * Called once the debugger disconnects.
159 - (void)debuggerDisconnected
161 // Invalidate the marked line so we don't look like we're still running.
162 sourceViewer.markedLine
= -1;
163 [sourceViewer setNeedsDisplay
:YES
];
167 * Forwards the message to run script execution to the connection
169 - (IBAction
)run
:(id)sender
174 - (IBAction
)attachedToggled
:(id)sender
176 connection.attached
= [sender state
] == NSOnState
;
180 * Forwards the message to "step in" to the connection
182 - (IBAction
)stepIn
:(id)sender
184 if ([[variablesTreeController selectedObjects
] count
] > 0)
185 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
191 * Forwards the message to "step out" to the connection
193 - (IBAction
)stepOut
:(id)sender
195 if ([[variablesTreeController selectedObjects
] count
] > 0)
196 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
198 [connection stepOut
];
202 * Forwards the message to "step over" to the connection
204 - (IBAction
)stepOver
:(id)sender
206 if ([[variablesTreeController selectedObjects
] count
] > 0)
207 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
209 [connection stepOver
];
212 - (void)fetchChildProperties
:(VariableNode
*)node
214 NSArray
* selection
= [stackArrayController selectedObjects
];
215 assert([selection count
] == 1);
216 NSInteger depth
= [[selection objectAtIndex
:0] index
];
217 NSInteger txn
= [connection getChildrenOfProperty
:node atDepth
:depth
];
218 [pendingProperties_ setObject
:node forKey
:[NSNumber numberWithInt
:txn
]];
222 * NSTableView delegate method that informs the controller that the stack selection did change and that
223 * we should update the source viewer
225 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
227 [self updateSourceViewer
];
228 [self expandVariables
];
232 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
234 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
236 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
237 [expandedVariables addObject
:[[node representedObject
] fullName
]];
241 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
243 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
245 [expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullName
]];
251 * Does the actual updating of the source viewer by reading in the file
253 - (void)updateSourceViewer
255 NSArray
* selection
= [stackArrayController selectedObjects
];
256 if (!selection ||
[selection count
] < 1)
258 if ([selection count
] > 1)
259 NSLog(@
"INVALID SELECTION");
260 StackFrame
* frame
= [selection objectAtIndex
:0];
263 [connection loadStackFrame
:frame
];
268 NSString
* filename
= [[NSURL URLWithString
:frame.filename
] path
];
269 if ([filename isEqualToString
:@
""])
272 // Replace the source if necessary.
273 if (frame.source
&& ![sourceViewer.file isEqualToString
:filename
])
275 [sourceViewer setString
:frame.source asFile
:filename
];
277 NSSet
* breakpoints
= [NSSet setWithArray
:[[BreakpointManager sharedManager
] breakpointsForFile
:filename
]];
278 [[sourceViewer numberView
] setMarkers
:breakpoints
];
281 [sourceViewer setMarkedLine
:frame.lineNumber
];
282 [sourceViewer scrollToLine
:frame.lineNumber
];
284 [[sourceViewer textView
] display
];
288 * Does some house keeping to the stack viewer
290 - (void)updateStackViewer
292 [stackArrayController rearrangeObjects
];
293 [stackArrayController setSelectionIndex
:0];
294 [self expandVariables
];
298 * Expands the variables based on the stored set
300 - (void)expandVariables
302 NSString
* selection
= [selectedVariable fullName
];
304 for (int i
= 0; i
< [variablesOutlineView numberOfRows
]; i
++)
306 NSTreeNode
* node
= [variablesOutlineView itemAtRow
:i
];
307 NSString
* fullName
= [[node representedObject
] fullName
];
309 // see if it needs expanding
310 if ([expandedVariables containsObject
:fullName
])
311 [variablesOutlineView expandItem
:node
];
313 // select it if we had it selected before
314 if ([fullName isEqualToString
:selection
])
315 [variablesTreeController setSelectionIndexPath
:[node indexPath
]];
319 #pragma mark BSSourceView Delegate
322 * The gutter was clicked, which indicates that a breakpoint needs to be changed
324 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
326 BreakpointManager
* mngr
= [BreakpointManager sharedManager
];
328 if ([mngr hasBreakpointAt
:line inFile
:file
])
330 [mngr removeBreakpointAt
:line inFile
:file
];
334 Breakpoint
* bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
335 [mngr addBreakpoint
:bp
];
339 [[sourceViewer numberView
] setMarkers
:[NSSet setWithArray
:[mngr breakpointsForFile
:file
]]];
340 [[sourceViewer numberView
] setNeedsDisplay
:YES
];
343 #pragma mark GDBpConnectionDelegate
347 aboutToClobber_
= YES
;
348 [pendingProperties_ removeAllObjects
];
351 - (void)newStackFrame
:(StackFrame
*)frame
355 [stackController.stack removeAllObjects
];
356 aboutToClobber_
= NO
;
358 [stackController push
:frame
];
359 [self updateStackViewer
];
360 [self updateSourceViewer
];
363 - (void)sourceUpdated
:(StackFrame
*)frame
365 [self updateSourceViewer
];
368 - (void)receivedProperties
:(NSArray
*)properties forTransaction
:(NSInteger
)transaction
370 NSNumber
* key
= [NSNumber numberWithInt
:transaction
];
371 VariableNode
* node
= [pendingProperties_ objectForKey
:key
];
373 [node setChildrenFromXMLChildren
:properties
];
374 [variablesTreeController rearrangeObjects
];
375 [pendingProperties_ removeObjectForKey
:key
];