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
)
21 - (NSString
*)createCommand
:(NSString
*)cmd
;
22 - (NSXMLDocument
*)processData
:(NSString
*)data
;
25 @implementation DebuggerConnection
27 @synthesize socket
, windowController
;
30 * Creates a new DebuggerConnection and initializes the socket from the given connection
33 - (id)initWithWindowController
:(DebuggerWindowController
*)wc port
:(int)aPort session
:(NSString
*)aSession
;
35 if (self = [super init
])
41 windowController
= wc
;
43 // now that we have our host information, open the socket
44 socket
= [[SocketWrapper alloc
] initWithConnection
:self];
45 [socket setDelegate
:self];
48 [[BreakpointManager sharedManager
] setConnection
: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"]];
128 [self refreshStatus
];
132 * Method that runs tells the debugger to give us its status and will update the status text on the window
134 - (void)refreshStatus
136 [socket send
:[self createCommand
:@
"status"]];
138 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
139 NSString
*status
= [[[doc rootElement
] attributeForName
:@
"status"] stringValue
];
140 [windowController setStatus
:[status capitalizedString
]];
142 if ([status isEqualToString
:@
"break"])
144 [self updateStackTraceAndRegisters
];
146 else if ([status isEqualToString
:@
"stopped"])
150 [windowController setStatus
:[status capitalizedString
]];
155 * Tells the debugger to step into the current command.
159 [socket send
:[self createCommand
:@
"step_into"]];
161 [self refreshStatus
];
165 * Tells the debugger to step out of the current context
169 [socket send
:[self createCommand
:@
"step_out"]];
171 [self refreshStatus
];
175 * Tells the debugger to step over the current function
179 [socket send
:[self createCommand
:@
"step_over"]];
181 [self refreshStatus
];
185 * This function queries the debug server for the current stacktrace and all the registers on
186 * level one. If a user then tries to expand past level one... TOOD: HOLY CRAP WHAT DO WE DO PAST LEVEL 1?
188 - (void)updateStackTraceAndRegisters
191 [socket send
:[self createCommand
:@
"stack_get"]];
192 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
193 NSArray
*children
= [[doc rootElement
] children
];
194 NSMutableArray
*stack
= [NSMutableArray array
];
195 NSMutableDictionary
*dict
= [NSMutableDictionary dictionary
];
196 for (int i
= 0; i
< [children count
]; i
++)
198 NSArray
*attrs
= [[children objectAtIndex
:i
] attributes
];
199 for (int j
= 0; j
< [attrs count
]; j
++)
201 [dict setValue
:[[attrs objectAtIndex
:j
] stringValue
] forKey
:[[attrs objectAtIndex
:j
] name
]];
203 [stack addObject
:dict
];
204 dict
= [NSMutableDictionary dictionary
];
206 [windowController setStack
:stack
];
209 [socket send
:[self createCommand
:@
"context_get"]];
210 [windowController setRegister
:[self processData
:[socket receive
]]];
214 * Tells the debugger engine to get a specifc property. This also takes in the NSXMLElement
215 * that requested it so that the child can be attached.
217 - (void)getProperty
:(NSString
*)property forNode
:(NSTreeNode
*)node
219 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"property_get -n \"%@\"", property
]]];
221 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
225 <property> <!-- this is the one we requested -->
226 <property ... /> <!-- these are what we want -->
231 // we now have to detach all the children so we can insert them into another document
232 NSXMLElement
*parent
= (NSXMLElement
*)[[doc rootElement
] childAtIndex
:0];
233 NSArray
*children
= [parent children
];
234 [parent setChildren
:nil];
235 [windowController addChildren
:children toNode
:node
];
238 #pragma mark Breakpoints
241 * Send an add breakpoint command
243 - (void)addBreakpoint
:(Breakpoint
*)bp
250 NSString
*cmd
= [self createCommand
:[NSString stringWithFormat
:@
"breakpoint_set -t line -f %@ -n %i", [bp file
], [bp line
]]];
252 NSXMLDocument
*info
= [self processData
:[socket receive
]];
253 [bp setDebuggerId
:[[[[info rootElement
] attributeForName
:@
"id"] stringValue
] intValue
]];
257 * Removes a breakpoint
259 - (void)removeBreakpoint
:(Breakpoint
*)bp
266 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"breakpoint_remove -d %i", [bp debuggerId
]]]];
273 * Helper method to create a string command with the -i <session> automatically tacked on
275 - (NSString
*)createCommand
:(NSString
*)cmd
277 return [NSString stringWithFormat
:@
"%@ -i %@", cmd
, session
];
281 * Helper function to parse the NSData into an NSXMLDocument
283 - (NSXMLDocument
*)processData
:(NSString
*)data
285 NSError
*parseError
= nil;
286 NSXMLDocument
*doc
= [[NSXMLDocument alloc
] initWithXMLString
:data options
:0 error
:&parseError
];
289 NSLog(@
"Could not parse XML? --- %@", parseError
);
290 NSLog(@
"Error UserInfo: %@", [parseError userInfo
]);
291 NSLog(@
"This is the XML Document: %@", data
);
295 // check and see if there's an error
296 NSArray
*error
= [[doc rootElement
] elementsForName
:@
"error"];
297 if ([error count
] > 0)
299 [windowController setError
:[[[[error objectAtIndex
:0] children
] objectAtIndex
:0] stringValue
]];