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
)initWithModel
:(DebuggerModel
*)model
32 port
:(NSUInteger
)aPort
33 autoAttach
:(BOOL)doAttach
35 if (self = [super init
]) {
38 _client
= [[ProtocolClient alloc
] initWithDelegate
:self];
40 [self setAutoAttach
:doAttach
];
50 // Getters /////////////////////////////////////////////////////////////////////
54 * Gets the port number
61 * Sets the attached state of the debugger. This will open and close the
62 * connection as appropriate.
64 - (void)setAutoAttach
:(BOOL)flag
{
65 if (flag
== _autoAttach
)
76 // Commands ////////////////////////////////////////////////////////////////////
80 * Tells the debugger to continue running the script. Returns the current stack frame.
83 [_client sendCommandWithFormat
:@
"run" handler
:^
(NSXMLDocument
* message
) {
84 [self debuggerStep
:message
];
89 * Tells the debugger to step into the current command.
92 [_client sendCommandWithFormat
:@
"step_into" handler
:^
(NSXMLDocument
* message
) {
93 [self debuggerStep
:message
];
98 * Tells the debugger to step out of the current context
101 [_client sendCommandWithFormat
:@
"step_out" handler
:^
(NSXMLDocument
* message
) {
102 [self debuggerStep
:message
];
107 * Tells the debugger to step over the current function
110 [_client sendCommandWithFormat
:@
"step_over" handler
:^
(NSXMLDocument
* message
) {
111 [self debuggerStep
:message
];
116 * Halts execution of the script.
119 [_client disconnect
];
120 self.model.status
= @
"Stopped";
124 * Ends the current debugging session.
127 [_client sendCommandWithFormat
:@
"detach"];
128 self.model.status
= @
"Stopped";
131 - (void)loadStackFrame
:(StackFrame
*)frame
{
135 // Get the source code of the file. Escape % in URL chars.
136 if ([frame.filename length
]) {
137 ProtocolClientMessageHandler handler
= ^
(NSXMLDocument
* message
) {
138 frame.source
= [[message rootElement
] base64DecodedValue
];
140 [_client sendCommandWithFormat
:@
"source -f %@" handler
:handler
, frame.filename
];
143 // Get the names of all the contexts.
144 ProtocolClientMessageHandler handler
= ^
(NSXMLDocument
* message
) {
145 [self loadContexts
:message forFrame
:frame
];
147 [_client sendCommandWithFormat
:@
"context_names -d %d" handler
:handler
, frame.index
];
149 // This frame will be fully loaded.
153 - (void)loadVariableNode
:(VariableNode
*)variable
154 forStackFrame
:(StackFrame
*)frame
{
155 if (variable.children.count
== variable.childCount
)
158 [self loadVariableNode
:variable forStackFrame
:frame dataPage
:0 loadedData
:@
[]];
161 - (void)loadVariableNode
:(VariableNode
*)variable
162 forStackFrame
:(StackFrame
*)frame
163 dataPage
:(unsigned int)dataPage
164 loadedData
:(NSArray
*)loadedData
{
165 ProtocolClientMessageHandler handler
= ^
(NSXMLDocument
* message
) {
168 <property> <!-- this is the one we requested -->
169 <property ... /> <!-- these are what we want -->
174 // Detach all the children so we can insert them into another document.
175 NSXMLElement
* parent
= (NSXMLElement
*)[[message rootElement
] childAtIndex
:0];
176 NSArray
* children
= [parent children
];
177 [parent setChildren
:nil];
179 // Check to see if there are more children to load.
180 NSArray
* newLoadedData
= [loadedData arrayByAddingObjectsFromArray
:children
];
182 NSInteger totalChildren
= [[[parent attributeForName
:@
"numchildren"] stringValue
] integerValue
];
183 if ([newLoadedData count
] < (NSUInteger
)totalChildren
) {
184 [self loadVariableNode
:variable
186 dataPage
:dataPage
+ 1
187 loadedData
:newLoadedData
];
189 [variable setChildrenFromXMLChildren
:newLoadedData
];
192 [_client sendCommandWithFormat
:@
"property_get -d %d -n %@ -p %u" handler
:handler
, frame.index
, variable.fullName
, dataPage
];
195 // Breakpoint Management ///////////////////////////////////////////////////////
196 #pragma mark Breakpoints
199 * Send an add breakpoint command
201 - (void)addBreakpoint
:(Breakpoint
*)bp
{
202 if (!self.model.connected
)
205 ProtocolClientMessageHandler handler
= ^
(NSXMLDocument
* message
) {
206 [bp setDebuggerId
:[[[[message rootElement
] attributeForName
:@
"id"] stringValue
] intValue
]];
208 if (bp.type
== kBreakpointTypeFile
) {
209 NSString
* file
= [ProtocolClient escapedFilePathURI
:[bp transformedPath
]];
210 [_client sendCommandWithFormat
:@
"breakpoint_set -t line -f %@ -n %i" handler
:handler
, file
, [bp line
]];
211 } else if (bp.type
== kBreakpointTypeFunctionEntry
) {
212 [_client sendCommandWithFormat
:@
"breakpoint_set -t call -m %@" handler
:handler
, bp.functionName
];
217 * Removes a breakpoint
219 - (void)removeBreakpoint
:(Breakpoint
*)bp
{
220 if (!self.model.connected
)
223 [_client sendCommandWithFormat
:@
"breakpoint_remove -d %i", [bp debuggerId
]];
227 * Sends a string to be evaluated by the engine.
229 - (void)evalScript
:(NSString
*)str callback
:(void (^
)(NSString
*))callback
{
230 if (!self.model.connected
)
233 NSData
* stringData
= [str dataUsingEncoding
:NSUTF8StringEncoding
];
234 ProtocolClientMessageHandler handler
= ^
(NSXMLDocument
* message
) {
235 NSXMLElement
* parent
= (NSXMLElement
*)[[message rootElement
] childAtIndex
:0];
236 NSString
* value
= [parent base64DecodedValue
];
239 [_client sendCustomCommandWithFormat
:@
"eval -i {txn} -- %@"
240 handler
:handler
, [stringData base64EncodedStringWithOptions
:0]];
243 // Protocol Client Delegate ////////////////////////////////////////////////////
244 #pragma mark Protocol Client Delegate
246 - (void)debuggerEngineConnected
:(ProtocolClient
*)client
{
247 [_model onNewConnection
];
251 * Called when the connection is finally closed. This will reopen the listening
252 * socket if the debugger remains attached.
254 - (void)debuggerEngineDisconnected
:(ProtocolClient
*)client
{
255 [_model onDisconnect
];
261 - (void)protocolClient
:(ProtocolClient
*)client receivedInitialMessage
:(NSXMLDocument
*)message
{
262 [self handleInitialResponse
:message
];
265 - (void)protocolClient
:(ProtocolClient
*)client receivedErrorMessage
:(NSXMLDocument
*)message
{
266 NSArray
* error
= [[message rootElement
] elementsForName
:@
"error"];
267 if ([error count
] > 0) {
268 NSLog(@
"Xdebug error: %@", error
);
269 NSString
* errorMessage
= [[[[error objectAtIndex
:0] children
] objectAtIndex
:0] stringValue
];
270 _model.lastError
= errorMessage
;
274 // Specific Response Handlers //////////////////////////////////////////////////
275 #pragma mark Response Handlers
278 * Initial packet received. We've started a brand-new connection to the engine.
280 - (void)handleInitialResponse
:(NSXMLDocument
*)response
{
281 if (!self.autoAttach
) {
282 [_client sendCommandWithFormat
:@
"detach"];
286 // Register any breakpoints that exist offline.
287 for (Breakpoint
* bp
in self.model.breakpointManager.breakpoints
)
288 [self addBreakpoint
:bp
];
290 // TODO: update the status.
294 * Receiver for status updates. This just freshens up the UI.
296 - (void)updateStatus
:(NSXMLDocument
*)response
{
297 NSString
* status
= [[[[response rootElement
] attributeForName
:@
"status"] stringValue
] capitalizedString
];
298 self.model.status
= status
;
299 if (!status ||
[status isEqualToString
:@
"Stopped"]) {
300 [_model onDisconnect
];
301 } else if ([status isEqualToString
:@
"Stopping"]) {
302 [_client sendCommandWithFormat
:@
"stop"];
307 * Step in/out/over and run all take this path. We first get the status of the
308 * debugger and then request fresh stack information.
310 - (void)debuggerStep
:(NSXMLDocument
*)response
{
311 [self updateStatus
:response
];
312 if (!self.model.connected
)
315 [_client sendCommandWithFormat
:@
"stack_depth" handler
:^
(NSXMLDocument
* message
) {
316 [self rebuildStack
:message
];
321 * We ask for the stack_depth and now we clobber the stack and start rebuilding
324 - (void)rebuildStack
:(NSXMLDocument
*)response
{
325 NSUInteger depth
= [[[[response rootElement
] attributeForName
:@
"depth"] stringValue
] intValue
];
327 // Send a request to get each frame of the stack, which will be added to this
328 // array. When the final frame arrives, the |tempStack| is released.
329 __block NSMutableArray
* tempStack
= [[NSMutableArray alloc
] init
];
331 for (NSUInteger i
= 0; i
< depth
; ++i
) {
332 ProtocolClientMessageHandler handler
= ^
(NSXMLDocument
* message
) {
333 [tempStack addObject
:[self transformXMLToStackFrame
:message
]];
334 if (i
== depth
- 1) {
335 [self.model updateStack
:[tempStack autorelease
]];
338 [_client sendCommandWithFormat
:@
"stack_get -d %d" handler
:handler
, i
];
343 * Creates a StackFrame object from an NSXMLDocument response from the "stack_get"
346 - (StackFrame
*)transformXMLToStackFrame
:(NSXMLDocument
*)response
{
347 NSXMLElement
* xmlframe
= (NSXMLElement
*)[[[response rootElement
] children
] objectAtIndex
:0];
348 StackFrame
* frame
= [[[StackFrame alloc
] init
] autorelease
];
349 frame.index
= [[[xmlframe attributeForName
:@
"level"] stringValue
] intValue
];
350 frame.filename
= [[xmlframe attributeForName
:@
"filename"] stringValue
];
351 frame.lineNumber
= [[[xmlframe attributeForName
:@
"lineno"] stringValue
] intValue
];
352 frame.function
= [[xmlframe attributeForName
:@
"where"] stringValue
];
357 * Enumerates all the contexts of a given stack frame. We then in turn get the
358 * contents of each one of these contexts.
360 - (void)loadContexts
:(NSXMLDocument
*)response forFrame
:(StackFrame
*)frame
{
361 NSXMLElement
* contextNames
= [response rootElement
];
362 for (NSXMLElement
* context
in [contextNames children
]) {
363 NSInteger cid
= [[[context attributeForName
:@
"id"] stringValue
] intValue
];
365 // Fetch each context's variables.
366 ProtocolClientMessageHandler handler
= ^
(NSXMLDocument
* message
) {
367 NSMutableArray
* variables
= [NSMutableArray array
];
369 // Merge the frame's existing variables.
371 [variables addObjectsFromArray
:frame.variables
];
373 // Add these new variables.
374 NSArray
* addVariables
= [[message rootElement
] children
];
376 for (NSXMLElement
* elm
in addVariables
) {
377 VariableNode
* node
= [[VariableNode alloc
] initWithXMLNode
:elm
];
378 [variables addObject
:[node autorelease
]];
382 frame.variables
= variables
;
384 [_client sendCommandWithFormat
:@
"context_get -d %d -c %d" handler
:handler
, frame.index
, cid
];
388 // Private /////////////////////////////////////////////////////////////////////
392 [_client connectOnPort
:_port
];
393 [_model onListeningOnPort
:_port
];