Adding support to actually set breakpoints on the debuger side
[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 * Reestablishes communication with the remote debugger so that a new connection doesn't have to be
111 * created every time you want to debug a page
112 */
113 - (void)reconnect
114 {
115 [socket close];
116 [windowController setStatus:@"Connecting"];
117 [windowController resetDisplays];
118 [socket connect];
119 }
120
121 /**
122 * Tells the debugger to continue running the script
123 */
124 - (void)run
125 {
126 [socket send:[self createCommand:@"run"]];
127 [self refreshStatus];
128 }
129
130 /**
131 * Method that runs tells the debugger to give us its status and will update the status text on the window
132 */
133 - (void)refreshStatus
134 {
135 [socket send:[self createCommand:@"status"]];
136
137 NSXMLDocument *doc = [self processData:[socket receive]];
138 NSString *status = [[[doc rootElement] attributeForName:@"status"] stringValue];
139 [windowController setStatus:[status capitalizedString]];
140
141 if ([status isEqualToString:@"break"])
142 {
143 [self updateStackTraceAndRegisters];
144 }
145 else if ([status isEqualToString:@"stopped"])
146 {
147 connected = NO;
148 [socket close];
149 [windowController setStatus:[status capitalizedString]];
150 }
151 }
152
153 /**
154 * Tells the debugger to step into the current command.
155 */
156 - (void)stepIn
157 {
158 [socket send:[self createCommand:@"step_into"]];
159 [socket receive];
160 [self refreshStatus];
161 }
162
163 /**
164 * Tells the debugger to step out of the current context
165 */
166 - (void)stepOut
167 {
168 [socket send:[self createCommand:@"step_out"]];
169 [socket receive];
170 [self refreshStatus];
171 }
172
173 /**
174 * Tells the debugger to step over the current function
175 */
176 - (void)stepOver
177 {
178 [socket send:[self createCommand:@"step_over"]];
179 [socket receive];
180 [self refreshStatus];
181 }
182
183 /**
184 * This function queries the debug server for the current stacktrace and all the registers on
185 * level one. If a user then tries to expand past level one... TOOD: HOLY CRAP WHAT DO WE DO PAST LEVEL 1?
186 */
187 - (void)updateStackTraceAndRegisters
188 {
189 // do the stack
190 [socket send:[self createCommand:@"stack_get"]];
191 NSXMLDocument *doc = [self processData:[socket receive]];
192 NSArray *children = [[doc rootElement] children];
193 NSMutableArray *stack = [NSMutableArray array];
194 NSMutableDictionary *dict = [NSMutableDictionary dictionary];
195 for (int i = 0; i < [children count]; i++)
196 {
197 NSArray *attrs = [[children objectAtIndex:i] attributes];
198 for (int j = 0; j < [attrs count]; j++)
199 {
200 [dict setValue:[[attrs objectAtIndex:j] stringValue] forKey:[[attrs objectAtIndex:j] name]];
201 }
202 [stack addObject:dict];
203 dict = [NSMutableDictionary dictionary];
204 }
205 [windowController setStack:stack];
206
207 // do the registers
208 [socket send:[self createCommand:@"context_get"]];
209 [windowController setRegister:[self processData:[socket receive]]];
210 }
211
212 /**
213 * Tells the debugger engine to get a specifc property. This also takes in the NSXMLElement
214 * that requested it so that the child can be attached.
215 */
216 - (void)getProperty:(NSString *)property forNode:(NSTreeNode *)node
217 {
218 [socket send:[self createCommand:[NSString stringWithFormat:@"property_get -n \"%@\"", property]]];
219
220 NSXMLDocument *doc = [self processData:[socket receive]];
221
222 /*
223 <response>
224 <property> <!-- this is the one we requested -->
225 <property ... /> <!-- these are what we want -->
226 </property>
227 </repsonse>
228 */
229
230 // we now have to detach all the children so we can insert them into another document
231 NSXMLElement *parent = (NSXMLElement *)[[doc rootElement] childAtIndex:0];
232 NSArray *children = [parent children];
233 [parent setChildren:nil];
234 [windowController addChildren:children toNode:node];
235 }
236
237 #pragma mark Breakpoints
238
239 /**
240 * Send an add breakpoint command
241 */
242 - (void)addBreakpoint:(Breakpoint *)bp
243 {
244 NSString *cmd = [self createCommand:[NSString stringWithFormat:@"breakpoint_set -t line -f %@ -n %i", [bp file], [bp line]]];
245 [socket send:cmd];
246 NSXMLDocument *info = [self processData:[socket receive]];
247 [bp setDebuggerId:[[[[info rootElement] attributeForName:@"id"] stringValue] intValue]];
248 }
249
250 /**
251 * Removes a breakpoint
252 */
253 - (void)removeBreakpoint:(Breakpoint *)bp
254 {
255 [socket send:[self createCommand:[NSString stringWithFormat:@"breakpoint_remove -d %i", [bp debuggerId]]]];
256 [socket receive];
257 }
258
259 #pragma mark Private
260
261 /**
262 * Helper method to create a string command with the -i <session> automatically tacked on
263 */
264 - (NSString *)createCommand:(NSString *)cmd
265 {
266 return [NSString stringWithFormat:@"%@ -i %@", cmd, session];
267 }
268
269 /**
270 * Helper function to parse the NSData into an NSXMLDocument
271 */
272 - (NSXMLDocument *)processData:(NSData *)data
273 {
274 NSXMLDocument *doc = [[NSXMLDocument alloc] initWithData:data options:NSXMLDocumentTidyXML error:nil];
275
276 // check and see if there's an error
277 NSArray *error = [[doc rootElement] elementsForName:@"error"];
278 if ([error count] > 0)
279 {
280 [windowController setError:[[[[error objectAtIndex:0] children] objectAtIndex:0] stringValue]];
281 return nil;
282 }
283
284 return doc;
285 }
286
287 @end