Merge branch 'leopard'
[macgdbp.git] / Source / DebuggerConnection.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2008, 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 #import "AppDelegate.h"
19
20 @interface DebuggerConnection (Private)
21
22 - (NSString *)createCommand:(NSString *)cmd;
23 - (NSXMLDocument *)processData:(NSData *)data;
24
25 @end
26
27 @implementation DebuggerConnection
28
29 @synthesize socket, windowController;
30
31 /**
32 * Creates a new DebuggerConnection and initializes the socket from the given connection
33 * paramters.
34 */
35 - (id)initWithWindowController:(DebuggerWindowController *)wc port:(int)aPort session:(NSString *)aSession;
36 {
37 if (self = [super init])
38 {
39 port = aPort;
40 session = aSession;
41 connected = NO;
42
43 windowController = wc;
44
45 // now that we have our host information, open the socket
46 socket = [[SocketWrapper alloc] initWithConnection:self];
47 [socket setDelegate:self];
48 [socket connect];
49 }
50 return self;
51 }
52
53 /**
54 * Gets the port number
55 */
56 - (int)port
57 {
58 return port;
59 }
60
61 /**
62 * Gets the session name
63 */
64 - (NSString *)session
65 {
66 return session;
67 }
68
69 /**
70 * Returns the name of the remote host
71 */
72 - (NSString *)remoteHost
73 {
74 if (!connected)
75 {
76 return @"(DISCONNECTED)";
77 }
78 return [socket remoteHost];
79 }
80
81 /**
82 * Returns whether or not we have an active connection
83 */
84 - (BOOL)isConnected
85 {
86 return connected;
87 }
88
89 /**
90 * Called by SocketWrapper after the connection is successful. This immediately calls
91 * -[SocketWrapper receive] to clear the way for communication, though the information
92 * could be useful server information that we don't use right now.
93 */
94 - (void)socketDidAccept:(id)obj
95 {
96 connected = YES;
97 [socket receive];
98 [self refreshStatus];
99 }
100
101 /**
102 * Receives errors from the SocketWrapper and updates the display
103 */
104 - (void)errorEncountered:(NSString *)error
105 {
106 [windowController setError:error];
107 }
108
109 /**
110 * Tells the debugger to continue running the script
111 */
112 - (void)run
113 {
114 [socket send:[self createCommand:@"run"]];
115 [self refreshStatus];
116 }
117
118 /**
119 * Method that runs tells the debugger to give us its status and will update the status text on the window
120 */
121 - (void)refreshStatus
122 {
123 [socket send:[self createCommand:@"status"]];
124
125 NSXMLDocument *doc = [self processData:[socket receive]];
126 NSString *status = [[[doc rootElement] attributeForName:@"status"] stringValue];
127 [windowController setStatus:[status capitalizedString]];
128
129 if ([status isEqualToString:@"break"])
130 {
131 [self updateStackTraceAndRegisters];
132 }
133 }
134
135 /**
136 * Tells the debugger to step into the current command.
137 */
138 - (void)stepIn
139 {
140 [socket send:[self createCommand:@"step_into"]];
141 [socket receive];
142 [self refreshStatus];
143 }
144
145 /**
146 * Tells the debugger to step out of the current context
147 */
148 - (void)stepOut
149 {
150 [socket send:[self createCommand:@"step_out"]];
151 [socket receive];
152 [self refreshStatus];
153 }
154
155 /**
156 * Tells the debugger to step over the current function
157 */
158 - (void)stepOver
159 {
160 [socket send:[self createCommand:@"step_over"]];
161 [socket receive];
162 [self refreshStatus];
163 }
164
165 /**
166 * This function queries the debug server for the current stacktrace and all the registers on
167 * level one. If a user then tries to expand past level one... TOOD: HOLY CRAP WHAT DO WE DO PAST LEVEL 1?
168 */
169 - (void)updateStackTraceAndRegisters
170 {
171 // do the stack
172 [socket send:[self createCommand:@"stack_get"]];
173 NSXMLDocument *doc = [self processData:[socket receive]];
174 NSArray *children = [[doc rootElement] children];
175 NSMutableArray *stack = [NSMutableArray array];
176 NSMutableDictionary *dict = [NSMutableDictionary dictionary];
177 for (int i = 0; i < [children count]; i++)
178 {
179 NSArray *attrs = [[children objectAtIndex:i] attributes];
180 for (int j = 0; j < [attrs count]; j++)
181 {
182 [dict setValue:[[attrs objectAtIndex:j] stringValue] forKey:[[attrs objectAtIndex:j] name]];
183 }
184 [stack addObject:dict];
185 dict = [NSMutableDictionary dictionary];
186 }
187 [windowController setStack:stack];
188
189 // do the registers
190 [socket send:[self createCommand:@"context_get"]];
191 [windowController setRegister:[self processData:[socket receive]]];
192 }
193
194 /**
195 * Tells the debugger engine to get a specifc property. This also takes in the NSXMLElement
196 * that requested it so that the child can be attached.
197 */
198 - (void)getProperty:(NSString *)property forNode:(NSTreeNode *)node
199 {
200 [socket send:[self createCommand:[NSString stringWithFormat:@"property_get -n \"%@\"", property]]];
201
202 NSXMLDocument *doc = [self processData:[socket receive]];
203
204 /*
205 <response>
206 <property> <!-- this is the one we requested -->
207 <property ... /> <!-- these are what we want -->
208 </property>
209 </repsonse>
210 */
211
212 // we now have to detach all the children so we can insert them into another document
213 NSXMLElement *parent = (NSXMLElement *)[[doc rootElement] childAtIndex:0];
214 NSArray *children = [parent children];
215 [parent setChildren:nil];
216 [windowController addChildren:children toNode:node];
217 }
218
219 /**
220 * Helper method to create a string command with the -i <session> automatically tacked on
221 */
222 - (NSString *)createCommand:(NSString *)cmd
223 {
224 return [NSString stringWithFormat:@"%@ -i %@", cmd, session];
225 }
226
227 /**
228 * Helper function to parse the NSData into an NSXMLDocument
229 */
230 - (NSXMLDocument *)processData:(NSData *)data
231 {
232 NSXMLDocument *doc = [[NSXMLDocument alloc] initWithData:data options:NSXMLDocumentTidyXML error:nil];
233
234 // check and see if there's an error
235 NSArray *error = [[doc rootElement] elementsForName:@"error"];
236 if ([error count] > 0)
237 {
238 [windowController setError:[[[[error objectAtIndex:0] children] objectAtIndex:0] stringValue]];
239 return nil;
240 }
241
242 return doc;
243 }
244
245 @end