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)initWithPort
: (int)port session
: (NSString
*)session
34 if (self = [super init
])
37 _session
= [session retain
];
40 _windowController
= [[DebuggerWindowController alloc
] initWithConnection
: self];
41 [[_windowController window
] makeKeyAndOrderFront
: self];
43 // now that we have our host information, open the socket
44 _socket
= [[SocketWrapper alloc
] initWithPort
: port
];
45 [_socket setDelegate
: self];
46 [_windowController setStatus
: @
"Connecting"];
53 * This is a forwarded message from DebuggerWindowController that tells the connection to prepare to
56 - (void)windowDidClose
58 [[NSApp delegate
] unregisterConnection
: self];
62 * Releases all of the object's data members and closes the streams
68 [_windowController release
];
74 * Gets the port number
82 * Gets the session name
90 * Returns the name of the remote host
92 - (NSString
*)remoteHost
96 return @
"(DISCONNECTED)";
98 return [_socket remoteHost
];
102 * Returns whether or not we have an active connection
110 * SocketWrapper delegate method that is called whenever new data is received
112 - (void)dataReceived
: (NSData
*)response deliverTo
: (SEL)selector
114 // if the caller of [_socket receive:] specified a deliverTo, just forward the message to them
117 [self performSelector
: selector withObject
: response
];
122 * SocketWrapper delegate method that is called after data is sent. This really
123 * isn't useful for much.
125 - (void)dataSent
: (NSString
*)data
127 NSLog(@
"send = %@", data
);
131 * Called by SocketWrapper after the connection is successful. This immediately calls
132 * -[SocketWrapper receive] to clear the way for communication
134 - (void)socketDidAccept
137 [_socket receive
: @selector(handshake
:)];
141 * Receives errors from the SocketWrapper and updates the display
143 - (void)errorEncountered
: (NSError
*)error
145 [_windowController setError
: [error domain
]];
149 * The initial packet handshake. This allows us to set things like the title of the window
150 * and glean information about hte server we are debugging
152 - (void)handshake
: (NSData
*)packet
154 [self refreshStatus
];
158 * Handler used by dataReceived:deliverTo: for anytime the status command is issued. It sets
159 * the window controller's status text
161 - (void)updateStatus
: (NSData
*)packet
163 NSXMLDocument
*doc
= [[NSXMLDocument alloc
] initWithData
: packet options
: NSXMLDocumentTidyXML error
: nil];
164 [_windowController setStatus
: [[[[doc rootElement
] attributeForName
: @
"status"] stringValue
] capitalizedString
]];
169 * Tells the debugger to continue running the script
173 [_socket send
: [self createCommand
: @
"run"]];
174 [self refreshStatus
];
178 * Method that runs tells the debugger to give us its status. This will call _updateStatus
179 * and will update the status text on the window
181 - (void)refreshStatus
183 [_socket send
: [self createCommand
: @
"status"]];
184 [_socket receive
: @selector(updateStatus
:)];
188 * Tells the debugger to step into the current command.
192 [_socket send
: [self createCommand
: @
"step_into"]];
193 [_socket receive
: nil];
194 [self refreshStatus
];
195 [self updateStackTraceAndRegisters
];
199 * Tells the debugger to step out of the current context
203 [_socket send
: [self createCommand
: @
"step_out"]];
204 [_socket receive
: nil];
205 [self refreshStatus
];
206 [self updateStackTraceAndRegisters
];
210 * Tells the debugger to step over the current function
214 [_socket send
: [self createCommand
: @
"step_over"]];
215 [_socket receive
: nil];
216 [self refreshStatus
];
217 [self updateStackTraceAndRegisters
];
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
: (NSData
*)packet
239 NSXMLDocument
*doc
= [[NSXMLDocument alloc
] initWithData
: packet options
: NSXMLDocumentTidyXML error
: nil];
240 NSArray
*children
= [[doc rootElement
] children
];
241 NSMutableArray
*stack
= [NSMutableArray array
];
242 NSMutableDictionary
*dict
= [NSMutableDictionary dictionary
];
243 for (int i
= 0; i
< [children count
]; i
++)
245 NSArray
*attrs
= [[children objectAtIndex
: i
] attributes
];
246 for (int j
= 0; j
< [attrs count
]; j
++)
248 [dict setValue
: [[attrs objectAtIndex
: j
] stringValue
] forKey
: [[attrs objectAtIndex
: j
] name
]];
250 [stack addObject
: dict
];
251 dict
= [NSMutableDictionary dictionary
];
253 [_windowController setStack
: stack
];
258 * Called when we have a new register to display
260 - (void)registerReceived
: (NSData
*)packet
262 NSXMLDocument
*doc
= [[NSXMLDocument alloc
] initWithData
: packet options
: NSXMLDocumentTidyXML error
: nil];
263 [_windowController setRegister
: doc
];
268 * Tells the debugger engine to get a specifc property. This also takes in the NSXMLElement
269 * that requested it so that the child can be attached in the delivery.
271 - (void)getProperty
: (NSString
*)property forElement
: (NSXMLElement
*)elm
273 [_socket send
: [self createCommand
: [NSString stringWithFormat
: @
"property_get -n \"%@\"", property
]]];
274 _depthFetchElement
= elm
;
275 [_socket receive
: @selector(propertyReceived
:)];
279 * Called when a property is received. This then adds the result as children to the passed object
281 - (void)propertyReceived
: (NSData
*)packet
283 NSXMLDocument
*doc
= [[NSXMLDocument alloc
] initWithData
: packet options
: NSXMLDocumentTidyXML error
: nil];
286 <property> <!-- this is the one we requested -->
287 <property ... /> <!-- these are what we want -->
292 // we now have to detach all the children so we can insert them into another document
293 NSXMLElement
*parent
= (NSXMLElement
*)[[doc rootElement
] childAtIndex
: 0];
294 NSArray
*children
= [parent children
];
295 [parent setChildren
: nil];
296 [_windowController addChildren
: children toNode
: _depthFetchElement
];
297 _depthFetchElement
= nil;
302 * Helper method to create a string command with the -i <session> automatically tacked on
304 - (NSString
*)createCommand
: (NSString
*)cmd
306 return [NSString stringWithFormat
: @
"%@ -i %@", cmd
, _session
];