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 NSUserDefaults
*defaults
= [NSUserDefaults standardUserDefaults
];
40 connection
= [[GDBpConnection alloc
] initWithWindowController
:self
41 port
:[defaults integerForKey
:@
"Port"]
42 session
:[defaults stringForKey
:@
"IDEKey"]];
43 expandedRegisters
= [[NSMutableSet alloc
] init
];
44 [[self window
] makeKeyAndOrderFront
:nil];
45 [[self window
] setDelegate
:self];
56 [expandedRegisters release
];
61 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
65 [self setStatus
:@
"Connecting"];
66 [[self window
] setExcludedFromWindowsMenu
:YES
];
67 [[self window
] center
];
68 [sourceViewer setDelegate
:self];
72 * Called right before the window closes so that we can tell the socket to close down
74 - (void)windowWillClose
:(NSNotification
*)notif
76 [[connection socket
] close
];
80 * Validates the menu items for the "Debugger" menu
82 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
84 SEL action
= [anItem action
];
86 if (action
== @selector(stepOut
:))
87 return ([connection isConnected
] && [stack count
] > 1);
88 else if (action
== @selector(stepIn
:) || action
== @selector(stepOver
:) || action
== @selector(run
:))
89 return [connection isConnected
];
91 return [[self window
] validateUserInterfaceItem
:anItem
];
95 * Resets all the displays to be empty
99 [registerController setContent
:nil];
100 [stackController setContent
:nil];
101 [[sourceViewer textView
] setString
:@
""];
105 * Sets the status and clears any error message
107 - (void)setStatus
:(NSString
*)aStatus
109 [errormsg setHidden
:YES
];
110 [statusmsg setStringValue
:aStatus
];
111 [[self window
] setTitle
:[NSString stringWithFormat
:@
"GDBp @ %@:%d/%@", [connection remoteHost
], [connection port
], [connection session
]]];
113 [stepInButton setEnabled
:NO
];
114 [stepOutButton setEnabled
:NO
];
115 [stepOverButton setEnabled
:NO
];
116 [runButton setEnabled
:NO
];
117 [reconnectButton setEnabled
:NO
];
119 if ([connection isConnected
])
121 if ([aStatus isEqualToString
:@
"Starting"])
123 [stepInButton setEnabled
:YES
];
124 [runButton setEnabled
:YES
];
129 [reconnectButton setEnabled
:YES
];
134 * Sets the status to be "Error" and then displays the error message
136 - (void)setError
:(NSString
*)anError
138 [errormsg setStringValue
:anError
];
139 [self setStatus
:@
"Error"];
140 [errormsg setHidden
:NO
];
144 * Sets the root node element of the stacktrace
146 - (void)setStack
:(NSArray
*)node
150 if ([stack count
] > 1)
152 [stepOutButton setEnabled
:YES
];
154 [stepInButton setEnabled
:YES
];
155 [stepOverButton setEnabled
:YES
];
156 [runButton setEnabled
:YES
];
158 [self updateSourceViewer
];
162 * Sets the stack root element so that the NSOutlineView can display it
164 - (void)setRegister
:(NSXMLDocument
*)elm
166 // XXX: Doing anything short of this will cause bindings to crash spectacularly for no reason whatsoever, and
167 // in seemingly arbitrary places. The class that crashes is _NSKeyValueObservationInfoCreateByRemoving.
168 // http://boredzo.org/blog/archives/2006-01-29/have-you-seen-this-crash says that this means nothing is
169 // being observed, but I doubt that he was using an NSOutlineView which seems to be one f!cking piece of
170 // sh!t when used with NSTreeController. http://www.cocoadev.com/index.pl?NSTreeControllerBugOrDeveloperError
171 // was the inspiration for this fix (below) but the author says that inserting does not work too well, but
172 // that's okay for us as we just need to replace the entire thing.
173 [registerController setContent
:nil];
174 [registerController setContent
:[[elm rootElement
] children
]];
176 for (int i
= 0; i
< [registerView numberOfRows
]; i
++)
178 NSTreeNode
*node
= [registerView itemAtRow
:i
];
179 if ([expandedRegisters containsObject
:[[node representedObject
] fullname
]])
181 [registerView expandItem
:node
];
187 * Forwards the message to run script execution to the connection
189 - (IBAction
)run
:(id)sender
195 * Tells the connection to ask the server to reconnect
197 - (IBAction
)reconnect
:(id)sender
199 [connection reconnect
];
203 * Forwards the message to "step in" to the connection
205 - (IBAction
)stepIn
:(id)sender
211 * Forwards the message to "step out" to the connection
213 - (IBAction
)stepOut
:(id)sender
215 [connection stepOut
];
219 * Forwards the message to "step over" to the connection
221 - (IBAction
)stepOver
:(id)sender
223 [connection stepOver
];
227 * NSTableView delegate method that informs the controller that the stack selection did change and that
228 * we should update the source viewer
230 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
232 [self updateSourceViewer
];
236 * Does the actual updating of the source viewer by reading in the file
238 - (void)updateSourceViewer
240 id selectedLevel
= [[stackController selection
] valueForKey
:@
"level"];
241 if (selectedLevel
== NSNoSelectionMarker
)
243 [[sourceViewer textView
] setString
:@
""];
246 int selection
= [selectedLevel intValue
];
248 if ([stack count
] < 1)
250 NSLog(@
"huh... we don't have a stack");
254 // get the filename and then set the text
255 NSString
*filename
= [[stack objectAtIndex
:selection
] valueForKey
:@
"filename"];
256 filename
= [[NSURL URLWithString
:filename
] path
];
257 if ([filename isEqualToString
:@
""])
262 [sourceViewer setFile
:filename
];
264 int line
= [[[stack objectAtIndex
:selection
] valueForKey
:@
"lineno"] intValue
];
265 [sourceViewer setMarkedLine
:line
];
266 [sourceViewer scrollToLine
:line
];
268 // make sure the font stays Monaco
269 //[sourceViewer setFont:[NSFont fontWithName:@"Monaco" size:10.0]];
273 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
275 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
277 NSTreeNode
*node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
278 [expandedRegisters addObject
:[[node representedObject
] fullname
]];
282 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
284 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
286 [expandedRegisters removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullname
]];
289 #pragma mark BSSourceView Delegate
292 * The gutter was clicked, which indicates that a breakpoint needs to be changed
294 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
296 BreakpointManager
*mngr
= [BreakpointManager sharedManager
];
298 if ([mngr hasBreakpointAt
:line inFile
:file
])
300 [mngr removeBreakpointAt
:line inFile
:file
];
304 Breakpoint
*bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
305 [mngr addBreakpoint
:bp
];
309 [[sourceViewer numberView
] setMarkers
:[NSSet setWithArray
:[mngr breakpointsForFile
:file
]]];
310 [[sourceViewer numberView
] setNeedsDisplay
:YES
];