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