Merge branch 'master' into no-gc
[macgdbp.git] / Source / DebuggerConnection.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2008, 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 - (NSXMLDocument *)processData:(NSString *)data;
24
25 @end
26
27 @implementation DebuggerConnection
28
29 @synthesize socket, windowController;
30
31 /**
32 * Creates a new DebuggerConnection and initializes the socket from the given connection
33 * paramters.
34 */
35 - (id)initWithWindowController:(DebuggerWindowController *)wc port:(int)aPort session:(NSString *)aSession;
36 {
37 if (self = [super init])
38 {
39 port = aPort;
40 session = [aSession retain];
41 connected = NO;
42
43 windowController = [wc retain];
44
45 // now that we have our host information, open the socket
46 socket = [[SocketWrapper alloc] initWithConnection:self];
47 [socket setDelegate:self];
48 [socket connect];
49 }
50 return self;
51 }
52
53 /**
54 * Deallocates the object
55 */
56 - (void)dealloc
57 {
58 [socket release];
59 [session release];
60 [windowController release];
61 [super dealloc];
62 }
63
64 /**
65 * Gets the port number
66 */
67 - (int)port
68 {
69 return port;
70 }
71
72 /**
73 * Gets the session name
74 */
75 - (NSString *)session
76 {
77 return session;
78 }
79
80 /**
81 * Returns the name of the remote host
82 */
83 - (NSString *)remoteHost
84 {
85 if (!connected)
86 {
87 return @"(DISCONNECTED)";
88 }
89 return [socket remoteHost];
90 }
91
92 /**
93 * Returns whether or not we have an active connection
94 */
95 - (BOOL)isConnected
96 {
97 return connected;
98 }
99
100 /**
101 * Called by SocketWrapper after the connection is successful. This immediately calls
102 * -[SocketWrapper receive] to clear the way for communication, though the information
103 * could be useful server information that we don't use right now.
104 */
105 - (void)socketDidAccept:(id)obj
106 {
107 connected = YES;
108 [socket receive];
109 [self refreshStatus];
110 }
111
112 /**
113 * Receives errors from the SocketWrapper and updates the display
114 */
115 - (void)errorEncountered:(NSString *)error
116 {
117 [windowController setError:error];
118 }
119
120 /**
121 * Reestablishes communication with the remote debugger so that a new connection doesn't have to be
122 * created every time you want to debug a page
123 */
124 - (void)reconnect
125 {
126 [socket close];
127 [windowController setStatus:@"Connecting"];
128 [windowController resetDisplays];
129 [socket connect];
130 }
131
132 /**
133 * Tells the debugger to continue running the script
134 */
135 - (void)run
136 {
137 [socket send:[self createCommand:@"run"]];
138 [socket receive];
139 [self refreshStatus];
140 }
141
142 /**
143 * Method that runs tells the debugger to give us its status and will update the status text on the window
144 */
145 - (void)refreshStatus
146 {
147 [socket send:[self createCommand:@"status"]];
148
149 NSXMLDocument *doc = [self processData:[socket receive]];
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 else if ([status isEqualToString:@"stopped"])
158 {
159 connected = NO;
160 [socket close];
161 [windowController setStatus:[status capitalizedString]];
162 }
163 }
164
165 /**
166 * Tells the debugger to step into the current command.
167 */
168 - (void)stepIn
169 {
170 [socket send:[self createCommand:@"step_into"]];
171 [socket receive];
172 [self refreshStatus];
173 }
174
175 /**
176 * Tells the debugger to step out of the current context
177 */
178 - (void)stepOut
179 {
180 [socket send:[self createCommand:@"step_out"]];
181 [socket receive];
182 [self refreshStatus];
183 }
184
185 /**
186 * Tells the debugger to step over the current function
187 */
188 - (void)stepOver
189 {
190 [socket send:[self createCommand:@"step_over"]];
191 [socket receive];
192 [self refreshStatus];
193 }
194
195 /**
196 * This function queries the debug server for the current stacktrace and all the registers on
197 * level one. If a user then tries to expand past level one... TOOD: HOLY CRAP WHAT DO WE DO PAST LEVEL 1?
198 */
199 - (void)updateStackTraceAndRegisters
200 {
201 // do the stack
202 [socket send:[self createCommand:@"stack_get"]];
203 NSXMLDocument *doc = [self processData:[socket receive]];
204 NSArray *children = [[doc rootElement] children];
205 NSMutableArray *stack = [NSMutableArray array];
206 NSMutableDictionary *dict = [NSMutableDictionary dictionary];
207 for (int i = 0; i < [children count]; i++)
208 {
209 NSArray *attrs = [[children objectAtIndex:i] attributes];
210 for (int j = 0; j < [attrs count]; j++)
211 {
212 [dict setValue:[[attrs objectAtIndex:j] stringValue] forKey:[[attrs objectAtIndex:j] name]];
213 }
214 [stack addObject:dict];
215 dict = [NSMutableDictionary dictionary];
216 }
217 [windowController setStack:stack];
218
219 // do the registers
220 [socket send:[self createCommand:@"context_get"]];
221 [windowController setRegister:[self processData:[socket receive]]];
222 }
223
224 /**
225 * Tells the debugger engine to get a specifc property. This also takes in the NSXMLElement
226 * that requested it so that the child can be attached.
227 */
228 - (void)getProperty:(NSString *)property forNode:(NSTreeNode *)node
229 {
230 [socket send:[self createCommand:[NSString stringWithFormat:@"property_get -n \"%@\"", property]]];
231
232 NSXMLDocument *doc = [self processData:[socket receive]];
233
234 /*
235 <response>
236 <property> <!-- this is the one we requested -->
237 <property ... /> <!-- these are what we want -->
238 </property>
239 </repsonse>
240 */
241
242 // we now have to detach all the children so we can insert them into another document
243 NSXMLElement *parent = (NSXMLElement *)[[doc rootElement] childAtIndex:0];
244 NSArray *children = [parent children];
245 [parent setChildren:nil];
246 [windowController addChildren:children toNode:node];
247 }
248
249 #pragma mark Breakpoints
250
251 /**
252 * Send an add breakpoint command
253 */
254 - (void)addBreakpoint:(Breakpoint *)bp
255 {
256 NSString *cmd = [self createCommand:[NSString stringWithFormat:@"breakpoint_set -t line -f %@ -n %i", [bp file], [bp line]]];
257 [socket send:cmd];
258 NSXMLDocument *info = [self processData:[socket receive]];
259 [bp setDebuggerId:[[[[info rootElement] attributeForName:@"id"] stringValue] intValue]];
260 }
261
262 /**
263 * Removes a breakpoint
264 */
265 - (void)removeBreakpoint:(Breakpoint *)bp
266 {
267 [socket send:[self createCommand:[NSString stringWithFormat:@"breakpoint_remove -d %i", [bp debuggerId]]]];
268 [socket receive];
269 }
270
271 #pragma mark Private
272
273 /**
274 * Helper method to create a string command with the -i <session> automatically tacked on
275 */
276 - (NSString *)createCommand:(NSString *)cmd
277 {
278 return [NSString stringWithFormat:@"%@ -i %@", cmd, session];
279 }
280
281 /**
282 * Helper function to parse the NSData into an NSXMLDocument
283 */
284 - (NSXMLDocument *)processData:(NSString *)data
285 {
286 NSError *parseError = nil;
287 NSXMLDocument *doc = [[NSXMLDocument alloc] initWithXMLString:data options:0 error:&parseError];
288 if (parseError)
289 {
290 NSLog(@"Could not parse XML? --- %@", parseError);
291 NSLog(@"Error UserInfo: %@", [parseError userInfo]);
292 NSLog(@"This is the XML Document: %@", data);
293 return nil;
294 }
295
296 // check and see if there's an error
297 NSArray *error = [[doc rootElement] elementsForName:@"error"];
298 if ([error count] > 0)
299 {
300 [windowController setError:[[[[error objectAtIndex:0] children] objectAtIndex:0] stringValue]];
301 return nil;
302 }
303
304 return doc;
305 }
306
307 @end