Use our new error 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 NSString *kErrorOccurredNotif = @"GDBpConnection_ErrorOccured_Notification";
21
22 @interface GDBpConnection (Private)
23 - (NSString *)createCommand:(NSString *)cmd;
24 - (NSXMLDocument *)processData:(NSString *)data;
25 - (StackFrame *)createStackFrame;
26 @end
27
28 @implementation GDBpConnection
29
30 @synthesize socket, windowController;
31
32 /**
33 * Creates a new DebuggerConnection and initializes the socket from the given connection
34 * paramters.
35 */
36 - (id)initWithWindowController:(DebuggerController *)wc port:(int)aPort session:(NSString *)aSession;
37 {
38 if (self = [super init])
39 {
40 port = aPort;
41 session = [aSession retain];
42 connected = NO;
43
44 windowController = [wc retain];
45
46 // now that we have our host information, open the socket
47 socket = [[SocketWrapper alloc] initWithConnection:self];
48 [socket setDelegate:self];
49 [socket connect];
50
51 [[BreakpointManager sharedManager] setConnection:self];
52 }
53 return self;
54 }
55
56 /**
57 * Deallocates the object
58 */
59 - (void)dealloc
60 {
61 [socket release];
62 [session release];
63 [windowController 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 refreshStatus];
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 [windowController setStatus:@"Connecting"];
144 [windowController resetDisplays];
145 [socket connect];
146 }
147
148 /**
149 * Tells the debugger to continue running the script
150 */
151 - (void)run
152 {
153 [socket send:[self createCommand:@"run"]];
154 [socket receive];
155 [self refreshStatus];
156 }
157
158 /**
159 * Method that runs tells the debugger to give us its status and will update the status text on the window
160 */
161 - (void)refreshStatus
162 {
163 [socket send:[self createCommand:@"status"]];
164
165 NSXMLDocument *doc = [self processData:[socket receive]];
166 NSString *status = [[[doc rootElement] attributeForName:@"status"] stringValue];
167 [windowController setStatus:[status capitalizedString]];
168
169 if ([status isEqualToString:@"break"])
170 {
171 [self updateStackTraceAndRegisters];
172 }
173 else if ([status isEqualToString:@"stopped"] || [status isEqualToString:@"stopping"])
174 {
175 connected = NO;
176 [socket close];
177 [windowController setStatus:@"Stopped"];
178 }
179 }
180
181 /**
182 * Tells the debugger to step into the current command.
183 */
184 - (StackFrame *)stepIn
185 {
186 [socket send:[self createCommand:@"step_into"]];
187 [socket receive];
188
189 StackFrame *frame = [self createStackFrame];
190 [self refreshStatus];
191
192 return frame;
193 }
194
195 /**
196 * Tells the debugger to step out of the current context
197 */
198 - (StackFrame *)stepOut
199 {
200 [socket send:[self createCommand:@"step_out"]];
201 [socket receive];
202
203 StackFrame *frame = [self createStackFrame];
204 [self refreshStatus];
205
206 return frame;
207 }
208
209 /**
210 * Tells the debugger to step over the current function
211 */
212 - (StackFrame *)stepOver
213 {
214 [socket send:[self createCommand:@"step_over"]];
215 [socket receive];
216
217 StackFrame *frame = [self createStackFrame];
218 [self refreshStatus];
219
220 return frame;
221 }
222
223 /**
224 * This function queries the debug server for the current stacktrace and all the registers on
225 * level one. If a user then tries to expand past level one... TOOD: HOLY CRAP WHAT DO WE DO PAST LEVEL 1?
226 */
227 - (void)updateStackTraceAndRegisters
228 {
229 // do the registers
230 [socket send:[self createCommand:@"context_get"]];
231 [windowController setRegister:[self processData:[socket receive]]];
232 }
233
234 /**
235 * Tells the debugger engine to get a specifc property. This also takes in the NSXMLElement
236 * that requested it so that the child can be attached.
237 */
238 - (NSArray *)getProperty:(NSString *)property
239 {
240 [socket send:[self createCommand:[NSString stringWithFormat:@"property_get -n \"%@\"", property]]];
241
242 NSXMLDocument *doc = [self processData:[socket receive]];
243
244 /*
245 <response>
246 <property> <!-- this is the one we requested -->
247 <property ... /> <!-- these are what we want -->
248 </property>
249 </repsonse>
250 */
251
252 // we now have to detach all the children so we can insert them into another document
253 NSXMLElement *parent = (NSXMLElement *)[[doc rootElement] childAtIndex:0];
254 NSArray *children = [parent children];
255 [parent setChildren:nil];
256 return children;
257 }
258
259 #pragma mark Breakpoints
260
261 /**
262 * Send an add breakpoint command
263 */
264 - (void)addBreakpoint:(Breakpoint *)bp
265 {
266 if (!connected)
267 {
268 return;
269 }
270
271 NSString *cmd = [self createCommand:[NSString stringWithFormat:@"breakpoint_set -t line -f %@ -n %i", [bp file], [bp line]]];
272 [socket send:cmd];
273 NSXMLDocument *info = [self processData:[socket receive]];
274 [bp setDebuggerId:[[[[info rootElement] attributeForName:@"id"] stringValue] intValue]];
275 }
276
277 /**
278 * Removes a breakpoint
279 */
280 - (void)removeBreakpoint:(Breakpoint *)bp
281 {
282 if (!connected)
283 {
284 return;
285 }
286
287 [socket send:[self createCommand:[NSString stringWithFormat:@"breakpoint_remove -d %i", [bp debuggerId]]]];
288 [socket receive];
289 }
290
291 #pragma mark Private
292
293 /**
294 * Helper method to create a string command with the -i <session> automatically tacked on
295 */
296 - (NSString *)createCommand:(NSString *)cmd
297 {
298 return [NSString stringWithFormat:@"%@ -i %@", cmd, session];
299 }
300
301 /**
302 * Helper function to parse the NSData into an NSXMLDocument
303 */
304 - (NSXMLDocument *)processData:(NSString *)data
305 {
306 NSError *parseError = nil;
307 NSXMLDocument *doc = [[NSXMLDocument alloc] initWithXMLString:data options:0 error:&parseError];
308 if (parseError)
309 {
310 NSLog(@"Could not parse XML? --- %@", parseError);
311 NSLog(@"Error UserInfo: %@", [parseError userInfo]);
312 NSLog(@"This is the XML Document: %@", data);
313 return nil;
314 }
315
316 // check and see if there's an error
317 NSArray *error = [[doc rootElement] elementsForName:@"error"];
318 if ([error count] > 0)
319 {
320 [self errorEncountered:[[[[error objectAtIndex:0] children] objectAtIndex:0] stringValue]];
321 return nil;
322 }
323
324 return [doc autorelease];
325 }
326
327 /**
328 * Creates a StackFrame based on the current position in the debugger
329 */
330 - (StackFrame *)createStackFrame
331 {
332 [socket send:[self createCommand:@"stack_get -d 0"]];
333 NSXMLDocument *doc = [self processData:[socket receive]];
334
335 NSXMLElement *xmlframe = [[[doc rootElement] children] objectAtIndex:0];
336 StackFrame *frame = [[StackFrame alloc]
337 initWithIndex:0
338 withFilename:[[xmlframe attributeForName:@"filename"] stringValue]
339 withSource:nil
340 atLine:[[[xmlframe attributeForName:@"lineno"] stringValue] intValue]
341 inFunction:[[xmlframe attributeForName:@"where"] stringValue]
342 withContexts:nil
343 ];
344
345 return [frame autorelease];
346 }
347
348 @end