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
] 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
118 // register any breakpoints that exist offline
119 for (Breakpoint
* bp
in [[BreakpointManager sharedManager
] breakpoints
])
121 [self addBreakpoint
:bp
];
124 // Load the debugger to make it look active.
125 [[[NSApp delegate
] debugger
] performSelectorOnMainThread
:@selector(startDebugger
) withObject
:nil waitUntilDone
:YES
];
129 * Receives errors from the SocketWrapper and updates the display
131 - (void)errorEncountered
:(NSString
*)error
133 [[NSNotificationCenter defaultCenter
]
134 postNotificationName
:kErrorOccurredNotif
136 userInfo
:[NSDictionary
137 dictionaryWithObject
:error
144 * Reestablishes communication with the remote debugger so that a new connection doesn't have to be
145 * created every time you want to debug a page
150 self.status
= @
"Connecting";
155 * Creates an entirely new stack and returns it as an array of StackFrame objects.
157 - (NSArray
*)getCurrentStack
159 // get the total stack depth
160 [socket send
:[self createCommand
:@
"stack_depth"]];
161 NSXMLDocument
* doc
= [self processData
:[socket receive
]];
162 int depth
= [[[[doc rootElement
] attributeForName
:@
"depth"] stringValue
] intValue
];
164 // get all stack frames
165 NSMutableArray
* stack
= [NSMutableArray arrayWithCapacity
:depth
];
166 for (int i
= 0; i
< depth
; i
++)
168 StackFrame
* frame
= [self createStackFrame
:i
];
169 [stack insertObject
:frame atIndex
:i
];
176 * Tells the debugger to continue running the script. Returns the current stack frame.
180 [socket send
:[self createCommand
:@
"run"]];
187 * Tells the debugger to step into the current command.
191 [socket send
:[self createCommand
:@
"step_into"]];
199 * Tells the debugger to step out of the current context
203 [socket send
:[self createCommand
:@
"step_out"]];
210 * Tells the debugger to step over the current function
214 [socket send
:[self createCommand
:@
"step_over"]];
221 * Tells the debugger engine to get a specifc property. This also takes in the NSXMLElement
222 * that requested it so that the child can be attached.
224 - (NSArray
*)getProperty
:(NSString
*)property
226 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"property_get -n \"%@\"", property
]]];
228 NSXMLDocument
* doc
= [self processData
:[socket receive
]];
232 <property> <!-- this is the one we requested -->
233 <property ... /> <!-- these are what we want -->
238 // we now have to detach all the children so we can insert them into another document
239 NSXMLElement
* parent
= (NSXMLElement
*)[[doc rootElement
] childAtIndex
:0];
240 NSArray
* children
= [parent children
];
241 [parent setChildren
:nil];
245 #pragma mark Breakpoints
248 * Send an add breakpoint command
250 - (void)addBreakpoint
:(Breakpoint
*)bp
255 NSString
* file
= [self escapedURIPath
:[bp transformedPath
]];
256 NSString
* cmd
= [self createCommand
:[NSString stringWithFormat
:@
"breakpoint_set -t line -f %@ -n %i", file
, [bp line
]]];
258 NSXMLDocument
* info
= [self processData
:[socket receive
]];
259 [bp setDebuggerId
:[[[[info rootElement
] attributeForName
:@
"id"] stringValue
] intValue
]];
263 * Removes a breakpoint
265 - (void)removeBreakpoint
:(Breakpoint
*)bp
272 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"breakpoint_remove -d %i", [bp debuggerId
]]]];
279 * Helper method to create a string command with the -i <session> automatically tacked on. Takes
280 * a variable number of arguments and parses the given command with +[NSString stringWithFormat:]
282 - (NSString
*)createCommand
:(NSString
*)cmd
, ...
286 va_start(argList
, cmd
);
287 NSString
* format
= [[NSString alloc
] initWithFormat
:cmd arguments
:argList
]; // format the command
291 NSLog(@
"--> %@", format
);
294 return [NSString stringWithFormat
:@
"%@ -i %@", [format autorelease
], session
];
298 * Helper function to parse the NSData into an NSXMLDocument
300 - (NSXMLDocument
*)processData
:(NSString
*)data
305 NSError
* parseError
= nil;
306 NSXMLDocument
* doc
= [[NSXMLDocument alloc
] initWithXMLString
:data options
:0 error
:&parseError
];
309 NSLog(@
"Could not parse XML? --- %@", parseError
);
310 NSLog(@
"Error UserInfo: %@", [parseError userInfo
]);
311 NSLog(@
"This is the XML Document: %@", data
);
315 // check and see if there's an error
316 NSArray
* error
= [[doc rootElement
] elementsForName
:@
"error"];
317 if ([error count
] > 0)
319 NSLog(@
"Xdebug error: %@", error
);
320 [self errorEncountered
:[[[[error objectAtIndex
:0] children
] objectAtIndex
:0] stringValue
]];
325 NSLog(@
"<-- %@", doc
);
328 return [doc autorelease
];
332 * Generates a stack frame for the given depth
334 - (StackFrame
*)createStackFrame
:(int)stackDepth
336 // get the stack frame
337 [socket send
:[self createCommand
:@
"stack_get -d %d", stackDepth
]];
338 NSXMLDocument
* doc
= [self processData
:[socket receive
]];
342 NSXMLElement
* xmlframe
= [[[doc rootElement
] children
] objectAtIndex
:0];
344 // get the names of all the contexts
345 [socket send
:[self createCommand
:@
"context_names -d 0"]];
346 NSXMLElement
* contextNames
= [[self processData
:[socket receive
]] rootElement
];
347 NSMutableArray
* variables
= [NSMutableArray array
];
348 for (NSXMLElement
* context
in [contextNames children
])
350 NSString
* name
= [[context attributeForName
:@
"name"] stringValue
];
351 int cid
= [[[context attributeForName
:@
"id"] stringValue
] intValue
];
353 // fetch the contexts
354 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"context_get -d %d -c %d", stackDepth
, cid
]]];
355 NSArray
* addVars
= [[[self processData
:[socket receive
]] rootElement
] children
];
356 if (addVars
!= nil && name
!= nil)
357 [variables addObjectsFromArray
:addVars
];
361 NSString
* filename
= [[xmlframe attributeForName
:@
"filename"] stringValue
];
362 NSString
* escapedFilename
= [filename stringByReplacingOccurrencesOfString
:@
"%" withString
:@
"%%"]; // escape % in URL chars
363 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"source -f %@", escapedFilename
]]];
364 NSString
* source
= [[[self processData
:[socket receive
]] rootElement
] value
]; // decode base64
366 // create stack frame
367 StackFrame
* frame
= [[StackFrame alloc
]
368 initWithIndex
:stackDepth
369 withFilename
:filename
371 atLine
:[[[xmlframe attributeForName
:@
"lineno"] stringValue
] intValue
]
372 inFunction
:[[xmlframe attributeForName
:@
"where"] stringValue
]
373 withVariables
:variables
376 return [frame autorelease
];
380 * Creates a StackFrame based on the current position in the debugger
382 - (StackFrame
*)createCurrentStackFrame
384 return [self createStackFrame
:0];
388 * Fetches the value of and sets the status instance variable
392 [socket send
:[self createCommand
:@
"status"]];
393 NSXMLDocument
* doc
= [self processData
:[socket receive
]];
394 self.status
= [[[[doc rootElement
] attributeForName
:@
"status"] stringValue
] capitalizedString
];
396 if (status
== nil ||
[status isEqualToString
:@
"Stopped"] ||
[status isEqualToString
:@
"Stopping"])
400 self.status
= @
"Stopped";
405 * Given a file path, this returns a file:// URI and escapes any spaces for the
408 - (NSString
*)escapedURIPath
:(NSString
*)path
410 // Custon GDBp paths are fine.
411 if ([[path substringToIndex
:4] isEqualToString
:@
"gdbp"])
414 // Create a temporary URL that will escape all the nasty characters.
415 NSURL
* url
= [NSURL fileURLWithPath
:path
];
416 NSString
* urlString
= [url absoluteString
];
418 // Remove the host because this is a file:// URL;
419 urlString
= [urlString stringByReplacingOccurrencesOfString
:[url host
] withString
:@
""];
421 // Escape % for use in printf-style NSString formatters.
422 urlString
= [urlString stringByReplacingOccurrencesOfString
:@
"%" withString
:@
"%%"];