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
;
23 - (StackFrame
*)createStackFrame
;
26 @implementation GDBpConnection
28 @synthesize socket
, windowController
;
31 * Creates a new DebuggerConnection and initializes the socket from the given connection
34 - (id)initWithWindowController
:(DebuggerController
*)wc port
:(int)aPort session
:(NSString
*)aSession
;
36 if (self = [super init
])
39 session
= [aSession retain
];
42 windowController
= [wc retain
];
44 // now that we have our host information, open the socket
45 socket
= [[SocketWrapper alloc
] initWithConnection
:self];
46 [socket setDelegate
:self];
49 [[BreakpointManager sharedManager
] setConnection
:self];
55 * Deallocates the object
61 [windowController release
];
66 * Gets the port number
74 * Gets the session name
82 * Returns the name of the remote host
84 - (NSString
*)remoteHost
88 return @
"(DISCONNECTED)";
90 return [socket remoteHost
];
94 * Returns whether or not we have an active connection
102 * Called by SocketWrapper after the connection is successful. This immediately calls
103 * -[SocketWrapper receive] to clear the way for communication, though the information
104 * could be useful server information that we don't use right now.
106 - (void)socketDidAccept
:(id)obj
110 [self refreshStatus
];
112 // register any breakpoints that exist offline
113 for (Breakpoint
*bp
in [[BreakpointManager sharedManager
] breakpoints
])
115 [self addBreakpoint
:bp
];
120 * Receives errors from the SocketWrapper and updates the display
122 - (void)errorEncountered
:(NSString
*)error
124 [windowController setError
:error
];
128 * Reestablishes communication with the remote debugger so that a new connection doesn't have to be
129 * created every time you want to debug a page
134 [windowController setStatus
:@
"Connecting"];
135 [windowController resetDisplays
];
140 * Tells the debugger to continue running the script
144 [socket send
:[self createCommand
:@
"run"]];
146 [self refreshStatus
];
150 * Method that runs tells the debugger to give us its status and will update the status text on the window
152 - (void)refreshStatus
154 [socket send
:[self createCommand
:@
"status"]];
156 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
157 NSString
*status
= [[[doc rootElement
] attributeForName
:@
"status"] stringValue
];
158 [windowController setStatus
:[status capitalizedString
]];
160 if ([status isEqualToString
:@
"break"])
162 [self updateStackTraceAndRegisters
];
164 else if ([status isEqualToString
:@
"stopped"] ||
[status isEqualToString
:@
"stopping"])
168 [windowController setStatus
:@
"Stopped"];
173 * Tells the debugger to step into the current command.
175 - (StackFrame
*)stepIn
177 [socket send
:[self createCommand
:@
"step_into"]];
180 StackFrame
*frame
= [self createStackFrame
];
181 [self refreshStatus
];
187 * Tells the debugger to step out of the current context
189 - (StackFrame
*)stepOut
191 [socket send
:[self createCommand
:@
"step_out"]];
194 StackFrame
*frame
= [self createStackFrame
];
195 [self refreshStatus
];
201 * Tells the debugger to step over the current function
203 - (StackFrame
*)stepOver
205 [socket send
:[self createCommand
:@
"step_over"]];
208 StackFrame
*frame
= [self createStackFrame
];
209 [self refreshStatus
];
215 * This function queries the debug server for the current stacktrace and all the registers on
216 * level one. If a user then tries to expand past level one... TOOD: HOLY CRAP WHAT DO WE DO PAST LEVEL 1?
218 - (void)updateStackTraceAndRegisters
221 [socket send
:[self createCommand
:@
"stack_get"]];
222 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
223 NSArray
*children
= [[doc rootElement
] children
];
224 NSMutableArray
*stack
= [NSMutableArray array
];
225 NSMutableDictionary
*dict
= [NSMutableDictionary dictionary
];
226 for (int i
= 0; i
< [children count
]; i
++)
228 NSArray
*attrs
= [[children objectAtIndex
:i
] attributes
];
229 for (int j
= 0; j
< [attrs count
]; j
++)
231 [dict setValue
:[[attrs objectAtIndex
:j
] stringValue
] forKey
:[[attrs objectAtIndex
:j
] name
]];
233 [stack addObject
:dict
];
234 dict
= [NSMutableDictionary dictionary
];
236 [windowController setStack
:stack
];
239 [socket send
:[self createCommand
:@
"context_get"]];
240 [windowController setRegister
:[self processData
:[socket receive
]]];
244 * Tells the debugger engine to get a specifc property. This also takes in the NSXMLElement
245 * that requested it so that the child can be attached.
247 - (NSArray
*)getProperty
:(NSString
*)property
249 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"property_get -n \"%@\"", property
]]];
251 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
255 <property> <!-- this is the one we requested -->
256 <property ... /> <!-- these are what we want -->
261 // we now have to detach all the children so we can insert them into another document
262 NSXMLElement
*parent
= (NSXMLElement
*)[[doc rootElement
] childAtIndex
:0];
263 NSArray
*children
= [parent children
];
264 [parent setChildren
:nil];
268 #pragma mark Breakpoints
271 * Send an add breakpoint command
273 - (void)addBreakpoint
:(Breakpoint
*)bp
280 NSString
*cmd
= [self createCommand
:[NSString stringWithFormat
:@
"breakpoint_set -t line -f %@ -n %i", [bp file
], [bp line
]]];
282 NSXMLDocument
*info
= [self processData
:[socket receive
]];
283 [bp setDebuggerId
:[[[[info rootElement
] attributeForName
:@
"id"] stringValue
] intValue
]];
287 * Removes a breakpoint
289 - (void)removeBreakpoint
:(Breakpoint
*)bp
296 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"breakpoint_remove -d %i", [bp debuggerId
]]]];
303 * Helper method to create a string command with the -i <session> automatically tacked on
305 - (NSString
*)createCommand
:(NSString
*)cmd
307 return [NSString stringWithFormat
:@
"%@ -i %@", cmd
, session
];
311 * Helper function to parse the NSData into an NSXMLDocument
313 - (NSXMLDocument
*)processData
:(NSString
*)data
315 NSError
*parseError
= nil;
316 NSXMLDocument
*doc
= [[NSXMLDocument alloc
] initWithXMLString
:data options
:0 error
:&parseError
];
319 NSLog(@
"Could not parse XML? --- %@", parseError
);
320 NSLog(@
"Error UserInfo: %@", [parseError userInfo
]);
321 NSLog(@
"This is the XML Document: %@", data
);
325 // check and see if there's an error
326 NSArray
*error
= [[doc rootElement
] elementsForName
:@
"error"];
327 if ([error count
] > 0)
329 [windowController setError
:[[[[error objectAtIndex
:0] children
] objectAtIndex
:0] stringValue
]];
333 return [doc autorelease
];
337 * Creates a StackFrame based on the current position in the debugger
339 - (StackFrame
*)createStackFrame
341 [socket send
:[self createCommand
:@
"stack_get -d 0"]];
342 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
344 NSXMLElement
*xmlframe
= [[[doc rootElement
] children
] objectAtIndex
:0];
345 StackFrame
*frame
= [[StackFrame alloc
]
346 initWithIndex
:[[[xmlframe attributeForName
:@
"level"] stringValue
] intValue
]
347 withFilename
:[[xmlframe attributeForName
:@
"filename"] stringValue
]
349 atLine
:[[[xmlframe attributeForName
:@
"lineno"] stringValue
] intValue
]
350 inFunction
:[[xmlframe attributeForName
:@
"where"] stringValue
]
354 return [frame autorelease
];