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()
23 @property(readwrite
, copy
) NSString
*status
;
25 - (NSString
*)createCommand
:(NSString
*)cmd
;
26 - (NSXMLDocument
*)processData
:(NSString
*)data
;
27 - (StackFrame
*)createStackFrame
;
31 @implementation GDBpConnection
33 @synthesize socket
, windowController
, status
;
36 * Creates a new DebuggerConnection and initializes the socket from the given connection
39 - (id)initWithWindowController
:(DebuggerController
*)wc port
:(int)aPort session
:(NSString
*)aSession
;
41 if (self = [super init
])
44 session
= [aSession retain
];
47 windowController
= [wc retain
];
49 // now that we have our host information, open the socket
50 socket
= [[SocketWrapper alloc
] initWithConnection
:self];
51 [socket setDelegate
:self];
54 [[BreakpointManager sharedManager
] setConnection
:self];
60 * Deallocates the object
66 [windowController release
];
71 * Gets the port number
79 * Gets the session name
87 * Returns the name of the remote host
89 - (NSString
*)remoteHost
93 return @
"(DISCONNECTED)";
95 return [socket remoteHost
];
99 * Returns whether or not we have an active connection
107 * Called by SocketWrapper after the connection is successful. This immediately calls
108 * -[SocketWrapper receive] to clear the way for communication, though the information
109 * could be useful server information that we don't use right now.
111 - (void)socketDidAccept
:(id)obj
115 [self refreshStatus
];
117 // register any breakpoints that exist offline
118 for (Breakpoint
*bp
in [[BreakpointManager sharedManager
] breakpoints
])
120 [self addBreakpoint
:bp
];
125 * Receives errors from the SocketWrapper and updates the display
127 - (void)errorEncountered
:(NSString
*)error
129 [[NSNotificationCenter defaultCenter
]
130 postNotificationName
:kErrorOccurredNotif
132 userInfo
:[NSDictionary
133 dictionaryWithObject
:error
140 * Reestablishes communication with the remote debugger so that a new connection doesn't have to be
141 * created every time you want to debug a page
146 self.status
= @
"Connecting";
147 [windowController resetDisplays
];
152 * Tells the debugger to continue running the script
156 [socket send
:[self createCommand
:@
"run"]];
158 [self refreshStatus
];
162 * Method that runs tells the debugger to give us its status and will update the status text on the window
164 - (void)refreshStatus
167 if ([status isEqualToString
:@
"Break"])
169 [self updateStackTraceAndRegisters
];
174 * Tells the debugger to step into the current command.
176 - (StackFrame
*)stepIn
178 [socket send
:[self createCommand
:@
"step_into"]];
181 StackFrame
*frame
= [self createStackFrame
];
182 [self refreshStatus
];
188 * Tells the debugger to step out of the current context
190 - (StackFrame
*)stepOut
192 [socket send
:[self createCommand
:@
"step_out"]];
195 StackFrame
*frame
= [self createStackFrame
];
196 [self refreshStatus
];
202 * Tells the debugger to step over the current function
204 - (StackFrame
*)stepOver
206 [socket send
:[self createCommand
:@
"step_over"]];
209 StackFrame
*frame
= [self createStackFrame
];
210 [self refreshStatus
];
216 * This function queries the debug server for the current stacktrace and all the registers on
217 * level one. If a user then tries to expand past level one... TOOD: HOLY CRAP WHAT DO WE DO PAST LEVEL 1?
219 - (void)updateStackTraceAndRegisters
222 [socket send
:[self createCommand
:@
"context_get"]];
223 [windowController setRegister
:[self processData
:[socket receive
]]];
227 * Tells the debugger engine to get a specifc property. This also takes in the NSXMLElement
228 * that requested it so that the child can be attached.
230 - (NSArray
*)getProperty
:(NSString
*)property
232 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"property_get -n \"%@\"", property
]]];
234 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
238 <property> <!-- this is the one we requested -->
239 <property ... /> <!-- these are what we want -->
244 // we now have to detach all the children so we can insert them into another document
245 NSXMLElement
*parent
= (NSXMLElement
*)[[doc rootElement
] childAtIndex
:0];
246 NSArray
*children
= [parent children
];
247 [parent setChildren
:nil];
251 #pragma mark Breakpoints
254 * Send an add breakpoint command
256 - (void)addBreakpoint
:(Breakpoint
*)bp
263 NSString
*cmd
= [self createCommand
:[NSString stringWithFormat
:@
"breakpoint_set -t line -f %@ -n %i", [bp file
], [bp line
]]];
265 NSXMLDocument
*info
= [self processData
:[socket receive
]];
266 [bp setDebuggerId
:[[[[info rootElement
] attributeForName
:@
"id"] stringValue
] intValue
]];
270 * Removes a breakpoint
272 - (void)removeBreakpoint
:(Breakpoint
*)bp
279 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"breakpoint_remove -d %i", [bp debuggerId
]]]];
286 * Helper method to create a string command with the -i <session> automatically tacked on
288 - (NSString
*)createCommand
:(NSString
*)cmd
290 return [NSString stringWithFormat
:@
"%@ -i %@", cmd
, session
];
294 * Helper function to parse the NSData into an NSXMLDocument
296 - (NSXMLDocument
*)processData
:(NSString
*)data
298 NSError
*parseError
= nil;
299 NSXMLDocument
*doc
= [[NSXMLDocument alloc
] initWithXMLString
:data options
:0 error
:&parseError
];
302 NSLog(@
"Could not parse XML? --- %@", parseError
);
303 NSLog(@
"Error UserInfo: %@", [parseError userInfo
]);
304 NSLog(@
"This is the XML Document: %@", data
);
308 // check and see if there's an error
309 NSArray
*error
= [[doc rootElement
] elementsForName
:@
"error"];
310 if ([error count
] > 0)
312 [self errorEncountered
:[[[[error objectAtIndex
:0] children
] objectAtIndex
:0] stringValue
]];
316 return [doc autorelease
];
320 * Creates a StackFrame based on the current position in the debugger
322 - (StackFrame
*)createStackFrame
324 [socket send
:[self createCommand
:@
"stack_get -d 0"]];
325 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
327 NSXMLElement
*xmlframe
= [[[doc rootElement
] children
] objectAtIndex
:0];
328 StackFrame
*frame
= [[StackFrame alloc
]
330 withFilename
:[[xmlframe attributeForName
:@
"filename"] stringValue
]
332 atLine
:[[[xmlframe attributeForName
:@
"lineno"] stringValue
] intValue
]
333 inFunction
:[[xmlframe attributeForName
:@
"where"] stringValue
]
337 return [frame autorelease
];
341 * Fetches the value of and sets the status instance variable
345 [socket send
:[self createCommand
:@
"status"]];
346 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
347 self.status
= [[[[doc rootElement
] attributeForName
:@
"status"] stringValue
] capitalizedString
];
349 if ([status isEqualToString
:@
"Stopped"] ||
[status isEqualToString
:@
"Stopping"])
353 self.status
= @
"Stopped";