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
:(NSData
*)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 * Tells the debugger to continue running the script
114 [socket send
:[self createCommand
:@
"run"]];
115 [self refreshStatus
];
119 * Method that runs tells the debugger to give us its status and will update the status text on the window
121 - (void)refreshStatus
123 [socket send
:[self createCommand
:@
"status"]];
125 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
126 NSString
*status
= [[[doc rootElement
] attributeForName
:@
"status"] stringValue
];
127 [windowController setStatus
:[status capitalizedString
]];
129 if ([status isEqualToString
:@
"break"])
131 [self updateStackTraceAndRegisters
];
136 * Tells the debugger to step into the current command.
140 [socket send
:[self createCommand
:@
"step_into"]];
142 [self refreshStatus
];
146 * Tells the debugger to step out of the current context
150 [socket send
:[self createCommand
:@
"step_out"]];
152 [self refreshStatus
];
156 * Tells the debugger to step over the current function
160 [socket send
:[self createCommand
:@
"step_over"]];
162 [self refreshStatus
];
166 * This function queries the debug server for the current stacktrace and all the registers on
167 * level one. If a user then tries to expand past level one... TOOD: HOLY CRAP WHAT DO WE DO PAST LEVEL 1?
169 - (void)updateStackTraceAndRegisters
172 [socket send
:[self createCommand
:@
"stack_get"]];
173 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
174 NSArray
*children
= [[doc rootElement
] children
];
175 NSMutableArray
*stack
= [NSMutableArray array
];
176 NSMutableDictionary
*dict
= [NSMutableDictionary dictionary
];
177 for (int i
= 0; i
< [children count
]; i
++)
179 NSArray
*attrs
= [[children objectAtIndex
:i
] attributes
];
180 for (int j
= 0; j
< [attrs count
]; j
++)
182 [dict setValue
:[[attrs objectAtIndex
:j
] stringValue
] forKey
:[[attrs objectAtIndex
:j
] name
]];
184 [stack addObject
:dict
];
185 dict
= [NSMutableDictionary dictionary
];
187 [windowController setStack
:stack
];
190 [socket send
:[self createCommand
:@
"context_get"]];
191 [windowController setRegister
:[self processData
:[socket receive
]]];
195 * Tells the debugger engine to get a specifc property. This also takes in the NSXMLElement
196 * that requested it so that the child can be attached.
198 - (void)getProperty
:(NSString
*)property forNode
:(NSTreeNode
*)node
200 [socket send
:[self createCommand
:[NSString stringWithFormat
:@
"property_get -n \"%@\"", property
]]];
202 NSXMLDocument
*doc
= [self processData
:[socket receive
]];
206 <property> <!-- this is the one we requested -->
207 <property ... /> <!-- these are what we want -->
212 // we now have to detach all the children so we can insert them into another document
213 NSXMLElement
*parent
= (NSXMLElement
*)[[doc rootElement
] childAtIndex
:0];
214 NSArray
*children
= [parent children
];
215 [parent setChildren
:nil];
216 [windowController addChildren
:children toNode
:node
];
220 * Helper method to create a string command with the -i <session> automatically tacked on
222 - (NSString
*)createCommand
:(NSString
*)cmd
224 return [NSString stringWithFormat
:@
"%@ -i %@", cmd
, session
];
228 * Helper function to parse the NSData into an NSXMLDocument
230 - (NSXMLDocument
*)processData
:(NSData
*)data
232 NSXMLDocument
*doc
= [[NSXMLDocument alloc
] initWithData
:data options
:NSXMLDocumentTidyXML error
:nil];
234 // check and see if there's an error
235 NSArray
*error
= [[doc rootElement
] elementsForName
:@
"error"];
236 if ([error count
] > 0)
238 [windowController setError
:[[[[error objectAtIndex
:0] children
] objectAtIndex
:0] stringValue
]];