Use a lock to make |-sendQueuedWrites| a critical section
[macgdbp.git] / Source / GDBpConnection.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2010, Blue Static <http://www.bluestatic.org>
4 *
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.
8 *
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.
12 *
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
15 */
16
17 #import <sys/socket.h>
18 #import <netinet/in.h>
19
20 #import "GDBpConnection.h"
21
22 #import "AppDelegate.h"
23
24
25 typedef enum _StackFrameComponents
26 {
27 kStackFrameContexts,
28 kStackFrameSource,
29 kStackFrameVariables
30 } StackFrameComponent;
31
32 // GDBpConnection (Private) ////////////////////////////////////////////////////
33
34 @interface GDBpConnection ()
35 @property (readwrite, copy) NSString* status;
36 @property (assign) CFSocketRef socket;
37 @property (assign) CFReadStreamRef readStream;
38 @property int lastReadTransaction;
39 @property (retain) NSMutableString* currentPacket;
40 @property (assign) CFWriteStreamRef writeStream;
41 @property int lastWrittenTransaction;
42 @property (retain) NSMutableArray* queuedWrites;
43
44 - (void)connect;
45 - (void)close;
46 - (void)socketDidAccept;
47 - (void)socketDisconnected;
48 - (void)readStreamHasData;
49 - (void)send:(NSString*)command;
50 - (void)performSend:(NSString*)command;
51 - (void)errorEncountered:(NSString*)error;
52
53 - (void)handleResponse:(NSXMLDocument*)response;
54 - (void)initReceived:(NSXMLDocument*)response;
55 - (void)updateStatus:(NSXMLDocument*)response;
56 - (void)debuggerStep:(NSXMLDocument*)response;
57 - (void)rebuildStack:(NSXMLDocument*)response;
58 - (void)getStackFrame:(NSXMLDocument*)response;
59 - (void)handleRouted:(NSArray*)path response:(NSXMLDocument*)response;
60
61 - (NSString*)createCommand:(NSString*)cmd, ...;
62 - (NSString*)createRouted:(NSString*)routingID command:(NSString*)cmd, ...;
63
64 - (void)sendQueuedWrites;
65
66 - (StackFrame*)createStackFrame:(int)depth;
67 - (NSString*)escapedURIPath:(NSString*)path;
68 @end
69
70 // CFNetwork Callbacks /////////////////////////////////////////////////////////
71
72 void ReadStreamCallback(CFReadStreamRef stream, CFStreamEventType eventType, void* connectionRaw)
73 {
74 GDBpConnection* connection = (GDBpConnection*)connectionRaw;
75 switch (eventType)
76 {
77 case kCFStreamEventHasBytesAvailable:
78 NSLog(@"About to read.");
79 [connection readStreamHasData];
80 break;
81
82 case kCFStreamEventErrorOccurred:
83 {
84 CFErrorRef error = CFReadStreamCopyError(stream);
85 CFReadStreamUnscheduleFromRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
86 CFReadStreamClose(stream);
87 CFRelease(stream);
88 [connection errorEncountered:[[(NSError*)error autorelease] description]];
89 break;
90 }
91
92 case kCFStreamEventEndEncountered:
93 CFReadStreamUnscheduleFromRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
94 CFReadStreamClose(stream);
95 CFRelease(stream);
96 [connection socketDisconnected];
97 break;
98 };
99 }
100
101 void WriteStreamCallback(CFWriteStreamRef stream, CFStreamEventType eventType, void* connectionRaw)
102 {
103 GDBpConnection* connection = (GDBpConnection*)connectionRaw;
104 switch (eventType)
105 {
106 case kCFStreamEventCanAcceptBytes:
107 [connection sendQueuedWrites];
108 break;
109
110 case kCFStreamEventErrorOccurred:
111 {
112 CFErrorRef error = CFWriteStreamCopyError(stream);
113 CFWriteStreamUnscheduleFromRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
114 CFWriteStreamClose(stream);
115 CFRelease(stream);
116 [connection errorEncountered:[[(NSError*)error autorelease] description]];
117 break;
118 }
119
120 case kCFStreamEventEndEncountered:
121 CFWriteStreamUnscheduleFromRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
122 CFWriteStreamClose(stream);
123 CFRelease(stream);
124 [connection socketDisconnected];
125 break;
126 }
127 }
128
129 void SocketAcceptCallback(CFSocketRef socket,
130 CFSocketCallBackType callbackType,
131 CFDataRef address,
132 const void* data,
133 void* connectionRaw)
134 {
135 assert(callbackType == kCFSocketAcceptCallBack);
136 NSLog(@"SocketAcceptCallback()");
137
138 GDBpConnection* connection = (GDBpConnection*)connectionRaw;
139
140 CFReadStreamRef readStream;
141 CFWriteStreamRef writeStream;
142
143 // Create the streams on the socket.
144 CFStreamCreatePairWithSocket(kCFAllocatorDefault,
145 *(CFSocketNativeHandle*)data, // Socket handle.
146 &readStream, // Read stream in-pointer.
147 &writeStream); // Write stream in-pointer.
148
149 // Create struct to register callbacks for the stream.
150 CFStreamClientContext context;
151 context.version = 0;
152 context.info = connection;
153 context.retain = NULL;
154 context.release = NULL;
155 context.copyDescription = NULL;
156
157 // Set the client of the read stream.
158 CFOptionFlags readFlags =
159 kCFStreamEventOpenCompleted |
160 kCFStreamEventHasBytesAvailable |
161 kCFStreamEventErrorOccurred |
162 kCFStreamEventEndEncountered;
163 if (CFReadStreamSetClient(readStream, readFlags, ReadStreamCallback, &context))
164 // Schedule in run loop to do asynchronous communication with the engine.
165 CFReadStreamScheduleWithRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
166 else
167 return;
168
169 // Open the stream now that it's scheduled on the run loop.
170 if (!CFReadStreamOpen(readStream))
171 {
172 CFStreamError error = CFReadStreamGetError(readStream);
173 NSLog(@"error! %@", error);
174 return;
175 }
176
177 // Set the client of the write stream.
178 CFOptionFlags writeFlags =
179 kCFStreamEventOpenCompleted |
180 kCFStreamEventCanAcceptBytes |
181 kCFStreamEventErrorOccurred |
182 kCFStreamEventEndEncountered;
183 if (CFWriteStreamSetClient(writeStream, writeFlags, WriteStreamCallback, &context))
184 // Schedule it in the run loop to receive error information.
185 CFWriteStreamScheduleWithRunLoop(writeStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
186 else
187 return;
188
189 // Open the write stream.
190 if (!CFWriteStreamOpen(writeStream))
191 {
192 CFStreamError error = CFWriteStreamGetError(writeStream);
193 NSLog(@"error! %@", error);
194 return;
195 }
196
197 connection.readStream = readStream;
198 connection.writeStream = writeStream;
199 [connection socketDidAccept];
200 }
201
202 // GDBpConnection //////////////////////////////////////////////////////////////
203
204 @implementation GDBpConnection
205 @synthesize socket = socket_;
206 @synthesize readStream = readStream_;
207 @synthesize lastReadTransaction = lastReadTransaction_;
208 @synthesize currentPacket = currentPacket_;
209 @synthesize writeStream = writeStream_;
210 @synthesize lastWrittenTransaction = lastWrittenTransaction_;
211 @synthesize queuedWrites = queuedWrites_;
212 @synthesize status;
213 @synthesize delegate;
214
215 /**
216 * Creates a new DebuggerConnection and initializes the socket from the given connection
217 * paramters.
218 */
219 - (id)initWithPort:(int)aPort
220 {
221 if (self = [super init])
222 {
223 port = aPort;
224 connected = NO;
225
226 [[BreakpointManager sharedManager] setConnection:self];
227
228 [self connect];
229 }
230 return self;
231 }
232
233 /**
234 * Deallocates the object
235 */
236 - (void)dealloc
237 {
238 [self close];
239 self.currentPacket = nil;
240
241 [super dealloc];
242 }
243
244 /**
245 * Gets the port number
246 */
247 - (int)port
248 {
249 return port;
250 }
251
252 /**
253 * Returns the name of the remote host
254 */
255 - (NSString*)remoteHost
256 {
257 if (!connected)
258 {
259 return @"(DISCONNECTED)";
260 }
261 // TODO: Either impl or remove.
262 return @"";
263 }
264
265 /**
266 * Returns whether or not we have an active connection
267 */
268 - (BOOL)isConnected
269 {
270 return connected;
271 }
272
273 /**
274 * Called by SocketWrapper after the connection is successful. This immediately calls
275 * -[SocketWrapper receive] to clear the way for communication, though the information
276 * could be useful server information that we don't use right now.
277 */
278 - (void)socketDidAccept
279 {
280 connected = YES;
281 transactionID = 1;
282 stackFrames_ = [[NSMutableDictionary alloc] init];
283 self.queuedWrites = [NSMutableArray array];
284 writeQueueLock_ = [NSRecursiveLock new];
285 }
286
287 /**
288 * Receives errors from the SocketWrapper and updates the display
289 */
290 - (void)errorEncountered:(NSString*)error
291 {
292 [delegate errorEncountered:error];
293 }
294
295 /**
296 * Reestablishes communication with the remote debugger so that a new connection doesn't have to be
297 * created every time you want to debug a page
298 */
299 - (void)reconnect
300 {
301 [self close];
302 self.status = @"Connecting";
303 [self connect];
304 }
305
306 /**
307 * Creates an entirely new stack and returns it as an array of StackFrame objects.
308 */
309 - (NSArray*)getCurrentStack
310 {
311 NSMutableArray* stack = [NSMutableArray array];
312 NSLog(@"NOTIMPLEMENTED(): %s", _cmd);
313 return stack;
314 }
315
316 /**
317 * Tells the debugger to continue running the script. Returns the current stack frame.
318 */
319 - (void)run
320 {
321 [self send:[self createCommand:@"run"]];
322 }
323
324 /**
325 * Tells the debugger to step into the current command.
326 */
327 - (void)stepIn
328 {
329 [self send:[self createCommand:@"step_into"]];
330 }
331
332 /**
333 * Tells the debugger to step out of the current context
334 */
335 - (void)stepOut
336 {
337 [self send:[self createCommand:@"step_out"]];
338 }
339
340 /**
341 * Tells the debugger to step over the current function
342 */
343 - (void)stepOver
344 {
345 [self send:[self createCommand:@"step_over"]];
346 }
347
348 /**
349 * Tells the debugger engine to get a specifc property. This also takes in the NSXMLElement
350 * that requested it so that the child can be attached.
351 */
352 - (NSArray*)getProperty:(NSString*)property
353 {
354 [socket send:[self createCommand:[NSString stringWithFormat:@"property_get -n \"%@\"", property]]];
355
356 NSXMLDocument* doc = [self processData:[socket receive]];
357
358 /*
359 <response>
360 <property> <!-- this is the one we requested -->
361 <property ... /> <!-- these are what we want -->
362 </property>
363 </repsonse>
364 */
365
366 // we now have to detach all the children so we can insert them into another document
367 NSXMLElement* parent = (NSXMLElement*)[[doc rootElement] childAtIndex:0];
368 NSArray* children = [parent children];
369 [parent setChildren:nil];
370 return children;
371 }
372
373 #pragma mark Breakpoints
374
375 /**
376 * Send an add breakpoint command
377 */
378 - (void)addBreakpoint:(Breakpoint*)bp
379 {
380 if (!connected)
381 return;
382
383 NSString* file = [self escapedURIPath:[bp transformedPath]];
384 NSString* cmd = [self createCommand:[NSString stringWithFormat:@"breakpoint_set -t line -f %@ -n %i", file, [bp line]]];
385 [socket send:cmd];
386 NSXMLDocument* info = [self processData:[socket receive]];
387 [bp setDebuggerId:[[[[info rootElement] attributeForName:@"id"] stringValue] intValue]];
388 }
389
390 /**
391 * Removes a breakpoint
392 */
393 - (void)removeBreakpoint:(Breakpoint*)bp
394 {
395 if (!connected)
396 {
397 return;
398 }
399
400 [socket send:[self createCommand:[NSString stringWithFormat:@"breakpoint_remove -d %i", [bp debuggerId]]]];
401 [socket receive];
402 }
403
404 #pragma mark Socket and Stream Callbacks
405
406 /**
407 * Creates, connects to, and schedules a CFSocket.
408 */
409 - (void)connect
410 {
411 // Pass ourselves to the callback so we don't have to use ugly globals.
412 CFSocketContext context;
413 context.version = 0;
414 context.info = self;
415 context.retain = NULL;
416 context.release = NULL;
417 context.copyDescription = NULL;
418
419 // Create the address structure.
420 struct sockaddr_in address;
421 memset(&address, 0, sizeof(address));
422 address.sin_len = sizeof(address);
423 address.sin_family = AF_INET;
424 address.sin_port = htons(port);
425 address.sin_addr.s_addr = htonl(INADDR_ANY);
426
427 // Create the socket signature.
428 CFSocketSignature signature;
429 signature.protocolFamily = PF_INET;
430 signature.socketType = SOCK_STREAM;
431 signature.protocol = IPPROTO_TCP;
432 signature.address = (CFDataRef)[NSData dataWithBytes:&address length:sizeof(address)];
433
434 socket_ = CFSocketCreateWithSocketSignature(kCFAllocatorDefault,
435 &signature, // Socket signature.
436 kCFSocketAcceptCallBack, // Callback types.
437 SocketAcceptCallback, // Callout function pointer.
438 &context); // Context to pass to callout.
439 if (!socket_)
440 {
441 [self errorEncountered:@"Could not open socket."];
442 return;
443 }
444
445 // Allow old, yet-to-be recycled sockets to be reused.
446 BOOL yes = YES;
447 setsockopt(CFSocketGetNative(socket_), SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(BOOL));
448
449 // Schedule the socket on the run loop.
450 CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(kCFAllocatorDefault, socket_, 0);
451 CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopCommonModes);
452 CFRelease(source);
453
454 self.status = @"Connecting";
455 }
456
457 /**
458 * Closes a socket and releases the ref.
459 */
460 - (void)close
461 {
462 // The socket goes down, so do the streams, which clean themselves up.
463 CFSocketInvalidate(socket_);
464 CFRelease(socket_);
465 [stackFrames_ release];
466 self.queuedWrites = nil;
467 [writeQueueLock_ release];
468 }
469
470 /**
471 * Notification that the socket disconnected.
472 */
473 - (void)socketDisconnected
474 {
475 [self close];
476 [delegate debuggerDisconnected];
477 }
478
479 /**
480 * Callback from the CFReadStream that there is data waiting to be read.
481 */
482 - (void)readStreamHasData
483 {
484 UInt8 buffer[1024];
485 CFIndex bytesRead = CFReadStreamRead(readStream_, buffer, 1024);
486 const char* charBuffer = (const char*)buffer;
487
488 // We haven't finished reading a packet, so just read more data in.
489 if (currentPacketIndex_ < packetSize_)
490 {
491 [currentPacket_ appendFormat:@"%s", buffer];
492 currentPacketIndex_ += bytesRead;
493 }
494 // Time to read a new packet.
495 else
496 {
497 // Read the message header: the size.
498 packetSize_ = atoi(charBuffer);
499 currentPacketIndex_ = bytesRead - strlen(charBuffer);
500 self.currentPacket = [NSMutableString stringWithFormat:@"%s", buffer + strlen(charBuffer) + 1];
501 }
502
503 // We have finished reading the packet.
504 if (currentPacketIndex_ >= packetSize_)
505 {
506 packetSize_ = 0;
507 currentPacketIndex_ = 0;
508
509 // Test if we can convert it into an NSXMLDocument.
510 NSError* error = nil;
511 NSXMLDocument* xmlTest = [[NSXMLDocument alloc] initWithXMLString:currentPacket_ options:NSXMLDocumentTidyXML error:&error];
512 if (error)
513 {
514 NSLog(@"Could not parse XML? --- %@", error);
515 NSLog(@"Error UserInfo: %@", [error userInfo]);
516 NSLog(@"This is the XML Document: %@", currentPacket_);
517 return;
518 }
519 [self handleResponse:[xmlTest autorelease]];
520 }
521 }
522
523 /**
524 * Writes a command into the write stream. If the stream is ready for writing,
525 * we do so immediately. If not, the command is queued and will be written
526 * when the stream is ready.
527 */
528 - (void)send:(NSString*)command
529 {
530 if (lastReadTransaction_ >= lastWrittenTransaction_ && CFWriteStreamCanAcceptBytes(writeStream_))
531 [self performSend:command];
532 else
533 [queuedWrites_ addObject:command];
534 }
535
536 /**
537 * This performs a blocking send. This should ONLY be called when we know we
538 * have write access to the stream. We will busy wait in case we don't do a full
539 * send.
540 */
541 - (void)performSend:(NSString*)command
542 {
543 BOOL done = NO;
544
545 char* string = (char*)[command UTF8String];
546 int stringLength = strlen(string);
547
548 // Log the command if TransportDebug is enabled.
549 if ([[[[NSProcessInfo processInfo] environment] objectForKey:@"TransportDebug"] boolValue])
550 NSLog(@"--> %@", command);
551
552 // Busy wait while writing. BAADD. Should background this operation.
553 while (!done)
554 {
555 if (CFWriteStreamCanAcceptBytes(writeStream_))
556 {
557 // Include the NULL byte in the string when we write.
558 int bytesWritten = CFWriteStreamWrite(writeStream_, (UInt8*)string, stringLength + 1);
559 if (bytesWritten < 0)
560 {
561 NSLog(@"write error");
562 }
563 // Incomplete write.
564 else if (bytesWritten < strlen(string))
565 {
566 // Adjust the buffer and wait for another chance to write.
567 stringLength -= bytesWritten;
568 memmove(string, string + bytesWritten, stringLength);
569 }
570 else
571 {
572 done = YES;
573
574 // We need to scan the string to find the transactionID.
575 NSRange occurrence = [command rangeOfString:@"-i "];
576 if (occurrence.location == NSNotFound)
577 {
578 NSLog(@"sent %@ without a transaction ID", command);
579 continue;
580 }
581 NSString* transaction = [command substringFromIndex:occurrence.location + occurrence.length];
582 lastWrittenTransaction_ = [transaction intValue];
583 NSLog(@"command = %@", command);
584 NSLog(@"read=%d, write=%d", lastReadTransaction_, lastWrittenTransaction_);
585 }
586 }
587 }
588 }
589
590 #pragma mark Response Handlers
591
592 - (void)handleResponse:(NSXMLDocument*)response
593 {
594 // Check and see if there's an error.
595 NSArray* error = [[response rootElement] elementsForName:@"error"];
596 if ([error count] > 0)
597 {
598 NSLog(@"Xdebug error: %@", error);
599 [delegate errorEncountered:[[[[error objectAtIndex:0] children] objectAtIndex:0] stringValue]];
600 }
601
602 // If TransportDebug is enabled, log the response.
603 if ([[[[NSProcessInfo processInfo] environment] objectForKey:@"TransportDebug"] boolValue])
604 NSLog(@"<-- %@", response);
605
606 // Get the name of the command from the engine's response.
607 NSString* command = [[[response rootElement] attributeForName:@"command"] stringValue];
608 NSString* transaction = [[[response rootElement] attributeForName:@"transaction_id"] stringValue];
609 NSArray* routingPath = [transaction componentsSeparatedByString:@"."];
610
611 NSInteger txnID = [[routingPath objectAtIndex:0] intValue];
612 if (txnID < lastReadTransaction_)
613 NSLog(@"out of date transaction %@", response);
614
615 if (txnID != lastWrittenTransaction_)
616 NSLog(@"txn doesn't match last written %@", response);
617
618 lastReadTransaction_ = [transaction intValue];
619 NSLog(@"read=%d, write=%d", lastReadTransaction_, lastWrittenTransaction_);
620
621 // Dispatch the command response to an appropriate handler.
622 if ([command isEqualToString:@"status"])
623 [self updateStatus:response];
624 else if ([command isEqualToString:@"run"] || [command isEqualToString:@"step_into"] ||
625 [command isEqualToString:@"step_over"] || [command isEqualToString:@"step_out"])
626 [self debuggerStep:response];
627 else if ([command isEqualToString:@"stack_depth"])
628 [self rebuildStack:response];
629 else if ([command isEqualToString:@"stack_get"])
630 [self getStackFrame:response];
631 else if ([routingPath count] > 1)
632 [self handleRouted:routingPath response:response];
633 else if ([[[response rootElement] name] isEqualToString:@"init"])
634 [self initReceived:response];
635
636 [self sendQueuedWrites];
637 }
638
639 /**
640 * Initial packet received. We've started a brand-new connection to the engine.
641 */
642 - (void)initReceived:(NSXMLDocument*)response
643 {
644 // Register any breakpoints that exist offline.
645 for (Breakpoint* bp in [[BreakpointManager sharedManager] breakpoints])
646 [self addBreakpoint:bp];
647
648 // Load the debugger to make it look active.
649 [delegate debuggerConnected];
650
651 // TODO: update the status.
652 }
653
654 /**
655 * Receiver for status updates. This just freshens up the UI.
656 */
657 - (void)updateStatus:(NSXMLDocument*)response
658 {
659 self.status = [[[[response rootElement] attributeForName:@"status"] stringValue] capitalizedString];
660 if (status == nil || [status isEqualToString:@"Stopped"] || [status isEqualToString:@"Stopping"])
661 {
662 connected = NO;
663 [self close];
664 [delegate debuggerDisconnected];
665
666 self.status = @"Stopped";
667 }
668 }
669
670 /**
671 * Step in/out/over and run all take this path. We first get the status of the
672 * debugger and then request fresh stack information.
673 */
674 - (void)debuggerStep:(NSXMLDocument*)response
675 {
676 [self send:[self createCommand:@"status"]];
677 NSString* command = [[[response rootElement] attributeForName:@"command"] stringValue];
678 NSUInteger routingID = [[[[response rootElement] attributeForName:@"transaction_id"] stringValue] intValue];
679
680 // If this is the run command, tell the delegate that a bunch of updates
681 // are coming. Also remove all existing stack routes and request a new stack.
682 if ([command isEqualToString:@"run"])
683 {
684 [delegate clobberStack];
685 [stackFrames_ removeAllObjects];
686 [self send:[self createCommand:@"stack_depth"]];
687 }
688
689 [self send:[self createRouted:[NSString stringWithFormat:@"%u", routingID] command:@"stack_get -d 0"]];
690 }
691
692 /**
693 * We ask for the stack_depth and now we clobber the stack and start rebuilding
694 * it.
695 */
696 - (void)rebuildStack:(NSXMLDocument*)response
697 {
698 NSInteger depth = [[[[response rootElement] attributeForName:@"depth"] stringValue] intValue];
699
700 // We now need to alloc a bunch of stack frames and get the basic information
701 // for them.
702 for (NSInteger i = 0; i < depth; i++)
703 {
704 NSString* command = [self createCommand:@"stack_get -d %d", i];
705
706 // Use the transaction ID to create a routing path.
707 NSNumber* routingID = [NSNumber numberWithInt:transactionID - 1];
708 [stackFrames_ setObject:[StackFrame alloc] forKey:routingID];
709
710 [self send:command];
711 }
712 }
713
714 /**
715 * The initial rebuild of the stack frame. We now have enough to initialize
716 * a StackFrame object.
717 */
718 - (void)getStackFrame:(NSXMLDocument*)response
719 {
720 // Get the routing information.
721 NSUInteger routingID = [[[[response rootElement] attributeForName:@"transaction_id"] stringValue] intValue];
722 NSNumber* routingNumber = [NSNumber numberWithInt:routingID];
723
724 // Make sure we initialized this frame in our last |-rebuildStack:|.
725 StackFrame* frame = [stackFrames_ objectForKey:routingNumber];
726 if (!frame)
727 return;
728
729 NSXMLElement* xmlframe = [[[response rootElement] children] objectAtIndex:0];
730
731 // Initialize the stack frame.
732 [frame initWithIndex:[[[xmlframe attributeForName:@"level"] stringValue] intValue]
733 withFilename:[[xmlframe attributeForName:@"filename"] stringValue]
734 withSource:nil
735 atLine:[[[xmlframe attributeForName:@"lineno"] stringValue] intValue]
736 inFunction:[[xmlframe attributeForName:@"where"] stringValue]
737 withVariables:nil];
738
739 // Now that we have an initialized frame, request additional information.
740 NSString* routingFormat = @"%u.%d";
741 NSString* routingPath = nil;
742
743 // Get the source code of the file. Escape % in URL chars.
744 NSString* escapedFilename = [frame.filename stringByReplacingOccurrencesOfString:@"%" withString:@"%%"];
745 routingPath = [NSString stringWithFormat:routingFormat, routingID, kStackFrameSource];
746 [self send:[self createRouted:routingPath command:[NSString stringWithFormat:@"source -f %@", escapedFilename]]];
747
748 // Get the names of all the contexts.
749 routingPath = [NSString stringWithFormat:routingFormat, routingID, kStackFrameContexts];
750 [self send:[self createRouted:routingPath command:@"context_names -d %d", frame.index]];
751 }
752
753 /**
754 * Routed responses are currently only used in getting all the stack frame data.
755 */
756 - (void)handleRouted:(NSArray*)path response:(NSXMLDocument*)response
757 {
758 NSLog(@"routed %@ = %@", path, response);
759
760 // Format of |path|: transactionID.routingID.component
761 StackFrameComponent component = [[path objectAtIndex:2] intValue];
762
763 // See if we can find the stack frame based on routingID.
764 NSNumber* routingNumber = [NSNumber numberWithInt:[[path objectAtIndex:1] intValue]];
765 StackFrame* frame = [stackFrames_ objectForKey:routingNumber];
766 if (!frame)
767 return;
768
769 if (component == kStackFrameSource)
770 frame.source = [[response rootElement] value];
771 }
772
773 #pragma mark Private
774
775 /**
776 * Helper method to create a string command with the -i <transaction id> automatically tacked on. Takes
777 * a variable number of arguments and parses the given command with +[NSString stringWithFormat:]
778 */
779 - (NSString*)createCommand:(NSString*)cmd, ...
780 {
781 // collect varargs
782 va_list argList;
783 va_start(argList, cmd);
784 NSString* format = [[NSString alloc] initWithFormat:cmd arguments:argList]; // format the command
785 va_end(argList);
786
787 return [NSString stringWithFormat:@"%@ -i %d", [format autorelease], transactionID++];
788 }
789
790 /**
791 * Helper to create a command string. This works a lot like |-createCommand:| but
792 * it also takes a routing ID, which is used to route response data to specific
793 * objects.
794 */
795 - (NSString*)createRouted:(NSString*)routingID command:(NSString*)cmd, ...
796 {
797 va_list argList;
798 va_start(argList, cmd);
799 NSString* format = [[NSString alloc] initWithFormat:cmd arguments:argList];
800 va_end(argList);
801
802 return [NSString stringWithFormat:@"%@ -i %d.%@", [format autorelease], transactionID++, routingID];
803 }
804
805 /**
806 * Checks if there are unsent commands in the |queuedWrites_| queue and sends
807 * them if it's OK to do so. This will not block.
808 */
809 - (void)sendQueuedWrites
810 {
811 [writeQueueLock_ lock];
812 if (lastReadTransaction_ >= lastWrittenTransaction_ && [queuedWrites_ count] > 0)
813 {
814 NSString* command = [queuedWrites_ objectAtIndex:0];
815 NSLog(@"Sending queued write: %@", command);
816
817 // We don't want to block because this is called from the main thread.
818 // |-performSend:| busy waits when the stream is not ready. Bail out
819 // before we do that becuase busy waiting is BAD.
820 if (CFWriteStreamCanAcceptBytes(writeStream_))
821 {
822 [self performSend:command];
823 [queuedWrites_ removeObjectAtIndex:0];
824 }
825 }
826 [writeQueueLock_ unlock];
827 }
828
829 /**
830 * Generates a stack frame for the given depth
831 */
832 - (StackFrame*)createStackFrame:(int)stackDepth
833 {
834 // get the names of all the contexts
835 [socket send:[self createCommand:@"context_names -d 0"]];
836 NSXMLElement* contextNames = [[self processData:[socket receive]] rootElement];
837 NSMutableArray* variables = [NSMutableArray array];
838 for (NSXMLElement* context in [contextNames children])
839 {
840 NSString* name = [[context attributeForName:@"name"] stringValue];
841 int cid = [[[context attributeForName:@"id"] stringValue] intValue];
842
843 // fetch the contexts
844 [socket send:[self createCommand:[NSString stringWithFormat:@"context_get -d %d -c %d", stackDepth, cid]]];
845 NSArray* addVars = [[[self processData:[socket receive]] rootElement] children];
846 if (addVars != nil && name != nil)
847 [variables addObjectsFromArray:addVars];
848 }
849
850 return nil;
851 }
852
853 /**
854 * Given a file path, this returns a file:// URI and escapes any spaces for the
855 * debugger engine.
856 */
857 - (NSString*)escapedURIPath:(NSString*)path
858 {
859 // Custon GDBp paths are fine.
860 if ([[path substringToIndex:4] isEqualToString:@"gdbp"])
861 return path;
862
863 // Create a temporary URL that will escape all the nasty characters.
864 NSURL* url = [NSURL fileURLWithPath:path];
865 NSString* urlString = [url absoluteString];
866
867 // Remove the host because this is a file:// URL;
868 urlString = [urlString stringByReplacingOccurrencesOfString:[url host] withString:@""];
869
870 // Escape % for use in printf-style NSString formatters.
871 urlString = [urlString stringByReplacingOccurrencesOfString:@"%" withString:@"%%"];
872 return urlString;
873 }
874
875 @end