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