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 NSString
* kErrorOccurredNotif
= @
"GDBpConnection_ErrorOccured_Notification";
22 @interface GDBpConnection()
23 @property(readwrite
, copy
) NSString
* status
;
25 - (NSString
*)createCommand
:(NSString
*)cmd
, ...
;
26 - (NSXMLDocument
*)processData
:(NSString
*)data
;
27 - (StackFrame
*)createStackFrame
:(int)depth
;
28 - (StackFrame
*)createCurrentStackFrame
;
30 - (NSString
*)escapedURIPath
:(NSString
*)path
;
33 @implementation GDBpConnection
35 @synthesize socket
, status
;
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
] initWithConnection
:self];
51 [socket setDelegate
: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
:(id)obj
118 // register any breakpoints that exist offline
119 for (Breakpoint
* bp
in [[BreakpointManager sharedManager
] breakpoints
])
121 [self addBreakpoint
:bp
];
124 [[[NSApp delegate
] debugger
] startDebugger
]; // this will just load the debugger to make it look active
128 * Receives errors from the SocketWrapper and updates the display
130 - (void)errorEncountered
:(NSString
*)error
132 [[NSNotificationCenter defaultCenter
]
133 postNotificationName
:kErrorOccurredNotif
135 userInfo
:[NSDictionary
136 dictionaryWithObject
:error
143 * Reestablishes communication with the remote debugger so that a new connection doesn't have to be
144 * created every time you want to debug a page
149 self.status
= @
"Connecting";
154 * Creates an entirely new stack and returns it as an array of StackFrame objects.
156 - (NSArray
*)getCurrentStack
158 // get the total stack depth
159 [socket send
:[self createCommand
:@
"stack_depth"]];
160 NSXMLDocument
* doc
= [self processData
:[socket receive
]];
161 int depth
= [[[[doc rootElement
] attributeForName
:@
"depth"] stringValue
] intValue
];
163 // get all stack frames
164 NSMutableArray
* stack
= [NSMutableArray arrayWithCapacity
:depth
];
165 for (int i
= 0; i
< depth
; i
++)
167 StackFrame
* frame
= [self createStackFrame
:i
];
168 [stack insertObject
:frame atIndex
:i
];
175 * Tells the debugger to continue running the script. Returns the current stack frame.
179 [socket send
:[self createCommand
:@
"run"]];
186 * Tells the debugger to step into the current command.
190 [socket send
:[self createCommand
:@
"step_into"]];
198 * Tells the debugger to step out of the current context
202 [socket send
:[self createCommand
:@
"step_out"]];
209 * Tells the debugger to step over the current function
213 [socket send
:[self createCommand
:@
"step_over"]];
220 * Tells the debugger engine to get a specifc property. This also takes in the NSXMLElement
221 * that requested it so that the child can be attached.
223 - (NSArray
*)getProperty
:(NSString
*)property
225 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"property_get -n \"%@\"", property
]]];
227 NSXMLDocument
* doc
= [self processData
:[socket receive
]];
231 <property> <!-- this is the one we requested -->
232 <property ... /> <!-- these are what we want -->
237 // we now have to detach all the children so we can insert them into another document
238 NSXMLElement
* parent
= (NSXMLElement
*)[[doc rootElement
] childAtIndex
:0];
239 NSArray
* children
= [parent children
];
240 [parent setChildren
:nil];
244 #pragma mark Breakpoints
247 * Send an add breakpoint command
249 - (void)addBreakpoint
:(Breakpoint
*)bp
254 NSString
* file
= [self escapedURIPath
:[bp transformedPath
]];
255 NSString
* cmd
= [self createCommand
:[NSString stringWithFormat
:@
"breakpoint_set -t line -f %@ -n %i", file
, [bp line
]]];
257 NSXMLDocument
* info
= [self processData
:[socket receive
]];
258 [bp setDebuggerId
:[[[[info rootElement
] attributeForName
:@
"id"] stringValue
] intValue
]];
262 * Removes a breakpoint
264 - (void)removeBreakpoint
:(Breakpoint
*)bp
271 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"breakpoint_remove -d %i", [bp debuggerId
]]]];
278 * Helper method to create a string command with the -i <session> automatically tacked on. Takes
279 * a variable number of arguments and parses the given command with +[NSString stringWithFormat:]
281 - (NSString
*)createCommand
:(NSString
*)cmd
, ...
285 va_start(argList
, cmd
);
286 NSString
* format
= [[NSString alloc
] initWithFormat
:cmd arguments
:argList
]; // format the command
290 NSLog(@
"--> %@", format
);
293 return [NSString stringWithFormat
:@
"%@ -i %@", [format autorelease
], session
];
297 * Helper function to parse the NSData into an NSXMLDocument
299 - (NSXMLDocument
*)processData
:(NSString
*)data
304 NSError
* parseError
= nil;
305 NSXMLDocument
* doc
= [[NSXMLDocument alloc
] initWithXMLString
:data options
:0 error
:&parseError
];
308 NSLog(@
"Could not parse XML? --- %@", parseError
);
309 NSLog(@
"Error UserInfo: %@", [parseError userInfo
]);
310 NSLog(@
"This is the XML Document: %@", data
);
314 // check and see if there's an error
315 NSArray
* error
= [[doc rootElement
] elementsForName
:@
"error"];
316 if ([error count
] > 0)
318 NSLog(@
"Xdebug error: %@", error
);
319 [self errorEncountered
:[[[[error objectAtIndex
:0] children
] objectAtIndex
:0] stringValue
]];
324 NSLog(@
"<-- %@", doc
);
327 return [doc autorelease
];
331 * Generates a stack frame for the given depth
333 - (StackFrame
*)createStackFrame
:(int)stackDepth
335 // get the stack frame
336 [socket send
:[self createCommand
:@
"stack_get -d %d", stackDepth
]];
337 NSXMLDocument
* doc
= [self processData
:[socket receive
]];
341 NSXMLElement
* xmlframe
= [[[doc rootElement
] children
] objectAtIndex
:0];
343 // get the names of all the contexts
344 [socket send
:[self createCommand
:@
"context_names -d 0"]];
345 NSXMLElement
* contextNames
= [[self processData
:[socket receive
]] rootElement
];
346 NSMutableArray
* variables
= [NSMutableArray array
];
347 for (NSXMLElement
* context
in [contextNames children
])
349 NSString
* name
= [[context attributeForName
:@
"name"] stringValue
];
350 int cid
= [[[context attributeForName
:@
"id"] stringValue
] intValue
];
352 // fetch the contexts
353 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"context_get -d %d -c %d", stackDepth
, cid
]]];
354 NSArray
* addVars
= [[[self processData
:[socket receive
]] rootElement
] children
];
355 if (addVars
!= nil && name
!= nil)
356 [variables addObjectsFromArray
:addVars
];
360 NSString
* filename
= [[xmlframe attributeForName
:@
"filename"] stringValue
];
361 NSString
* escapedFilename
= [filename stringByReplacingOccurrencesOfString
:@
"%" withString
:@
"%%"]; // escape % in URL chars
362 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"source -f %@", escapedFilename
]]];
363 NSString
* source
= [[[self processData
:[socket receive
]] rootElement
] value
]; // decode base64
365 // create stack frame
366 StackFrame
* frame
= [[StackFrame alloc
]
367 initWithIndex
:stackDepth
368 withFilename
:filename
370 atLine
:[[[xmlframe attributeForName
:@
"lineno"] stringValue
] intValue
]
371 inFunction
:[[xmlframe attributeForName
:@
"where"] stringValue
]
372 withVariables
:variables
375 return [frame autorelease
];
379 * Creates a StackFrame based on the current position in the debugger
381 - (StackFrame
*)createCurrentStackFrame
383 return [self createStackFrame
:0];
387 * Fetches the value of and sets the status instance variable
391 [socket send
:[self createCommand
:@
"status"]];
392 NSXMLDocument
* doc
= [self processData
:[socket receive
]];
393 self.status
= [[[[doc rootElement
] attributeForName
:@
"status"] stringValue
] capitalizedString
];
395 if (status
== nil ||
[status isEqualToString
:@
"Stopped"] ||
[status isEqualToString
:@
"Stopping"])
399 self.status
= @
"Stopped";
404 * Given a file path, this returns a file:// URI and escapes any spaces for the
407 - (NSString
*)escapedURIPath
:(NSString
*)path
409 // Custon GDBp paths are fine.
410 if ([[path substringToIndex
:4] isEqualToString
:@
"gdbp"])
413 // Create a temporary URL that will escape all the nasty characters.
414 NSURL
* url
= [NSURL fileURLWithPath
:path
];
415 NSString
* urlString
= [url absoluteString
];
417 // Remove the host because this is a file:// URL;
418 urlString
= [urlString stringByReplacingOccurrencesOfString
:[url host
] withString
:@
""];
420 // Escape % for use in printf-style NSString formatters.
421 urlString
= [urlString stringByReplacingOccurrencesOfString
:@
"%" withString
:@
"%%"];