3 * Copyright (c) 2007 - 2008, 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 @interface GDBpConnection (Private
)
21 - (NSString
*)createCommand
:(NSString
*)cmd
;
22 - (NSXMLDocument
*)processData
:(NSString
*)data
;
25 @implementation GDBpConnection
27 @synthesize socket
, windowController
;
30 * Creates a new DebuggerConnection and initializes the socket from the given connection
33 - (id)initWithWindowController
:(DebuggerController
*)wc port
:(int)aPort session
:(NSString
*)aSession
;
35 if (self = [super init
])
38 session
= [aSession retain
];
41 windowController
= [wc retain
];
43 // now that we have our host information, open the socket
44 socket
= [[SocketWrapper alloc
] initWithConnection
:self];
45 [socket setDelegate
:self];
48 [[BreakpointManager sharedManager
] setConnection
:self];
54 * Deallocates the object
60 [windowController release
];
65 * Gets the port number
73 * Gets the session name
81 * Returns the name of the remote host
83 - (NSString
*)remoteHost
87 return @
"(DISCONNECTED)";
89 return [socket remoteHost
];
93 * Returns whether or not we have an active connection
101 * Called by SocketWrapper after the connection is successful. This immediately calls
102 * -[SocketWrapper receive] to clear the way for communication, though the information
103 * could be useful server information that we don't use right now.
105 - (void)socketDidAccept
:(id)obj
109 [self refreshStatus
];
111 // register any breakpoints that exist offline
112 for (Breakpoint
*bp
in [[BreakpointManager sharedManager
] breakpoints
])
114 [self addBreakpoint
:bp
];
119 * Receives errors from the SocketWrapper and updates the display
121 - (void)errorEncountered
:(NSString
*)error
123 [windowController setError
:error
];
127 * Reestablishes communication with the remote debugger so that a new connection doesn't have to be
128 * created every time you want to debug a page
133 [windowController setStatus
:@
"Connecting"];
134 [windowController resetDisplays
];
139 * Tells the debugger to continue running the script
143 [socket send
:[self createCommand
:@
"run"]];
145 [self refreshStatus
];
149 * Method that runs tells the debugger to give us its status and will update the status text on the window
151 - (void)refreshStatus
153 [socket send
:[self createCommand
:@
"status"]];
155 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
156 NSString
*status
= [[[doc rootElement
] attributeForName
:@
"status"] stringValue
];
157 [windowController setStatus
:[status capitalizedString
]];
159 if ([status isEqualToString
:@
"break"])
161 [self updateStackTraceAndRegisters
];
163 else if ([status isEqualToString
:@
"stopped"] ||
[status isEqualToString
:@
"stopping"])
167 [windowController setStatus
:@
"Stopped"];
172 * Tells the debugger to step into the current command.
176 [socket send
:[self createCommand
:@
"step_into"]];
178 [self refreshStatus
];
182 * Tells the debugger to step out of the current context
186 [socket send
:[self createCommand
:@
"step_out"]];
188 [self refreshStatus
];
192 * Tells the debugger to step over the current function
196 [socket send
:[self createCommand
:@
"step_over"]];
198 [self refreshStatus
];
202 * This function queries the debug server for the current stacktrace and all the registers on
203 * level one. If a user then tries to expand past level one... TOOD: HOLY CRAP WHAT DO WE DO PAST LEVEL 1?
205 - (void)updateStackTraceAndRegisters
208 [socket send
:[self createCommand
:@
"stack_get"]];
209 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
210 NSArray
*children
= [[doc rootElement
] children
];
211 NSMutableArray
*stack
= [NSMutableArray array
];
212 NSMutableDictionary
*dict
= [NSMutableDictionary dictionary
];
213 for (int i
= 0; i
< [children count
]; i
++)
215 NSArray
*attrs
= [[children objectAtIndex
:i
] attributes
];
216 for (int j
= 0; j
< [attrs count
]; j
++)
218 [dict setValue
:[[attrs objectAtIndex
:j
] stringValue
] forKey
:[[attrs objectAtIndex
:j
] name
]];
220 [stack addObject
:dict
];
221 dict
= [NSMutableDictionary dictionary
];
223 [windowController setStack
:stack
];
226 [socket send
:[self createCommand
:@
"context_get"]];
227 [windowController setRegister
:[self processData
:[socket receive
]]];
231 * Tells the debugger engine to get a specifc property. This also takes in the NSXMLElement
232 * that requested it so that the child can be attached.
234 - (NSArray
*)getProperty
:(NSString
*)property
236 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"property_get -n \"%@\"", property
]]];
238 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
242 <property> <!-- this is the one we requested -->
243 <property ... /> <!-- these are what we want -->
248 // we now have to detach all the children so we can insert them into another document
249 NSXMLElement
*parent
= (NSXMLElement
*)[[doc rootElement
] childAtIndex
:0];
250 NSArray
*children
= [parent children
];
251 [parent setChildren
:nil];
255 #pragma mark Breakpoints
258 * Send an add breakpoint command
260 - (void)addBreakpoint
:(Breakpoint
*)bp
267 NSString
*cmd
= [self createCommand
:[NSString stringWithFormat
:@
"breakpoint_set -t line -f %@ -n %i", [bp file
], [bp line
]]];
269 NSXMLDocument
*info
= [self processData
:[socket receive
]];
270 [bp setDebuggerId
:[[[[info rootElement
] attributeForName
:@
"id"] stringValue
] intValue
]];
274 * Removes a breakpoint
276 - (void)removeBreakpoint
:(Breakpoint
*)bp
283 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"breakpoint_remove -d %i", [bp debuggerId
]]]];
290 * Helper method to create a string command with the -i <session> automatically tacked on
292 - (NSString
*)createCommand
:(NSString
*)cmd
294 return [NSString stringWithFormat
:@
"%@ -i %@", cmd
, session
];
298 * Helper function to parse the NSData into an NSXMLDocument
300 - (NSXMLDocument
*)processData
:(NSString
*)data
302 NSError
*parseError
= nil;
303 NSXMLDocument
*doc
= [[NSXMLDocument alloc
] initWithXMLString
:data options
:0 error
:&parseError
];
306 NSLog(@
"Could not parse XML? --- %@", parseError
);
307 NSLog(@
"Error UserInfo: %@", [parseError userInfo
]);
308 NSLog(@
"This is the XML Document: %@", data
);
312 // check and see if there's an error
313 NSArray
*error
= [[doc rootElement
] elementsForName
:@
"error"];
314 if ([error count
] > 0)
316 [windowController setError
:[[[[error objectAtIndex
:0] children
] objectAtIndex
:0] stringValue
]];
320 return [doc autorelease
];