3 * Copyright (c) 2007 - 2010, 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"
19 #import <sys/socket.h>
20 #import <netinet/in.h>
22 #import "AppDelegate.h"
23 #import "LoggingController.h"
25 // DebuggerConnection (Private) ////////////////////////////////////////////////
27 @interface DebuggerConnection ()
29 @property (assign
) CFSocketRef socket
;
30 @property (assign
) CFReadStreamRef readStream
;
31 @property NSUInteger lastReadTransaction
;
32 @property (retain
) NSMutableString
* currentPacket
;
33 @property (assign
) CFWriteStreamRef writeStream
;
34 @property NSUInteger lastWrittenTransaction
;
35 @property (retain
) NSMutableArray
* queuedWrites
;
37 - (void)connectionThreadStart
;
39 - (void)socketDidAccept
;
40 - (void)socketDisconnected
;
41 - (void)readStreamHasData
;
43 - (void)performSend
:(NSString
*)command
;
44 - (void)sendQueuedWrites
;
46 - (void)performQuitSignal
;
48 - (void)handleResponse
:(NSXMLDocument
*)response
;
49 - (void)handlePacket
:(NSString
*)packet
;
51 // Threadsafe wrappers for the delegate's methods.
52 - (void)errorEncountered
:(NSString
*)error
;
53 - (LogEntry
*)recordSend
:(NSString
*)command
;
54 - (LogEntry
*)recordReceive
:(NSString
*)command
;
58 // CFNetwork Callbacks /////////////////////////////////////////////////////////
60 void ReadStreamCallback(CFReadStreamRef stream
, CFStreamEventType eventType
, void* connectionRaw
)
62 DebuggerConnection
* connection
= (DebuggerConnection
*)connectionRaw
;
65 case kCFStreamEventHasBytesAvailable
:
66 [connection readStreamHasData
];
69 case kCFStreamEventErrorOccurred
:
71 CFErrorRef error
= CFReadStreamCopyError(stream
);
72 CFReadStreamUnscheduleFromRunLoop(stream
, CFRunLoopGetCurrent(), kCFRunLoopCommonModes
);
73 CFReadStreamClose(stream
);
75 [connection errorEncountered
:[[(NSError
*)error autorelease
] description
]];
79 case kCFStreamEventEndEncountered
:
80 CFReadStreamUnscheduleFromRunLoop(stream
, CFRunLoopGetCurrent(), kCFRunLoopCommonModes
);
81 CFReadStreamClose(stream
);
83 [connection socketDisconnected
];
88 void WriteStreamCallback(CFWriteStreamRef stream
, CFStreamEventType eventType
, void* connectionRaw
)
90 DebuggerConnection
* connection
= (DebuggerConnection
*)connectionRaw
;
93 case kCFStreamEventCanAcceptBytes
:
94 [connection sendQueuedWrites
];
97 case kCFStreamEventErrorOccurred
:
99 CFErrorRef error
= CFWriteStreamCopyError(stream
);
100 CFWriteStreamUnscheduleFromRunLoop(stream
, CFRunLoopGetCurrent(), kCFRunLoopCommonModes
);
101 CFWriteStreamClose(stream
);
103 [connection errorEncountered
:[[(NSError
*)error autorelease
] description
]];
107 case kCFStreamEventEndEncountered
:
108 CFWriteStreamUnscheduleFromRunLoop(stream
, CFRunLoopGetCurrent(), kCFRunLoopCommonModes
);
109 CFWriteStreamClose(stream
);
111 [connection socketDisconnected
];
116 void SocketAcceptCallback(CFSocketRef socket
,
117 CFSocketCallBackType callbackType
,
122 assert(callbackType
== kCFSocketAcceptCallBack
);
123 NSLog(@
"SocketAcceptCallback()");
125 DebuggerConnection
* connection
= (DebuggerConnection
*)connectionRaw
;
126 if (![connection inReconnectMode
])
129 CFReadStreamRef readStream
;
130 CFWriteStreamRef writeStream
;
132 // Create the streams on the socket.
133 CFStreamCreatePairWithSocket(kCFAllocatorDefault
,
134 *(CFSocketNativeHandle
*)data
, // Socket handle.
135 &readStream
, // Read stream in-pointer.
136 &writeStream
); // Write stream in-pointer.
138 // Create struct to register callbacks for the stream.
139 CFStreamClientContext context
;
141 context.info
= connection
;
142 context.retain
= NULL
;
143 context.release
= NULL
;
144 context.copyDescription
= NULL
;
146 // Set the client of the read stream.
147 CFOptionFlags readFlags
=
148 kCFStreamEventOpenCompleted |
149 kCFStreamEventHasBytesAvailable |
150 kCFStreamEventErrorOccurred |
151 kCFStreamEventEndEncountered
;
152 if (CFReadStreamSetClient(readStream
, readFlags
, ReadStreamCallback
, &context
))
153 // Schedule in run loop to do asynchronous communication with the engine.
154 CFReadStreamScheduleWithRunLoop(readStream
, CFRunLoopGetCurrent(), kCFRunLoopCommonModes
);
158 // Open the stream now that it's scheduled on the run loop.
159 if (!CFReadStreamOpen(readStream
))
161 CFStreamError error
= CFReadStreamGetError(readStream
);
162 NSLog(@
"error! %@", error
);
166 // Set the client of the write stream.
167 CFOptionFlags writeFlags
=
168 kCFStreamEventOpenCompleted |
169 kCFStreamEventCanAcceptBytes |
170 kCFStreamEventErrorOccurred |
171 kCFStreamEventEndEncountered
;
172 if (CFWriteStreamSetClient(writeStream
, writeFlags
, WriteStreamCallback
, &context
))
173 // Schedule it in the run loop to receive error information.
174 CFWriteStreamScheduleWithRunLoop(writeStream
, CFRunLoopGetCurrent(), kCFRunLoopCommonModes
);
178 // Open the write stream.
179 if (!CFWriteStreamOpen(writeStream
))
181 CFStreamError error
= CFWriteStreamGetError(writeStream
);
182 NSLog(@
"error! %@", error
);
186 connection.readStream
= readStream
;
187 connection.writeStream
= writeStream
;
188 [connection socketDidAccept
];
191 // Other Run Loop Callbacks ////////////////////////////////////////////////////
193 void PerformQuitSignal(void* info
)
195 DebuggerConnection
* obj
= (DebuggerConnection
*)info
;
196 [obj performQuitSignal
];
199 ////////////////////////////////////////////////////////////////////////////////
201 @implementation DebuggerConnection
203 @synthesize port
= port_
;
204 @synthesize connected
= connected_
;
205 @synthesize reconnect
= reconnect_
;
206 @synthesize delegate
= delegate_
;
208 @synthesize socket
= socket_
;
209 @synthesize readStream
= readStream_
;
210 @synthesize lastReadTransaction
= lastReadTransaction_
;
211 @synthesize currentPacket
= currentPacket_
;
212 @synthesize writeStream
= writeStream_
;
213 @synthesize lastWrittenTransaction
= lastWrittenTransaction_
;
214 @synthesize queuedWrites
= queuedWrites_
;
216 - (id)initWithPort
:(NSUInteger
)aPort
218 if (self = [super init
])
228 self.currentPacket
= nil;
239 * Kicks off the socket on another thread.
243 if (thread_
&& !connected_
) {
244 // A thread has been detached but the socket has yet to connect. Do not
245 // spawn a new thread otherwise multiple threads will be blocked on the same
249 [NSThread detachNewThreadSelector
:@selector(connectionThreadStart
) toTarget
:self withObject
:nil];
253 * Creates, connects to, and schedules a CFSocket.
255 - (void)connectionThreadStart
257 NSAutoreleasePool
* pool
= [[NSAutoreleasePool alloc
] init
];
259 thread_
= [NSThread currentThread
];
260 runLoop_
= [NSRunLoop currentRunLoop
];
262 // Pass ourselves to the callback so we don't have to use ugly globals.
263 CFSocketContext context
;
266 context.retain
= NULL
;
267 context.release
= NULL
;
268 context.copyDescription
= NULL
;
270 // Create the address structure.
271 struct sockaddr_in address
;
272 memset(&address
, 0, sizeof(address
));
273 address.sin_len
= sizeof(address
);
274 address.sin_family
= AF_INET
;
275 address.sin_port
= htons(port_
);
276 address.sin_addr.s_addr
= htonl(INADDR_ANY
);
278 // Create the socket signature.
279 CFSocketSignature signature
;
280 signature.protocolFamily
= PF_INET
;
281 signature.socketType
= SOCK_STREAM
;
282 signature.protocol
= IPPROTO_TCP
;
283 signature.address
= (CFDataRef
)[NSData dataWithBytes
:&address length
:sizeof(address
)];
286 socket_
= CFSocketCreateWithSocketSignature(kCFAllocatorDefault
,
287 &signature
, // Socket signature.
288 kCFSocketAcceptCallBack
, // Callback types.
289 SocketAcceptCallback
, // Callout function pointer.
290 &context
); // Context to pass to callout.
292 [self errorEncountered
:@
"Could not open socket."];
297 // Allow old, yet-to-be recycled sockets to be reused.
299 setsockopt(CFSocketGetNative(socket_
), SOL_SOCKET
, SO_REUSEADDR
, &yes
, sizeof(BOOL));
300 setsockopt(CFSocketGetNative(socket_
), SOL_SOCKET
, SO_REUSEPORT
, &yes
, sizeof(BOOL));
302 // Schedule the socket on the run loop.
303 CFRunLoopSourceRef source
= CFSocketCreateRunLoopSource(kCFAllocatorDefault
, socket_
, 0);
304 CFRunLoopAddSource([runLoop_ getCFRunLoop
], source
, kCFRunLoopCommonModes
);
307 // Create a source that is used to quit.
308 CFRunLoopSourceContext quitContext
= { 0 };
309 quitContext.version
= 0;
310 quitContext.info
= self;
311 quitContext.perform
= PerformQuitSignal
;
312 quitSource_
= CFRunLoopSourceCreate(kCFAllocatorDefault
, 0, &quitContext
);
313 CFRunLoopAddSource([runLoop_ getCFRunLoop
], quitSource_
, kCFRunLoopCommonModes
);
320 CFRunLoopSourceInvalidate(quitSource_
);
321 CFRelease(quitSource_
);
328 * Called by SocketWrapper after the connection is successful. This immediately calls
329 * -[SocketWrapper receive] to clear the way for communication, though the information
330 * could be useful server information that we don't use right now.
332 - (void)socketDidAccept
336 self.queuedWrites
= [NSMutableArray array
];
337 writeQueueLock_
= [NSRecursiveLock
new];
341 * Closes a socket and releases the ref.
348 if (runLoop_
&& quitSource_
) {
349 CFRunLoopSourceSignal(quitSource_
);
350 CFRunLoopWakeUp([runLoop_ getCFRunLoop
]);
355 * Quits the run loop and stops the thread.
357 - (void)performQuitSignal
359 self.queuedWrites
= nil;
361 [writeQueueLock_ release
];
364 CFRunLoopStop([runLoop_ getCFRunLoop
]);
367 // The socket goes down, so do the streams, which clean themselves up.
369 NSLog(@
"invalidating socket");
370 CFSocketInvalidate(socket_
);
377 * Notification that the socket disconnected.
379 - (void)socketDisconnected
382 // The state still is connected, which means that we did not get here
383 // through normal disconnected procedure (a call to |-close|, followed by
384 // the downing of the socket and the stream, which also produces this
385 // messsage). Instead, the stream callbacks encountered EOF unexpectedly.
388 if ([delegate_ respondsToSelector
:@selector(connectionDidClose
:)])
389 [delegate_ connectionDidClose
:self];
393 * Writes a command into the write stream. If the stream is ready for writing,
394 * we do so immediately. If not, the command is queued and will be written
395 * when the stream is ready.
397 - (void)send
:(NSString
*)command
399 if (lastReadTransaction_
>= lastWrittenTransaction_
&& CFWriteStreamCanAcceptBytes(writeStream_
)) {
400 [self performSend
:command
];
402 [writeQueueLock_ lock
];
403 [queuedWrites_ addObject
:command
];
404 [writeQueueLock_ unlock
];
406 [self sendQueuedWrites
];
410 * This will send a command to the debugger engine. It will append the
411 * transaction ID automatically. It accepts a NSString command along with a
412 * a variable number of arguments to substitute into the command, a la
413 * +[NSString stringWithFormat:]. Returns the transaction ID as a NSNumber.
415 - (NSNumber
*)sendCommandWithFormat
:(NSString
*)format
, ...
417 // Collect varargs and format command.
419 va_start(args
, format
);
420 NSString
* command
= [[NSString alloc
] initWithFormat
:format arguments
:args
];
423 NSNumber
* callbackKey
= [NSNumber numberWithInt
:transactionID
++];
424 NSString
* taggedCommand
= [NSString stringWithFormat
:@
"%@ -i %@", [command autorelease
], callbackKey
];
425 [self performSelector
:@selector(send
:)
427 withObject
:taggedCommand
428 waitUntilDone
:connected_
];
434 * Given a file path, this returns a file:// URI and escapes any spaces for the
437 - (NSString
*)escapedURIPath
:(NSString
*)path
439 // Custon GDBp paths are fine.
440 if ([[path substringToIndex
:4] isEqualToString
:@
"gdbp"])
443 // Create a temporary URL that will escape all the nasty characters.
444 NSURL
* url
= [NSURL fileURLWithPath
:path
];
445 NSString
* urlString
= [url absoluteString
];
447 // Remove the host because this is a file:// URL;
448 urlString
= [urlString stringByReplacingOccurrencesOfString
:[url host
] withString
:@
""];
450 // Escape % for use in printf-style NSString formatters.
451 urlString
= [urlString stringByReplacingOccurrencesOfString
:@
"%" withString
:@
"%%"];
456 * Returns the transaction_id from an NSXMLDocument.
458 - (NSInteger
)transactionIDFromResponse
:(NSXMLDocument
*)response
460 return [[[[response rootElement
] attributeForName
:@
"transaction_id"] stringValue
] intValue
];
464 * Scans a command string for the transaction ID component. If it is not found,
465 * returns NSNotFound.
467 - (NSInteger
)transactionIDFromCommand
:(NSString
*)command
469 NSRange occurrence
= [command rangeOfString
:@
"-i "];
470 if (occurrence.location
== NSNotFound
)
472 NSString
* transaction
= [command substringFromIndex
:occurrence.location
+ occurrence.length
];
473 return [transaction intValue
];
476 // Private /////////////////////////////////////////////////////////////////////
479 // Delegate Thread-Safe Wrappers ///////////////////////////////////////////////
482 * Receives errors from the SocketWrapper and updates the display
484 - (void)errorEncountered
:(NSString
*)error
486 if (![delegate_ respondsToSelector
:@selector(errorEncountered
:)])
488 [delegate_ performSelectorOnMainThread
:@selector(errorEncountered
:)
493 - (LogEntry
*)recordSend
:(NSString
*)command
495 LoggingController
* logger
= [[AppDelegate instance
] loggingController
];
496 LogEntry
* entry
= [LogEntry newSendEntry
:command
];
497 entry.lastReadTransactionID
= lastReadTransaction_
;
498 entry.lastWrittenTransactionID
= lastWrittenTransaction_
;
499 [logger performSelectorOnMainThread
:@selector(recordEntry
:)
502 return [entry autorelease
];
505 - (LogEntry
*)recordReceive
:(NSString
*)command
507 LoggingController
* logger
= [[AppDelegate instance
] loggingController
];
508 LogEntry
* entry
= [LogEntry newReceiveEntry
:command
];
509 entry.lastReadTransactionID
= lastReadTransaction_
;
510 entry.lastWrittenTransactionID
= lastWrittenTransaction_
;
511 [logger performSelectorOnMainThread
:@selector(recordEntry
:)
514 return [entry autorelease
];
517 // Stream Managers /////////////////////////////////////////////////////////////
520 * Callback from the CFReadStream that there is data waiting to be read.
522 - (void)readStreamHasData
524 const NSUInteger kBufferSize
= 1024;
525 UInt8 buffer
[kBufferSize
];
526 CFIndex bufferOffset
= 0; // Starting point in |buffer| to work with.
527 CFIndex bytesRead
= CFReadStreamRead(readStream_
, buffer
, kBufferSize
);
528 const char* charBuffer
= (const char*)buffer
;
530 // The read loop works by going through the buffer until all the bytes have
532 while (bufferOffset
< bytesRead
)
534 // Find the NULL separator, or the end of the string.
535 NSUInteger partLength
= 0;
536 for (NSUInteger i
= bufferOffset
; i
< bytesRead
&& charBuffer
[i
] != '\0'; ++i
, ++partLength
) ;
538 // If there is not a current packet, set some state.
539 if (!self.currentPacket
)
541 // Read the message header: the size. This will be |partLength| bytes.
542 packetSize_
= atoi(charBuffer
+ bufferOffset
);
543 currentPacketIndex_
= 0;
544 self.currentPacket
= [NSMutableString stringWithCapacity
:packetSize_
];
545 bufferOffset
+= partLength
+ 1; // Pass over the NULL byte.
546 continue; // Spin the loop to begin reading actual data.
549 // Substring the byte stream and append it to the packet string.
550 CFStringRef bufferString
= CFStringCreateWithBytes(kCFAllocatorDefault
,
551 buffer
+ bufferOffset
, // Byte pointer, offset by start index.
552 partLength
, // Length.
553 kCFStringEncodingUTF8
,
555 [self.currentPacket appendString
:(NSString
*)bufferString
];
556 CFRelease(bufferString
);
559 currentPacketIndex_
+= partLength
;
560 bufferOffset
+= partLength
+ 1;
562 // If this read finished the packet, handle it and reset.
563 if (currentPacketIndex_
>= packetSize_
)
565 [self handlePacket
:[[currentPacket_ retain
] autorelease
]];
566 self.currentPacket
= nil;
568 currentPacketIndex_
= 0;
574 * Performs the packet handling of a raw string XML packet. From this point on,
575 * the packets are associated with a transaction and are then dispatched.
577 - (void)handlePacket
:(NSString
*)packet
579 // Test if we can convert it into an NSXMLDocument.
580 NSError
* error
= nil;
581 NSXMLDocument
* xmlTest
= [[NSXMLDocument alloc
] initWithXMLString
:currentPacket_ options
:NSXMLDocumentTidyXML error
:&error
];
583 // Try to recover if we encountered an error.
586 // We do not want to starve the write queue, so manually parse out the
588 NSRange location
= [currentPacket_ rangeOfString
:@
"transaction_id"];
589 if (location.location
!= NSNotFound
)
591 NSUInteger start
= location.location
+ location.length
;
592 NSUInteger end
= start
;
594 NSCharacterSet
* numericSet
= [NSCharacterSet decimalDigitCharacterSet
];
596 // Loop over the characters after the attribute name to extract the ID.
597 while (end
< [currentPacket_ length
])
599 unichar c
= [currentPacket_ characterAtIndex
:end
];
600 if ([numericSet characterIsMember
:c
])
602 // If this character is numeric, extend the range to substring.
609 // If this character is nonnumeric and we have nothing in the
610 // range, skip this character.
616 // We've moved past the numeric ID so we should stop searching.
622 // If we were able to extract the transaction ID, update the last read.
623 NSRange substringRange
= NSMakeRange(start
, end
- start
);
624 NSString
* transactionStr
= [currentPacket_ substringWithRange
:substringRange
];
625 if ([transactionStr length
])
626 lastReadTransaction_
= [transactionStr intValue
];
629 // Otherwise, assume +1 and hope it works.
630 ++lastReadTransaction_
;
631 } else if (!reconnect_
) {
632 // See if the transaction can be parsed out.
633 NSInteger transaction
= [self transactionIDFromResponse
:xmlTest
];
634 if (transaction
< lastReadTransaction_
) {
635 NSLog(@
"tx = %d vs %d", transaction
, lastReadTransaction_
);
636 NSLog(@
"out of date transaction %@", packet
);
640 if (transaction
!= lastWrittenTransaction_
)
641 NSLog(@
"txn %d <> %d last written, %d last read", transaction
, lastWrittenTransaction_
, lastReadTransaction_
);
643 lastReadTransaction_
= transaction
;
646 // Log this receive event.
647 LogEntry
* log
= [self recordReceive
:currentPacket_
];
650 // Finally, dispatch the handler for this response.
651 [self handleResponse
:[xmlTest autorelease
]];
654 - (void)handleResponse
:(NSXMLDocument
*)response
656 // Check and see if there's an error.
657 NSArray
* error
= [[response rootElement
] elementsForName
:@
"error"];
658 if ([error count
] > 0)
660 NSLog(@
"Xdebug error: %@", error
);
661 NSString
* errorMessage
= [[[[error objectAtIndex
:0] children
] objectAtIndex
:0] stringValue
];
662 [self errorEncountered
:errorMessage
];
665 if ([[[response rootElement
] name
] isEqualToString
:@
"init"]) {
668 [delegate_ performSelectorOnMainThread
:@selector(handleInitialResponse
:)
674 if ([delegate_ respondsToSelector
:@selector(handleResponse
:)])
675 [delegate_ performSelectorOnMainThread
:@selector(handleResponse
:)
679 [self sendQueuedWrites
];
683 * This performs a blocking send. This should ONLY be called when we know we
684 * have write access to the stream. We will busy wait in case we don't do a full
687 - (void)performSend
:(NSString
*)command
689 // If this is an out-of-date transaction, do not bother sending it.
690 NSInteger transaction
= [self transactionIDFromCommand
:command
];
691 if (transaction
!= NSNotFound
&& transaction
< lastWrittenTransaction_
)
696 char* string
= (char*)[command UTF8String
];
697 int stringLength
= strlen(string
);
699 // Busy wait while writing. BAADD. Should background this operation.
702 if (CFWriteStreamCanAcceptBytes(writeStream_
))
704 // Include the NULL byte in the string when we write.
705 int bytesWritten
= CFWriteStreamWrite(writeStream_
, (UInt8
*)string
, stringLength
+ 1);
706 if (bytesWritten
< 0)
708 NSLog(@
"write error");
711 else if (bytesWritten
< strlen(string
))
713 // Adjust the buffer and wait for another chance to write.
714 stringLength
-= bytesWritten
;
715 memmove(string
, string
+ bytesWritten
, stringLength
);
721 // We need to scan the string to find the transactionID.
722 if (transaction
== NSNotFound
)
724 NSLog(@
"sent %@ without a transaction ID", command
);
727 lastWrittenTransaction_
= transaction
;
732 // Log this trancation.
733 [self recordSend
:command
];
737 * Checks if there are unsent commands in the |queuedWrites_| queue and sends
738 * them if it's OK to do so. This will not block.
740 - (void)sendQueuedWrites
745 [writeQueueLock_ lock
];
746 if (lastReadTransaction_
>= lastWrittenTransaction_
&& [queuedWrites_ count
] > 0)
748 NSString
* command
= [queuedWrites_ objectAtIndex
:0];
750 // We don't want to block because this is called from the main thread.
751 // |-performSend:| busy waits when the stream is not ready. Bail out
752 // before we do that becuase busy waiting is BAD.
753 if (CFWriteStreamCanAcceptBytes(writeStream_
))
755 [self performSend
:command
];
756 [queuedWrites_ removeObjectAtIndex
:0];
759 [writeQueueLock_ unlock
];