3 * Copyright (c) 2007 - 2008, 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
;
27 @implementation DebuggerController
29 @synthesize connection
, sourceViewer
;
32 * Initializes the window controller and sets the connection using preference
37 if (self = [super initWithWindowNibName
:@
"Debugger"])
39 stackController
= [[StackController alloc
] init
];
41 NSUserDefaults
*defaults
= [NSUserDefaults standardUserDefaults
];
42 connection
= [[GDBpConnection alloc
] initWithWindowController
:self
43 port
:[defaults integerForKey
:@
"Port"]
44 session
:[defaults stringForKey
:@
"IDEKey"]];
45 expandedRegisters
= [[NSMutableSet alloc
] init
];
46 [[self window
] makeKeyAndOrderFront
:nil];
47 [[self window
] setDelegate
:self];
58 [expandedRegisters release
];
59 [stackController release
];
64 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
68 [self setStatus
:@
"Connecting"];
69 [[self window
] setExcludedFromWindowsMenu
:YES
];
70 [sourceViewer setDelegate
:self];
71 [stackArrayController setSortDescriptors
:[NSArray arrayWithObject
:[[[NSSortDescriptor alloc
] initWithKey
:@
"index" ascending
:YES
] autorelease
]]];
75 * Called right before the window closes so that we can tell the socket to close down
77 - (void)windowWillClose
:(NSNotification
*)notif
79 [[connection socket
] close
];
83 * Validates the menu items for the "Debugger" menu
85 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
87 SEL action
= [anItem action
];
89 if (action
== @selector(stepOut
:))
90 return ([connection isConnected
] && [stack count
] > 1);
91 else if (action
== @selector(stepIn
:) || action
== @selector(stepOver
:) || action
== @selector(run
:))
92 return [connection isConnected
];
94 return [[self window
] validateUserInterfaceItem
:anItem
];
98 * Resets all the displays to be empty
100 - (void)resetDisplays
102 [registerController setContent
:nil];
103 [stackController2 setContent
:nil];
104 [[sourceViewer textView
] setString
:@
""];
108 * Sets the status and clears any error message
110 - (void)setStatus
:(NSString
*)aStatus
112 [errormsg setHidden
:YES
];
113 [statusmsg setStringValue
:aStatus
];
114 [[self window
] setTitle
:[NSString stringWithFormat
:@
"GDBp @ %@:%d/%@", [connection remoteHost
], [connection port
], [connection session
]]];
116 [stepInButton setEnabled
:NO
];
117 [stepOutButton setEnabled
:NO
];
118 [stepOverButton setEnabled
:NO
];
119 [runButton setEnabled
:NO
];
120 [reconnectButton setEnabled
:NO
];
122 if ([connection isConnected
])
124 if ([aStatus isEqualToString
:@
"Starting"])
126 [stepInButton setEnabled
:YES
];
127 [runButton setEnabled
:YES
];
132 [reconnectButton setEnabled
:YES
];
137 * Sets the status to be "Error" and then displays the error message
139 - (void)setError
:(NSString
*)anError
141 [errormsg setStringValue
:anError
];
142 [self setStatus
:@
"Error"];
143 [errormsg setHidden
:NO
];
147 * Sets the root node element of the stacktrace
149 - (void)setStack
:(NSArray
*)node
153 if ([stack count
] > 1)
155 [stepOutButton setEnabled
:YES
];
157 [stepInButton setEnabled
:YES
];
158 [stepOverButton setEnabled
:YES
];
159 [runButton setEnabled
:YES
];
161 [self updateSourceViewer
];
165 * Sets the stack root element so that the NSOutlineView can display it
167 - (void)setRegister
:(NSXMLDocument
*)elm
169 // XXX: Doing anything short of this will cause bindings to crash spectacularly for no reason whatsoever, and
170 // in seemingly arbitrary places. The class that crashes is _NSKeyValueObservationInfoCreateByRemoving.
171 // http://boredzo.org/blog/archives/2006-01-29/have-you-seen-this-crash says that this means nothing is
172 // being observed, but I doubt that he was using an NSOutlineView which seems to be one f!cking piece of
173 // sh!t when used with NSTreeController. http://www.cocoadev.com/index.pl?NSTreeControllerBugOrDeveloperError
174 // was the inspiration for this fix (below) but the author says that inserting does not work too well, but
175 // that's okay for us as we just need to replace the entire thing.
176 [registerController setContent
:nil];
177 [registerController setContent
:[[elm rootElement
] children
]];
179 for (int i
= 0; i
< [registerView numberOfRows
]; i
++)
181 NSTreeNode
*node
= [registerView itemAtRow
:i
];
182 if ([expandedRegisters containsObject
:[[node representedObject
] fullname
]])
184 [registerView expandItem
:node
];
190 * Forwards the message to run script execution to the connection
192 - (IBAction
)run
:(id)sender
198 * Tells the connection to ask the server to reconnect
200 - (IBAction
)reconnect
:(id)sender
202 [connection reconnect
];
206 * Forwards the message to "step in" to the connection
208 - (IBAction
)stepIn
:(id)sender
210 StackFrame
*frame
= [connection stepIn
];
211 if ([frame isShiftedFrame
:[stackController peek
]])
212 [stackController pop
];
213 [stackController push
:frame
];
214 [stackArrayController rearrangeObjects
];
215 NSLog(@
"stack = %@", stackController.stack
);
219 * Forwards the message to "step out" to the connection
221 - (IBAction
)stepOut
:(id)sender
223 StackFrame
*frame
= [connection stepOut
];
224 [stackController pop
]; // frame we were out of
225 [stackController pop
]; // frame we are returning to
226 [stackController push
:frame
];
227 [stackArrayController rearrangeObjects
];
228 NSLog(@
"stack = %@", stackController.stack
);
232 * Forwards the message to "step over" to the connection
234 - (IBAction
)stepOver
:(id)sender
236 StackFrame
*frame
= [connection stepOver
];
238 [stackController pop
];
239 [stackController push
:frame
];
240 [stackArrayController rearrangeObjects
];
241 NSLog(@
"stack = %@", stackController.stack
);
245 * NSTableView delegate method that informs the controller that the stack selection did change and that
246 * we should update the source viewer
248 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
250 [self updateSourceViewer
];
254 * Does the actual updating of the source viewer by reading in the file
256 - (void)updateSourceViewer
258 id selectedLevel
= [[stackController2 selection
] valueForKey
:@
"level"];
259 if (selectedLevel
== NSNoSelectionMarker
)
261 [[sourceViewer textView
] setString
:@
""];
264 int selection
= [selectedLevel intValue
];
266 if ([stack count
] < 1)
268 NSLog(@
"huh... we don't have a stack");
272 // get the filename and then set the text
273 NSString
*filename
= [[stack objectAtIndex
:selection
] valueForKey
:@
"filename"];
274 filename
= [[NSURL URLWithString
:filename
] path
];
275 if ([filename isEqualToString
:@
""])
280 [sourceViewer setFile
:filename
];
282 int line
= [[[stack objectAtIndex
:selection
] valueForKey
:@
"lineno"] intValue
];
283 [sourceViewer setMarkedLine
:line
];
284 [sourceViewer scrollToLine
:line
];
286 // make sure the font stays Monaco
287 //[sourceViewer setFont:[NSFont fontWithName:@"Monaco" size:10.0]];
291 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
293 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
295 NSTreeNode
*node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
296 [expandedRegisters addObject
:[[node representedObject
] fullname
]];
300 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
302 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
304 [expandedRegisters removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullname
]];
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
];