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 @implementation DebuggerConnection
22 * Creates a new DebuggerConnection and initializes the socket from the given connection
25 - (id)initWithPort
: (int)port session
: (NSString
*)session
27 if (self = [super init
])
30 _session
= [session retain
];
33 _windowController
= [[DebuggerWindowController alloc
] initWithConnection
: self];
34 [[_windowController window
] makeKeyAndOrderFront
: self];
36 // now that we have our host information, open the socket
37 _socket
= [[SocketWrapper alloc
] initWithPort
: port
];
38 [_socket setDelegate
: self];
39 [_windowController setStatus
: @
"Connecting"];
42 // clean up after ourselves
43 [[NSNotificationCenter defaultCenter
] addObserver
: self
44 selector
: @selector(applicationWillTerminate
:)
45 name
: NSApplicationWillTerminateNotification
52 * Release ourselves when we're about to die
54 - (void)applicationWillTerminate
: (NSNotification
*)notif
60 * Releases all of the object's data members and closes the streams
71 * Gets the port number
79 * Gets the session name
87 * Returns the name of the remote host
89 - (NSString
*)remoteHost
93 return @
"(DISCONNECTED)";
95 return [_socket remoteHost
];
99 * SocketWrapper delegate method that is called whenever new data is received
101 - (void)dataReceived
: (NSString
*)response deliverTo
: (SEL)selector
103 // if the caller of [_socket receive:] specified a deliverTo, just forward the message to them
106 [self performSelector
: selector withObject
: response
];
108 NSLog(@
"receive = %@", response
);
112 * SocketWrapper delegate method that is called after data is sent. This really
113 * isn't useful for much.
115 - (void)dataSent
: (NSString
*)data
117 NSLog(@
"send = %@", data
);
121 * Called by SocketWrapper after the connection is successful. This immediately calls
122 * -[SocketWrapper receive] to clear the way for communication
124 - (void)socketDidAccept
127 [_socket receive
: @selector(_handshake
:)];
131 * Receives errors from the SocketWrapper and updates the display
133 - (void)errorEncountered
: (NSError
*)error
135 [_windowController setError
: [error domain
]];
139 * The initial packet handshake. This allows us to set things like the title of the window
140 * and glean information about hte server we are debugging
142 - (void)_handshake
: (NSString
*)packet
144 [self refreshStatus
];
148 * Handler used by dataReceived:deliverTo: for anytime the status command is issued. It sets
149 * the window controller's status text
151 - (void)_updateStatus
: (NSString
*)packet
153 NSXMLDocument
*doc
= [[NSXMLDocument alloc
] initWithXMLString
: packet options
: NSXMLDocumentTidyXML error
: nil];
154 [_windowController setStatus
: [[[[doc rootElement
] attributeForName
: @
"status"] stringValue
] capitalizedString
]];
159 * Tells the debugger to continue running the script
163 [_socket send
: [self _createCommand
: @
"run"]];
164 [self refreshStatus
];
168 * Method that runs tells the debugger to give us its status. This will call _updateStatus
169 * and will update the status text on the window
171 - (void)refreshStatus
173 [_socket send
: [self _createCommand
: @
"status"]];
174 [_socket receive
: @selector(_updateStatus
:)];
178 * Tells the debugger to step into the current command.
180 // TODO: populate the stack trace and registers list
183 [_socket send
: [self _createCommand
: @
"step_into"]];
184 [_socket receive
: nil];
185 [self refreshStatus
];
186 [self updateStackTraceAndRegisters
];
190 * This function queries the debug server for the current stacktrace and all the registers on
191 * level one. If a user then tries to expand past level one... TOOD: HOLY CRAP WHAT DO WE DO PAST LEVEL 1?
193 - (void)updateStackTraceAndRegisters
195 [_socket send
: [self _createCommand
: @
"stack_depth"]];
196 [_socket receive
: @selector(_stackDepthReceived
:)];
200 * Packet delivery method for when we have the stack depth. This just goes back and asks the
201 * debug server for the stack at that depth
203 - (void)_stackDepthReceived
: (NSString
*)packet
205 NSXMLDocument
*doc
= [[NSXMLDocument alloc
] initWithXMLString
: packet options
: NSXMLDocumentTidyXML error
: nil];
206 int depth
= [[[[doc rootElement
] attributeForName
: @
"depth"] stringValue
] intValue
];
207 [_socket send
: [self _createCommand
: [NSString stringWithFormat
: @
"stack_get -d %d", (depth
- 1)]]];
208 [_socket receive
: @selector(_stackReceived
:)];
212 * Called by the dataReceived delivery delegate. This updates the window controller's data
213 * for the stack trace
215 - (void)_stackReceived
: (NSString
*)packet
217 NSXMLDocument
*doc
= [[NSXMLDocument alloc
] initWithXMLString
: packet options
: NSXMLDocumentTidyXML error
: nil];
218 NSArray
*children
= [[doc rootElement
] children
];
219 NSMutableArray
*stack
= [NSMutableArray array
];
220 NSMutableDictionary
*dict
= [NSMutableDictionary dictionary
];
221 for (int i
= 0; i
< [children count
]; i
++)
223 NSArray
*attrs
= [[children objectAtIndex
: i
] attributes
];
224 for (int j
= 0; j
< [attrs count
]; j
++)
226 [dict setValue
: [[attrs objectAtIndex
: j
] stringValue
] forKey
: [[attrs objectAtIndex
: j
] name
]];
228 [stack addObject
: dict
];
229 dict
= [NSMutableDictionary dictionary
];
231 [_windowController setStack
: stack
];
235 * Helper method to create a string command with the -i <session> automatically tacked on
237 - (NSString
*)_createCommand
: (NSString
*)cmd
239 return [NSString stringWithFormat
: @
"%@ -i %@", cmd
, _session
];