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