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
;
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
65 [expandedVariables release
];
66 [stackController release
];
71 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
75 [[self window
] setExcludedFromWindowsMenu
:YES
];
76 [[self window
] setTitle
:[NSString stringWithFormat
:@
"GDBp @ %@:%d/%@", [connection remoteHost
], [connection port
], [connection session
]]];
77 [sourceViewer setDelegate
:self];
78 [stackArrayController setSortDescriptors
:[NSArray arrayWithObject
:[[[NSSortDescriptor alloc
] initWithKey
:@
"index" ascending
:YES
] autorelease
]]];
82 * Called right before the window closes so that we can tell the socket to close down
84 - (void)windowWillClose
:(NSNotification
*)notif
86 [[connection socket
] close
];
90 * Validates the menu items for the "Debugger" menu
92 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
94 SEL action
= [anItem action
];
96 if (action
== @selector(stepOut
:))
97 return ([connection isConnected
] && [stackController.stack count
] > 1);
98 else if (action
== @selector(stepIn
:) || action
== @selector(stepOver
:) || action
== @selector(run
:))
99 return [connection isConnected
];
100 else if (action
== @selector(reconnect
:))
101 return ![connection isConnected
];
103 return [[self window
] validateUserInterfaceItem
:anItem
];
107 * Resets all the displays to be empty
109 - (void)resetDisplays
111 [variablesTreeController setContent
:nil];
112 [stackController.stack removeAllObjects
];
113 [[sourceViewer textView
] setString
:@
""];
117 * Sets the status to be "Error" and then displays the error message
119 - (void)setError
:(NSString
*)anError
121 [errormsg setStringValue
:anError
];
122 [errormsg setHidden
:NO
];
126 * Handles a GDBpConnection error
128 - (void)handleConnectionError
:(NSNotification
*)notif
130 [self setError
:[[notif userInfo
] valueForKey
:@
"NSString"]];
134 * Called once the socket accepts and MacGDBp is connected to the debugger
136 - (void)startDebugger
142 * Forwards the message to run script execution to the connection
144 - (IBAction
)run
:(id)sender
150 * Tells the connection to ask the server to reconnect
152 - (IBAction
)reconnect
:(id)sender
154 [connection reconnect
];
155 [self resetDisplays
];
159 * Forwards the message to "step in" to the connection
161 - (IBAction
)stepIn
:(id)sender
163 StackFrame
*frame
= [connection stepIn
];
164 if ([frame isShiftedFrame
:[stackController peek
]])
165 [stackController pop
];
166 [stackController push
:frame
];
167 [self updateStackViewer
];
171 * Forwards the message to "step out" to the connection
173 - (IBAction
)stepOut
:(id)sender
175 StackFrame
*frame
= [connection stepOut
];
176 [stackController pop
]; // frame we were out of
177 [stackController pop
]; // frame we are returning to
178 [stackController push
:frame
];
179 [self updateStackViewer
];
183 * Forwards the message to "step over" to the connection
185 - (IBAction
)stepOver
:(id)sender
187 StackFrame
*frame
= [connection stepOver
];
188 [stackController pop
];
189 [stackController push
:frame
];
190 [self updateStackViewer
];
194 * NSTableView delegate method that informs the controller that the stack selection did change and that
195 * we should update the source viewer
197 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
199 [self updateSourceViewer
];
200 [self expandVariables
];
204 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
206 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
208 NSTreeNode
*node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
209 [expandedVariables addObject
:[[node representedObject
] fullname
]];
213 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
215 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
217 [expandedVariables removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullname
]];
223 * Does the actual updating of the source viewer by reading in the file
225 - (void)updateSourceViewer
227 id selection
= [stackArrayController selection
];
228 if ([selection valueForKey
:@
"filename"] == NSNoSelectionMarker
)
232 NSString
*filename
= [selection valueForKey
:@
"filename"];
233 filename
= [[NSURL URLWithString
:filename
] path
];
234 if ([filename isEqualToString
:@
""])
237 // replace the source if necessary
238 if (![sourceViewer.file isEqualToString
:filename
])
240 NSString
*source
= [selection valueForKey
:@
"source"];
241 [sourceViewer setString
:source asFile
:filename
];
244 int line
= [[selection valueForKey
:@
"lineNumber"] intValue
];
245 [sourceViewer setMarkedLine
:line
];
246 [sourceViewer scrollToLine
:line
];
248 [[sourceViewer textView
] display
];
252 * Does some house keeping to the stack viewer
254 - (void)updateStackViewer
256 [stackArrayController rearrangeObjects
];
257 [stackArrayController setSelectionIndex
:0];
258 [self expandVariables
];
262 * Expands the variables based on the stored set
264 - (void)expandVariables
266 for (int i
= 0; i
< [variablesOutlineView numberOfRows
]; i
++)
268 NSTreeNode
*node
= [variablesOutlineView itemAtRow
:i
];
269 if ([expandedVariables containsObject
:[[node representedObject
] fullname
]])
271 [variablesOutlineView expandItem
:node
];
276 #pragma mark BSSourceView Delegate
279 * The gutter was clicked, which indicates that a breakpoint needs to be changed
281 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
283 BreakpointManager
*mngr
= [BreakpointManager sharedManager
];
285 if ([mngr hasBreakpointAt
:line inFile
:file
])
287 [mngr removeBreakpointAt
:line inFile
:file
];
291 Breakpoint
*bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
292 [mngr addBreakpoint
:bp
];
296 [[sourceViewer numberView
] setMarkers
:[NSSet setWithArray
:[mngr breakpointsForFile
:file
]]];
297 [[sourceViewer numberView
] setNeedsDisplay
:YES
];