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
43 if (self = [super init
])
48 // now that we have our host information, open the socket
49 socket
= [[SocketWrapper alloc
] initWithPort
:port
];
50 socket.delegate
= self;
53 self.status
= @
"Connecting";
55 [[BreakpointManager sharedManager
] setConnection
:self];
61 * Deallocates the object
70 * Gets the port number
78 * Returns the name of the remote host
80 - (NSString
*)remoteHost
84 return @
"(DISCONNECTED)";
86 return [socket remoteHost
];
90 * Returns whether or not we have an active connection
98 * Called by SocketWrapper after the connection is successful. This immediately calls
99 * -[SocketWrapper receive] to clear the way for communication, though the information
100 * could be useful server information that we don't use right now.
102 - (void)socketDidAccept
104 [self performSelectorOnMainThread
:@selector(doSocketAccept
:) withObject
:nil waitUntilDone
:YES
];
108 * Receives errors from the SocketWrapper and updates the display
110 - (void)errorEncountered
:(NSString
*)error
112 [delegate errorEncountered
:error
];
116 * Reestablishes communication with the remote debugger so that a new connection doesn't have to be
117 * created every time you want to debug a page
122 self.status
= @
"Connecting";
127 * Creates an entirely new stack and returns it as an array of StackFrame objects.
129 - (NSArray
*)getCurrentStack
131 // get the total stack depth
132 [socket send
:[self createCommand
:@
"stack_depth"]];
133 NSXMLDocument
* doc
= [self processData
:[socket receive
]];
134 int depth
= [[[[doc rootElement
] attributeForName
:@
"depth"] stringValue
] intValue
];
136 // get all stack frames
137 NSMutableArray
* stack
= [NSMutableArray arrayWithCapacity
:depth
];
138 for (int i
= 0; i
< depth
; i
++)
140 StackFrame
* frame
= [self createStackFrame
:i
];
141 [stack insertObject
:frame atIndex
:i
];
148 * Tells the debugger to continue running the script. Returns the current stack frame.
152 [socket send
:[self createCommand
:@
"run"]];
159 * Tells the debugger to step into the current command.
163 [socket send
:[self createCommand
:@
"step_into"]];
170 * Tells the debugger to step out of the current context
174 [socket send
:[self createCommand
:@
"step_out"]];
181 * Tells the debugger to step over the current function
185 [socket send
:[self createCommand
:@
"step_over"]];
192 * Tells the debugger engine to get a specifc property. This also takes in the NSXMLElement
193 * that requested it so that the child can be attached.
195 - (NSArray
*)getProperty
:(NSString
*)property
197 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"property_get -n \"%@\"", property
]]];
199 NSXMLDocument
* doc
= [self processData
:[socket receive
]];
203 <property> <!-- this is the one we requested -->
204 <property ... /> <!-- these are what we want -->
209 // we now have to detach all the children so we can insert them into another document
210 NSXMLElement
* parent
= (NSXMLElement
*)[[doc rootElement
] childAtIndex
:0];
211 NSArray
* children
= [parent children
];
212 [parent setChildren
:nil];
216 #pragma mark Breakpoints
219 * Send an add breakpoint command
221 - (void)addBreakpoint
:(Breakpoint
*)bp
226 NSString
* file
= [self escapedURIPath
:[bp transformedPath
]];
227 NSString
* cmd
= [self createCommand
:[NSString stringWithFormat
:@
"breakpoint_set -t line -f %@ -n %i", file
, [bp line
]]];
229 NSXMLDocument
* info
= [self processData
:[socket receive
]];
230 [bp setDebuggerId
:[[[[info rootElement
] attributeForName
:@
"id"] stringValue
] intValue
]];
234 * Removes a breakpoint
236 - (void)removeBreakpoint
:(Breakpoint
*)bp
243 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"breakpoint_remove -d %i", [bp debuggerId
]]]];
250 * Helper method to create a string command with the -i <transaction id> automatically tacked on. Takes
251 * a variable number of arguments and parses the given command with +[NSString stringWithFormat:]
253 - (NSString
*)createCommand
:(NSString
*)cmd
, ...
257 va_start(argList
, cmd
);
258 NSString
* format
= [[NSString alloc
] initWithFormat
:cmd arguments
:argList
]; // format the command
261 if ([[[[NSProcessInfo processInfo
] environment
] objectForKey
:@
"TransportDebug"] boolValue
])
262 NSLog(@
"--> %@", cmd
);
264 return [NSString stringWithFormat
:@
"%@ -i %d", [format autorelease
], transactionID
++];
268 * Helper function to parse the NSData into an NSXMLDocument
270 - (NSXMLDocument
*)processData
:(NSString
*)data
275 NSError
* parseError
= nil;
276 NSXMLDocument
* doc
= [[NSXMLDocument alloc
] initWithXMLString
:data options
:0 error
:&parseError
];
279 NSLog(@
"Could not parse XML? --- %@", parseError
);
280 NSLog(@
"Error UserInfo: %@", [parseError userInfo
]);
281 NSLog(@
"This is the XML Document: %@", data
);
285 // check and see if there's an error
286 NSArray
* error
= [[doc rootElement
] elementsForName
:@
"error"];
287 if ([error count
] > 0)
289 NSLog(@
"Xdebug error: %@", error
);
290 [delegate errorEncountered
:[[[[error objectAtIndex
:0] children
] objectAtIndex
:0] stringValue
]];
294 if ([[[[NSProcessInfo processInfo
] environment
] objectForKey
:@
"TransportDebug"] boolValue
])
295 NSLog(@
"<-- %@", doc
);
297 return [doc autorelease
];
301 * Generates a stack frame for the given depth
303 - (StackFrame
*)createStackFrame
:(int)stackDepth
305 // get the stack frame
306 [socket send
:[self createCommand
:@
"stack_get -d %d", stackDepth
]];
307 NSXMLDocument
* doc
= [self processData
:[socket receive
]];
311 NSXMLElement
* xmlframe
= [[[doc rootElement
] children
] objectAtIndex
:0];
313 // get the names of all the contexts
314 [socket send
:[self createCommand
:@
"context_names -d 0"]];
315 NSXMLElement
* contextNames
= [[self processData
:[socket receive
]] rootElement
];
316 NSMutableArray
* variables
= [NSMutableArray array
];
317 for (NSXMLElement
* context
in [contextNames children
])
319 NSString
* name
= [[context attributeForName
:@
"name"] stringValue
];
320 int cid
= [[[context attributeForName
:@
"id"] stringValue
] intValue
];
322 // fetch the contexts
323 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"context_get -d %d -c %d", stackDepth
, cid
]]];
324 NSArray
* addVars
= [[[self processData
:[socket receive
]] rootElement
] children
];
325 if (addVars
!= nil && name
!= nil)
326 [variables addObjectsFromArray
:addVars
];
330 NSString
* filename
= [[xmlframe attributeForName
:@
"filename"] stringValue
];
331 NSString
* escapedFilename
= [filename stringByReplacingOccurrencesOfString
:@
"%" withString
:@
"%%"]; // escape % in URL chars
332 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"source -f %@", escapedFilename
]]];
333 NSString
* source
= [[[self processData
:[socket receive
]] rootElement
] value
]; // decode base64
335 // create stack frame
336 StackFrame
* frame
= [[StackFrame alloc
]
337 initWithIndex
:stackDepth
338 withFilename
:filename
340 atLine
:[[[xmlframe attributeForName
:@
"lineno"] stringValue
] intValue
]
341 inFunction
:[[xmlframe attributeForName
:@
"where"] stringValue
]
342 withVariables
:variables
345 return [frame autorelease
];
349 * Creates a StackFrame based on the current position in the debugger
351 - (StackFrame
*)createCurrentStackFrame
353 return [self createStackFrame
:0];
357 * Fetches the value of and sets the status instance variable
361 [socket send
:[self createCommand
:@
"status"]];
362 NSXMLDocument
* doc
= [self processData
:[socket receive
]];
363 self.status
= [[[[doc rootElement
] attributeForName
:@
"status"] stringValue
] capitalizedString
];
365 if (status
== nil ||
[status isEqualToString
:@
"Stopped"] ||
[status isEqualToString
:@
"Stopping"])
370 [delegate debuggerDisconnected
];
372 self.status
= @
"Stopped";
377 * Given a file path, this returns a file:// URI and escapes any spaces for the
380 - (NSString
*)escapedURIPath
:(NSString
*)path
382 // Custon GDBp paths are fine.
383 if ([[path substringToIndex
:4] isEqualToString
:@
"gdbp"])
386 // Create a temporary URL that will escape all the nasty characters.
387 NSURL
* url
= [NSURL fileURLWithPath
:path
];
388 NSString
* urlString
= [url absoluteString
];
390 // Remove the host because this is a file:// URL;
391 urlString
= [urlString stringByReplacingOccurrencesOfString
:[url host
] withString
:@
""];
393 // Escape % for use in printf-style NSString formatters.
394 urlString
= [urlString stringByReplacingOccurrencesOfString
:@
"%" withString
:@
"%%"];
399 * Helper method for |-socketDidAccept| to be called on the main thread.
401 - (void)doSocketAccept
:_nil
408 // register any breakpoints that exist offline
409 for (Breakpoint
* bp
in [[BreakpointManager sharedManager
] breakpoints
])
411 [self addBreakpoint
:bp
];
414 // Load the debugger to make it look active.
415 [delegate debuggerConnected
];