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 "DebuggerWindowController.h"
18 #import "DebuggerConnection.h"
19 #import "NSXMLElementAdditions.h"
20 #import "AppDelegate.h"
21 #import "BreakpointManager.h"
23 @interface DebuggerWindowController (Private
)
24 - (void)updateSourceViewer
;
27 @implementation DebuggerWindowController
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
= [[DebuggerConnection alloc
] initWithWindowController
:self
41 port
:[defaults integerForKey
:@
"Port"]
42 session
:[defaults stringForKey
:@
"IDEKey"]];
43 expandedRegisters
= [[NSMutableSet alloc
] init
];
44 [[self window
] makeKeyAndOrderFront
:nil];
55 [expandedRegisters release
];
60 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
64 [self setStatus
:@
"Connecting"];
65 [[self window
] setExcludedFromWindowsMenu
:YES
];
66 [[self window
] center
];
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 * Resets all the displays to be empty
83 [registerController setContent
:nil];
84 [stackController setContent
:nil];
85 [[sourceViewer textView
] setString
:@
""];
89 * Sets the status and clears any error message
91 - (void)setStatus
:(NSString
*)aStatus
93 [errormsg setHidden
:YES
];
94 [statusmsg setStringValue
:aStatus
];
95 [[self window
] setTitle
:[NSString stringWithFormat
:@
"GDBp @ %@:%d/%@", [connection remoteHost
], [connection port
], [connection session
]]];
97 [stepInButton setEnabled
:NO
];
98 [stepOutButton setEnabled
:NO
];
99 [stepOverButton setEnabled
:NO
];
100 [runButton setEnabled
:NO
];
101 [reconnectButton setEnabled
:NO
];
103 if ([connection isConnected
])
105 if ([aStatus isEqualToString
:@
"Starting"])
107 [stepInButton setEnabled
:YES
];
108 [runButton setEnabled
:YES
];
113 [reconnectButton setEnabled
:YES
];
118 * Sets the status to be "Error" and then displays the error message
120 - (void)setError
:(NSString
*)anError
122 [errormsg setStringValue
:anError
];
123 [self setStatus
:@
"Error"];
124 [errormsg setHidden
:NO
];
128 * Sets the root node element of the stacktrace
130 - (void)setStack
:(NSArray
*)node
134 if ([stack count
] > 1)
136 [stepOutButton setEnabled
:YES
];
138 [stepInButton setEnabled
:YES
];
139 [stepOverButton setEnabled
:YES
];
140 [runButton setEnabled
:YES
];
142 [self updateSourceViewer
];
146 * Sets the stack root element so that the NSOutlineView can display it
148 - (void)setRegister
:(NSXMLDocument
*)elm
150 // XXX: Doing anything short of this will cause bindings to crash spectacularly for no reason whatsoever, and
151 // in seemingly arbitrary places. The class that crashes is _NSKeyValueObservationInfoCreateByRemoving.
152 // http://boredzo.org/blog/archives/2006-01-29/have-you-seen-this-crash says that this means nothing is
153 // being observed, but I doubt that he was using an NSOutlineView which seems to be one f!cking piece of
154 // sh!t when used with NSTreeController. http://www.cocoadev.com/index.pl?NSTreeControllerBugOrDeveloperError
155 // was the inspiration for this fix (below) but the author says that inserting does not work too well, but
156 // that's okay for us as we just need to replace the entire thing.
157 [registerController setContent
:nil];
158 [registerController setContent
:[[elm rootElement
] children
]];
160 for (int i
= 0; i
< [registerView numberOfRows
]; i
++)
162 NSTreeNode
*node
= [registerView itemAtRow
:i
];
163 if ([expandedRegisters containsObject
:[[node representedObject
] fullname
]])
165 [registerView expandItem
:node
];
171 * Forwards the message to run script execution to the connection
173 - (IBAction
)run
:(id)sender
179 * Tells the connection to ask the server to reconnect
181 - (IBAction
)reconnect
:(id)sender
183 [connection reconnect
];
187 * Forwards the message to "step in" to the connection
189 - (IBAction
)stepIn
:(id)sender
195 * Forwards the message to "step out" to the connection
197 - (IBAction
)stepOut
:(id)sender
199 [connection stepOut
];
203 * Forwards the message to "step over" to the connection
205 - (IBAction
)stepOver
:(id)sender
207 [connection stepOver
];
211 * NSTableView delegate method that informs the controller that the stack selection did change and that
212 * we should update the source viewer
214 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
216 [self updateSourceViewer
];
220 * Does the actual updating of the source viewer by reading in the file
222 - (void)updateSourceViewer
224 id selectedLevel
= [[stackController selection
] valueForKey
:@
"level"];
225 if (selectedLevel
== NSNoSelectionMarker
)
227 [[sourceViewer textView
] setString
:@
""];
230 int selection
= [selectedLevel intValue
];
232 if ([stack count
] < 1)
234 NSLog(@
"huh... we don't have a stack");
238 // get the filename and then set the text
239 NSString
*filename
= [[stack objectAtIndex
:selection
] valueForKey
:@
"filename"];
240 filename
= [[NSURL URLWithString
:filename
] path
];
241 if ([filename isEqualToString
:@
""])
246 [sourceViewer setFile
:filename
];
248 int line
= [[[stack objectAtIndex
:selection
] valueForKey
:@
"lineno"] intValue
];
249 [sourceViewer setMarkedLine
:line
];
250 [sourceViewer scrollToLine
:line
];
252 // make sure the font stays Monaco
253 //[sourceViewer setFont:[NSFont fontWithName:@"Monaco" size:10.0]];
257 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
259 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
261 NSTreeNode
*node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
262 [expandedRegisters addObject
:[[node representedObject
] fullname
]];
266 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
268 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
270 [expandedRegisters removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] fullname
]];
273 #pragma mark BSSourceView Delegate
276 * The gutter was clicked, which indicates that a breakpoint needs to be changed
278 - (void)gutterClickedAtLine
:(int)line forFile
:(NSString
*)file
280 BreakpointManager
*mngr
= [BreakpointManager sharedManager
];
282 if ([mngr hasBreakpointAt
:line inFile
:file
])
284 [mngr removeBreakpointAt
:line inFile
:file
];
288 Breakpoint
*bp
= [[Breakpoint alloc
] initWithLine
:line inFile
:file
];
289 [mngr addBreakpoint
:bp
];
292 [[sourceViewer numberView
] setMarkers
:[NSSet setWithArray
:[mngr breakpointsForFile
:file
]]];
293 [[sourceViewer numberView
] setNeedsDisplay
:YES
];