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"
19 @interface DebuggerConnection (Private
)
21 - (NSString
*)createCommand
: (NSString
*)cmd
;
25 @implementation DebuggerConnection
28 * Creates a new DebuggerConnection and initializes the socket from the given connection
31 - (id)initWithPort
: (int)port session
: (NSString
*)session
33 if (self = [super init
])
36 _session
= [session retain
];
39 _windowController
= [[DebuggerWindowController alloc
] initWithConnection
: self];
40 [[_windowController window
] makeKeyAndOrderFront
: self];
42 // now that we have our host information, open the socket
43 _socket
= [[SocketWrapper alloc
] initWithPort
: port
];
44 [_socket setDelegate
: self];
45 [_windowController setStatus
: @
"Connecting"];
48 // clean up after ourselves
49 [[NSNotificationCenter defaultCenter
] addObserver
: self
50 selector
: @selector(applicationWillTerminate
:)
51 name
: NSApplicationWillTerminateNotification
58 * Release ourselves when we're about to die
60 - (void)applicationWillTerminate
: (NSNotification
*)notif
66 * Releases all of the object's data members and closes the streams
77 * Gets the port number
85 * Gets the session name
93 * Returns the name of the remote host
95 - (NSString
*)remoteHost
99 return @
"(DISCONNECTED)";
101 return [_socket remoteHost
];
105 * SocketWrapper delegate method that is called whenever new data is received
107 - (void)dataReceived
: (NSData
*)response deliverTo
: (SEL)selector
109 // if the caller of [_socket receive:] specified a deliverTo, just forward the message to them
112 [self performSelector
: selector withObject
: response
];
117 * SocketWrapper delegate method that is called after data is sent. This really
118 * isn't useful for much.
120 - (void)dataSent
: (NSString
*)data
122 NSLog(@
"send = %@", data
);
126 * Called by SocketWrapper after the connection is successful. This immediately calls
127 * -[SocketWrapper receive] to clear the way for communication
129 - (void)socketDidAccept
132 [_socket receive
: @selector(handshake
:)];
136 * Receives errors from the SocketWrapper and updates the display
138 - (void)errorEncountered
: (NSError
*)error
140 [_windowController setError
: [error domain
]];
144 * The initial packet handshake. This allows us to set things like the title of the window
145 * and glean information about hte server we are debugging
147 - (void)handshake
: (NSData
*)packet
149 [self refreshStatus
];
153 * Handler used by dataReceived:deliverTo: for anytime the status command is issued. It sets
154 * the window controller's status text
156 - (void)updateStatus
: (NSData
*)packet
158 NSXMLDocument
*doc
= [[NSXMLDocument alloc
] initWithData
: packet options
: NSXMLDocumentTidyXML error
: nil];
159 [_windowController setStatus
: [[[[doc rootElement
] attributeForName
: @
"status"] stringValue
] capitalizedString
]];
164 * Tells the debugger to continue running the script
168 [_socket send
: [self createCommand
: @
"run"]];
169 [self refreshStatus
];
173 * Method that runs tells the debugger to give us its status. This will call _updateStatus
174 * and will update the status text on the window
176 - (void)refreshStatus
178 [_socket send
: [self createCommand
: @
"status"]];
179 [_socket receive
: @selector(updateStatus
:)];
183 * Tells the debugger to step into the current command.
187 [_socket send
: [self createCommand
: @
"step_into"]];
188 [_socket receive
: nil];
189 [self refreshStatus
];
190 [self updateStackTraceAndRegisters
];
194 * Tells the debugger to step out of the current context
198 [_socket send
: [self createCommand
: @
"step_out"]];
199 [_socket receive
: nil];
200 [self refreshStatus
];
201 [self updateStackTraceAndRegisters
];
205 * Tells the debugger to step over the current function
209 [_socket send
: [self createCommand
: @
"step_over"]];
210 [_socket receive
: nil];
211 [self refreshStatus
];
212 [self updateStackTraceAndRegisters
];
216 * This function queries the debug server for the current stacktrace and all the registers on
217 * level one. If a user then tries to expand past level one... TOOD: HOLY CRAP WHAT DO WE DO PAST LEVEL 1?
219 - (void)updateStackTraceAndRegisters
221 [_socket send
: [self createCommand
: @
"stack_get"]];
222 [_socket receive
: @selector(stackReceived
:)];
224 [_socket send
: [self createCommand
: @
"context_get"]];
225 [_socket receive
: @selector(registerReceived
:)];
229 * Called by the dataReceived delivery delegate. This updates the window controller's data
230 * for the stack trace
232 - (void)stackReceived
: (NSData
*)packet
234 NSXMLDocument
*doc
= [[NSXMLDocument alloc
] initWithData
: packet options
: NSXMLDocumentTidyXML error
: nil];
235 NSArray
*children
= [[doc rootElement
] children
];
236 NSMutableArray
*stack
= [NSMutableArray array
];
237 NSMutableDictionary
*dict
= [NSMutableDictionary dictionary
];
238 for (int i
= 0; i
< [children count
]; i
++)
240 NSArray
*attrs
= [[children objectAtIndex
: i
] attributes
];
241 for (int j
= 0; j
< [attrs count
]; j
++)
243 [dict setValue
: [[attrs objectAtIndex
: j
] stringValue
] forKey
: [[attrs objectAtIndex
: j
] name
]];
245 [stack addObject
: dict
];
246 dict
= [NSMutableDictionary dictionary
];
248 [_windowController setStack
: stack
];
253 * Called when we have a new register to display
255 - (void)registerReceived
: (NSData
*)packet
257 NSXMLDocument
*doc
= [[NSXMLDocument alloc
] initWithData
: packet options
: NSXMLDocumentTidyXML error
: nil];
258 [_windowController setRegister
: [doc rootElement
]];
263 * Tells the debugger engine to get a specifc property. This also takes in the NSXMLElement
264 * that requested it so that the child can be attached in the delivery.
266 - (void)getProperty
: (NSString
*)property forElement
: (NSXMLElement
*)elm
268 [_socket send
: [self createCommand
: [NSString stringWithFormat
: @
"property_get -n \"%@\"", property
]]];
269 _depthFetchElement
= elm
;
270 [_socket receive
: @selector(propertyReceived
:)];
274 * Called when a property is received. This then adds the result as children to the passed object
276 - (void)propertyReceived
: (NSData
*)packet
278 NSXMLDocument
*doc
= [[NSXMLDocument alloc
] initWithData
: packet options
: NSXMLDocumentTidyXML error
: nil];
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
= [[doc rootElement
] childAtIndex
: 0];
289 NSArray
*children
= [parent children
];
290 [parent setChildren
: nil];
291 [_windowController addChildren
: children toNode
: _depthFetchElement
];
292 _depthFetchElement
= nil;
297 * Helper method to create a string command with the -i <session> automatically tacked on
299 - (NSString
*)createCommand
: (NSString
*)cmd
301 return [NSString stringWithFormat
: @
"%@ -i %@", cmd
, _session
];