3 * Copyright (c) 2007 - 2011, 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 "DebuggerBackEnd.h"
19 #import "AppDelegate.h"
20 #import "Breakpoint.h"
21 #import "DebuggerModel.h"
22 #import "NSXMLElementAdditions.h"
23 #import "StackFrame.h"
25 @implementation DebuggerBackEnd
{
26 // The connection to the debugger engine.
28 ProtocolClient
* _client
;
31 - (instancetype
)initWithPort
:(NSUInteger
)aPort autoAttach
:(BOOL)doAttach
33 if (self = [super init
]) {
35 _client
= [[ProtocolClient alloc
] initWithDelegate
:self];
37 _autoAttach
= doAttach
;
39 [_client connectOnPort
:_port
];
49 // Getters /////////////////////////////////////////////////////////////////////
53 * Gets the port number
60 * Sets the attached state of the debugger. This will open and close the
61 * connection as appropriate.
63 - (void)setAutoAttach
:(BOOL)flag
{
64 if (flag
== _autoAttach
)
70 [_client connectOnPort
:_port
];
75 // Commands ////////////////////////////////////////////////////////////////////
79 * Tells the debugger to continue running the script. Returns the current stack frame.
82 [_client sendCommandWithFormat
:@
"run" handler
:^
(NSXMLDocument
* message
) {
83 [self debuggerStep
:message
];
88 * Tells the debugger to step into the current command.
91 [_client sendCommandWithFormat
:@
"step_into" handler
:^
(NSXMLDocument
* message
) {
92 [self debuggerStep
:message
];
97 * Tells the debugger to step out of the current context
100 [_client sendCommandWithFormat
:@
"step_out" handler
:^
(NSXMLDocument
* message
) {
101 [self debuggerStep
:message
];
106 * Tells the debugger to step over the current function
109 [_client sendCommandWithFormat
:@
"step_over" handler
:^
(NSXMLDocument
* message
) {
110 [self debuggerStep
:message
];
115 * Halts execution of the script.
118 [_client disconnect
];
119 self.model.status
= @
"Stopped";
123 * Ends the current debugging session.
126 [_client sendCommandWithFormat
:@
"detach"];
127 self.model.status
= @
"Stopped";
130 - (void)loadStackFrame
:(StackFrame
*)frame
{
134 // Get the source code of the file. Escape % in URL chars.
135 if ([frame.filename length
]) {
136 ProtocolClientMessageHandler handler
= ^
(NSXMLDocument
* message
) {
137 frame.source
= [[message rootElement
] base64DecodedValue
];
139 [_client sendCommandWithFormat
:@
"source -f %@" handler
:handler
, frame.filename
];
142 // Get the names of all the contexts.
143 ProtocolClientMessageHandler handler
= ^
(NSXMLDocument
* message
) {
144 [self loadContexts
:message forFrame
:frame
];
146 [_client sendCommandWithFormat
:@
"context_names -d %d" handler
:handler
, frame.index
];
148 // This frame will be fully loaded.
152 - (void)loadVariableNode
:(VariableNode
*)variable
153 forStackFrame
:(StackFrame
*)frame
{
154 if (variable.children.count
== variable.childCount
)
157 [self loadVariableNode
:variable forStackFrame
:frame dataPage
:0 loadedData
:@
[]];
160 - (void)loadVariableNode
:(VariableNode
*)variable
161 forStackFrame
:(StackFrame
*)frame
162 dataPage
:(unsigned int)dataPage
163 loadedData
:(NSArray
*)loadedData
{
164 ProtocolClientMessageHandler handler
= ^
(NSXMLDocument
* message
) {
167 <property> <!-- this is the one we requested -->
168 <property ... /> <!-- these are what we want -->
173 // Detach all the children so we can insert them into another document.
174 NSXMLElement
* parent
= (NSXMLElement
*)[[message rootElement
] childAtIndex
:0];
175 NSArray
* children
= [parent children
];
176 [parent setChildren
:nil];
178 // Check to see if there are more children to load.
179 NSArray
* newLoadedData
= [loadedData arrayByAddingObjectsFromArray
:children
];
181 NSInteger totalChildren
= [[[parent attributeForName
:@
"numchildren"] stringValue
] integerValue
];
182 if ([newLoadedData count
] < (NSUInteger
)totalChildren
) {
183 [self loadVariableNode
:variable
185 dataPage
:dataPage
+ 1
186 loadedData
:newLoadedData
];
188 [variable setChildrenFromXMLChildren
:newLoadedData
];
191 [_client sendCommandWithFormat
:@
"property_get -d %d -n %@ -p %u" handler
:handler
, frame.index
, variable.fullName
, dataPage
];
194 // Breakpoint Management ///////////////////////////////////////////////////////
195 #pragma mark Breakpoints
198 * Send an add breakpoint command
200 - (void)addBreakpoint
:(Breakpoint
*)bp
{
201 if (!self.model.connected
)
204 ProtocolClientMessageHandler handler
= ^
(NSXMLDocument
* message
) {
205 [bp setDebuggerId
:[[[[message rootElement
] attributeForName
:@
"id"] stringValue
] intValue
]];
207 if (bp.type
== kBreakpointTypeFile
) {
208 NSString
* file
= [ProtocolClient escapedFilePathURI
:[bp transformedPath
]];
209 [_client sendCommandWithFormat
:@
"breakpoint_set -t line -f %@ -n %i" handler
:handler
, file
, [bp line
]];
210 } else if (bp.type
== kBreakpointTypeFunctionEntry
) {
211 [_client sendCommandWithFormat
:@
"breakpoint_set -t call -m %@" handler
:handler
, bp.functionName
];
216 * Removes a breakpoint
218 - (void)removeBreakpoint
:(Breakpoint
*)bp
{
219 if (!self.model.connected
)
222 [_client sendCommandWithFormat
:@
"breakpoint_remove -d %i", [bp debuggerId
]];
226 * Sends a string to be evaluated by the engine.
228 - (void)evalScript
:(NSString
*)str callback
:(void (^
)(NSString
*))callback
{
229 if (!self.model.connected
)
232 NSData
* stringData
= [str dataUsingEncoding
:NSUTF8StringEncoding
];
233 ProtocolClientMessageHandler handler
= ^
(NSXMLDocument
* message
) {
234 NSXMLElement
* parent
= (NSXMLElement
*)[[message rootElement
] childAtIndex
:0];
235 NSString
* value
= [parent base64DecodedValue
];
238 [_client sendCustomCommandWithFormat
:@
"eval -i {txn} -- %@"
239 handler
:handler
, [stringData base64EncodedStringWithOptions
:0]];
242 // Protocol Client Delegate ////////////////////////////////////////////////////
243 #pragma mark Protocol Client Delegate
245 - (void)debuggerEngineConnected
:(ProtocolClient
*)client
{
246 [_model onNewConnection
];
250 * Called when the connection is finally closed. This will reopen the listening
251 * socket if the debugger remains attached.
253 - (void)debuggerEngineDisconnected
:(ProtocolClient
*)client
{
254 [_model onDisconnect
];
257 [_client connectOnPort
:_port
];
260 - (void)protocolClient
:(ProtocolClient
*)client receivedInitialMessage
:(NSXMLDocument
*)message
{
261 [self handleInitialResponse
:message
];
264 - (void)protocolClient
:(ProtocolClient
*)client receivedErrorMessage
:(NSXMLDocument
*)message
{
265 NSArray
* error
= [[message rootElement
] elementsForName
:@
"error"];
266 if ([error count
] > 0) {
267 NSLog(@
"Xdebug error: %@", error
);
268 NSString
* errorMessage
= [[[[error objectAtIndex
:0] children
] objectAtIndex
:0] stringValue
];
269 _model.lastError
= errorMessage
;
273 // Specific Response Handlers //////////////////////////////////////////////////
274 #pragma mark Response Handlers
277 * Initial packet received. We've started a brand-new connection to the engine.
279 - (void)handleInitialResponse
:(NSXMLDocument
*)response
{
280 if (!self.autoAttach
) {
281 [_client sendCommandWithFormat
:@
"detach"];
285 // Register any breakpoints that exist offline.
286 for (Breakpoint
* bp
in self.model.breakpointManager.breakpoints
)
287 [self addBreakpoint
:bp
];
289 // TODO: update the status.
293 * Receiver for status updates. This just freshens up the UI.
295 - (void)updateStatus
:(NSXMLDocument
*)response
{
296 NSString
* status
= [[[[response rootElement
] attributeForName
:@
"status"] stringValue
] capitalizedString
];
297 self.model.status
= status
;
298 if (!status ||
[status isEqualToString
:@
"Stopped"]) {
299 [_model onDisconnect
];
300 } else if ([status isEqualToString
:@
"Stopping"]) {
301 [_client sendCommandWithFormat
:@
"stop"];
306 * Step in/out/over and run all take this path. We first get the status of the
307 * debugger and then request fresh stack information.
309 - (void)debuggerStep
:(NSXMLDocument
*)response
{
310 [self updateStatus
:response
];
311 if (!self.model.connected
)
314 [_client sendCommandWithFormat
:@
"stack_depth" handler
:^
(NSXMLDocument
* message
) {
315 [self rebuildStack
:message
];
320 * We ask for the stack_depth and now we clobber the stack and start rebuilding
323 - (void)rebuildStack
:(NSXMLDocument
*)response
{
324 NSUInteger depth
= [[[[response rootElement
] attributeForName
:@
"depth"] stringValue
] intValue
];
326 // Send a request to get each frame of the stack, which will be added to this
327 // array. When the final frame arrives, the |tempStack| is released.
328 __block NSMutableArray
* tempStack
= [[NSMutableArray alloc
] init
];
330 for (NSUInteger i
= 0; i
< depth
; ++i
) {
331 ProtocolClientMessageHandler handler
= ^
(NSXMLDocument
* message
) {
332 [tempStack addObject
:[self transformXMLToStackFrame
:message
]];
333 if (i
== depth
- 1) {
334 [self.model updateStack
:[tempStack autorelease
]];
337 [_client sendCommandWithFormat
:@
"stack_get -d %d" handler
:handler
, i
];
342 * Creates a StackFrame object from an NSXMLDocument response from the "stack_get"
345 - (StackFrame
*)transformXMLToStackFrame
:(NSXMLDocument
*)response
{
346 NSXMLElement
* xmlframe
= (NSXMLElement
*)[[[response rootElement
] children
] objectAtIndex
:0];
347 StackFrame
* frame
= [[[StackFrame alloc
] init
] autorelease
];
348 frame.index
= [[[xmlframe attributeForName
:@
"level"] stringValue
] intValue
];
349 frame.filename
= [[xmlframe attributeForName
:@
"filename"] stringValue
];
350 frame.lineNumber
= [[[xmlframe attributeForName
:@
"lineno"] stringValue
] intValue
];
351 frame.function
= [[xmlframe attributeForName
:@
"where"] stringValue
];
356 * Enumerates all the contexts of a given stack frame. We then in turn get the
357 * contents of each one of these contexts.
359 - (void)loadContexts
:(NSXMLDocument
*)response forFrame
:(StackFrame
*)frame
{
360 NSXMLElement
* contextNames
= [response rootElement
];
361 for (NSXMLElement
* context
in [contextNames children
]) {
362 NSInteger cid
= [[[context attributeForName
:@
"id"] stringValue
] intValue
];
364 // Fetch each context's variables.
365 ProtocolClientMessageHandler handler
= ^
(NSXMLDocument
* message
) {
366 NSMutableArray
* variables
= [NSMutableArray array
];
368 // Merge the frame's existing variables.
370 [variables addObjectsFromArray
:frame.variables
];
372 // Add these new variables.
373 NSArray
* addVariables
= [[message rootElement
] children
];
375 for (NSXMLElement
* elm
in addVariables
) {
376 VariableNode
* node
= [[VariableNode alloc
] initWithXMLNode
:elm
];
377 [variables addObject
:[node autorelease
]];
381 frame.variables
= variables
;
383 [_client sendCommandWithFormat
:@
"context_get -d %d -c %d" handler
:handler
, frame.index
, cid
];