Turning off GC and adding back manual memory management
[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:(NSData *)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 [self refreshStatus];
139 }
140
141 /**
142 * Method that runs tells the debugger to give us its status and will update the status text on the window
143 */
144 - (void)refreshStatus
145 {
146 [socket send:[self createCommand:@"status"]];
147
148 NSXMLDocument *doc = [self processData:[socket receive]];
149 NSString *status = [[[doc rootElement] attributeForName:@"status"] stringValue];
150 [windowController setStatus:[status capitalizedString]];
151
152 if ([status isEqualToString:@"break"])
153 {
154 [self updateStackTraceAndRegisters];
155 }
156 else if ([status isEqualToString:@"stopped"])
157 {
158 connected = NO;
159 [socket close];
160 [windowController setStatus:[status capitalizedString]];
161 }
162 }
163
164 /**
165 * Tells the debugger to step into the current command.
166 */
167 - (void)stepIn
168 {
169 [socket send:[self createCommand:@"step_into"]];
170 [socket receive];
171 [self refreshStatus];
172 }
173
174 /**
175 * Tells the debugger to step out of the current context
176 */
177 - (void)stepOut
178 {
179 [socket send:[self createCommand:@"step_out"]];
180 [socket receive];
181 [self refreshStatus];
182 }
183
184 /**
185 * Tells the debugger to step over the current function
186 */
187 - (void)stepOver
188 {
189 [socket send:[self createCommand:@"step_over"]];
190 [socket receive];
191 [self refreshStatus];
192 }
193
194 /**
195 * This function queries the debug server for the current stacktrace and all the registers on
196 * level one. If a user then tries to expand past level one... TOOD: HOLY CRAP WHAT DO WE DO PAST LEVEL 1?
197 */
198 - (void)updateStackTraceAndRegisters
199 {
200 // do the stack
201 [socket send:[self createCommand:@"stack_get"]];
202 NSXMLDocument *doc = [self processData:[socket receive]];
203 NSArray *children = [[doc rootElement] children];
204 NSMutableArray *stack = [NSMutableArray array];
205 NSMutableDictionary *dict = [NSMutableDictionary dictionary];
206 for (int i = 0; i < [children count]; i++)
207 {
208 NSArray *attrs = [[children objectAtIndex:i] attributes];
209 for (int j = 0; j < [attrs count]; j++)
210 {
211 [dict setValue:[[attrs objectAtIndex:j] stringValue] forKey:[[attrs objectAtIndex:j] name]];
212 }
213 [stack addObject:dict];
214 dict = [NSMutableDictionary dictionary];
215 }
216 [windowController setStack:stack];
217
218 // do the registers
219 [socket send:[self createCommand:@"context_get"]];
220 [windowController setRegister:[self processData:[socket receive]]];
221 }
222
223 /**
224 * Tells the debugger engine to get a specifc property. This also takes in the NSXMLElement
225 * that requested it so that the child can be attached.
226 */
227 - (void)getProperty:(NSString *)property forNode:(NSTreeNode *)node
228 {
229 [socket send:[self createCommand:[NSString stringWithFormat:@"property_get -n \"%@\"", property]]];
230
231 NSXMLDocument *doc = [self processData:[socket receive]];
232
233 /*
234 <response>
235 <property> <!-- this is the one we requested -->
236 <property ... /> <!-- these are what we want -->
237 </property>
238 </repsonse>
239 */
240
241 // we now have to detach all the children so we can insert them into another document
242 NSXMLElement *parent = (NSXMLElement *)[[doc rootElement] childAtIndex:0];
243 NSArray *children = [parent children];
244 [parent setChildren:nil];
245 [windowController addChildren:children toNode:node];
246 }
247
248 #pragma mark Breakpoints
249
250 /**
251 * Send an add breakpoint command
252 */
253 - (void)addBreakpoint:(Breakpoint *)bp
254 {
255 NSString *cmd = [self createCommand:[NSString stringWithFormat:@"breakpoint_set -t line -f %@ -n %i", [bp file], [bp line]]];
256 [socket send:cmd];
257 NSXMLDocument *info = [self processData:[socket receive]];
258 [bp setDebuggerId:[[[[info rootElement] attributeForName:@"id"] stringValue] intValue]];
259 }
260
261 /**
262 * Removes a breakpoint
263 */
264 - (void)removeBreakpoint:(Breakpoint *)bp
265 {
266 [socket send:[self createCommand:[NSString stringWithFormat:@"breakpoint_remove -d %i", [bp debuggerId]]]];
267 [socket receive];
268 }
269
270 #pragma mark Private
271
272 /**
273 * Helper method to create a string command with the -i <session> automatically tacked on
274 */
275 - (NSString *)createCommand:(NSString *)cmd
276 {
277 return [NSString stringWithFormat:@"%@ -i %@", cmd, session];
278 }
279
280 /**
281 * Helper function to parse the NSData into an NSXMLDocument
282 */
283 - (NSXMLDocument *)processData:(NSData *)data
284 {
285 NSXMLDocument *doc = [[NSXMLDocument alloc] initWithData:data options:NSXMLDocumentTidyXML error:nil];
286
287 // check and see if there's an error
288 NSArray *error = [[doc rootElement] elementsForName:@"error"];
289 if ([error count] > 0)
290 {
291 [windowController setError:[[[[error objectAtIndex:0] children] objectAtIndex:0] stringValue]];
292 return nil;
293 }
294
295 return doc;
296 }
297
298 @end