3 * Copyright (c) 2007 - 2010, 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 "DebuggerProcessor.h"
19 #import "AppDelegate.h"
21 // GDBpConnection (Private) ////////////////////////////////////////////////////
23 @interface DebuggerProcessor ()
24 @property (readwrite
, copy
) NSString
* status
;
26 - (void)initReceived
:(NSXMLDocument
*)response
;
27 - (void)updateStatus
:(NSXMLDocument
*)response
;
28 - (void)debuggerStep
:(NSXMLDocument
*)response
;
29 - (void)rebuildStack
:(NSXMLDocument
*)response
;
30 - (void)getStackFrame
:(NSXMLDocument
*)response
;
31 - (void)setSource
:(NSXMLDocument
*)response
;
32 - (void)contextsReceived
:(NSXMLDocument
*)response
;
33 - (void)variablesReceived
:(NSXMLDocument
*)response
;
34 - (void)propertiesReceived
:(NSXMLDocument
*)response
;
38 // GDBpConnection //////////////////////////////////////////////////////////////
40 @implementation DebuggerProcessor
46 * Creates a new DebuggerConnection and initializes the socket from the given connection
49 - (id)initWithPort
:(NSUInteger
)aPort
51 if (self = [super init
])
56 stackFrames_
= [[NSMutableDictionary alloc
] init
];
57 callbackContext_
= [NSMutableDictionary
new];
59 [[BreakpointManager sharedManager
] setConnection
:self];
66 * Deallocates the object
71 [stackFrames_ release
];
72 [callbackContext_ release
];
77 // Getters /////////////////////////////////////////////////////////////////////
81 * Gets the port number
89 * Returns the name of the remote host
91 - (NSString
*)remoteHost
95 return @
"(DISCONNECTED)";
97 // TODO: Either impl or remove.
102 * Returns whether or not we have an active connection
109 // Commands ////////////////////////////////////////////////////////////////////
110 #pragma mark Commands
113 * Reestablishes communication with the remote debugger so that a new connection doesn't have to be
114 * created every time you want to debug a page
119 self.status
= @
"Connecting";
124 * Tells the debugger to continue running the script. Returns the current stack frame.
128 [self sendCommandWithCallback
:@selector(debuggerStep
:) format
:@
"run"];
132 * Tells the debugger to step into the current command.
136 [self sendCommandWithCallback
:@selector(debuggerStep
:) format
:@
"step_into"];
140 * Tells the debugger to step out of the current context
144 [self sendCommandWithCallback
:@selector(debuggerStep
:) format
:@
"step_out"];
148 * Tells the debugger to step over the current function
152 [self sendCommandWithCallback
:@selector(debuggerStep
:) format
:@
"step_over"];
156 * Tells the debugger engine to get a specifc property. This also takes in the NSXMLElement
157 * that requested it so that the child can be attached.
159 - (NSInteger
)getProperty
:(NSString
*)property
161 [self sendCommandWithCallback
:@selector(propertiesReceived
:) format
:@
"property_get -n \"%@\"", property
];
164 // Breakpoint Management ///////////////////////////////////////////////////////
165 #pragma mark Breakpoints
168 * Send an add breakpoint command
170 - (void)addBreakpoint
:(Breakpoint
*)bp
175 NSString
* file
= [self escapedURIPath
:[bp transformedPath
]];
176 NSNumber
* transaction
= [self sendCommandWithCallback
:@selector(breakpointReceived
:)
177 format
:@
"breakpoint_set -t line -f %@ -n %i", file
, [bp line
]];
178 [callbackContext_ setObject
:bp forKey
:transaction
];
182 * Removes a breakpoint
184 - (void)removeBreakpoint
:(Breakpoint
*)bp
191 [self sendCommandWithCallback
:nil format
:@
"breakpoint_remove -d %i", [bp debuggerId
]];
194 // Specific Response Handlers //////////////////////////////////////////////////
195 #pragma mark Response Handlers
198 * Initial packet received. We've started a brand-new connection to the engine.
200 - (void)initReceived
:(NSXMLDocument
*)response
202 // Register any breakpoints that exist offline.
203 for (Breakpoint
* bp
in [[BreakpointManager sharedManager
] breakpoints
])
204 [self addBreakpoint
:bp
];
206 // Load the debugger to make it look active.
207 [delegate debuggerConnected
];
209 // TODO: update the status.
213 * Receiver for status updates. This just freshens up the UI.
215 - (void)updateStatus
:(NSXMLDocument
*)response
217 self.status
= [[[[response rootElement
] attributeForName
:@
"status"] stringValue
] capitalizedString
];
218 if (status
== nil ||
[status isEqualToString
:@
"Stopped"] ||
[status isEqualToString
:@
"Stopping"])
222 [delegate debuggerDisconnected
];
224 self.status
= @
"Stopped";
229 * Step in/out/over and run all take this path. We first get the status of the
230 * debugger and then request fresh stack information.
232 - (void)debuggerStep
:(NSXMLDocument
*)response
234 [self updateStatus
:response
];
238 // If this is the run command, tell the delegate that a bunch of updates
239 // are coming. Also remove all existing stack routes and request a new stack.
240 // TODO: figure out if we can not clobber the stack every time.
241 NSString
* command
= [[[response rootElement
] attributeForName
:@
"command"] stringValue
];
242 if (YES ||
[command isEqualToString
:@
"run"])
244 if ([delegate respondsToSelector
:@selector(clobberStack
)])
245 [delegate clobberStack
];
246 [stackFrames_ removeAllObjects
];
247 stackFirstTransactionID_
= [[self sendCommandWithCallback
:@selector(rebuildStack
:) format
:@
"stack_depth"] intValue
];
252 * We ask for the stack_depth and now we clobber the stack and start rebuilding
255 - (void)rebuildStack
:(NSXMLDocument
*)response
257 NSInteger depth
= [[[[response rootElement
] attributeForName
:@
"depth"] stringValue
] intValue
];
259 if (stackFirstTransactionID_
== [self transactionIDFromResponse
:response
])
262 // We now need to alloc a bunch of stack frames and get the basic information
264 for (NSInteger i
= 0; i
< depth
; i
++)
266 // Use the transaction ID to create a routing path.
267 NSNumber
* routingID
= [self sendCommandWithCallback
:@selector(getStackFrame
:) format
:@
"stack_get -d %d", i
];
268 [stackFrames_ setObject
:[StackFrame alloc
] forKey
:routingID
];
273 * The initial rebuild of the stack frame. We now have enough to initialize
274 * a StackFrame object.
276 - (void)getStackFrame
:(NSXMLDocument
*)response
278 // Get the routing information.
279 NSInteger routingID
= [self transactionIDFromResponse
:response
];
280 if (routingID
< stackFirstTransactionID_
)
282 NSNumber
* routingNumber
= [NSNumber numberWithInt
:routingID
];
284 // Make sure we initialized this frame in our last |-rebuildStack:|.
285 StackFrame
* frame
= [stackFrames_ objectForKey
:routingNumber
];
289 NSXMLElement
* xmlframe
= [[[response rootElement
] children
] objectAtIndex
:0];
291 // Initialize the stack frame.
292 [frame initWithIndex
:[[[xmlframe attributeForName
:@
"level"] stringValue
] intValue
]
293 withFilename
:[[xmlframe attributeForName
:@
"filename"] stringValue
]
295 atLine
:[[[xmlframe attributeForName
:@
"lineno"] stringValue
] intValue
]
296 inFunction
:[[xmlframe attributeForName
:@
"where"] stringValue
]
299 // Get the source code of the file. Escape % in URL chars.
300 NSString
* escapedFilename
= [frame.filename stringByReplacingOccurrencesOfString
:@
"%" withString
:@
"%%"];
301 NSNumber
* transaction
= [self sendCommandWithCallback
:@selector(setSource
:) format
:@
"source -f %@", escapedFilename
];
302 [callbackContext_ setObject
:routingNumber forKey
:transaction
];
304 // Get the names of all the contexts.
305 transaction
= [self sendCommandWithCallback
:@selector(contextsReceived
:) format
:@
"context_names -d %d", frame.index
];
306 [callbackContext_ setObject
:routingNumber forKey
:transaction
];
308 if ([delegate respondsToSelector
:@selector(newStackFrame
:)])
309 [delegate newStackFrame
:frame
];
313 * Callback for setting the source of a file while rebuilding a specific stack
316 - (void)setSource
:(NSXMLDocument
*)response
318 NSNumber
* transaction
= [NSNumber numberWithInt
:[self transactionIDFromResponse
:response
]];
319 if ([transaction intValue
] < stackFirstTransactionID_
)
321 NSNumber
* routingNumber
= [callbackContext_ objectForKey
:transaction
];
325 [callbackContext_ removeObjectForKey
:transaction
];
326 StackFrame
* frame
= [stackFrames_ objectForKey
:routingNumber
];
330 frame.source
= [[response rootElement
] value
];
332 if ([delegate respondsToSelector
:@selector(sourceUpdated
:)])
333 [delegate sourceUpdated
:frame
];
337 * Enumerates all the contexts of a given stack frame. We then in turn get the
338 * contents of each one of these contexts.
340 - (void)contextsReceived
:(NSXMLDocument
*)response
342 // Get the stack frame's routing ID and use it again.
343 NSNumber
* receivedTransaction
= [NSNumber numberWithInt
:[self transactionIDFromResponse
:response
]];
344 if ([receivedTransaction intValue
] < stackFirstTransactionID_
)
346 NSNumber
* routingID
= [callbackContext_ objectForKey
:receivedTransaction
];
350 // Get the stack frame by the |routingID|.
351 StackFrame
* frame
= [stackFrames_ objectForKey
:routingID
];
353 NSXMLElement
* contextNames
= [response rootElement
];
354 for (NSXMLElement
* context
in [contextNames children
])
356 NSInteger cid
= [[[context attributeForName
:@
"id"] stringValue
] intValue
];
358 // Fetch each context's variables.
359 NSNumber
* transaction
= [self sendCommandWithCallback
:@selector(variablesReceived
:)
360 format
:@
"context_get -d %d -c %d", frame.index
, cid
];
361 [callbackContext_ setObject
:routingID forKey
:transaction
];
366 * Receives the variables from the context and attaches them to the stack frame.
368 - (void)variablesReceived
:(NSXMLDocument
*)response
370 // Get the stack frame's routing ID and use it again.
371 NSInteger transaction
= [self transactionIDFromResponse
:response
];
372 if (transaction
< stackFirstTransactionID_
)
374 NSNumber
* receivedTransaction
= [NSNumber numberWithInt
:transaction
];
375 NSNumber
* routingID
= [callbackContext_ objectForKey
:receivedTransaction
];
379 // Get the stack frame by the |routingID|.
380 StackFrame
* frame
= [stackFrames_ objectForKey
:routingID
];
382 NSMutableArray
* variables
= [NSMutableArray array
];
384 // Merge the frame's existing variables.
386 [variables addObjectsFromArray
:frame.variables
];
388 // Add these new variables.
389 NSArray
* addVariables
= [[response rootElement
] children
];
391 [variables addObjectsFromArray
:addVariables
];
393 frame.variables
= variables
;
397 * Callback from a |-getProperty:| request.
399 - (void)propertiesReceived
:(NSXMLDocument
*)response
401 NSInteger transaction
= [self transactionIDFromResponse
:response
];
405 <property> <!-- this is the one we requested -->
406 <property ... /> <!-- these are what we want -->
411 // Detach all the children so we can insert them into another document.
412 NSXMLElement
* parent
= (NSXMLElement
*)[[response rootElement
] childAtIndex
:0];
413 NSArray
* children
= [parent children
];
414 [parent setChildren
:nil];
416 [delegate receivedProperties
:children forTransaction
:transaction
];
420 * Callback for setting a breakpoint.
422 - (void)breakpointReceived
:(NSXMLDocument
*)response
424 NSNumber
* transaction
= [NSNumber numberWithInt
:[self transactionIDFromResponse
:response
]];
425 Breakpoint
* bp
= [callbackContext_ objectForKey
:transaction
];
429 [callbackContext_ removeObjectForKey
:callbackContext_
];
430 [bp setDebuggerId
:[[[[response rootElement
] attributeForName
:@
"id"] stringValue
] intValue
]];
434 * Given a file path, this returns a file:// URI and escapes any spaces for the
437 - (NSString
*)escapedURIPath
:(NSString
*)path
439 // Custon GDBp paths are fine.
440 if ([[path substringToIndex
:4] isEqualToString
:@
"gdbp"])
443 // Create a temporary URL that will escape all the nasty characters.
444 NSURL
* url
= [NSURL fileURLWithPath
:path
];
445 NSString
* urlString
= [url absoluteString
];
447 // Remove the host because this is a file:// URL;
448 urlString
= [urlString stringByReplacingOccurrencesOfString
:[url host
] withString
:@
""];
450 // Escape % for use in printf-style NSString formatters.
451 urlString
= [urlString stringByReplacingOccurrencesOfString
:@
"%" withString
:@
"%%"];