3 * Copyright (c) 2007 - 2008, 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
;
23 - (NSXMLDocument
*)processData
:(NSString
*)data
;
27 @implementation DebuggerConnection
29 @synthesize socket
, windowController
;
32 * Creates a new DebuggerConnection and initializes the socket from the given connection
35 - (id)initWithWindowController
:(DebuggerWindowController
*)wc port
:(int)aPort session
:(NSString
*)aSession
;
37 if (self = [super init
])
43 windowController
= wc
;
45 // now that we have our host information, open the socket
46 socket
= [[SocketWrapper alloc
] initWithConnection
:self];
47 [socket setDelegate
:self];
54 * Gets the port number
62 * Gets the session name
70 * Returns the name of the remote host
72 - (NSString
*)remoteHost
76 return @
"(DISCONNECTED)";
78 return [socket remoteHost
];
82 * Returns whether or not we have an active connection
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.
94 - (void)socketDidAccept
:(id)obj
102 * Receives errors from the SocketWrapper and updates the display
104 - (void)errorEncountered
:(NSString
*)error
106 [windowController setError
:error
];
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
116 [windowController setStatus
:@
"Connecting"];
117 [windowController resetDisplays
];
122 * Tells the debugger to continue running the script
126 [socket send
:[self createCommand
:@
"run"]];
127 [self refreshStatus
];
131 * Method that runs tells the debugger to give us its status and will update the status text on the window
133 - (void)refreshStatus
135 [socket send
:[self createCommand
:@
"status"]];
137 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
138 NSString
*status
= [[[doc rootElement
] attributeForName
:@
"status"] stringValue
];
139 [windowController setStatus
:[status capitalizedString
]];
141 if ([status isEqualToString
:@
"break"])
143 [self updateStackTraceAndRegisters
];
145 else if ([status isEqualToString
:@
"stopped"])
149 [windowController setStatus
:[status capitalizedString
]];
154 * Tells the debugger to step into the current command.
158 [socket send
:[self createCommand
:@
"step_into"]];
160 [self refreshStatus
];
164 * Tells the debugger to step out of the current context
168 [socket send
:[self createCommand
:@
"step_out"]];
170 [self refreshStatus
];
174 * Tells the debugger to step over the current function
178 [socket send
:[self createCommand
:@
"step_over"]];
180 [self refreshStatus
];
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?
187 - (void)updateStackTraceAndRegisters
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
++)
197 NSArray
*attrs
= [[children objectAtIndex
:i
] attributes
];
198 for (int j
= 0; j
< [attrs count
]; j
++)
200 [dict setValue
:[[attrs objectAtIndex
:j
] stringValue
] forKey
:[[attrs objectAtIndex
:j
] name
]];
202 [stack addObject
:dict
];
203 dict
= [NSMutableDictionary dictionary
];
205 [windowController setStack
:stack
];
208 [socket send
:[self createCommand
:@
"context_get"]];
209 [windowController setRegister
:[self processData
:[socket receive
]]];
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.
216 - (void)getProperty
:(NSString
*)property forNode
:(NSTreeNode
*)node
218 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"property_get -n \"%@\"", property
]]];
220 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
224 <property> <!-- this is the one we requested -->
225 <property ... /> <!-- these are what we want -->
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
];
237 #pragma mark Breakpoints
240 * Send an add breakpoint command
242 - (void)addBreakpoint
:(Breakpoint
*)bp
244 NSString
*cmd
= [self createCommand
:[NSString stringWithFormat
:@
"breakpoint_set -t line -f %@ -n %i", [bp file
], [bp line
]]];
246 NSXMLDocument
*info
= [self processData
:[socket receive
]];
247 [bp setDebuggerId
:[[[[info rootElement
] attributeForName
:@
"id"] stringValue
] intValue
]];
251 * Removes a breakpoint
253 - (void)removeBreakpoint
:(Breakpoint
*)bp
255 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"breakpoint_remove -d %i", [bp debuggerId
]]]];
262 * Helper method to create a string command with the -i <session> automatically tacked on
264 - (NSString
*)createCommand
:(NSString
*)cmd
266 return [NSString stringWithFormat
:@
"%@ -i %@", cmd
, session
];
270 * Helper function to parse the NSData into an NSXMLDocument
272 - (NSXMLDocument
*)processData
:(NSString
*)data
274 NSError
*parseError
= nil;
275 NSXMLDocument
*doc
= [[NSXMLDocument alloc
] initWithXMLString
:data options
:0 error
:&parseError
];
278 NSLog(@
"Could not parse XML? --- %@", parseError
);
279 NSLog(@
"Error UserInfo: %@", [parseError userInfo
]);
280 NSLog(@
"This is the XML Document: %@", data
);
284 // check and see if there's an error
285 NSArray
*error
= [[doc rootElement
] elementsForName
:@
"error"];
286 if ([error count
] > 0)
288 [windowController setError
:[[[[error objectAtIndex
:0] children
] objectAtIndex
:0] stringValue
]];