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];
74 * Called right before the window closes so that we can tell the socket to close down
76 - (void)windowWillClose
:(NSNotification
*)notif
78 [[connection socket
] close
];
82 * Validates the menu items for the "Debugger" menu
84 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
86 SEL action
= [anItem action
];
88 if (action
== @selector(stepOut
:))
89 return ([connection isConnected
] && [stack count
] > 1);
90 else if (action
== @selector(stepIn
:) || action
== @selector(stepOver
:) || action
== @selector(run
:))
91 return [connection isConnected
];
93 return [[self window
] validateUserInterfaceItem
:anItem
];
97 * Resets all the displays to be empty
101 [registerController setContent
:nil];
102 [stackController2 setContent
:nil];
103 [[sourceViewer textView
] setString
:@
""];
107 * Sets the status and clears any error message
109 - (void)setStatus
:(NSString
*)aStatus
111 [errormsg setHidden
:YES
];
112 [statusmsg setStringValue
:aStatus
];
113 [[self window
] setTitle
:[NSString stringWithFormat
:@
"GDBp @ %@:%d/%@", [connection remoteHost
], [connection port
], [connection session
]]];
115 [stepInButton setEnabled
:NO
];
116 [stepOutButton setEnabled
:NO
];
117 [stepOverButton setEnabled
:NO
];
118 [runButton setEnabled
:NO
];
119 [reconnectButton setEnabled
:NO
];
121 if ([connection isConnected
])
123 if ([aStatus isEqualToString
:@
"Starting"])
125 [stepInButton setEnabled
:YES
];
126 [runButton setEnabled
:YES
];
131 [reconnectButton setEnabled
:YES
];
136 * Sets the status to be "Error" and then displays the error message
138 - (void)setError
:(NSString
*)anError
140 [errormsg setStringValue
:anError
];
141 [self setStatus
:@
"Error"];
142 [errormsg setHidden
:NO
];
146 * Sets the root node element of the stacktrace
148 - (void)setStack
:(NSArray
*)node
152 if ([stack count
] > 1)
154 [stepOutButton setEnabled
:YES
];
156 [stepInButton setEnabled
:YES
];
157 [stepOverButton setEnabled
:YES
];
158 [runButton setEnabled
:YES
];
160 [self updateSourceViewer
];
164 * Sets the stack root element so that the NSOutlineView can display it
166 - (void)setRegister
:(NSXMLDocument
*)elm
168 // XXX: Doing anything short of this will cause bindings to crash spectacularly for no reason whatsoever, and
169 // in seemingly arbitrary places. The class that crashes is _NSKeyValueObservationInfoCreateByRemoving.
170 // http://boredzo.org/blog/archives/2006-01-29/have-you-seen-this-crash says that this means nothing is
171 // being observed, but I doubt that he was using an NSOutlineView which seems to be one f!cking piece of
172 // sh!t when used with NSTreeController. http://www.cocoadev.com/index.pl?NSTreeControllerBugOrDeveloperError
173 // was the inspiration for this fix (below) but the author says that inserting does not work too well, but
174 // that's okay for us as we just need to replace the entire thing.
175 [registerController setContent
:nil];
176 [registerController setContent
:[[elm rootElement
] children
]];
178 for (int i
= 0; i
< [registerView numberOfRows
]; i
++)
180 NSTreeNode
*node
= [registerView itemAtRow
:i
];
181 if ([expandedRegisters containsObject
:[[node representedObject
] fullname
]])
183 [registerView expandItem
:node
];
189 * Forwards the message to run script execution to the connection
191 - (IBAction
)run
:(id)sender
197 * Tells the connection to ask the server to reconnect
199 - (IBAction
)reconnect
:(id)sender
201 [connection reconnect
];
205 * Forwards the message to "step in" to the connection
207 - (IBAction
)stepIn
:(id)sender
209 StackFrame
*frame
= [connection stepIn
];
210 if ([frame isShiftedFrame
:[stackController peek
]])
211 [stackController pop
];
212 [stackController push
:frame
];
213 NSLog(@
"stack = %@", stackController.stack
);
217 * Forwards the message to "step out" to the connection
219 - (IBAction
)stepOut
:(id)sender
221 StackFrame
*frame
= [connection stepOut
];
222 [stackController pop
];
223 [stackController push
:frame
];
224 NSLog(@
"stack = %@", stackController.stack
);
228 * Forwards the message to "step over" to the connection
230 - (IBAction
)stepOver
:(id)sender
232 StackFrame
*frame
= [connection stepOver
];
234 [stackController pop
];
235 [stackController push
:frame
];
237 NSLog(@
"stack = %@", stackController.stack
);
241 * NSTableView delegate method that informs the controller that the stack selection did change and that
242 * we should update the source viewer
244 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
246 [self updateSourceViewer
];
250 * Does the actual updating of the source viewer by reading in the file
252 - (void)updateSourceViewer
254 id selectedLevel
= [[stackController2 selection
] valueForKey
:@
"level"];
255 if (selectedLevel
== NSNoSelectionMarker
)
257 [[sourceViewer textView
] setString
:@
""];
260 int selection
= [selectedLevel intValue
];
262 if ([stack count
] < 1)
264 NSLog(@
"huh... we don't have a stack");
268 // get the filename and then set the text
269 NSString
*filename
= [[stack objectAtIndex
:selection
] valueForKey
:@
"filename"];
270 filename
= [[NSURL URLWithString
:filename
] path
];
271 if ([filename isEqualToString
:@
""])
276 [sourceViewer setFile
:filename
];
278 int line
= [[[stack objectAtIndex
:selection
] valueForKey
:@
"lineno"] intValue
];
279 [sourceViewer setMarkedLine
:line
];
280 [sourceViewer scrollToLine
:line
];
282 // make sure the font stays Monaco
283 //[sourceViewer setFont:[NSFont fontWithName:@"Monaco" size:10.0]];
287 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
289 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
291 NSTreeNode
*node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
292 [expandedRegisters addObject
:[[node representedObject
] fullname
]];
296 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
298 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
300 [expandedRegisters removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullname
]];
303 #pragma mark BSSourceView Delegate
306 * The gutter was clicked, which indicates that a breakpoint needs to be changed
308 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
310 BreakpointManager
*mngr
= [BreakpointManager sharedManager
];
312 if ([mngr hasBreakpointAt
:line inFile
:file
])
314 [mngr removeBreakpointAt
:line inFile
:file
];
318 Breakpoint
*bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
319 [mngr addBreakpoint
:bp
];
323 [[sourceViewer numberView
] setMarkers
:[NSSet setWithArray
:[mngr breakpointsForFile
:file
]]];
324 [[sourceViewer numberView
] setNeedsDisplay
:YES
];