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 [stackArrayController rearrangeObjects
];
117 [[sourceViewer textView
] setString
:@
""];
118 sourceViewer.file
= nil;
122 * Sets the status to be "Error" and then displays the error message
124 - (void)setError
:(NSString
*)anError
126 [errormsg setStringValue
:anError
];
127 [errormsg setHidden
:NO
];
131 * Handles a GDBpConnection error
133 - (void)handleConnectionError
:(NSNotification
*)notif
135 [self setError
:[[notif userInfo
] valueForKey
:@
"NSString"]];
139 * Called once the socket accepts and MacGDBp is connected to the debugger
141 - (void)startDebugger
147 * Forwards the message to run script execution to the connection
149 - (IBAction
)run
:(id)sender
151 StackFrame
*frame
= [connection run
];
152 [stackController pop
];
154 if ([connection isConnected
] && frame
!= nil)
156 [stackController push
:frame
];
157 [self updateStackViewer
];
162 * Tells the connection to ask the server to reconnect
164 - (IBAction
)reconnect
:(id)sender
166 [connection reconnect
];
167 [self resetDisplays
];
171 * Forwards the message to "step in" to the connection
173 - (IBAction
)stepIn
:(id)sender
175 if ([[variablesTreeController selectedObjects
] count
] > 0)
176 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
178 StackFrame
*frame
= [connection stepIn
];
179 if ([frame isShiftedFrame
:[stackController peek
]])
180 [stackController pop
];
181 [stackController push
:frame
];
182 [self updateStackViewer
];
186 * Forwards the message to "step out" to the connection
188 - (IBAction
)stepOut
:(id)sender
190 if ([[variablesTreeController selectedObjects
] count
] > 0)
191 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
193 StackFrame
*frame
= [connection stepOut
];
194 [stackController pop
]; // frame we were out of
195 [stackController pop
]; // frame we are returning to
196 [stackController push
:frame
];
197 [self updateStackViewer
];
201 * Forwards the message to "step over" to the connection
203 - (IBAction
)stepOver
:(id)sender
205 if ([[variablesTreeController selectedObjects
] count
] > 0)
206 selectedVariable
= [[variablesTreeController selectedObjects
] objectAtIndex
:0];
208 StackFrame
*frame
= [connection stepOver
];
209 [stackController pop
];
210 [stackController push
:frame
];
211 [self updateStackViewer
];
215 * NSTableView delegate method that informs the controller that the stack selection did change and that
216 * we should update the source viewer
218 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
220 [self updateSourceViewer
];
221 [self expandVariables
];
225 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
227 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
229 NSTreeNode
*node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
230 [expandedVariables addObject
:[[node representedObject
] fullname
]];
234 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
236 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
238 [expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullname
]];
244 * Does the actual updating of the source viewer by reading in the file
246 - (void)updateSourceViewer
248 id selection
= [stackArrayController selection
];
249 if ([selection valueForKey
:@
"filename"] == NSNoSelectionMarker
)
253 NSString
*filename
= [selection valueForKey
:@
"filename"];
254 filename
= [[NSURL URLWithString
:filename
] path
];
255 if ([filename isEqualToString
:@
""])
258 // replace the source if necessary
259 if (![sourceViewer.file isEqualToString
:filename
])
261 NSString
*source
= [selection valueForKey
:@
"source"];
262 [sourceViewer setString
:source asFile
:filename
];
264 NSSet
*breakpoints
= [NSSet setWithArray
:[[BreakpointManager sharedManager
] breakpointsForFile
:filename
]];
265 [[sourceViewer numberView
] setMarkers
:breakpoints
];
268 int line
= [[selection valueForKey
:@
"lineNumber"] intValue
];
269 [sourceViewer setMarkedLine
:line
];
270 [sourceViewer scrollToLine
:line
];
272 [[sourceViewer textView
] display
];
276 * Does some house keeping to the stack viewer
278 - (void)updateStackViewer
280 [stackArrayController rearrangeObjects
];
281 [stackArrayController setSelectionIndex
:0];
282 [self expandVariables
];
286 * Expands the variables based on the stored set
288 - (void)expandVariables
290 NSString
*selection
= [selectedVariable fullname
];
292 for (int i
= 0; i
< [variablesOutlineView numberOfRows
]; i
++)
294 NSTreeNode
*node
= [variablesOutlineView itemAtRow
:i
];
295 NSString
*fullname
= [[node representedObject
] fullname
];
297 // see if it needs expanding
298 if ([expandedVariables containsObject
:fullname
])
299 [variablesOutlineView expandItem
:node
];
301 // select it if we had it selected before
302 if ([fullname isEqualToString
:selection
])
303 [variablesTreeController setSelectionIndexPath
:[node indexPath
]];
307 #pragma mark BSSourceView Delegate
310 * The gutter was clicked, which indicates that a breakpoint needs to be changed
312 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
314 BreakpointManager
*mngr
= [BreakpointManager sharedManager
];
316 if ([mngr hasBreakpointAt
:line inFile
:file
])
318 [mngr removeBreakpointAt
:line inFile
:file
];
322 Breakpoint
*bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
323 [mngr addBreakpoint
:bp
];
327 [[sourceViewer numberView
] setMarkers
:[NSSet setWithArray
:[mngr breakpointsForFile
:file
]]];
328 [[sourceViewer numberView
] setNeedsDisplay
:YES
];