]> src.bluestatic.org Git - macgdbp.git/blob - Source/GDBpConnection.m
Adding the StackFrame class
[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 @end
24
25 @implementation GDBpConnection
26
27 @synthesize socket, windowController;
28
29 /**
30 * Creates a new DebuggerConnection and initializes the socket from the given connection
31 * paramters.
32 */
33 - (id)initWithWindowController:(DebuggerController *)wc port:(int)aPort session:(NSString *)aSession;
34 {
35 if (self = [super init])
36 {
37 port = aPort;
38 session = [aSession retain];
39 connected = NO;
40
41 windowController = [wc retain];
42
43 // now that we have our host information, open the socket
44 socket = [[SocketWrapper alloc] initWithConnection:self];
45 [socket setDelegate:self];
46 [socket connect];
47
48 [[BreakpointManager sharedManager] setConnection:self];
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 // register any breakpoints that exist offline
112 for (Breakpoint *bp in [[BreakpointManager sharedManager] breakpoints])
113 {
114 [self addBreakpoint:bp];
115 }
116 }
117
118 /**
119 * Receives errors from the SocketWrapper and updates the display
120 */
121 - (void)errorEncountered:(NSString *)error
122 {
123 [windowController setError:error];
124 }
125
126 /**
127 * Reestablishes communication with the remote debugger so that a new connection doesn't have to be
128 * created every time you want to debug a page
129 */
130 - (void)reconnect
131 {
132 [socket close];
133 [windowController setStatus:@"Connecting"];
134 [windowController resetDisplays];
135 [socket connect];
136 }
137
138 /**
139 * Tells the debugger to continue running the script
140 */
141 - (void)run
142 {
143 [socket send:[self createCommand:@"run"]];
144 [socket receive];
145 [self refreshStatus];
146 }
147
148 /**
149 * Method that runs tells the debugger to give us its status and will update the status text on the window
150 */
151 - (void)refreshStatus
152 {
153 [socket send:[self createCommand:@"status"]];
154
155 NSXMLDocument *doc = [self processData:[socket receive]];
156 NSString *status = [[[doc rootElement] attributeForName:@"status"] stringValue];
157 [windowController setStatus:[status capitalizedString]];
158
159 if ([status isEqualToString:@"break"])
160 {
161 [self updateStackTraceAndRegisters];
162 }
163 else if ([status isEqualToString:@"stopped"] || [status isEqualToString:@"stopping"])
164 {
165 connected = NO;
166 [socket close];
167 [windowController setStatus:@"Stopped"];
168 }
169 }
170
171 /**
172 * Tells the debugger to step into the current command.
173 */
174 - (void)stepIn
175 {
176 [socket send:[self createCommand:@"step_into"]];
177 [socket receive];
178 [self refreshStatus];
179 }
180
181 /**
182 * Tells the debugger to step out of the current context
183 */
184 - (void)stepOut
185 {
186 [socket send:[self createCommand:@"step_out"]];
187 [socket receive];
188 [self refreshStatus];
189 }
190
191 /**
192 * Tells the debugger to step over the current function
193 */
194 - (void)stepOver
195 {
196 [socket send:[self createCommand:@"step_over"]];
197 [socket receive];
198 [self refreshStatus];
199 }
200
201 /**
202 * This function queries the debug server for the current stacktrace and all the registers on
203 * level one. If a user then tries to expand past level one... TOOD: HOLY CRAP WHAT DO WE DO PAST LEVEL 1?
204 */
205 - (void)updateStackTraceAndRegisters
206 {
207 // do the stack
208 [socket send:[self createCommand:@"stack_get"]];
209 NSXMLDocument *doc = [self processData:[socket receive]];
210 NSArray *children = [[doc rootElement] children];
211 NSMutableArray *stack = [NSMutableArray array];
212 NSMutableDictionary *dict = [NSMutableDictionary dictionary];
213 for (int i = 0; i < [children count]; i++)
214 {
215 NSArray *attrs = [[children objectAtIndex:i] attributes];
216 for (int j = 0; j < [attrs count]; j++)
217 {
218 [dict setValue:[[attrs objectAtIndex:j] stringValue] forKey:[[attrs objectAtIndex:j] name]];
219 }
220 [stack addObject:dict];
221 dict = [NSMutableDictionary dictionary];
222 }
223 [windowController setStack:stack];
224
225 // do the registers
226 [socket send:[self createCommand:@"context_get"]];
227 [windowController setRegister:[self processData:[socket receive]]];
228 }
229
230 /**
231 * Tells the debugger engine to get a specifc property. This also takes in the NSXMLElement
232 * that requested it so that the child can be attached.
233 */
234 - (NSArray *)getProperty:(NSString *)property
235 {
236 [socket send:[self createCommand:[NSString stringWithFormat:@"property_get -n \"%@\"", property]]];
237
238 NSXMLDocument *doc = [self processData:[socket receive]];
239
240 /*
241 <response>
242 <property> <!-- this is the one we requested -->
243 <property ... /> <!-- these are what we want -->
244 </property>
245 </repsonse>
246 */
247
248 // we now have to detach all the children so we can insert them into another document
249 NSXMLElement *parent = (NSXMLElement *)[[doc rootElement] childAtIndex:0];
250 NSArray *children = [parent children];
251 [parent setChildren:nil];
252 return children;
253 }
254
255 #pragma mark Breakpoints
256
257 /**
258 * Send an add breakpoint command
259 */
260 - (void)addBreakpoint:(Breakpoint *)bp
261 {
262 if (!connected)
263 {
264 return;
265 }
266
267 NSString *cmd = [self createCommand:[NSString stringWithFormat:@"breakpoint_set -t line -f %@ -n %i", [bp file], [bp line]]];
268 [socket send:cmd];
269 NSXMLDocument *info = [self processData:[socket receive]];
270 [bp setDebuggerId:[[[[info rootElement] attributeForName:@"id"] stringValue] intValue]];
271 }
272
273 /**
274 * Removes a breakpoint
275 */
276 - (void)removeBreakpoint:(Breakpoint *)bp
277 {
278 if (!connected)
279 {
280 return;
281 }
282
283 [socket send:[self createCommand:[NSString stringWithFormat:@"breakpoint_remove -d %i", [bp debuggerId]]]];
284 [socket receive];
285 }
286
287 #pragma mark Private
288
289 /**
290 * Helper method to create a string command with the -i <session> automatically tacked on
291 */
292 - (NSString *)createCommand:(NSString *)cmd
293 {
294 return [NSString stringWithFormat:@"%@ -i %@", cmd, session];
295 }
296
297 /**
298 * Helper function to parse the NSData into an NSXMLDocument
299 */
300 - (NSXMLDocument *)processData:(NSString *)data
301 {
302 NSError *parseError = nil;
303 NSXMLDocument *doc = [[NSXMLDocument alloc] initWithXMLString:data options:0 error:&parseError];
304 if (parseError)
305 {
306 NSLog(@"Could not parse XML? --- %@", parseError);
307 NSLog(@"Error UserInfo: %@", [parseError userInfo]);
308 NSLog(@"This is the XML Document: %@", data);
309 return nil;
310 }
311
312 // check and see if there's an error
313 NSArray *error = [[doc rootElement] elementsForName:@"error"];
314 if ([error count] > 0)
315 {
316 [windowController setError:[[[[error objectAtIndex:0] children] objectAtIndex:0] stringValue]];
317 return nil;
318 }
319
320 return [doc autorelease];
321 }
322
323 @end