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
;
32 @implementation GDBpConnection
34 @synthesize socket
, status
;
37 * Creates a new DebuggerConnection and initializes the socket from the given connection
40 - (id)initWithPort
:(int)aPort session
:(NSString
*)aSession
;
42 if (self = [super init
])
45 session
= [aSession retain
];
48 // now that we have our host information, open the socket
49 socket
= [[SocketWrapper alloc
] initWithConnection
:self];
50 [socket setDelegate
:self];
53 self.status
= @
"Connecting";
55 [[BreakpointManager sharedManager
] setConnection
:self];
61 * Deallocates the object
71 * Gets the port number
79 * Gets the session name
87 * Returns the name of the remote host
89 - (NSString
*)remoteHost
93 return @
"(DISCONNECTED)";
95 return [socket remoteHost
];
99 * Returns whether or not we have an active connection
107 * Called by SocketWrapper after the connection is successful. This immediately calls
108 * -[SocketWrapper receive] to clear the way for communication, though the information
109 * could be useful server information that we don't use right now.
111 - (void)socketDidAccept
:(id)obj
117 // register any breakpoints that exist offline
118 for (Breakpoint
*bp
in [[BreakpointManager sharedManager
] breakpoints
])
120 [self addBreakpoint
:bp
];
123 [[[NSApp delegate
] debugger
] startDebugger
]; // this will just load the debugger to make it look active
127 * Receives errors from the SocketWrapper and updates the display
129 - (void)errorEncountered
:(NSString
*)error
131 [[NSNotificationCenter defaultCenter
]
132 postNotificationName
:kErrorOccurredNotif
134 userInfo
:[NSDictionary
135 dictionaryWithObject
:error
142 * Reestablishes communication with the remote debugger so that a new connection doesn't have to be
143 * created every time you want to debug a page
148 self.status
= @
"Connecting";
153 * Creates an entirely new stack and returns it as an array of StackFrame objects.
155 - (NSArray
*)getCurrentStack
157 // get the total stack depth
158 [socket send
:[self createCommand
:@
"stack_depth"]];
159 NSXMLDocument
* doc
= [self processData
:[socket receive
]];
160 int depth
= [[[[doc rootElement
] attributeForName
:@
"depth"] stringValue
] intValue
];
162 // get all stack frames
163 NSMutableArray
* stack
= [NSMutableArray arrayWithCapacity
:depth
];
164 for (int i
= 0; i
< depth
; i
++)
166 StackFrame
* frame
= [self createStackFrame
:i
];
167 [stack insertObject
:frame atIndex
:i
];
174 * Tells the debugger to continue running the script. Returns the current stack frame.
178 [socket send
:[self createCommand
:@
"run"]];
186 return [self createCurrentStackFrame
];
190 * Tells the debugger to step into the current command.
192 - (StackFrame
*)stepIn
194 [socket send
:[self createCommand
:@
"step_into"]];
202 return [self createCurrentStackFrame
];
206 * Tells the debugger to step out of the current context
208 - (StackFrame
*)stepOut
210 [socket send
:[self createCommand
:@
"step_out"]];
218 return [self createCurrentStackFrame
];
222 * Tells the debugger to step over the current function
224 - (StackFrame
*)stepOver
226 [socket send
:[self createCommand
:@
"step_over"]];
234 return [self createCurrentStackFrame
];
238 * Tells the debugger engine to get a specifc property. This also takes in the NSXMLElement
239 * that requested it so that the child can be attached.
241 - (NSArray
*)getProperty
:(NSString
*)property
243 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"property_get -n \"%@\"", property
]]];
245 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
249 <property> <!-- this is the one we requested -->
250 <property ... /> <!-- these are what we want -->
255 // we now have to detach all the children so we can insert them into another document
256 NSXMLElement
*parent
= (NSXMLElement
*)[[doc rootElement
] childAtIndex
:0];
257 NSArray
*children
= [parent children
];
258 [parent setChildren
:nil];
262 #pragma mark Breakpoints
265 * Send an add breakpoint command
267 - (void)addBreakpoint
:(Breakpoint
*)bp
274 NSString
*cmd
= [self createCommand
:[NSString stringWithFormat
:@
"breakpoint_set -t line -f %@ -n %i", [bp transformedPath
], [bp line
]]];
276 NSXMLDocument
*info
= [self processData
:[socket receive
]];
277 [bp setDebuggerId
:[[[[info rootElement
] attributeForName
:@
"id"] stringValue
] intValue
]];
281 * Removes a breakpoint
283 - (void)removeBreakpoint
:(Breakpoint
*)bp
290 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"breakpoint_remove -d %i", [bp debuggerId
]]]];
297 * Helper method to create a string command with the -i <session> automatically tacked on. Takes
298 * a variable number of arguments and parses the given command with +[NSString stringWithFormat:]
300 - (NSString
*)createCommand
:(NSString
*)cmd
, ...
304 va_start(argList
, cmd
);
305 NSString
*format
= [[NSString alloc
] initWithFormat
:cmd arguments
:argList
]; // format the command
308 return [NSString stringWithFormat
:@
"%@ -i %@", [format autorelease
], session
];
312 * Helper function to parse the NSData into an NSXMLDocument
314 - (NSXMLDocument
*)processData
:(NSString
*)data
319 NSError
*parseError
= nil;
320 NSXMLDocument
*doc
= [[NSXMLDocument alloc
] initWithXMLString
:data options
:0 error
:&parseError
];
323 NSLog(@
"Could not parse XML? --- %@", parseError
);
324 NSLog(@
"Error UserInfo: %@", [parseError userInfo
]);
325 NSLog(@
"This is the XML Document: %@", data
);
329 // check and see if there's an error
330 NSArray
*error
= [[doc rootElement
] elementsForName
:@
"error"];
331 if ([error count
] > 0)
333 NSLog(@
"Xdebug error: %@", error
);
334 [self errorEncountered
:[[[[error objectAtIndex
:0] children
] objectAtIndex
:0] stringValue
]];
338 return [doc autorelease
];
342 * Generates a stack frame for the given depth
344 - (StackFrame
*)createStackFrame
:(int)stackDepth
346 // get the stack frame
347 [socket send
:[self createCommand
:@
"stack_get -d %d", stackDepth
]];
348 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
352 NSXMLElement
*xmlframe
= [[[doc rootElement
] children
] objectAtIndex
:0];
354 // get the names of all the contexts
355 [socket send
:[self createCommand
:@
"context_names -d 0"]];
356 NSXMLElement
*contextNames
= [[self processData
:[socket receive
]] rootElement
];
357 NSMutableArray
*variables
= [NSMutableArray array
];
358 for (NSXMLElement
*context
in [contextNames children
])
360 NSString
*name
= [[context attributeForName
:@
"name"] stringValue
];
361 int cid
= [[[context attributeForName
:@
"id"] stringValue
] intValue
];
363 // fetch the contexts
364 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"context_get -d %d -c %d", stackDepth
, cid
]]];
365 NSArray
*addVars
= [[[self processData
:[socket receive
]] rootElement
] children
];
366 if (addVars
!= nil && name
!= nil)
367 [variables addObjectsFromArray
:addVars
];
371 NSString
*filename
= [[xmlframe attributeForName
:@
"filename"] stringValue
];
372 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"source -f %@", filename
]]];
373 NSString
*source
= [[[self processData
:[socket receive
]] rootElement
] value
]; // decode base64
375 // create stack frame
376 StackFrame
*frame
= [[StackFrame alloc
]
377 initWithIndex
:stackDepth
378 withFilename
:filename
380 atLine
:[[[xmlframe attributeForName
:@
"lineno"] stringValue
] intValue
]
381 inFunction
:[[xmlframe attributeForName
:@
"where"] stringValue
]
382 withVariables
:variables
385 return [frame autorelease
];
389 * Creates a StackFrame based on the current position in the debugger
391 - (StackFrame
*)createCurrentStackFrame
393 return [self createStackFrame
:0];
397 * Fetches the value of and sets the status instance variable
401 [socket send
:[self createCommand
:@
"status"]];
402 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
403 self.status
= [[[[doc rootElement
] attributeForName
:@
"status"] stringValue
] capitalizedString
];
405 if (status
== nil ||
[status isEqualToString
:@
"Stopped"] ||
[status isEqualToString
:@
"Stopping"])
409 self.status
= @
"Stopped";