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 "GDBpConnection.h"
18 #import "AppDelegate.h"
20 NSString
*kErrorOccurredNotif
= @
"GDBpConnection_ErrorOccured_Notification";
22 @interface GDBpConnection (Private
)
23 - (NSString
*)createCommand
:(NSString
*)cmd
;
24 - (NSXMLDocument
*)processData
:(NSString
*)data
;
25 - (StackFrame
*)createStackFrame
;
28 @implementation GDBpConnection
30 @synthesize socket
, windowController
;
33 * Creates a new DebuggerConnection and initializes the socket from the given connection
36 - (id)initWithWindowController
:(DebuggerController
*)wc port
:(int)aPort session
:(NSString
*)aSession
;
38 if (self = [super init
])
41 session
= [aSession retain
];
44 windowController
= [wc retain
];
46 // now that we have our host information, open the socket
47 socket
= [[SocketWrapper alloc
] initWithConnection
:self];
48 [socket setDelegate
:self];
51 [[BreakpointManager sharedManager
] setConnection
:self];
57 * Deallocates the object
63 [windowController release
];
68 * Gets the port number
76 * Gets the session name
84 * Returns the name of the remote host
86 - (NSString
*)remoteHost
90 return @
"(DISCONNECTED)";
92 return [socket remoteHost
];
96 * Returns whether or not we have an active connection
104 * Called by SocketWrapper after the connection is successful. This immediately calls
105 * -[SocketWrapper receive] to clear the way for communication, though the information
106 * could be useful server information that we don't use right now.
108 - (void)socketDidAccept
:(id)obj
112 [self refreshStatus
];
114 // register any breakpoints that exist offline
115 for (Breakpoint
*bp
in [[BreakpointManager sharedManager
] breakpoints
])
117 [self addBreakpoint
:bp
];
122 * Receives errors from the SocketWrapper and updates the display
124 - (void)errorEncountered
:(NSString
*)error
126 [[NSNotificationCenter defaultCenter
]
127 postNotificationName
:kErrorOccurredNotif
129 userInfo
:[NSDictionary
130 dictionaryWithObject
:error
137 * Reestablishes communication with the remote debugger so that a new connection doesn't have to be
138 * created every time you want to debug a page
143 [windowController setStatus
:@
"Connecting"];
144 [windowController resetDisplays
];
149 * Tells the debugger to continue running the script
153 [socket send
:[self createCommand
:@
"run"]];
155 [self refreshStatus
];
159 * Method that runs tells the debugger to give us its status and will update the status text on the window
161 - (void)refreshStatus
163 [socket send
:[self createCommand
:@
"status"]];
165 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
166 NSString
*status
= [[[doc rootElement
] attributeForName
:@
"status"] stringValue
];
167 [windowController setStatus
:[status capitalizedString
]];
169 if ([status isEqualToString
:@
"break"])
171 [self updateStackTraceAndRegisters
];
173 else if ([status isEqualToString
:@
"stopped"] ||
[status isEqualToString
:@
"stopping"])
177 [windowController setStatus
:@
"Stopped"];
182 * Tells the debugger to step into the current command.
184 - (StackFrame
*)stepIn
186 [socket send
:[self createCommand
:@
"step_into"]];
189 StackFrame
*frame
= [self createStackFrame
];
190 [self refreshStatus
];
196 * Tells the debugger to step out of the current context
198 - (StackFrame
*)stepOut
200 [socket send
:[self createCommand
:@
"step_out"]];
203 StackFrame
*frame
= [self createStackFrame
];
204 [self refreshStatus
];
210 * Tells the debugger to step over the current function
212 - (StackFrame
*)stepOver
214 [socket send
:[self createCommand
:@
"step_over"]];
217 StackFrame
*frame
= [self createStackFrame
];
218 [self refreshStatus
];
224 * This function queries the debug server for the current stacktrace and all the registers on
225 * level one. If a user then tries to expand past level one... TOOD: HOLY CRAP WHAT DO WE DO PAST LEVEL 1?
227 - (void)updateStackTraceAndRegisters
230 [socket send
:[self createCommand
:@
"context_get"]];
231 [windowController setRegister
:[self processData
:[socket receive
]]];
235 * Tells the debugger engine to get a specifc property. This also takes in the NSXMLElement
236 * that requested it so that the child can be attached.
238 - (NSArray
*)getProperty
:(NSString
*)property
240 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"property_get -n \"%@\"", property
]]];
242 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
246 <property> <!-- this is the one we requested -->
247 <property ... /> <!-- these are what we want -->
252 // we now have to detach all the children so we can insert them into another document
253 NSXMLElement
*parent
= (NSXMLElement
*)[[doc rootElement
] childAtIndex
:0];
254 NSArray
*children
= [parent children
];
255 [parent setChildren
:nil];
259 #pragma mark Breakpoints
262 * Send an add breakpoint command
264 - (void)addBreakpoint
:(Breakpoint
*)bp
271 NSString
*cmd
= [self createCommand
:[NSString stringWithFormat
:@
"breakpoint_set -t line -f %@ -n %i", [bp file
], [bp line
]]];
273 NSXMLDocument
*info
= [self processData
:[socket receive
]];
274 [bp setDebuggerId
:[[[[info rootElement
] attributeForName
:@
"id"] stringValue
] intValue
]];
278 * Removes a breakpoint
280 - (void)removeBreakpoint
:(Breakpoint
*)bp
287 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"breakpoint_remove -d %i", [bp debuggerId
]]]];
294 * Helper method to create a string command with the -i <session> automatically tacked on
296 - (NSString
*)createCommand
:(NSString
*)cmd
298 return [NSString stringWithFormat
:@
"%@ -i %@", cmd
, session
];
302 * Helper function to parse the NSData into an NSXMLDocument
304 - (NSXMLDocument
*)processData
:(NSString
*)data
306 NSError
*parseError
= nil;
307 NSXMLDocument
*doc
= [[NSXMLDocument alloc
] initWithXMLString
:data options
:0 error
:&parseError
];
310 NSLog(@
"Could not parse XML? --- %@", parseError
);
311 NSLog(@
"Error UserInfo: %@", [parseError userInfo
]);
312 NSLog(@
"This is the XML Document: %@", data
);
316 // check and see if there's an error
317 NSArray
*error
= [[doc rootElement
] elementsForName
:@
"error"];
318 if ([error count
] > 0)
320 [self errorEncountered
:[[[[error objectAtIndex
:0] children
] objectAtIndex
:0] stringValue
]];
324 return [doc autorelease
];
328 * Creates a StackFrame based on the current position in the debugger
330 - (StackFrame
*)createStackFrame
332 [socket send
:[self createCommand
:@
"stack_get -d 0"]];
333 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
335 NSXMLElement
*xmlframe
= [[[doc rootElement
] children
] objectAtIndex
:0];
336 StackFrame
*frame
= [[StackFrame alloc
]
338 withFilename
:[[xmlframe attributeForName
:@
"filename"] stringValue
]
340 atLine
:[[[xmlframe attributeForName
:@
"lineno"] stringValue
] intValue
]
341 inFunction
:[[xmlframe attributeForName
:@
"where"] stringValue
]
345 return [frame autorelease
];