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
100 // register any breakpoints that exist offline
101 for (Breakpoint
*bp
in [[BreakpointManager sharedManager
] breakpoints
])
103 [self addBreakpoint
:bp
];
108 * Receives errors from the SocketWrapper and updates the display
110 - (void)errorEncountered
:(NSString
*)error
112 [windowController setError
:error
];
116 * Reestablishes communication with the remote debugger so that a new connection doesn't have to be
117 * created every time you want to debug a page
122 [windowController setStatus
:@
"Connecting"];
123 [windowController resetDisplays
];
128 * Tells the debugger to continue running the script
132 [socket send
:[self createCommand
:@
"run"]];
134 [self refreshStatus
];
138 * Method that runs tells the debugger to give us its status and will update the status text on the window
140 - (void)refreshStatus
142 [socket send
:[self createCommand
:@
"status"]];
144 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
145 NSString
*status
= [[[doc rootElement
] attributeForName
:@
"status"] stringValue
];
146 [windowController setStatus
:[status capitalizedString
]];
148 if ([status isEqualToString
:@
"break"])
150 [self updateStackTraceAndRegisters
];
152 else if ([status isEqualToString
:@
"stopped"])
156 [windowController setStatus
:[status capitalizedString
]];
161 * Tells the debugger to step into the current command.
165 [socket send
:[self createCommand
:@
"step_into"]];
167 [self refreshStatus
];
171 * Tells the debugger to step out of the current context
175 [socket send
:[self createCommand
:@
"step_out"]];
177 [self refreshStatus
];
181 * Tells the debugger to step over the current function
185 [socket send
:[self createCommand
:@
"step_over"]];
187 [self refreshStatus
];
191 * This function queries the debug server for the current stacktrace and all the registers on
192 * level one. If a user then tries to expand past level one... TOOD: HOLY CRAP WHAT DO WE DO PAST LEVEL 1?
194 - (void)updateStackTraceAndRegisters
197 [socket send
:[self createCommand
:@
"stack_get"]];
198 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
199 NSArray
*children
= [[doc rootElement
] children
];
200 NSMutableArray
*stack
= [NSMutableArray array
];
201 NSMutableDictionary
*dict
= [NSMutableDictionary dictionary
];
202 for (int i
= 0; i
< [children count
]; i
++)
204 NSArray
*attrs
= [[children objectAtIndex
:i
] attributes
];
205 for (int j
= 0; j
< [attrs count
]; j
++)
207 [dict setValue
:[[attrs objectAtIndex
:j
] stringValue
] forKey
:[[attrs objectAtIndex
:j
] name
]];
209 [stack addObject
:dict
];
210 dict
= [NSMutableDictionary dictionary
];
212 [windowController setStack
:stack
];
215 [socket send
:[self createCommand
:@
"context_get"]];
216 [windowController setRegister
:[self processData
:[socket receive
]]];
220 * Tells the debugger engine to get a specifc property. This also takes in the NSXMLElement
221 * that requested it so that the child can be attached.
223 - (NSArray
*)getProperty
:(NSString
*)property
225 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"property_get -n \"%@\"", property
]]];
227 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
231 <property> <!-- this is the one we requested -->
232 <property ... /> <!-- these are what we want -->
237 // we now have to detach all the children so we can insert them into another document
238 NSXMLElement
*parent
= (NSXMLElement
*)[[doc rootElement
] childAtIndex
:0];
239 NSArray
*children
= [parent children
];
240 [parent setChildren
:nil];
244 #pragma mark Breakpoints
247 * Send an add breakpoint command
249 - (void)addBreakpoint
:(Breakpoint
*)bp
256 NSString
*cmd
= [self createCommand
:[NSString stringWithFormat
:@
"breakpoint_set -t line -f %@ -n %i", [bp file
], [bp line
]]];
258 NSXMLDocument
*info
= [self processData
:[socket receive
]];
259 [bp setDebuggerId
:[[[[info rootElement
] attributeForName
:@
"id"] stringValue
] intValue
]];
263 * Removes a breakpoint
265 - (void)removeBreakpoint
:(Breakpoint
*)bp
272 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"breakpoint_remove -d %i", [bp debuggerId
]]]];
279 * Helper method to create a string command with the -i <session> automatically tacked on
281 - (NSString
*)createCommand
:(NSString
*)cmd
283 return [NSString stringWithFormat
:@
"%@ -i %@", cmd
, session
];
287 * Helper function to parse the NSData into an NSXMLDocument
289 - (NSXMLDocument
*)processData
:(NSString
*)data
291 NSError
*parseError
= nil;
292 NSXMLDocument
*doc
= [[NSXMLDocument alloc
] initWithXMLString
:data options
:0 error
:&parseError
];
295 NSLog(@
"Could not parse XML? --- %@", parseError
);
296 NSLog(@
"Error UserInfo: %@", [parseError userInfo
]);
297 NSLog(@
"This is the XML Document: %@", data
);
301 // check and see if there's an error
302 NSArray
*error
= [[doc rootElement
] elementsForName
:@
"error"];
303 if ([error count
] > 0)
305 [windowController setError
:[[[[error objectAtIndex
:0] children
] objectAtIndex
:0] stringValue
]];