Remove all of the old stack system
[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 registers
221 [socket send:[self createCommand:@"context_get"]];
222 [windowController setRegister:[self processData:[socket receive]]];
223 }
224
225 /**
226 * Tells the debugger engine to get a specifc property. This also takes in the NSXMLElement
227 * that requested it so that the child can be attached.
228 */
229 - (NSArray *)getProperty:(NSString *)property
230 {
231 [socket send:[self createCommand:[NSString stringWithFormat:@"property_get -n \"%@\"", property]]];
232
233 NSXMLDocument *doc = [self processData:[socket receive]];
234
235 /*
236 <response>
237 <property> <!-- this is the one we requested -->
238 <property ... /> <!-- these are what we want -->
239 </property>
240 </repsonse>
241 */
242
243 // we now have to detach all the children so we can insert them into another document
244 NSXMLElement *parent = (NSXMLElement *)[[doc rootElement] childAtIndex:0];
245 NSArray *children = [parent children];
246 [parent setChildren:nil];
247 return children;
248 }
249
250 #pragma mark Breakpoints
251
252 /**
253 * Send an add breakpoint command
254 */
255 - (void)addBreakpoint:(Breakpoint *)bp
256 {
257 if (!connected)
258 {
259 return;
260 }
261
262 NSString *cmd = [self createCommand:[NSString stringWithFormat:@"breakpoint_set -t line -f %@ -n %i", [bp file], [bp line]]];
263 [socket send:cmd];
264 NSXMLDocument *info = [self processData:[socket receive]];
265 [bp setDebuggerId:[[[[info rootElement] attributeForName:@"id"] stringValue] intValue]];
266 }
267
268 /**
269 * Removes a breakpoint
270 */
271 - (void)removeBreakpoint:(Breakpoint *)bp
272 {
273 if (!connected)
274 {
275 return;
276 }
277
278 [socket send:[self createCommand:[NSString stringWithFormat:@"breakpoint_remove -d %i", [bp debuggerId]]]];
279 [socket receive];
280 }
281
282 #pragma mark Private
283
284 /**
285 * Helper method to create a string command with the -i <session> automatically tacked on
286 */
287 - (NSString *)createCommand:(NSString *)cmd
288 {
289 return [NSString stringWithFormat:@"%@ -i %@", cmd, session];
290 }
291
292 /**
293 * Helper function to parse the NSData into an NSXMLDocument
294 */
295 - (NSXMLDocument *)processData:(NSString *)data
296 {
297 NSError *parseError = nil;
298 NSXMLDocument *doc = [[NSXMLDocument alloc] initWithXMLString:data options:0 error:&parseError];
299 if (parseError)
300 {
301 NSLog(@"Could not parse XML? --- %@", parseError);
302 NSLog(@"Error UserInfo: %@", [parseError userInfo]);
303 NSLog(@"This is the XML Document: %@", data);
304 return nil;
305 }
306
307 // check and see if there's an error
308 NSArray *error = [[doc rootElement] elementsForName:@"error"];
309 if ([error count] > 0)
310 {
311 [windowController setError:[[[[error objectAtIndex:0] children] objectAtIndex:0] stringValue]];
312 return nil;
313 }
314
315 return [doc autorelease];
316 }
317
318 /**
319 * Creates a StackFrame based on the current position in the debugger
320 */
321 - (StackFrame *)createStackFrame
322 {
323 [socket send:[self createCommand:@"stack_get -d 0"]];
324 NSXMLDocument *doc = [self processData:[socket receive]];
325
326 NSXMLElement *xmlframe = [[[doc rootElement] children] objectAtIndex:0];
327 StackFrame *frame = [[StackFrame alloc]
328 initWithIndex:0
329 withFilename:[[xmlframe attributeForName:@"filename"] stringValue]
330 withSource:nil
331 atLine:[[[xmlframe attributeForName:@"lineno"] stringValue] intValue]
332 inFunction:[[xmlframe attributeForName:@"where"] stringValue]
333 withContexts:nil
334 ];
335
336 return [frame autorelease];
337 }
338
339 @end