Casting to remove a warning
[macgdbp.git] / Source / DebuggerConnection.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2002 - 2007, Blue Static <http://www.bluestatic.org>
4 *
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.
8 *
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.
12 *
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
15 */
16
17 #import "DebuggerConnection.h"
18
19 @interface DebuggerConnection (Private)
20
21 - (NSString *)createCommand: (NSString *)cmd;
22
23 @end
24
25 @implementation DebuggerConnection
26
27 /**
28 * Creates a new DebuggerConnection and initializes the socket from the given connection
29 * paramters.
30 */
31 - (id)initWithPort: (int)port session: (NSString *)session
32 {
33 if (self = [super init])
34 {
35 _port = port;
36 _session = [session retain];
37 _connected = NO;
38
39 _windowController = [[DebuggerWindowController alloc] initWithConnection: self];
40 [[_windowController window] makeKeyAndOrderFront: self];
41
42 // now that we have our host information, open the socket
43 _socket = [[SocketWrapper alloc] initWithPort: port];
44 [_socket setDelegate: self];
45 [_windowController setStatus: @"Connecting"];
46 [_socket connect];
47
48 // clean up after ourselves
49 [[NSNotificationCenter defaultCenter] addObserver: self
50 selector: @selector(applicationWillTerminate:)
51 name: NSApplicationWillTerminateNotification
52 object: NSApp];
53 }
54 return self;
55 }
56
57 /**
58 * Release ourselves when we're about to die
59 */
60 - (void)applicationWillTerminate: (NSNotification *)notif
61 {
62 [self release];
63 }
64
65 /**
66 * Releases all of the object's data members and closes the streams
67 */
68 - (void)dealloc
69 {
70 [_session release];
71 [_socket release];
72
73 [super dealloc];
74 }
75
76 /**
77 * Gets the port number
78 */
79 - (int)port
80 {
81 return _port;
82 }
83
84 /**
85 * Gets the session name
86 */
87 - (NSString *)session
88 {
89 return _session;
90 }
91
92 /**
93 * Returns the name of the remote host
94 */
95 - (NSString *)remoteHost
96 {
97 if (!_connected)
98 {
99 return @"(DISCONNECTED)";
100 }
101 return [_socket remoteHost];
102 }
103
104 /**
105 * Returns whether or not we have an active connection
106 */
107 - (BOOL)isConnected
108 {
109 return _connected;
110 }
111
112 /**
113 * SocketWrapper delegate method that is called whenever new data is received
114 */
115 - (void)dataReceived: (NSData *)response deliverTo: (SEL)selector
116 {
117 // if the caller of [_socket receive:] specified a deliverTo, just forward the message to them
118 if (selector != nil)
119 {
120 [self performSelector: selector withObject: response];
121 }
122 }
123
124 /**
125 * SocketWrapper delegate method that is called after data is sent. This really
126 * isn't useful for much.
127 */
128 - (void)dataSent: (NSString *)data
129 {
130 NSLog(@"send = %@", data);
131 }
132
133 /**
134 * Called by SocketWrapper after the connection is successful. This immediately calls
135 * -[SocketWrapper receive] to clear the way for communication
136 */
137 - (void)socketDidAccept
138 {
139 _connected = YES;
140 [_socket receive: @selector(handshake:)];
141 }
142
143 /**
144 * Receives errors from the SocketWrapper and updates the display
145 */
146 - (void)errorEncountered: (NSError *)error
147 {
148 [_windowController setError: [error domain]];
149 }
150
151 /**
152 * The initial packet handshake. This allows us to set things like the title of the window
153 * and glean information about hte server we are debugging
154 */
155 - (void)handshake: (NSData *)packet
156 {
157 [self refreshStatus];
158 }
159
160 /**
161 * Handler used by dataReceived:deliverTo: for anytime the status command is issued. It sets
162 * the window controller's status text
163 */
164 - (void)updateStatus: (NSData *)packet
165 {
166 NSXMLDocument *doc = [[NSXMLDocument alloc] initWithData: packet options: NSXMLDocumentTidyXML error: nil];
167 [_windowController setStatus: [[[[doc rootElement] attributeForName: @"status"] stringValue] capitalizedString]];
168 [doc release];
169 }
170
171 /**
172 * Tells the debugger to continue running the script
173 */
174 - (void)run
175 {
176 [_socket send: [self createCommand: @"run"]];
177 [self refreshStatus];
178 }
179
180 /**
181 * Method that runs tells the debugger to give us its status. This will call _updateStatus
182 * and will update the status text on the window
183 */
184 - (void)refreshStatus
185 {
186 [_socket send: [self createCommand: @"status"]];
187 [_socket receive: @selector(updateStatus:)];
188 }
189
190 /**
191 * Tells the debugger to step into the current command.
192 */
193 - (void)stepIn
194 {
195 [_socket send: [self createCommand: @"step_into"]];
196 [_socket receive: nil];
197 [self refreshStatus];
198 [self updateStackTraceAndRegisters];
199 }
200
201 /**
202 * Tells the debugger to step out of the current context
203 */
204 - (void)stepOut
205 {
206 [_socket send: [self createCommand: @"step_out"]];
207 [_socket receive: nil];
208 [self refreshStatus];
209 [self updateStackTraceAndRegisters];
210 }
211
212 /**
213 * Tells the debugger to step over the current function
214 */
215 - (void)stepOver
216 {
217 [_socket send: [self createCommand: @"step_over"]];
218 [_socket receive: nil];
219 [self refreshStatus];
220 [self updateStackTraceAndRegisters];
221 }
222
223 /**
224 * This function queries the debug server for the current stacktrace and all the registers on
225 * level one. If a user then tries to expand past level one... TOOD: HOLY CRAP WHAT DO WE DO PAST LEVEL 1?
226 */
227 - (void)updateStackTraceAndRegisters
228 {
229 [_socket send: [self createCommand: @"stack_get"]];
230 [_socket receive: @selector(stackReceived:)];
231
232 [_socket send: [self createCommand: @"context_get"]];
233 [_socket receive: @selector(registerReceived:)];
234 }
235
236 /**
237 * Called by the dataReceived delivery delegate. This updates the window controller's data
238 * for the stack trace
239 */
240 - (void)stackReceived: (NSData *)packet
241 {
242 NSXMLDocument *doc = [[NSXMLDocument alloc] initWithData: packet options: NSXMLDocumentTidyXML error: nil];
243 NSArray *children = [[doc rootElement] children];
244 NSMutableArray *stack = [NSMutableArray array];
245 NSMutableDictionary *dict = [NSMutableDictionary dictionary];
246 for (int i = 0; i < [children count]; i++)
247 {
248 NSArray *attrs = [[children objectAtIndex: i] attributes];
249 for (int j = 0; j < [attrs count]; j++)
250 {
251 [dict setValue: [[attrs objectAtIndex: j] stringValue] forKey: [[attrs objectAtIndex: j] name]];
252 }
253 [stack addObject: dict];
254 dict = [NSMutableDictionary dictionary];
255 }
256 [_windowController setStack: stack];
257 [doc release];
258 }
259
260 /**
261 * Called when we have a new register to display
262 */
263 - (void)registerReceived: (NSData *)packet
264 {
265 NSXMLDocument *doc = [[NSXMLDocument alloc] initWithData: packet options: NSXMLDocumentTidyXML error: nil];
266 [_windowController setRegister: [doc rootElement]];
267 [doc release];
268 }
269
270 /**
271 * Tells the debugger engine to get a specifc property. This also takes in the NSXMLElement
272 * that requested it so that the child can be attached in the delivery.
273 */
274 - (void)getProperty: (NSString *)property forElement: (NSXMLElement *)elm
275 {
276 [_socket send: [self createCommand: [NSString stringWithFormat: @"property_get -n \"%@\"", property]]];
277 _depthFetchElement = elm;
278 [_socket receive: @selector(propertyReceived:)];
279 }
280
281 /**
282 * Called when a property is received. This then adds the result as children to the passed object
283 */
284 - (void)propertyReceived: (NSData *)packet
285 {
286 NSXMLDocument *doc = [[NSXMLDocument alloc] initWithData: packet options: NSXMLDocumentTidyXML error: nil];
287 /*
288 <response>
289 <property> <!-- this is the one we requested -->
290 <property ... /> <!-- these are what we want -->
291 </property>
292 </repsonse>
293 */
294
295 // we now have to detach all the children so we can insert them into another document
296 NSXMLElement *parent = (NSXMLElement *)[[doc rootElement] childAtIndex: 0];
297 NSArray *children = [parent children];
298 [parent setChildren: nil];
299 [_windowController addChildren: children toNode: _depthFetchElement];
300 _depthFetchElement = nil;
301 [doc release];
302 }
303
304 /**
305 * Helper method to create a string command with the -i <session> automatically tacked on
306 */
307 - (NSString *)createCommand: (NSString *)cmd
308 {
309 return [NSString stringWithFormat: @"%@ -i %@", cmd, _session];
310 }
311
312 @end