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 [sourceViewer setDelegate
:self];
71 * Called right before the window closes so that we can tell the socket to close down
73 - (void)windowWillClose
:(NSNotification
*)notif
75 [[connection socket
] close
];
79 * Validates the menu items for the "Debugger" menu
81 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
83 SEL action
= [anItem action
];
85 if (action
== @selector(stepOut
:))
86 return ([connection isConnected
] && [stack count
] > 1);
87 else if (action
== @selector(stepIn
:) || action
== @selector(stepOver
:) || action
== @selector(run
:))
88 return [connection isConnected
];
90 return [[self window
] validateUserInterfaceItem
:anItem
];
94 * Resets all the displays to be empty
98 [registerController setContent
:nil];
99 [stackController setContent
:nil];
100 [[sourceViewer textView
] setString
:@
""];
104 * Sets the status and clears any error message
106 - (void)setStatus
:(NSString
*)aStatus
108 [errormsg setHidden
:YES
];
109 [statusmsg setStringValue
:aStatus
];
110 [[self window
] setTitle
:[NSString stringWithFormat
:@
"GDBp @ %@:%d/%@", [connection remoteHost
], [connection port
], [connection session
]]];
112 [stepInButton setEnabled
:NO
];
113 [stepOutButton setEnabled
:NO
];
114 [stepOverButton setEnabled
:NO
];
115 [runButton setEnabled
:NO
];
116 [reconnectButton setEnabled
:NO
];
118 if ([connection isConnected
])
120 if ([aStatus isEqualToString
:@
"Starting"])
122 [stepInButton setEnabled
:YES
];
123 [runButton setEnabled
:YES
];
128 [reconnectButton setEnabled
:YES
];
133 * Sets the status to be "Error" and then displays the error message
135 - (void)setError
:(NSString
*)anError
137 [errormsg setStringValue
:anError
];
138 [self setStatus
:@
"Error"];
139 [errormsg setHidden
:NO
];
143 * Sets the root node element of the stacktrace
145 - (void)setStack
:(NSArray
*)node
149 if ([stack count
] > 1)
151 [stepOutButton setEnabled
:YES
];
153 [stepInButton setEnabled
:YES
];
154 [stepOverButton setEnabled
:YES
];
155 [runButton setEnabled
:YES
];
157 [self updateSourceViewer
];
161 * Sets the stack root element so that the NSOutlineView can display it
163 - (void)setRegister
:(NSXMLDocument
*)elm
165 // XXX: Doing anything short of this will cause bindings to crash spectacularly for no reason whatsoever, and
166 // in seemingly arbitrary places. The class that crashes is _NSKeyValueObservationInfoCreateByRemoving.
167 // http://boredzo.org/blog/archives/2006-01-29/have-you-seen-this-crash says that this means nothing is
168 // being observed, but I doubt that he was using an NSOutlineView which seems to be one f!cking piece of
169 // sh!t when used with NSTreeController. http://www.cocoadev.com/index.pl?NSTreeControllerBugOrDeveloperError
170 // was the inspiration for this fix (below) but the author says that inserting does not work too well, but
171 // that's okay for us as we just need to replace the entire thing.
172 [registerController setContent
:nil];
173 [registerController setContent
:[[elm rootElement
] children
]];
175 for (int i
= 0; i
< [registerView numberOfRows
]; i
++)
177 NSTreeNode
*node
= [registerView itemAtRow
:i
];
178 if ([expandedRegisters containsObject
:[[node representedObject
] fullname
]])
180 [registerView expandItem
:node
];
186 * Forwards the message to run script execution to the connection
188 - (IBAction
)run
:(id)sender
194 * Tells the connection to ask the server to reconnect
196 - (IBAction
)reconnect
:(id)sender
198 [connection reconnect
];
202 * Forwards the message to "step in" to the connection
204 - (IBAction
)stepIn
:(id)sender
210 * Forwards the message to "step out" to the connection
212 - (IBAction
)stepOut
:(id)sender
214 [connection stepOut
];
218 * Forwards the message to "step over" to the connection
220 - (IBAction
)stepOver
:(id)sender
222 [connection stepOver
];
226 * NSTableView delegate method that informs the controller that the stack selection did change and that
227 * we should update the source viewer
229 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
231 [self updateSourceViewer
];
235 * Does the actual updating of the source viewer by reading in the file
237 - (void)updateSourceViewer
239 id selectedLevel
= [[stackController selection
] valueForKey
:@
"level"];
240 if (selectedLevel
== NSNoSelectionMarker
)
242 [[sourceViewer textView
] setString
:@
""];
245 int selection
= [selectedLevel intValue
];
247 if ([stack count
] < 1)
249 NSLog(@
"huh... we don't have a stack");
253 // get the filename and then set the text
254 NSString
*filename
= [[stack objectAtIndex
:selection
] valueForKey
:@
"filename"];
255 filename
= [[NSURL URLWithString
:filename
] path
];
256 if ([filename isEqualToString
:@
""])
261 [sourceViewer setFile
:filename
];
263 int line
= [[[stack objectAtIndex
:selection
] valueForKey
:@
"lineno"] intValue
];
264 [sourceViewer setMarkedLine
:line
];
265 [sourceViewer scrollToLine
:line
];
267 // make sure the font stays Monaco
268 //[sourceViewer setFont:[NSFont fontWithName:@"Monaco" size:10.0]];
272 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
274 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
276 NSTreeNode
*node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
277 [expandedRegisters addObject
:[[node representedObject
] fullname
]];
281 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
283 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
285 [expandedRegisters removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullname
]];
288 #pragma mark BSSourceView Delegate
291 * The gutter was clicked, which indicates that a breakpoint needs to be changed
293 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
295 BreakpointManager
*mngr
= [BreakpointManager sharedManager
];
297 if ([mngr hasBreakpointAt
:line inFile
:file
])
299 [mngr removeBreakpointAt
:line inFile
:file
];
303 Breakpoint
*bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
304 [mngr addBreakpoint
:bp
];
308 [[sourceViewer numberView
] setMarkers
:[NSSet setWithArray
:[mngr breakpointsForFile
:file
]]];
309 [[sourceViewer numberView
] setNeedsDisplay
:YES
];