Trying to get the register viewer to work
[macgdbp.git] / Source / DebuggerConnection.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2002 - 2007, Blue Static <http://www.bluestatic.org>
4 *
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.
8 *
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.
12 *
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
15 */
16
17 #import "DebuggerConnection.h"
18
19 @interface DebuggerConnection (Private)
20
21 - (NSString *)createCommand: (NSString *)cmd;
22
23 @end
24
25 @implementation DebuggerConnection
26
27 /**
28 * Creates a new DebuggerConnection and initializes the socket from the given connection
29 * paramters.
30 */
31 - (id)initWithPort: (int)port session: (NSString *)session
32 {
33 if (self = [super init])
34 {
35 _port = port;
36 _session = [session retain];
37 _connected = NO;
38
39 _windowController = [[DebuggerWindowController alloc] initWithConnection: self];
40 [[_windowController window] makeKeyAndOrderFront: self];
41
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"];
46 [_socket connect];
47
48 // clean up after ourselves
49 [[NSNotificationCenter defaultCenter] addObserver: self
50 selector: @selector(applicationWillTerminate:)
51 name: NSApplicationWillTerminateNotification
52 object: NSApp];
53 }
54 return self;
55 }
56
57 /**
58 * Release ourselves when we're about to die
59 */
60 - (void)applicationWillTerminate: (NSNotification *)notif
61 {
62 [self release];
63 }
64
65 /**
66 * Releases all of the object's data members and closes the streams
67 */
68 - (void)dealloc
69 {
70 [_session release];
71 [_socket release];
72
73 [super dealloc];
74 }
75
76 /**
77 * Gets the port number
78 */
79 - (int)port
80 {
81 return _port;
82 }
83
84 /**
85 * Gets the session name
86 */
87 - (NSString *)session
88 {
89 return _session;
90 }
91
92 /**
93 * Returns the name of the remote host
94 */
95 - (NSString *)remoteHost
96 {
97 if (!_connected)
98 {
99 return @"(DISCONNECTED)";
100 }
101 return [_socket remoteHost];
102 }
103
104 /**
105 * SocketWrapper delegate method that is called whenever new data is received
106 */
107 - (void)dataReceived: (NSData *)response deliverTo: (SEL)selector
108 {
109 NSLog(@"received = %@", [[[NSString alloc] initWithData: response encoding: NSUTF8StringEncoding] autorelease]);
110 // if the caller of [_socket receive:] specified a deliverTo, just forward the message to them
111 if (selector != nil)
112 {
113 [self performSelector: selector withObject: response];
114 }
115 }
116
117 /**
118 * SocketWrapper delegate method that is called after data is sent. This really
119 * isn't useful for much.
120 */
121 - (void)dataSent: (NSString *)data
122 {
123 NSLog(@"send = %@", data);
124 }
125
126 /**
127 * Called by SocketWrapper after the connection is successful. This immediately calls
128 * -[SocketWrapper receive] to clear the way for communication
129 */
130 - (void)socketDidAccept
131 {
132 _connected = YES;
133 [_socket receive: @selector(handshake:)];
134 }
135
136 /**
137 * Receives errors from the SocketWrapper and updates the display
138 */
139 - (void)errorEncountered: (NSError *)error
140 {
141 [_windowController setError: [error domain]];
142 }
143
144 /**
145 * The initial packet handshake. This allows us to set things like the title of the window
146 * and glean information about hte server we are debugging
147 */
148 - (void)handshake: (NSData *)packet
149 {
150 [self refreshStatus];
151 }
152
153 /**
154 * Handler used by dataReceived:deliverTo: for anytime the status command is issued. It sets
155 * the window controller's status text
156 */
157 - (void)updateStatus: (NSData *)packet
158 {
159 NSXMLDocument *doc = [[NSXMLDocument alloc] initWithData: packet options: NSXMLDocumentTidyXML error: nil];
160 [_windowController setStatus: [[[[doc rootElement] attributeForName: @"status"] stringValue] capitalizedString]];
161 [doc release];
162 }
163
164 /**
165 * Tells the debugger to continue running the script
166 */
167 - (void)run
168 {
169 [_socket send: [self createCommand: @"run"]];
170 [self refreshStatus];
171 }
172
173 /**
174 * Method that runs tells the debugger to give us its status. This will call _updateStatus
175 * and will update the status text on the window
176 */
177 - (void)refreshStatus
178 {
179 [_socket send: [self createCommand: @"status"]];
180 [_socket receive: @selector(updateStatus:)];
181 }
182
183 /**
184 * Tells the debugger to step into the current command.
185 */
186 - (void)stepIn
187 {
188 [_socket send: [self createCommand: @"step_into"]];
189 [_socket receive: nil];
190 [self refreshStatus];
191 [self updateStackTraceAndRegisters];
192 }
193
194 /**
195 * Tells the debugger to step out of the current context
196 */
197 - (void)stepOut
198 {
199 [_socket send: [self createCommand: @"step_out"]];
200 [_socket receive: nil];
201 [self refreshStatus];
202 [self updateStackTraceAndRegisters];
203 }
204
205 /**
206 * Tells the debugger to step over the current function
207 */
208 - (void)stepOver
209 {
210 [_socket send: [self createCommand: @"step_over"]];
211 [_socket receive: nil];
212 [self refreshStatus];
213 [self updateStackTraceAndRegisters];
214 }
215
216 /**
217 * This function queries the debug server for the current stacktrace and all the registers on
218 * level one. If a user then tries to expand past level one... TOOD: HOLY CRAP WHAT DO WE DO PAST LEVEL 1?
219 */
220 - (void)updateStackTraceAndRegisters
221 {
222 [_socket send: [self createCommand: @"stack_get"]];
223 [_socket receive: @selector(stackReceived:)];
224
225 [_socket send: [self createCommand: @"context_get"]];
226 [_socket receive: @selector(registerReceived:)];
227 }
228
229 /**
230 * Called by the dataReceived delivery delegate. This updates the window controller's data
231 * for the stack trace
232 */
233 - (void)stackReceived: (NSData *)packet
234 {
235 NSXMLDocument *doc = [[NSXMLDocument alloc] initWithData: packet options: NSXMLDocumentTidyXML error: nil];
236 NSArray *children = [[doc rootElement] children];
237 NSMutableArray *stack = [NSMutableArray array];
238 NSMutableDictionary *dict = [NSMutableDictionary dictionary];
239 for (int i = 0; i < [children count]; i++)
240 {
241 NSArray *attrs = [[children objectAtIndex: i] attributes];
242 for (int j = 0; j < [attrs count]; j++)
243 {
244 [dict setValue: [[attrs objectAtIndex: j] stringValue] forKey: [[attrs objectAtIndex: j] name]];
245 }
246 [stack addObject: dict];
247 dict = [NSMutableDictionary dictionary];
248 }
249 [_windowController setStack: stack];
250 [doc release];
251 }
252
253 /**
254 * Called when we have a new register to display
255 */
256 - (void)registerReceived: (NSData *)packet
257 {
258 NSXMLDocument *doc = [[NSXMLDocument alloc] initWithData: packet options: NSXMLDocumentTidyXML error: nil];
259 [_windowController setRegister: [doc rootElement]];
260 [doc release];
261 }
262
263 /**
264 * Helper method to create a string command with the -i <session> automatically tacked on
265 */
266 - (NSString *)createCommand: (NSString *)cmd
267 {
268 return [NSString stringWithFormat: @"%@ -i %@", cmd, _session];
269 }
270
271 @end