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