3 * Copyright (c) 2002 - 2007, 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 "DebuggerConnection.h"
18 #import "AppDelegate.h"
20 @interface DebuggerConnection (Private
)
22 - (NSString
*)createCommand
:(NSString
*)cmd
;
26 @implementation DebuggerConnection
29 * Creates a new DebuggerConnection and initializes the socket from the given connection
32 - (id)initWithWindowController
:(DebuggerWindowController
*)wc port
:(int)aPort session
:(NSString
*)aSession
;
34 if (self = [super init
])
37 session
= [aSession retain
];
40 windowController
= [wc retain
];
42 // now that we have our host information, open the socket
43 socket
= [[SocketWrapper alloc
] initWithPort
:port
];
44 [socket setDelegate
:self];
51 * Releases all of the object's data members and closes the streams
57 [windowController release
];
63 * Gets the port number
71 * Gets the session name
79 * Returns the name of the remote host
81 - (NSString
*)remoteHost
85 return @
"(DISCONNECTED)";
87 return [socket remoteHost
];
91 * Returns whether or not we have an active connection
99 * SocketWrapper delegate method that is called whenever new data is received
101 - (void)dataReceived
:(NSData
*)response deliverTo
:(SEL)selector
103 NSXMLDocument
*doc
= [[NSXMLDocument alloc
] initWithData
:response options
:NSXMLDocumentTidyXML error
:nil];
105 // check and see if there's an error
106 NSArray
*error
= [[doc rootElement
] elementsForName
:@
"error"];
107 if ([error count
] > 0)
109 [windowController setError
:[[[[error objectAtIndex
:0] children
] objectAtIndex
:0] stringValue
]];
113 // if the caller of [_socket receive:] specified a deliverTo, just forward the message to them
116 [self performSelector
:selector withObject
:doc
];
123 * SocketWrapper delegate method that is called after data is sent. This really
124 * isn't useful for much.
126 - (void)dataSent
:(NSString
*)data
130 * Called by SocketWrapper after the connection is successful. This immediately calls
131 * -[SocketWrapper receive] to clear the way for communication
133 - (void)socketDidAccept
136 [socket receive
:@selector(handshake
:)];
140 * Receives errors from the SocketWrapper and updates the display
142 - (void)errorEncountered
:(NSError
*)error
144 [windowController setError
:[error domain
]];
148 * The initial packet handshake. This allows us to set things like the title of the window
149 * and glean information about hte server we are debugging
151 - (void)handshake
:(NSXMLDocument
*)doc
153 [self refreshStatus
];
157 * Handler used by dataReceived:deliverTo: for anytime the status command is issued. It sets
158 * the window controller's status text
160 - (void)updateStatus
:(NSXMLDocument
*)doc
162 NSString
*status
= [[[doc rootElement
] attributeForName
:@
"status"] stringValue
];
163 [windowController setStatus
:[status capitalizedString
]];
165 if ([status isEqualToString
:@
"break"])
167 [self updateStackTraceAndRegisters
];
172 * Tells the debugger to continue running the script
176 [socket send
:[self createCommand
:@
"run"]];
177 [self refreshStatus
];
181 * Method that runs tells the debugger to give us its status. This will call _updateStatus
182 * and will update the status text on the window
184 - (void)refreshStatus
186 [socket send
:[self createCommand
:@
"status"]];
187 [socket receive
:@selector(updateStatus
:)];
191 * Tells the debugger to step into the current command.
195 [socket send
:[self createCommand
:@
"step_into"]];
196 [socket receive
:nil];
197 [self refreshStatus
];
201 * Tells the debugger to step out of the current context
205 [socket send
:[self createCommand
:@
"step_out"]];
206 [socket receive
:nil];
207 [self refreshStatus
];
211 * Tells the debugger to step over the current function
215 [socket send
:[self createCommand
:@
"step_over"]];
216 [socket receive
:nil];
217 [self refreshStatus
];
221 * This function queries the debug server for the current stacktrace and all the registers on
222 * level one. If a user then tries to expand past level one... TOOD: HOLY CRAP WHAT DO WE DO PAST LEVEL 1?
224 - (void)updateStackTraceAndRegisters
226 [socket send
:[self createCommand
:@
"stack_get"]];
227 [socket receive
:@selector(stackReceived
:)];
229 [socket send
:[self createCommand
:@
"context_get"]];
230 [socket receive
:@selector(registerReceived
:)];
234 * Called by the dataReceived delivery delegate. This updates the window controller's data
235 * for the stack trace
237 - (void)stackReceived
:(NSXMLDocument
*)doc
239 NSArray
*children
= [[doc rootElement
] children
];
240 NSMutableArray
*stack
= [NSMutableArray array
];
241 NSMutableDictionary
*dict
= [NSMutableDictionary dictionary
];
242 for (int i
= 0; i
< [children count
]; i
++)
244 NSArray
*attrs
= [[children objectAtIndex
:i
] attributes
];
245 for (int j
= 0; j
< [attrs count
]; j
++)
247 [dict setValue
:[[attrs objectAtIndex
:j
] stringValue
] forKey
:[[attrs objectAtIndex
:j
] name
]];
249 [stack addObject
:dict
];
250 dict
= [NSMutableDictionary dictionary
];
252 [windowController setStack
:stack
];
256 * Called when we have a new register to display
258 - (void)registerReceived
:(NSXMLDocument
*)doc
260 [windowController setRegister
:doc
];
264 * Tells the debugger engine to get a specifc property. This also takes in the NSXMLElement
265 * that requested it so that the child can be attached in the delivery.
267 - (void)getProperty
:(NSString
*)property forNode
:(NSTreeNode
*)node
269 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"property_get -n \"%@\"", property
]]];
270 depthFetchNode
= node
;
271 [socket receive
:@selector(propertyReceived
:)];
275 * Called when a property is received. This then adds the result as children to the passed object
277 - (void)propertyReceived
:(NSXMLDocument
*)doc
281 <property> <!-- this is the one we requested -->
282 <property ... /> <!-- these are what we want -->
287 // we now have to detach all the children so we can insert them into another document
288 NSXMLElement
*parent
= (NSXMLElement
*)[[doc rootElement
] childAtIndex
:0];
289 NSArray
*children
= [parent children
];
290 [parent setChildren
:nil];
291 [windowController addChildren
:children toNode
:depthFetchNode
];
292 depthFetchNode
= nil;
296 * Helper method to create a string command with the -i <session> automatically tacked on
298 - (NSString
*)createCommand
:(NSString
*)cmd
300 return [NSString stringWithFormat
:@
"%@ -i %@", cmd
, session
];