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 "GDBpConnection.h"
19 #import "NSXMLElementAdditions.h"
20 #import "AppDelegate.h"
21 #import "BreakpointManager.h"
23 @interface DebuggerController (Private
)
24 - (void)updateSourceViewer
;
25 - (void)updateStackViewer
;
26 - (void)expandVariables
;
30 @implementation DebuggerController
32 @synthesize connection
, sourceViewer
, inspector
;
35 * Initializes the window controller and sets the connection using preference
40 if (self = [super initWithWindowNibName
:@
"Debugger"])
42 stackController
= [[StackController alloc
] init
];
44 NSUserDefaults
* defaults
= [NSUserDefaults standardUserDefaults
];
45 connection
= [[GDBpConnection alloc
] initWithPort
:[defaults integerForKey
:@
"Port"] session
:[defaults stringForKey
:@
"IDEKey"]];
46 expandedVariables
= [[NSMutableSet alloc
] init
];
47 [[self window
] makeKeyAndOrderFront
:nil];
48 [[self window
] setDelegate
:self];
50 [[NSNotificationCenter defaultCenter
]
52 selector
:@selector(handleConnectionError
:)
53 name
:kErrorOccurredNotif
57 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:@
"InspectorWindowVisible"])
58 [inspector orderFront
:self];
69 [expandedVariables release
];
70 [stackController release
];
75 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
79 [[self window
] setExcludedFromWindowsMenu
:YES
];
80 [[self window
] setTitle
:[NSString stringWithFormat
:@
"GDBp @ %@:%d/%@", [connection remoteHost
], [connection port
], [connection session
]]];
81 [sourceViewer setDelegate
:self];
82 [stackArrayController setSortDescriptors
:[NSArray arrayWithObject
:[[[NSSortDescriptor alloc
] initWithKey
:@
"index" ascending
:YES
] autorelease
]]];
86 * Called right before the window closes so that we can tell the socket to close down
88 - (void)windowWillClose
:(NSNotification
*)notif
90 [[connection socket
] close
];
94 * Validates the menu items for the "Debugger" menu
96 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
98 SEL action
= [anItem action
];
100 if (action
== @selector(stepOut
:))
101 return ([connection isConnected
] && [stackController.stack count
] > 1);
102 else if (action
== @selector(stepIn
:) || action
== @selector(stepOver
:) || action
== @selector(run
:))
103 return [connection isConnected
];
104 else if (action
== @selector(reconnect
:))
105 return ![connection isConnected
];
107 return [[self window
] validateUserInterfaceItem
:anItem
];
111 * Resets all the displays to be empty
113 - (void)resetDisplays
115 [variablesTreeController setContent
:nil];
116 [stackController.stack removeAllObjects
];
117 [stackArrayController rearrangeObjects
];
118 [[sourceViewer textView
] setString
:@
""];
119 sourceViewer.file
= nil;
123 * Sets the status to be "Error" and then displays the error message
125 - (void)setError
:(NSString
*)anError
127 [errormsg setStringValue
:anError
];
128 [errormsg setHidden
:NO
];
132 * Handles a GDBpConnection error
134 - (void)handleConnectionError
:(NSNotification
*)notif
136 [self setError
:[[notif userInfo
] valueForKey
:@
"NSString"]];
140 * Called once the socket accepts and MacGDBp is connected to the debugger
142 - (void)startDebugger
144 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:@
"BreakOnFirstLine"])
149 * Forwards the message to run script execution to the connection
151 - (IBAction
)run
:(id)sender
154 if ([connection isConnected
])
159 * Tells the connection to ask the server to reconnect
161 - (IBAction
)reconnect
:(id)sender
163 [connection reconnect
];
164 [self resetDisplays
];
168 * Forwards the message to "step in" to the connection
170 - (IBAction
)stepIn
:(id)sender
172 if ([[variablesTreeController selectedObjects
] count
] > 0)
173 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
176 if ([connection isConnected
])
181 * Forwards the message to "step out" to the connection
183 - (IBAction
)stepOut
:(id)sender
185 if ([[variablesTreeController selectedObjects
] count
] > 0)
186 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
188 [connection stepOut
];
189 if ([connection isConnected
])
194 * Forwards the message to "step over" to the connection
196 - (IBAction
)stepOver
:(id)sender
198 if ([[variablesTreeController selectedObjects
] count
] > 0)
199 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
201 [connection stepOver
];
202 if ([connection isConnected
])
207 * NSTableView delegate method that informs the controller that the stack selection did change and that
208 * we should update the source viewer
210 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
212 [self updateSourceViewer
];
213 [self expandVariables
];
217 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
219 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
221 NSTreeNode
* node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
222 [expandedVariables addObject
:[[node representedObject
] fullname
]];
226 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
228 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
230 [expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullname
]];
236 * Does the actual updating of the source viewer by reading in the file
238 - (void)updateSourceViewer
240 id selection
= [stackArrayController selection
];
241 if ([selection valueForKey
:@
"filename"] == NSNoSelectionMarker
)
245 NSString
* filename
= [selection valueForKey
:@
"filename"];
246 filename
= [[NSURL URLWithString
:filename
] path
];
247 if ([filename isEqualToString
:@
""])
250 // replace the source if necessary
251 if (![sourceViewer.file isEqualToString
:filename
])
253 NSString
* source
= [selection valueForKey
:@
"source"];
254 [sourceViewer setString
:source asFile
:filename
];
256 NSSet
* breakpoints
= [NSSet setWithArray
:[[BreakpointManager sharedManager
] breakpointsForFile
:filename
]];
257 [[sourceViewer numberView
] setMarkers
:breakpoints
];
260 int line
= [[selection valueForKey
:@
"lineNumber"] intValue
];
261 [sourceViewer setMarkedLine
:line
];
262 [sourceViewer scrollToLine
:line
];
264 [[sourceViewer textView
] display
];
268 * Does some house keeping to the stack viewer
270 - (void)updateStackViewer
272 [stackArrayController rearrangeObjects
];
273 [stackArrayController setSelectionIndex
:0];
274 [self expandVariables
];
278 * Expands the variables based on the stored set
280 - (void)expandVariables
282 NSString
* selection
= [selectedVariable fullname
];
284 for (int i
= 0; i
< [variablesOutlineView numberOfRows
]; i
++)
286 NSTreeNode
* node
= [variablesOutlineView itemAtRow
:i
];
287 NSString
* fullname
= [[node representedObject
] fullname
];
289 // see if it needs expanding
290 if ([expandedVariables containsObject
:fullname
])
291 [variablesOutlineView expandItem
:node
];
293 // select it if we had it selected before
294 if ([fullname isEqualToString
:selection
])
295 [variablesTreeController setSelectionIndexPath
:[node indexPath
]];
300 * This updates the entire stack. Xdebug is queried to get the stack, non-shifted
301 * frames are reused and new ones are fetched.
305 NSArray
* stack
= [connection getCurrentStack
];
309 [stackController.stack removeAllObjects
];
310 [stackController.stack addObjectsFromArray
:stack
];
311 [self updateStackViewer
];
312 [self updateSourceViewer
];
315 #pragma mark BSSourceView Delegate
318 * The gutter was clicked, which indicates that a breakpoint needs to be changed
320 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
322 BreakpointManager
* mngr
= [BreakpointManager sharedManager
];
324 if ([mngr hasBreakpointAt
:line inFile
:file
])
326 [mngr removeBreakpointAt
:line inFile
:file
];
330 Breakpoint
* bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
331 [mngr addBreakpoint
:bp
];
335 [[sourceViewer numberView
] setMarkers
:[NSSet setWithArray
:[mngr breakpointsForFile
:file
]]];
336 [[sourceViewer numberView
] setNeedsDisplay
:YES
];