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