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
;
25 - (void)updateStackViewer
;
28 @implementation DebuggerController
30 @synthesize connection
, sourceViewer
;
33 * Initializes the window controller and sets the connection using preference
38 if (self = [super initWithWindowNibName
:@
"Debugger"])
40 stackController
= [[StackController alloc
] init
];
42 NSUserDefaults
*defaults
= [NSUserDefaults standardUserDefaults
];
43 connection
= [[GDBpConnection alloc
] initWithPort
:[defaults integerForKey
:@
"Port"] session
:[defaults stringForKey
:@
"IDEKey"]];
44 expandedRegisters
= [[NSMutableSet alloc
] init
];
45 [[self window
] makeKeyAndOrderFront
:nil];
46 [[self window
] setDelegate
:self];
48 [[NSNotificationCenter defaultCenter
]
50 selector
:@selector(handleConnectionError
:)
51 name
:kErrorOccurredNotif
64 [expandedRegisters release
];
65 [stackController release
];
70 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
74 [[self window
] setExcludedFromWindowsMenu
:YES
];
75 [[self window
] setTitle
:[NSString stringWithFormat
:@
"GDBp @ %@:%d/%@", [connection remoteHost
], [connection port
], [connection session
]]];
76 [sourceViewer setDelegate
:self];
77 [stackArrayController setSortDescriptors
:[NSArray arrayWithObject
:[[[NSSortDescriptor alloc
] initWithKey
:@
"index" ascending
:YES
] autorelease
]]];
81 * Called right before the window closes so that we can tell the socket to close down
83 - (void)windowWillClose
:(NSNotification
*)notif
85 [[connection socket
] close
];
89 * Validates the menu items for the "Debugger" menu
91 - (BOOL)validateUserInterfaceItem
:(id <NSValidatedUserInterfaceItem
>)anItem
93 SEL action
= [anItem action
];
95 if (action
== @selector(stepOut
:))
96 return ([connection isConnected
] && [stackController.stack count
] > 1);
97 else if (action
== @selector(stepIn
:) || action
== @selector(stepOver
:) || action
== @selector(run
:))
98 return [connection isConnected
];
99 else if (action
== @selector(reconnect
:))
100 return ![connection isConnected
];
102 return [[self window
] validateUserInterfaceItem
:anItem
];
106 * Resets all the displays to be empty
108 - (void)resetDisplays
110 [registerController setContent
:nil];
111 [stackController.stack removeAllObjects
];
112 [[sourceViewer textView
] setString
:@
""];
116 * Sets the status to be "Error" and then displays the error message
118 - (void)setError
:(NSString
*)anError
120 [errormsg setStringValue
:anError
];
121 [errormsg setHidden
:NO
];
125 * Handles a GDBpConnection error
127 - (void)handleConnectionError
:(NSNotification
*)notif
129 [self setError
:[[notif userInfo
] valueForKey
:@
"NSString"]];
133 * Sets the stack root element so that the NSOutlineView can display it
135 - (void)setRegister
:(NSXMLDocument
*)elm
137 // XXX: Doing anything short of this will cause bindings to crash spectacularly for no reason whatsoever, and
138 // in seemingly arbitrary places. The class that crashes is _NSKeyValueObservationInfoCreateByRemoving.
139 // http://boredzo.org/blog/archives/2006-01-29/have-you-seen-this-crash says that this means nothing is
140 // being observed, but I doubt that he was using an NSOutlineView which seems to be one f!cking piece of
141 // sh!t when used with NSTreeController. http://www.cocoadev.com/index.pl?NSTreeControllerBugOrDeveloperError
142 // was the inspiration for this fix (below) but the author says that inserting does not work too well, but
143 // that's okay for us as we just need to replace the entire thing.
144 [registerController setContent
:nil];
145 [registerController setContent
:[[elm rootElement
] children
]];
147 for (int i
= 0; i
< [registerView numberOfRows
]; i
++)
149 NSTreeNode
*node
= [registerView itemAtRow
:i
];
150 if ([expandedRegisters containsObject
:[[node representedObject
] fullname
]])
152 [registerView expandItem
:node
];
158 * Forwards the message to run script execution to the connection
160 - (IBAction
)run
:(id)sender
166 * Tells the connection to ask the server to reconnect
168 - (IBAction
)reconnect
:(id)sender
170 [connection reconnect
];
171 [self resetDisplays
];
175 * Forwards the message to "step in" to the connection
177 - (IBAction
)stepIn
:(id)sender
179 StackFrame
*frame
= [connection stepIn
];
180 if ([frame isShiftedFrame
:[stackController peek
]])
181 [stackController pop
];
182 [stackController push
:frame
];
183 [self updateStackViewer
];
187 * Forwards the message to "step out" to the connection
189 - (IBAction
)stepOut
:(id)sender
191 StackFrame
*frame
= [connection stepOut
];
192 [stackController pop
]; // frame we were out of
193 [stackController pop
]; // frame we are returning to
194 [stackController push
:frame
];
195 [self updateStackViewer
];
199 * Forwards the message to "step over" to the connection
201 - (IBAction
)stepOver
:(id)sender
203 StackFrame
*frame
= [connection stepOver
];
204 [stackController pop
];
205 [stackController push
:frame
];
206 [self updateStackViewer
];
210 * NSTableView delegate method that informs the controller that the stack selection did change and that
211 * we should update the source viewer
213 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
215 [self updateSourceViewer
];
219 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
221 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
223 NSTreeNode
*node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
224 [expandedRegisters addObject
:[[node representedObject
] fullname
]];
228 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
230 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
232 [expandedRegisters removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullname
]];
238 * Does the actual updating of the source viewer by reading in the file
240 - (void)updateSourceViewer
242 id selection
= [stackArrayController selection
];
243 if ([selection valueForKey
:@
"filename"] == NSNoSelectionMarker
)
248 // get the filename and then set the text
249 NSString
*filename
= [selection valueForKey
:@
"filename"];
250 filename
= [[NSURL URLWithString
:filename
] path
];
251 if ([filename isEqualToString
:@
""])
256 if (![sourceViewer.file isEqualToString
:filename
])
257 [sourceViewer setFile
:filename
];
259 int line
= [[selection valueForKey
:@
"lineNumber"] intValue
];
260 [sourceViewer setMarkedLine
:line
];
261 [sourceViewer scrollToLine
:line
];
263 [[sourceViewer textView
] display
];
265 // make sure the font stays Monaco
266 //[sourceViewer setFont:[NSFont fontWithName:@"Monaco" size:10.0]];
270 * Does some house keeping to the stack viewer
272 - (void)updateStackViewer
274 [stackArrayController rearrangeObjects
];
275 [stackArrayController setSelectionIndex
:0];
278 #pragma mark BSSourceView Delegate
281 * The gutter was clicked, which indicates that a breakpoint needs to be changed
283 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
285 BreakpointManager
*mngr
= [BreakpointManager sharedManager
];
287 if ([mngr hasBreakpointAt
:line inFile
:file
])
289 [mngr removeBreakpointAt
:line inFile
:file
];
293 Breakpoint
*bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
294 [mngr addBreakpoint
:bp
];
298 [[sourceViewer numberView
] setMarkers
:[NSSet setWithArray
:[mngr breakpointsForFile
:file
]]];
299 [[sourceViewer numberView
] setNeedsDisplay
:YES
];