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
;
31 @implementation GDBpConnection
33 @synthesize socket
, status
;
36 * Creates a new DebuggerConnection and initializes the socket from the given connection
39 - (id)initWithPort
:(int)aPort session
:(NSString
*)aSession
;
41 if (self = [super init
])
44 session
= [aSession retain
];
47 // now that we have our host information, open the socket
48 socket
= [[SocketWrapper alloc
] initWithConnection
:self];
49 [socket setDelegate
:self];
52 self.status
= @
"Connecting";
54 [[BreakpointManager sharedManager
] setConnection
:self];
60 * Deallocates the object
70 * Gets the port number
78 * Gets the session name
86 * Returns the name of the remote host
88 - (NSString
*)remoteHost
92 return @
"(DISCONNECTED)";
94 return [socket remoteHost
];
98 * Returns whether or not we have an active connection
106 * Called by SocketWrapper after the connection is successful. This immediately calls
107 * -[SocketWrapper receive] to clear the way for communication, though the information
108 * could be useful server information that we don't use right now.
110 - (void)socketDidAccept
:(id)obj
116 // register any breakpoints that exist offline
117 for (Breakpoint
*bp
in [[BreakpointManager sharedManager
] breakpoints
])
119 [self addBreakpoint
:bp
];
122 [[[NSApp delegate
] debugger
] startDebugger
]; // this will just load the debugger to make it look active
126 * Receives errors from the SocketWrapper and updates the display
128 - (void)errorEncountered
:(NSString
*)error
130 [[NSNotificationCenter defaultCenter
]
131 postNotificationName
:kErrorOccurredNotif
133 userInfo
:[NSDictionary
134 dictionaryWithObject
:error
141 * Reestablishes communication with the remote debugger so that a new connection doesn't have to be
142 * created every time you want to debug a page
147 self.status
= @
"Connecting";
152 * Tells the debugger to continue running the script
156 [socket send
:[self createCommand
:@
"run"]];
164 return [self createStackFrame
];
168 * Tells the debugger to step into the current command.
170 - (StackFrame
*)stepIn
172 [socket send
:[self createCommand
:@
"step_into"]];
180 return [self createStackFrame
];
184 * Tells the debugger to step out of the current context
186 - (StackFrame
*)stepOut
188 [socket send
:[self createCommand
:@
"step_out"]];
196 return [self createStackFrame
];
200 * Tells the debugger to step over the current function
202 - (StackFrame
*)stepOver
204 [socket send
:[self createCommand
:@
"step_over"]];
212 return [self createStackFrame
];
216 * Tells the debugger engine to get a specifc property. This also takes in the NSXMLElement
217 * that requested it so that the child can be attached.
219 - (NSArray
*)getProperty
:(NSString
*)property
221 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"property_get -n \"%@\"", property
]]];
223 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
227 <property> <!-- this is the one we requested -->
228 <property ... /> <!-- these are what we want -->
233 // we now have to detach all the children so we can insert them into another document
234 NSXMLElement
*parent
= (NSXMLElement
*)[[doc rootElement
] childAtIndex
:0];
235 NSArray
*children
= [parent children
];
236 [parent setChildren
:nil];
240 #pragma mark Breakpoints
243 * Send an add breakpoint command
245 - (void)addBreakpoint
:(Breakpoint
*)bp
252 NSString
*cmd
= [self createCommand
:[NSString stringWithFormat
:@
"breakpoint_set -t line -f %@ -n %i", [bp transformedPath
], [bp line
]]];
254 NSXMLDocument
*info
= [self processData
:[socket receive
]];
255 [bp setDebuggerId
:[[[[info rootElement
] attributeForName
:@
"id"] stringValue
] intValue
]];
259 * Removes a breakpoint
261 - (void)removeBreakpoint
:(Breakpoint
*)bp
268 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"breakpoint_remove -d %i", [bp debuggerId
]]]];
275 * Helper method to create a string command with the -i <session> automatically tacked on. Takes
276 * a variable number of arguments and parses the given command with +[NSString stringWithFormat:]
278 - (NSString
*)createCommand
:(NSString
*)cmd
, ...
282 va_start(argList
, cmd
);
283 NSString
*format
= [[NSString alloc
] initWithFormat
:cmd arguments
:argList
]; // format the command
286 return [NSString stringWithFormat
:@
"%@ -i %@", [format autorelease
], session
];
290 * Helper function to parse the NSData into an NSXMLDocument
292 - (NSXMLDocument
*)processData
:(NSString
*)data
297 NSError
*parseError
= nil;
298 NSXMLDocument
*doc
= [[NSXMLDocument alloc
] initWithXMLString
:data options
:0 error
:&parseError
];
301 NSLog(@
"Could not parse XML? --- %@", parseError
);
302 NSLog(@
"Error UserInfo: %@", [parseError userInfo
]);
303 NSLog(@
"This is the XML Document: %@", data
);
307 // check and see if there's an error
308 NSArray
*error
= [[doc rootElement
] elementsForName
:@
"error"];
309 if ([error count
] > 0)
311 NSLog(@
"Xdebug error: %@", error
);
312 [self errorEncountered
:[[[[error objectAtIndex
:0] children
] objectAtIndex
:0] stringValue
]];
316 return [doc autorelease
];
320 * Creates a StackFrame based on the current position in the debugger
322 - (StackFrame
*)createStackFrame
324 // get the stack frame
325 [socket send
:[self createCommand
:@
"stack_get -d 0"]];
326 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
330 NSXMLElement
*xmlframe
= [[[doc rootElement
] children
] objectAtIndex
:0];
332 // get the names of all the contexts
333 [socket send
:[self createCommand
:@
"context_names -d 0"]];
334 NSXMLElement
*contextNames
= [[self processData
:[socket receive
]] rootElement
];
335 NSMutableArray
*variables
= [NSMutableArray array
];
336 for (NSXMLElement
*context
in [contextNames children
])
338 NSString
*name
= [[context attributeForName
:@
"name"] stringValue
];
339 int cid
= [[[context attributeForName
:@
"id"] stringValue
] intValue
];
341 // fetch the contexts
342 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"context_get -d 0 -c %d", cid
]]];
343 NSArray
*addVars
= [[[self processData
:[socket receive
]] rootElement
] children
];
344 if (addVars
!= nil && name
!= nil)
345 [variables addObjectsFromArray
:addVars
];
349 NSString
*filename
= [[xmlframe attributeForName
:@
"filename"] stringValue
];
350 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"source -f %@", filename
]]];
351 NSString
*source
= [[[self processData
:[socket receive
]] rootElement
] value
]; // decode base64
353 // create stack frame
354 StackFrame
*frame
= [[StackFrame alloc
]
356 withFilename
:filename
358 atLine
:[[[xmlframe attributeForName
:@
"lineno"] stringValue
] intValue
]
359 inFunction
:[[xmlframe attributeForName
:@
"where"] stringValue
]
360 withVariables
:variables
363 return [frame autorelease
];
367 * Fetches the value of and sets the status instance variable
371 [socket send
:[self createCommand
:@
"status"]];
372 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
373 self.status
= [[[[doc rootElement
] attributeForName
:@
"status"] stringValue
] capitalizedString
];
375 if (status
== nil ||
[status isEqualToString
:@
"Stopped"] ||
[status isEqualToString
:@
"Stopping"])
379 self.status
= @
"Stopped";