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
;
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 expandedVariables
= [[NSMutableSet alloc
] init
];
46 [[self window
] makeKeyAndOrderFront
:nil];
47 [[self window
] setDelegate
:self];
49 [[NSNotificationCenter defaultCenter
]
51 selector
:@selector(handleConnectionError
:)
52 name
:kErrorOccurredNotif
56 if ([[NSUserDefaults standardUserDefaults
] boolForKey
:@
"InspectorWindowVisible"])
57 [inspector orderFront
:self];
68 [expandedVariables release
];
69 [stackController release
];
74 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
78 [[self window
] setExcludedFromWindowsMenu
:YES
];
79 [[self window
] setTitle
:[NSString stringWithFormat
:@
"GDBp @ %@:%d/%@", [connection remoteHost
], [connection port
], [connection session
]]];
80 [sourceViewer setDelegate
:self];
81 [stackArrayController setSortDescriptors
:[NSArray arrayWithObject
:[[[NSSortDescriptor alloc
] initWithKey
:@
"index" ascending
:YES
] autorelease
]]];
85 * Called right before the window closes so that we can tell the socket to close down
87 - (void)windowWillClose
:(NSNotification
*)notif
89 [[connection socket
] close
];
93 * Validates the menu items for the "Debugger" menu
95 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
97 SEL action
= [anItem action
];
99 if (action
== @selector(stepOut
:))
100 return ([connection isConnected
] && [stackController.stack count
] > 1);
101 else if (action
== @selector(stepIn
:) || action
== @selector(stepOver
:) || action
== @selector(run
:))
102 return [connection isConnected
];
103 else if (action
== @selector(reconnect
:))
104 return ![connection isConnected
];
106 return [[self window
] validateUserInterfaceItem
:anItem
];
110 * Resets all the displays to be empty
112 - (void)resetDisplays
114 [variablesTreeController setContent
:nil];
115 [stackController.stack removeAllObjects
];
116 [[sourceViewer textView
] setString
:@
""];
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)handleConnectionError
:(NSNotification
*)notif
133 [self setError
:[[notif userInfo
] valueForKey
:@
"NSString"]];
137 * Called once the socket accepts and MacGDBp is connected to the debugger
139 - (void)startDebugger
145 * Forwards the message to run script execution to the connection
147 - (IBAction
)run
:(id)sender
149 StackFrame
*frame
= [connection run
];
150 [stackController pop
];
152 if ([connection isConnected
] && frame
!= nil)
154 [stackController push
:frame
];
155 [self updateStackViewer
];
160 * Tells the connection to ask the server to reconnect
162 - (IBAction
)reconnect
:(id)sender
164 [connection reconnect
];
165 [self resetDisplays
];
169 * Forwards the message to "step in" to the connection
171 - (IBAction
)stepIn
:(id)sender
173 if ([[variablesTreeController selectedObjects
] count
] > 0)
174 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
176 StackFrame
*frame
= [connection stepIn
];
177 if ([frame isShiftedFrame
:[stackController peek
]])
178 [stackController pop
];
179 [stackController push
:frame
];
180 [self updateStackViewer
];
184 * Forwards the message to "step out" to the connection
186 - (IBAction
)stepOut
:(id)sender
188 if ([[variablesTreeController selectedObjects
] count
] > 0)
189 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
191 StackFrame
*frame
= [connection stepOut
];
192 [stackController pop
]; // frame we were out of
193 [stackController pop
]; // frame we are returning to
194 [stackController push
:frame
];
195 [self updateStackViewer
];
199 * Forwards the message to "step over" to the connection
201 - (IBAction
)stepOver
:(id)sender
203 if ([[variablesTreeController selectedObjects
] count
] > 0)
204 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
206 StackFrame
*frame
= [connection stepOver
];
207 [stackController pop
];
208 [stackController push
:frame
];
209 [self updateStackViewer
];
213 * NSTableView delegate method that informs the controller that the stack selection did change and that
214 * we should update the source viewer
216 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
218 [self updateSourceViewer
];
219 [self expandVariables
];
223 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
225 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
227 NSTreeNode
*node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
228 [expandedVariables addObject
:[[node representedObject
] fullname
]];
232 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
234 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
236 [expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullname
]];
242 * Does the actual updating of the source viewer by reading in the file
244 - (void)updateSourceViewer
246 id selection
= [stackArrayController selection
];
247 if ([selection valueForKey
:@
"filename"] == NSNoSelectionMarker
)
251 NSString
*filename
= [selection valueForKey
:@
"filename"];
252 filename
= [[NSURL URLWithString
:filename
] path
];
253 if ([filename isEqualToString
:@
""])
256 // replace the source if necessary
257 if (![sourceViewer.file isEqualToString
:filename
])
259 NSString
*source
= [selection valueForKey
:@
"source"];
260 [sourceViewer setString
:source asFile
:filename
];
262 NSSet
*breakpoints
= [NSSet setWithArray
:[[BreakpointManager sharedManager
] breakpointsForFile
:filename
]];
263 [[sourceViewer numberView
] setMarkers
:breakpoints
];
266 int line
= [[selection valueForKey
:@
"lineNumber"] intValue
];
267 [sourceViewer setMarkedLine
:line
];
268 [sourceViewer scrollToLine
:line
];
270 [[sourceViewer textView
] display
];
274 * Does some house keeping to the stack viewer
276 - (void)updateStackViewer
278 [stackArrayController rearrangeObjects
];
279 [stackArrayController setSelectionIndex
:0];
280 [self expandVariables
];
284 * Expands the variables based on the stored set
286 - (void)expandVariables
288 NSString
*selection
= [selectedVariable fullname
];
290 for (int i
= 0; i
< [variablesOutlineView numberOfRows
]; i
++)
292 NSTreeNode
*node
= [variablesOutlineView itemAtRow
:i
];
293 NSString
*fullname
= [[node representedObject
] fullname
];
295 // see if it needs expanding
296 if ([expandedVariables containsObject
:fullname
])
297 [variablesOutlineView expandItem
:node
];
299 // select it if we had it selected before
300 if ([fullname isEqualToString
:selection
])
301 [variablesTreeController setSelectionIndexPath
:[node indexPath
]];
305 #pragma mark BSSourceView Delegate
308 * The gutter was clicked, which indicates that a breakpoint needs to be changed
310 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
312 BreakpointManager
*mngr
= [BreakpointManager sharedManager
];
314 if ([mngr hasBreakpointAt
:line inFile
:file
])
316 [mngr removeBreakpointAt
:line inFile
:file
];
320 Breakpoint
*bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
321 [mngr addBreakpoint
:bp
];
325 [[sourceViewer numberView
] setMarkers
:[NSSet setWithArray
:[mngr breakpointsForFile
:file
]]];
326 [[sourceViewer numberView
] setNeedsDisplay
:YES
];