3 * Copyright (c) 2007 - 2009, 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"] session
:[defaults stringForKey
:@
"IDEKey"]];
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
], [connection session
]]];
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
83 [[connection socket
] close
];
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 functioni 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
179 if ([connection isConnected
])
184 * Tells the connection to ask the server to reconnect
186 - (IBAction
)reconnect
:(id)sender
188 [connection reconnect
];
189 [self resetDisplays
];
193 * Forwards the message to "step in" to the connection
195 - (IBAction
)stepIn
:(id)sender
197 if ([[variablesTreeController selectedObjects
] count
] > 0)
198 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
201 if ([connection isConnected
])
206 * Forwards the message to "step out" to the connection
208 - (IBAction
)stepOut
:(id)sender
210 if ([[variablesTreeController selectedObjects
] count
] > 0)
211 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
213 [connection stepOut
];
214 if ([connection isConnected
])
219 * Forwards the message to "step over" to the connection
221 - (IBAction
)stepOver
:(id)sender
223 if ([[variablesTreeController selectedObjects
] count
] > 0)
224 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
226 [connection stepOver
];
227 if ([connection isConnected
])
232 * NSTableView delegate method that informs the controller that the stack selection did change and that
233 * we should update the source viewer
235 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
237 [self updateSourceViewer
];
238 [self expandVariables
];
242 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
244 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
246 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
247 [expandedVariables addObject
:[[node representedObject
] fullname
]];
251 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
253 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
255 [expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullname
]];
261 * Does the actual updating of the source viewer by reading in the file
263 - (void)updateSourceViewer
265 id selection
= [stackArrayController selection
];
266 if ([selection valueForKey
:@
"filename"] == NSNoSelectionMarker
)
270 NSString
* filename
= [selection valueForKey
:@
"filename"];
271 filename
= [[NSURL URLWithString
:filename
] path
];
272 if ([filename isEqualToString
:@
""])
275 // replace the source if necessary
276 if (![sourceViewer.file isEqualToString
:filename
])
278 NSString
* source
= [selection valueForKey
:@
"source"];
279 [sourceViewer setString
:source asFile
:filename
];
281 NSSet
* breakpoints
= [NSSet setWithArray
:[[BreakpointManager sharedManager
] breakpointsForFile
:filename
]];
282 [[sourceViewer numberView
] setMarkers
:breakpoints
];
285 int line
= [[selection valueForKey
:@
"lineNumber"] intValue
];
286 [sourceViewer setMarkedLine
:line
];
287 [sourceViewer scrollToLine
:line
];
289 [[sourceViewer textView
] display
];
293 * Does some house keeping to the stack viewer
295 - (void)updateStackViewer
297 [stackArrayController rearrangeObjects
];
298 [stackArrayController setSelectionIndex
:0];
299 [self expandVariables
];
303 * Expands the variables based on the stored set
305 - (void)expandVariables
307 NSString
* selection
= [selectedVariable fullname
];
309 for (int i
= 0; i
< [variablesOutlineView numberOfRows
]; i
++)
311 NSTreeNode
* node
= [variablesOutlineView itemAtRow
:i
];
312 NSString
* fullname
= [[node representedObject
] fullname
];
314 // see if it needs expanding
315 if ([expandedVariables containsObject
:fullname
])
316 [variablesOutlineView expandItem
:node
];
318 // select it if we had it selected before
319 if ([fullname isEqualToString
:selection
])
320 [variablesTreeController setSelectionIndexPath
:[node indexPath
]];
325 * This updates the entire stack. Xdebug is queried to get the stack, non-shifted
326 * frames are reused and new ones are fetched.
330 NSArray
* stack
= [connection getCurrentStack
];
334 [stackController.stack removeAllObjects
];
335 [stackController.stack addObjectsFromArray
:stack
];
336 [self updateStackViewer
];
337 [self updateSourceViewer
];
340 #pragma mark BSSourceView Delegate
343 * The gutter was clicked, which indicates that a breakpoint needs to be changed
345 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
347 BreakpointManager
* mngr
= [BreakpointManager sharedManager
];
349 if ([mngr hasBreakpointAt
:line inFile
:file
])
351 [mngr removeBreakpointAt
:line inFile
:file
];
355 Breakpoint
* bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
356 [mngr addBreakpoint
:bp
];
360 [[sourceViewer numberView
] setMarkers
:[NSSet setWithArray
:[mngr breakpointsForFile
:file
]]];
361 [[sourceViewer numberView
] setNeedsDisplay
:YES
];