3 * Copyright (c) 2007 - 2009, Blue Static <http://www.bluestatic.org>
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.
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.
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
17 #import "GDBpConnection.h"
18 #import "AppDelegate.h"
20 @interface GDBpConnection()
21 @property(readwrite
, copy
) NSString
* status
;
23 - (NSString
*)createCommand
:(NSString
*)cmd
, ...
;
24 - (NSXMLDocument
*)processData
:(NSString
*)data
;
25 - (StackFrame
*)createStackFrame
:(int)depth
;
26 - (StackFrame
*)createCurrentStackFrame
;
28 - (NSString
*)escapedURIPath
:(NSString
*)path
;
29 - (void)doSocketAccept
:_nil
;
32 @implementation GDBpConnection
38 * Creates a new DebuggerConnection and initializes the socket from the given connection
41 - (id)initWithPort
:(int)aPort session
:(NSString
*)aSession
;
43 if (self = [super init
])
46 session
= [aSession retain
];
49 // now that we have our host information, open the socket
50 socket
= [[SocketWrapper alloc
] initWithPort
:port
];
51 socket.delegate
= self;
54 self.status
= @
"Connecting";
56 [[BreakpointManager sharedManager
] setConnection
:self];
62 * Deallocates the object
72 * Gets the port number
80 * Gets the session name
88 * Returns the name of the remote host
90 - (NSString
*)remoteHost
94 return @
"(DISCONNECTED)";
96 return [socket remoteHost
];
100 * Returns whether or not we have an active connection
108 * Called by SocketWrapper after the connection is successful. This immediately calls
109 * -[SocketWrapper receive] to clear the way for communication, though the information
110 * could be useful server information that we don't use right now.
112 - (void)socketDidAccept
114 [self performSelectorOnMainThread
:@selector(doSocketAccept
:) withObject
:nil waitUntilDone
:YES
];
118 * Receives errors from the SocketWrapper and updates the display
120 - (void)errorEncountered
:(NSString
*)error
122 [delegate errorEncountered
:error
];
126 * Reestablishes communication with the remote debugger so that a new connection doesn't have to be
127 * created every time you want to debug a page
132 self.status
= @
"Connecting";
137 * Creates an entirely new stack and returns it as an array of StackFrame objects.
139 - (NSArray
*)getCurrentStack
141 // get the total stack depth
142 [socket send
:[self createCommand
:@
"stack_depth"]];
143 NSXMLDocument
* doc
= [self processData
:[socket receive
]];
144 int depth
= [[[[doc rootElement
] attributeForName
:@
"depth"] stringValue
] intValue
];
146 // get all stack frames
147 NSMutableArray
* stack
= [NSMutableArray arrayWithCapacity
:depth
];
148 for (int i
= 0; i
< depth
; i
++)
150 StackFrame
* frame
= [self createStackFrame
:i
];
151 [stack insertObject
:frame atIndex
:i
];
158 * Tells the debugger to continue running the script. Returns the current stack frame.
162 [socket send
:[self createCommand
:@
"run"]];
169 * Tells the debugger to step into the current command.
173 [socket send
:[self createCommand
:@
"step_into"]];
180 * Tells the debugger to step out of the current context
184 [socket send
:[self createCommand
:@
"step_out"]];
191 * Tells the debugger to step over the current function
195 [socket send
:[self createCommand
:@
"step_over"]];
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.
205 - (NSArray
*)getProperty
:(NSString
*)property
207 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"property_get -n \"%@\"", property
]]];
209 NSXMLDocument
* doc
= [self processData
:[socket receive
]];
213 <property> <!-- this is the one we requested -->
214 <property ... /> <!-- these are what we want -->
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];
226 #pragma mark Breakpoints
229 * Send an add breakpoint command
231 - (void)addBreakpoint
:(Breakpoint
*)bp
236 NSString
* file
= [self escapedURIPath
:[bp transformedPath
]];
237 NSString
* cmd
= [self createCommand
:[NSString stringWithFormat
:@
"breakpoint_set -t line -f %@ -n %i", file
, [bp line
]]];
239 NSXMLDocument
* info
= [self processData
:[socket receive
]];
240 [bp setDebuggerId
:[[[[info rootElement
] attributeForName
:@
"id"] stringValue
] intValue
]];
244 * Removes a breakpoint
246 - (void)removeBreakpoint
:(Breakpoint
*)bp
253 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"breakpoint_remove -d %i", [bp debuggerId
]]]];
260 * Helper method to create a string command with the -i <session> automatically tacked on. Takes
261 * a variable number of arguments and parses the given command with +[NSString stringWithFormat:]
263 - (NSString
*)createCommand
:(NSString
*)cmd
, ...
267 va_start(argList
, cmd
);
268 NSString
* format
= [[NSString alloc
] initWithFormat
:cmd arguments
:argList
]; // format the command
272 NSLog(@
"--> %@", format
);
275 return [NSString stringWithFormat
:@
"%@ -i %@", [format autorelease
], session
];
279 * Helper function to parse the NSData into an NSXMLDocument
281 - (NSXMLDocument
*)processData
:(NSString
*)data
286 NSError
* parseError
= nil;
287 NSXMLDocument
* doc
= [[NSXMLDocument alloc
] initWithXMLString
:data options
:0 error
:&parseError
];
290 NSLog(@
"Could not parse XML? --- %@", parseError
);
291 NSLog(@
"Error UserInfo: %@", [parseError userInfo
]);
292 NSLog(@
"This is the XML Document: %@", data
);
296 // check and see if there's an error
297 NSArray
* error
= [[doc rootElement
] elementsForName
:@
"error"];
298 if ([error count
] > 0)
300 NSLog(@
"Xdebug error: %@", error
);
301 [delegate errorEncountered
:[[[[error objectAtIndex
:0] children
] objectAtIndex
:0] stringValue
]];
306 NSLog(@
"<-- %@", doc
);
309 return [doc autorelease
];
313 * Generates a stack frame for the given depth
315 - (StackFrame
*)createStackFrame
:(int)stackDepth
317 // get the stack frame
318 [socket send
:[self createCommand
:@
"stack_get -d %d", stackDepth
]];
319 NSXMLDocument
* doc
= [self processData
:[socket receive
]];
323 NSXMLElement
* xmlframe
= [[[doc rootElement
] children
] objectAtIndex
:0];
325 // get the names of all the contexts
326 [socket send
:[self createCommand
:@
"context_names -d 0"]];
327 NSXMLElement
* contextNames
= [[self processData
:[socket receive
]] rootElement
];
328 NSMutableArray
* variables
= [NSMutableArray array
];
329 for (NSXMLElement
* context
in [contextNames children
])
331 NSString
* name
= [[context attributeForName
:@
"name"] stringValue
];
332 int cid
= [[[context attributeForName
:@
"id"] stringValue
] intValue
];
334 // fetch the contexts
335 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"context_get -d %d -c %d", stackDepth
, cid
]]];
336 NSArray
* addVars
= [[[self processData
:[socket receive
]] rootElement
] children
];
337 if (addVars
!= nil && name
!= nil)
338 [variables addObjectsFromArray
:addVars
];
342 NSString
* filename
= [[xmlframe attributeForName
:@
"filename"] stringValue
];
343 NSString
* escapedFilename
= [filename stringByReplacingOccurrencesOfString
:@
"%" withString
:@
"%%"]; // escape % in URL chars
344 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"source -f %@", escapedFilename
]]];
345 NSString
* source
= [[[self processData
:[socket receive
]] rootElement
] value
]; // decode base64
347 // create stack frame
348 StackFrame
* frame
= [[StackFrame alloc
]
349 initWithIndex
:stackDepth
350 withFilename
:filename
352 atLine
:[[[xmlframe attributeForName
:@
"lineno"] stringValue
] intValue
]
353 inFunction
:[[xmlframe attributeForName
:@
"where"] stringValue
]
354 withVariables
:variables
357 return [frame autorelease
];
361 * Creates a StackFrame based on the current position in the debugger
363 - (StackFrame
*)createCurrentStackFrame
365 return [self createStackFrame
:0];
369 * Fetches the value of and sets the status instance variable
373 [socket send
:[self createCommand
:@
"status"]];
374 NSXMLDocument
* doc
= [self processData
:[socket receive
]];
375 self.status
= [[[[doc rootElement
] attributeForName
:@
"status"] stringValue
] capitalizedString
];
377 if (status
== nil ||
[status isEqualToString
:@
"Stopped"] ||
[status isEqualToString
:@
"Stopping"])
382 [delegate debuggerDisconnected
];
384 self.status
= @
"Stopped";
389 * Given a file path, this returns a file:// URI and escapes any spaces for the
392 - (NSString
*)escapedURIPath
:(NSString
*)path
394 // Custon GDBp paths are fine.
395 if ([[path substringToIndex
:4] isEqualToString
:@
"gdbp"])
398 // Create a temporary URL that will escape all the nasty characters.
399 NSURL
* url
= [NSURL fileURLWithPath
:path
];
400 NSString
* urlString
= [url absoluteString
];
402 // Remove the host because this is a file:// URL;
403 urlString
= [urlString stringByReplacingOccurrencesOfString
:[url host
] withString
:@
""];
405 // Escape % for use in printf-style NSString formatters.
406 urlString
= [urlString stringByReplacingOccurrencesOfString
:@
"%" withString
:@
"%%"];
411 * Helper method for |-socketDidAccept| to be called on the main thread.
413 - (void)doSocketAccept
:_nil
419 // register any breakpoints that exist offline
420 for (Breakpoint
* bp
in [[BreakpointManager sharedManager
] breakpoints
])
422 [self addBreakpoint
:bp
];
425 // Load the debugger to make it look active.
426 [delegate debuggerConnected
];