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
271 if ([[[[NSProcessInfo processInfo
] environment
] objectForKey
:@
"TransportDebug"] boolValue
])
272 NSLog(@
"--> %@", cmd
);
274 return [NSString stringWithFormat
:@
"%@ -i %@", [format autorelease
], session
];
278 * Helper function to parse the NSData into an NSXMLDocument
280 - (NSXMLDocument
*)processData
:(NSString
*)data
285 NSError
* parseError
= nil;
286 NSXMLDocument
* doc
= [[NSXMLDocument alloc
] initWithXMLString
:data options
:0 error
:&parseError
];
289 NSLog(@
"Could not parse XML? --- %@", parseError
);
290 NSLog(@
"Error UserInfo: %@", [parseError userInfo
]);
291 NSLog(@
"This is the XML Document: %@", data
);
295 // check and see if there's an error
296 NSArray
* error
= [[doc rootElement
] elementsForName
:@
"error"];
297 if ([error count
] > 0)
299 NSLog(@
"Xdebug error: %@", error
);
300 [delegate errorEncountered
:[[[[error objectAtIndex
:0] children
] objectAtIndex
:0] stringValue
]];
304 if ([[[[NSProcessInfo processInfo
] environment
] objectForKey
:@
"TransportDebug"] boolValue
])
305 NSLog(@
"<-- %@", doc
);
307 return [doc autorelease
];
311 * Generates a stack frame for the given depth
313 - (StackFrame
*)createStackFrame
:(int)stackDepth
315 // get the stack frame
316 [socket send
:[self createCommand
:@
"stack_get -d %d", stackDepth
]];
317 NSXMLDocument
* doc
= [self processData
:[socket receive
]];
321 NSXMLElement
* xmlframe
= [[[doc rootElement
] children
] objectAtIndex
:0];
323 // get the names of all the contexts
324 [socket send
:[self createCommand
:@
"context_names -d 0"]];
325 NSXMLElement
* contextNames
= [[self processData
:[socket receive
]] rootElement
];
326 NSMutableArray
* variables
= [NSMutableArray array
];
327 for (NSXMLElement
* context
in [contextNames children
])
329 NSString
* name
= [[context attributeForName
:@
"name"] stringValue
];
330 int cid
= [[[context attributeForName
:@
"id"] stringValue
] intValue
];
332 // fetch the contexts
333 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"context_get -d %d -c %d", stackDepth
, cid
]]];
334 NSArray
* addVars
= [[[self processData
:[socket receive
]] rootElement
] children
];
335 if (addVars
!= nil && name
!= nil)
336 [variables addObjectsFromArray
:addVars
];
340 NSString
* filename
= [[xmlframe attributeForName
:@
"filename"] stringValue
];
341 NSString
* escapedFilename
= [filename stringByReplacingOccurrencesOfString
:@
"%" withString
:@
"%%"]; // escape % in URL chars
342 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"source -f %@", escapedFilename
]]];
343 NSString
* source
= [[[self processData
:[socket receive
]] rootElement
] value
]; // decode base64
345 // create stack frame
346 StackFrame
* frame
= [[StackFrame alloc
]
347 initWithIndex
:stackDepth
348 withFilename
:filename
350 atLine
:[[[xmlframe attributeForName
:@
"lineno"] stringValue
] intValue
]
351 inFunction
:[[xmlframe attributeForName
:@
"where"] stringValue
]
352 withVariables
:variables
355 return [frame autorelease
];
359 * Creates a StackFrame based on the current position in the debugger
361 - (StackFrame
*)createCurrentStackFrame
363 return [self createStackFrame
:0];
367 * Fetches the value of and sets the status instance variable
371 [socket send
:[self createCommand
:@
"status"]];
372 NSXMLDocument
* doc
= [self processData
:[socket receive
]];
373 self.status
= [[[[doc rootElement
] attributeForName
:@
"status"] stringValue
] capitalizedString
];
375 if (status
== nil ||
[status isEqualToString
:@
"Stopped"] ||
[status isEqualToString
:@
"Stopping"])
380 [delegate debuggerDisconnected
];
382 self.status
= @
"Stopped";
387 * Given a file path, this returns a file:// URI and escapes any spaces for the
390 - (NSString
*)escapedURIPath
:(NSString
*)path
392 // Custon GDBp paths are fine.
393 if ([[path substringToIndex
:4] isEqualToString
:@
"gdbp"])
396 // Create a temporary URL that will escape all the nasty characters.
397 NSURL
* url
= [NSURL fileURLWithPath
:path
];
398 NSString
* urlString
= [url absoluteString
];
400 // Remove the host because this is a file:// URL;
401 urlString
= [urlString stringByReplacingOccurrencesOfString
:[url host
] withString
:@
""];
403 // Escape % for use in printf-style NSString formatters.
404 urlString
= [urlString stringByReplacingOccurrencesOfString
:@
"%" withString
:@
"%%"];
409 * Helper method for |-socketDidAccept| to be called on the main thread.
411 - (void)doSocketAccept
:_nil
417 // register any breakpoints that exist offline
418 for (Breakpoint
* bp
in [[BreakpointManager sharedManager
] breakpoints
])
420 [self addBreakpoint
:bp
];
423 // Load the debugger to make it look active.
424 [delegate debuggerConnected
];