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
28 @synthesize socket
, windowController
;
31 * Creates a new DebuggerConnection and initializes the socket from the given connection
34 - (id)initWithWindowController
:(DebuggerWindowController
*)wc port
:(int)aPort session
:(NSString
*)aSession
;
36 if (self = [super init
])
42 windowController
= wc
;
44 // now that we have our host information, open the socket
45 socket
= [[SocketWrapper alloc
] initWithConnection
:self];
46 [socket setDelegate
:self];
53 * Gets the port number
61 * Gets the session name
69 * Returns the name of the remote host
71 - (NSString
*)remoteHost
75 return @
"(DISCONNECTED)";
77 return [socket remoteHost
];
81 * Returns whether or not we have an active connection
89 * SocketWrapper delegate method that is called whenever new data is received
91 - (void)dataReceived
:(NSData
*)response deliverTo
:(SEL)selector
93 NSXMLDocument
*doc
= [[NSXMLDocument alloc
] initWithData
:response options
:NSXMLDocumentTidyXML error
:nil];
95 // check and see if there's an error
96 NSArray
*error
= [[doc rootElement
] elementsForName
:@
"error"];
97 if ([error count
] > 0)
99 [windowController setError
:[[[[error objectAtIndex
:0] children
] objectAtIndex
:0] stringValue
]];
103 // if the caller of [_socket receive:] specified a deliverTo, just forward the message to them
106 [self performSelector
:selector withObject
:doc
];
111 * SocketWrapper delegate method that is called after data is sent. This really
112 * isn't useful for much.
114 - (void)dataSent
:(NSString
*)data
118 * Called by SocketWrapper after the connection is successful. This immediately calls
119 * -[SocketWrapper receive] to clear the way for communication
121 - (void)socketDidAccept
124 [socket receive
:@selector(handshake
:)];
128 * Receives errors from the SocketWrapper and updates the display
130 - (void)errorEncountered
:(NSError
*)error
132 [windowController setError
:[error domain
]];
136 * The initial packet handshake. This allows us to set things like the title of the window
137 * and glean information about hte server we are debugging
139 - (void)handshake
:(NSXMLDocument
*)doc
141 [self refreshStatus
];
145 * Handler used by dataReceived:deliverTo: for anytime the status command is issued. It sets
146 * the window controller's status text
148 - (void)updateStatus
:(NSXMLDocument
*)doc
150 NSString
*status
= [[[doc rootElement
] attributeForName
:@
"status"] stringValue
];
151 [windowController setStatus
:[status capitalizedString
]];
153 if ([status isEqualToString
:@
"break"])
155 [self updateStackTraceAndRegisters
];
160 * Tells the debugger to continue running the script
164 [socket send
:[self createCommand
:@
"run"]];
165 [self refreshStatus
];
169 * Method that runs tells the debugger to give us its status. This will call _updateStatus
170 * and will update the status text on the window
172 - (void)refreshStatus
174 [socket send
:[self createCommand
:@
"status"]];
175 [socket receive
:@selector(updateStatus
:)];
179 * Tells the debugger to step into the current command.
183 [socket send
:[self createCommand
:@
"step_into"]];
184 [socket receive
:nil];
185 [self refreshStatus
];
189 * Tells the debugger to step out of the current context
193 [socket send
:[self createCommand
:@
"step_out"]];
194 [socket receive
:nil];
195 [self refreshStatus
];
199 * Tells the debugger to step over the current function
203 [socket send
:[self createCommand
:@
"step_over"]];
204 [socket receive
:nil];
205 [self refreshStatus
];
209 * This function queries the debug server for the current stacktrace and all the registers on
210 * level one. If a user then tries to expand past level one... TOOD: HOLY CRAP WHAT DO WE DO PAST LEVEL 1?
212 - (void)updateStackTraceAndRegisters
214 [socket send
:[self createCommand
:@
"stack_get"]];
215 [socket receive
:@selector(stackReceived
:)];
217 [socket send
:[self createCommand
:@
"context_get"]];
218 [socket receive
:@selector(registerReceived
:)];
222 * Called by the dataReceived delivery delegate. This updates the window controller's data
223 * for the stack trace
225 - (void)stackReceived
:(NSXMLDocument
*)doc
227 NSArray
*children
= [[doc rootElement
] children
];
228 NSMutableArray
*stack
= [NSMutableArray array
];
229 NSMutableDictionary
*dict
= [NSMutableDictionary dictionary
];
230 for (int i
= 0; i
< [children count
]; i
++)
232 NSArray
*attrs
= [[children objectAtIndex
:i
] attributes
];
233 for (int j
= 0; j
< [attrs count
]; j
++)
235 [dict setValue
:[[attrs objectAtIndex
:j
] stringValue
] forKey
:[[attrs objectAtIndex
:j
] name
]];
237 [stack addObject
:dict
];
238 dict
= [NSMutableDictionary dictionary
];
240 [windowController setStack
:stack
];
244 * Called when we have a new register to display
246 - (void)registerReceived
:(NSXMLDocument
*)doc
248 [windowController setRegister
:doc
];
252 * Tells the debugger engine to get a specifc property. This also takes in the NSXMLElement
253 * that requested it so that the child can be attached in the delivery.
255 - (void)getProperty
:(NSString
*)property forNode
:(NSTreeNode
*)node
257 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"property_get -n \"%@\"", property
]]];
258 depthFetchNode
= node
;
259 [socket receive
:@selector(propertyReceived
:)];
263 * Called when a property is received. This then adds the result as children to the passed object
265 - (void)propertyReceived
:(NSXMLDocument
*)doc
269 <property> <!-- this is the one we requested -->
270 <property ... /> <!-- these are what we want -->
275 // we now have to detach all the children so we can insert them into another document
276 NSXMLElement
*parent
= (NSXMLElement
*)[[doc rootElement
] childAtIndex
:0];
277 NSArray
*children
= [parent children
];
278 [parent setChildren
:nil];
279 [windowController addChildren
:children toNode
:depthFetchNode
];
280 depthFetchNode
= nil;
284 * Helper method to create a string command with the -i <session> automatically tacked on
286 - (NSString
*)createCommand
:(NSString
*)cmd
288 return [NSString stringWithFormat
:@
"%@ -i %@", cmd
, session
];