]> src.bluestatic.org Git - macgdbp.git/blob - Source/GDBpConnection.m
Start using our new StackFrame and StackController classes as struts
[macgdbp.git] / Source / GDBpConnection.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 "GDBpConnection.h"
18 #import "AppDelegate.h"
19
20 @interface GDBpConnection (Private)
21 - (NSString *)createCommand:(NSString *)cmd;
22 - (NSXMLDocument *)processData:(NSString *)data;
23 - (StackFrame *)createStackFrame;
24 @end
25
26 @implementation GDBpConnection
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:(DebuggerController *)wc port:(int)aPort session:(NSString *)aSession;
35 {
36 if (self = [super init])
37 {
38 port = aPort;
39 session = [aSession retain];
40 connected = NO;
41
42 windowController = [wc retain];
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 [[BreakpointManager sharedManager] setConnection:self];
50 }
51 return self;
52 }
53
54 /**
55 * Deallocates the object
56 */
57 - (void)dealloc
58 {
59 [socket release];
60 [session release];
61 [windowController release];
62 [super dealloc];
63 }
64
65 /**
66 * Gets the port number
67 */
68 - (int)port
69 {
70 return port;
71 }
72
73 /**
74 * Gets the session name
75 */
76 - (NSString *)session
77 {
78 return session;
79 }
80
81 /**
82 * Returns the name of the remote host
83 */
84 - (NSString *)remoteHost
85 {
86 if (!connected)
87 {
88 return @"(DISCONNECTED)";
89 }
90 return [socket remoteHost];
91 }
92
93 /**
94 * Returns whether or not we have an active connection
95 */
96 - (BOOL)isConnected
97 {
98 return connected;
99 }
100
101 /**
102 * Called by SocketWrapper after the connection is successful. This immediately calls
103 * -[SocketWrapper receive] to clear the way for communication, though the information
104 * could be useful server information that we don't use right now.
105 */
106 - (void)socketDidAccept:(id)obj
107 {
108 connected = YES;
109 [socket receive];
110 [self refreshStatus];
111
112 // register any breakpoints that exist offline
113 for (Breakpoint *bp in [[BreakpointManager sharedManager] breakpoints])
114 {
115 [self addBreakpoint:bp];
116 }
117 }
118
119 /**
120 * Receives errors from the SocketWrapper and updates the display
121 */
122 - (void)errorEncountered:(NSString *)error
123 {
124 [windowController setError:error];
125 }
126
127 /**
128 * Reestablishes communication with the remote debugger so that a new connection doesn't have to be
129 * created every time you want to debug a page
130 */
131 - (void)reconnect
132 {
133 [socket close];
134 [windowController setStatus:@"Connecting"];
135 [windowController resetDisplays];
136 [socket connect];
137 }
138
139 /**
140 * Tells the debugger to continue running the script
141 */
142 - (void)run
143 {
144 [socket send:[self createCommand:@"run"]];
145 [socket receive];
146 [self refreshStatus];
147 }
148
149 /**
150 * Method that runs tells the debugger to give us its status and will update the status text on the window
151 */
152 - (void)refreshStatus
153 {
154 [socket send:[self createCommand:@"status"]];
155
156 NSXMLDocument *doc = [self processData:[socket receive]];
157 NSString *status = [[[doc rootElement] attributeForName:@"status"] stringValue];
158 [windowController setStatus:[status capitalizedString]];
159
160 if ([status isEqualToString:@"break"])
161 {
162 [self updateStackTraceAndRegisters];
163 }
164 else if ([status isEqualToString:@"stopped"] || [status isEqualToString:@"stopping"])
165 {
166 connected = NO;
167 [socket close];
168 [windowController setStatus:@"Stopped"];
169 }
170 }
171
172 /**
173 * Tells the debugger to step into the current command.
174 */
175 - (StackFrame *)stepIn
176 {
177 [socket send:[self createCommand:@"step_into"]];
178 [socket receive];
179
180 StackFrame *frame = [self createStackFrame];
181 [self refreshStatus];
182
183 return frame;
184 }
185
186 /**
187 * Tells the debugger to step out of the current context
188 */
189 - (StackFrame *)stepOut
190 {
191 [socket send:[self createCommand:@"step_out"]];
192 [socket receive];
193
194 StackFrame *frame = [self createStackFrame];
195 [self refreshStatus];
196
197 return frame;
198 }
199
200 /**
201 * Tells the debugger to step over the current function
202 */
203 - (StackFrame *)stepOver
204 {
205 [socket send:[self createCommand:@"step_over"]];
206 [socket receive];
207
208 StackFrame *frame = [self createStackFrame];
209 [self refreshStatus];
210
211 return frame;
212 }
213
214 /**
215 * This function queries the debug server for the current stacktrace and all the registers on
216 * level one. If a user then tries to expand past level one... TOOD: HOLY CRAP WHAT DO WE DO PAST LEVEL 1?
217 */
218 - (void)updateStackTraceAndRegisters
219 {
220 // do the stack
221 [socket send:[self createCommand:@"stack_get"]];
222 NSXMLDocument *doc = [self processData:[socket receive]];
223 NSArray *children = [[doc rootElement] children];
224 NSMutableArray *stack = [NSMutableArray array];
225 NSMutableDictionary *dict = [NSMutableDictionary dictionary];
226 for (int i = 0; i < [children count]; i++)
227 {
228 NSArray *attrs = [[children objectAtIndex:i] attributes];
229 for (int j = 0; j < [attrs count]; j++)
230 {
231 [dict setValue:[[attrs objectAtIndex:j] stringValue] forKey:[[attrs objectAtIndex:j] name]];
232 }
233 [stack addObject:dict];
234 dict = [NSMutableDictionary dictionary];
235 }
236 [windowController setStack:stack];
237
238 // do the registers
239 [socket send:[self createCommand:@"context_get"]];
240 [windowController setRegister:[self processData:[socket receive]]];
241 }
242
243 /**
244 * Tells the debugger engine to get a specifc property. This also takes in the NSXMLElement
245 * that requested it so that the child can be attached.
246 */
247 - (NSArray *)getProperty:(NSString *)property
248 {
249 [socket send:[self createCommand:[NSString stringWithFormat:@"property_get -n \"%@\"", property]]];
250
251 NSXMLDocument *doc = [self processData:[socket receive]];
252
253 /*
254 <response>
255 <property> <!-- this is the one we requested -->
256 <property ... /> <!-- these are what we want -->
257 </property>
258 </repsonse>
259 */
260
261 // we now have to detach all the children so we can insert them into another document
262 NSXMLElement *parent = (NSXMLElement *)[[doc rootElement] childAtIndex:0];
263 NSArray *children = [parent children];
264 [parent setChildren:nil];
265 return children;
266 }
267
268 #pragma mark Breakpoints
269
270 /**
271 * Send an add breakpoint command
272 */
273 - (void)addBreakpoint:(Breakpoint *)bp
274 {
275 if (!connected)
276 {
277 return;
278 }
279
280 NSString *cmd = [self createCommand:[NSString stringWithFormat:@"breakpoint_set -t line -f %@ -n %i", [bp file], [bp line]]];
281 [socket send:cmd];
282 NSXMLDocument *info = [self processData:[socket receive]];
283 [bp setDebuggerId:[[[[info rootElement] attributeForName:@"id"] stringValue] intValue]];
284 }
285
286 /**
287 * Removes a breakpoint
288 */
289 - (void)removeBreakpoint:(Breakpoint *)bp
290 {
291 if (!connected)
292 {
293 return;
294 }
295
296 [socket send:[self createCommand:[NSString stringWithFormat:@"breakpoint_remove -d %i", [bp debuggerId]]]];
297 [socket receive];
298 }
299
300 #pragma mark Private
301
302 /**
303 * Helper method to create a string command with the -i <session> automatically tacked on
304 */
305 - (NSString *)createCommand:(NSString *)cmd
306 {
307 return [NSString stringWithFormat:@"%@ -i %@", cmd, session];
308 }
309
310 /**
311 * Helper function to parse the NSData into an NSXMLDocument
312 */
313 - (NSXMLDocument *)processData:(NSString *)data
314 {
315 NSError *parseError = nil;
316 NSXMLDocument *doc = [[NSXMLDocument alloc] initWithXMLString:data options:0 error:&parseError];
317 if (parseError)
318 {
319 NSLog(@"Could not parse XML? --- %@", parseError);
320 NSLog(@"Error UserInfo: %@", [parseError userInfo]);
321 NSLog(@"This is the XML Document: %@", data);
322 return nil;
323 }
324
325 // check and see if there's an error
326 NSArray *error = [[doc rootElement] elementsForName:@"error"];
327 if ([error count] > 0)
328 {
329 [windowController setError:[[[[error objectAtIndex:0] children] objectAtIndex:0] stringValue]];
330 return nil;
331 }
332
333 return [doc autorelease];
334 }
335
336 /**
337 * Creates a StackFrame based on the current position in the debugger
338 */
339 - (StackFrame *)createStackFrame
340 {
341 [socket send:[self createCommand:@"stack_get -d 0"]];
342 NSXMLDocument *doc = [self processData:[socket receive]];
343
344 NSXMLElement *xmlframe = [[[doc rootElement] children] objectAtIndex:0];
345 StackFrame *frame = [[StackFrame alloc]
346 initWithIndex:[[[xmlframe attributeForName:@"level"] stringValue] intValue]
347 withFilename:[[xmlframe attributeForName:@"filename"] stringValue]
348 withSource:nil
349 atLine:[[[xmlframe attributeForName:@"lineno"] stringValue] intValue]
350 inFunction:[[xmlframe attributeForName:@"where"] stringValue]
351 withContexts:nil
352 ];
353
354 return [frame autorelease];
355 }
356
357 @end