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
;
29 @implementation DebuggerController
31 @synthesize connection
, sourceViewer
, inspector
;
34 * Initializes the window controller and sets the connection using preference
39 if (self = [super initWithWindowNibName
:@
"Debugger"])
41 stackController
= [[StackController alloc
] init
];
43 NSUserDefaults
* defaults
= [NSUserDefaults standardUserDefaults
];
44 connection
= [[GDBpConnection 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 * Called right before the window closes so that we can tell the socket to close down
81 - (void)windowWillClose
:(NSNotification
*)notif
87 * Validates the menu items for the "Debugger" menu
89 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
91 SEL action
= [anItem action
];
93 if (action
== @selector(stepOut
:))
94 return ([connection isConnected
] && [stackController.stack count
] > 1);
95 else if (action
== @selector(stepIn
:) || action
== @selector(stepOver
:) || action
== @selector(run
:))
96 return [connection isConnected
];
97 else if (action
== @selector(reconnect
:))
98 return ![connection isConnected
];
100 return [[self window
] validateUserInterfaceItem
:anItem
];
104 * Shows the inspector window
106 - (IBAction
)showInspectorWindow
:(id)sender
108 if (![inspector isVisible
])
109 [inspector makeKeyAndOrderFront
:sender
];
111 [inspector orderOut
:sender
];
115 * Resets all the displays to be empty
117 - (void)resetDisplays
119 [variablesTreeController setContent
:nil];
120 [stackController.stack removeAllObjects
];
121 [stackArrayController rearrangeObjects
];
122 [[sourceViewer textView
] setString
:@
""];
123 sourceViewer.file
= nil;
127 * Sets the status to be "Error" and then displays the error message
129 - (void)setError
:(NSString
*)anError
131 [errormsg setStringValue
:anError
];
132 [errormsg setHidden
:NO
];
136 * Handles a GDBpConnection error
138 - (void)errorEncountered
:(NSString
*)error
140 [self setError
:error
];
144 * Delegate function for GDBpConnection for when the debugger connects.
146 - (void)debuggerConnected
148 [self startDebugger
];
152 * Called once the socket accepts and MacGDBp is connected to the debugger
154 - (void)startDebugger
156 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:@
"BreakOnFirstLine"])
161 * Called once the debugger disconnects.
163 - (void)debuggerDisconnected
165 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:@
"AutoReconnect"])
166 [self reconnect
:self];
168 // Invalidate the marked line so we don't look like we're still running.
169 sourceViewer.markedLine
= -1;
170 [sourceViewer setNeedsDisplay
:YES
];
174 * Forwards the message to run script execution to the connection
176 - (IBAction
)run
:(id)sender
182 * Tells the connection to ask the server to reconnect
184 - (IBAction
)reconnect
:(id)sender
186 [connection reconnect
];
187 [self resetDisplays
];
191 * Forwards the message to "step in" to the connection
193 - (IBAction
)stepIn
:(id)sender
195 if ([[variablesTreeController selectedObjects
] count
] > 0)
196 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
202 * Forwards the message to "step out" to the connection
204 - (IBAction
)stepOut
:(id)sender
206 if ([[variablesTreeController selectedObjects
] count
] > 0)
207 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
209 [connection stepOut
];
213 * Forwards the message to "step over" to the connection
215 - (IBAction
)stepOver
:(id)sender
217 if ([[variablesTreeController selectedObjects
] count
] > 0)
218 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
220 [connection stepOver
];
224 * NSTableView delegate method that informs the controller that the stack selection did change and that
225 * we should update the source viewer
227 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
229 [self updateSourceViewer
];
230 [self expandVariables
];
234 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
236 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
238 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
239 [expandedVariables addObject
:[[node representedObject
] fullname
]];
243 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
245 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
247 [expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullname
]];
253 * Does the actual updating of the source viewer by reading in the file
255 - (void)updateSourceViewer
257 id selection
= [stackArrayController selection
];
258 if ([selection valueForKey
:@
"filename"] == NSNoSelectionMarker
)
262 NSString
* filename
= [selection valueForKey
:@
"filename"];
263 filename
= [[NSURL URLWithString
:filename
] path
];
264 if ([filename isEqualToString
:@
""])
267 // replace the source if necessary
268 if (![sourceViewer.file isEqualToString
:filename
])
270 NSString
* source
= [selection valueForKey
:@
"source"];
271 [sourceViewer setString
:source asFile
:filename
];
273 NSSet
* breakpoints
= [NSSet setWithArray
:[[BreakpointManager sharedManager
] breakpointsForFile
:filename
]];
274 [[sourceViewer numberView
] setMarkers
:breakpoints
];
277 int line
= [[selection valueForKey
:@
"lineNumber"] intValue
];
278 [sourceViewer setMarkedLine
:line
];
279 [sourceViewer scrollToLine
:line
];
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
]];
317 * This updates the entire stack. Xdebug is queried to get the stack, non-shifted
318 * frames are reused and new ones are fetched.
322 NSArray
* stack
= [connection getCurrentStack
];
326 [stackController.stack removeAllObjects
];
327 [stackController.stack addObjectsFromArray
:stack
];
328 [self updateStackViewer
];
329 [self updateSourceViewer
];
332 #pragma mark BSSourceView Delegate
335 * The gutter was clicked, which indicates that a breakpoint needs to be changed
337 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
339 BreakpointManager
* mngr
= [BreakpointManager sharedManager
];
341 if ([mngr hasBreakpointAt
:line inFile
:file
])
343 [mngr removeBreakpointAt
:line inFile
:file
];
347 Breakpoint
* bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
348 [mngr addBreakpoint
:bp
];
352 [[sourceViewer numberView
] setMarkers
:[NSSet setWithArray
:[mngr breakpointsForFile
:file
]]];
353 [[sourceViewer numberView
] setNeedsDisplay
:YES
];