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)connectInternal
;
39 - (void)socketDidAccept
;
40 - (void)socketDisconnected
;
41 - (void)readStreamHasData
;
43 - (void)performSend
:(NSString
*)command
;
44 - (void)sendQueuedWrites
;
46 - (void)handleResponse
:(NSXMLDocument
*)response
;
47 - (void)handlePacket
:(NSString
*)packet
;
49 - (void)errorEncountered
:(NSString
*)error
;
53 // CFNetwork Callbacks /////////////////////////////////////////////////////////
55 void ReadStreamCallback(CFReadStreamRef stream
, CFStreamEventType eventType
, void* connectionRaw
)
57 DebuggerConnection
* connection
= (DebuggerConnection
*)connectionRaw
;
60 case kCFStreamEventHasBytesAvailable
:
61 [connection readStreamHasData
];
64 case kCFStreamEventErrorOccurred
:
66 CFErrorRef error
= CFReadStreamCopyError(stream
);
67 CFReadStreamUnscheduleFromRunLoop(stream
, CFRunLoopGetCurrent(), kCFRunLoopCommonModes
);
68 CFReadStreamClose(stream
);
70 [connection errorEncountered
:[[(NSError
*)error autorelease
] description
]];
74 case kCFStreamEventEndEncountered
:
75 CFReadStreamUnscheduleFromRunLoop(stream
, CFRunLoopGetCurrent(), kCFRunLoopCommonModes
);
76 CFReadStreamClose(stream
);
78 [connection socketDisconnected
];
83 void WriteStreamCallback(CFWriteStreamRef stream
, CFStreamEventType eventType
, void* connectionRaw
)
85 DebuggerConnection
* connection
= (DebuggerConnection
*)connectionRaw
;
88 case kCFStreamEventCanAcceptBytes
:
89 [connection sendQueuedWrites
];
92 case kCFStreamEventErrorOccurred
:
94 CFErrorRef error
= CFWriteStreamCopyError(stream
);
95 CFWriteStreamUnscheduleFromRunLoop(stream
, CFRunLoopGetCurrent(), kCFRunLoopCommonModes
);
96 CFWriteStreamClose(stream
);
98 [connection errorEncountered
:[[(NSError
*)error autorelease
] description
]];
102 case kCFStreamEventEndEncountered
:
103 CFWriteStreamUnscheduleFromRunLoop(stream
, CFRunLoopGetCurrent(), kCFRunLoopCommonModes
);
104 CFWriteStreamClose(stream
);
106 [connection socketDisconnected
];
111 void SocketAcceptCallback(CFSocketRef socket
,
112 CFSocketCallBackType callbackType
,
117 assert(callbackType
== kCFSocketAcceptCallBack
);
118 NSLog(@
"SocketAcceptCallback()");
120 DebuggerConnection
* connection
= (DebuggerConnection
*)connectionRaw
;
122 CFReadStreamRef readStream
;
123 CFWriteStreamRef writeStream
;
125 // Create the streams on the socket.
126 CFStreamCreatePairWithSocket(kCFAllocatorDefault
,
127 *(CFSocketNativeHandle
*)data
, // Socket handle.
128 &readStream
, // Read stream in-pointer.
129 &writeStream
); // Write stream in-pointer.
131 // Create struct to register callbacks for the stream.
132 CFStreamClientContext context
;
134 context.info
= connection
;
135 context.retain
= NULL
;
136 context.release
= NULL
;
137 context.copyDescription
= NULL
;
139 // Set the client of the read stream.
140 CFOptionFlags readFlags
=
141 kCFStreamEventOpenCompleted |
142 kCFStreamEventHasBytesAvailable |
143 kCFStreamEventErrorOccurred |
144 kCFStreamEventEndEncountered
;
145 if (CFReadStreamSetClient(readStream
, readFlags
, ReadStreamCallback
, &context
))
146 // Schedule in run loop to do asynchronous communication with the engine.
147 CFReadStreamScheduleWithRunLoop(readStream
, CFRunLoopGetCurrent(), kCFRunLoopCommonModes
);
151 // Open the stream now that it's scheduled on the run loop.
152 if (!CFReadStreamOpen(readStream
))
154 CFStreamError error
= CFReadStreamGetError(readStream
);
155 NSLog(@
"error! %@", error
);
159 // Set the client of the write stream.
160 CFOptionFlags writeFlags
=
161 kCFStreamEventOpenCompleted |
162 kCFStreamEventCanAcceptBytes |
163 kCFStreamEventErrorOccurred |
164 kCFStreamEventEndEncountered
;
165 if (CFWriteStreamSetClient(writeStream
, writeFlags
, WriteStreamCallback
, &context
))
166 // Schedule it in the run loop to receive error information.
167 CFWriteStreamScheduleWithRunLoop(writeStream
, CFRunLoopGetCurrent(), kCFRunLoopCommonModes
);
171 // Open the write stream.
172 if (!CFWriteStreamOpen(writeStream
))
174 CFStreamError error
= CFWriteStreamGetError(writeStream
);
175 NSLog(@
"error! %@", error
);
179 connection.readStream
= readStream
;
180 connection.writeStream
= writeStream
;
181 [connection socketDidAccept
];
184 ////////////////////////////////////////////////////////////////////////////////
186 @implementation DebuggerConnection
188 @synthesize port
= port_
;
189 @synthesize connected
= connected_
;
190 @synthesize delegate
= delegate_
;
192 @synthesize socket
= socket_
;
193 @synthesize readStream
= readStream_
;
194 @synthesize lastReadTransaction
= lastReadTransaction_
;
195 @synthesize currentPacket
= currentPacket_
;
196 @synthesize writeStream
= writeStream_
;
197 @synthesize lastWrittenTransaction
= lastWrittenTransaction_
;
198 @synthesize queuedWrites
= queuedWrites_
;
200 - (id)initWithPort
:(NSUInteger
)aPort
202 if (self = [super init
])
211 self.currentPacket
= nil;
216 * Kicks off the socket on another thread.
220 [NSThread detachNewThreadSelector
:@selector(connectInternal
) toTarget
:self withObject
:nil];
224 * Creates, connects to, and schedules a CFSocket.
226 - (void)connectInternal
228 NSAutoreleasePool
* pool
= [[NSAutoreleasePool alloc
] init
];
230 runLoop_
= [NSRunLoop currentRunLoop
];
232 // Pass ourselves to the callback so we don't have to use ugly globals.
233 CFSocketContext context
;
236 context.retain
= NULL
;
237 context.release
= NULL
;
238 context.copyDescription
= NULL
;
240 // Create the address structure.
241 struct sockaddr_in address
;
242 memset(&address
, 0, sizeof(address
));
243 address.sin_len
= sizeof(address
);
244 address.sin_family
= AF_INET
;
245 address.sin_port
= htons(port_
);
246 address.sin_addr.s_addr
= htonl(INADDR_ANY
);
248 // Create the socket signature.
249 CFSocketSignature signature
;
250 signature.protocolFamily
= PF_INET
;
251 signature.socketType
= SOCK_STREAM
;
252 signature.protocol
= IPPROTO_TCP
;
253 signature.address
= (CFDataRef
)[NSData dataWithBytes
:&address length
:sizeof(address
)];
255 socket_
= CFSocketCreateWithSocketSignature(kCFAllocatorDefault
,
256 &signature
, // Socket signature.
257 kCFSocketAcceptCallBack
, // Callback types.
258 SocketAcceptCallback
, // Callout function pointer.
259 &context
); // Context to pass to callout.
262 [self errorEncountered
:@
"Could not open socket."];
266 // Allow old, yet-to-be recycled sockets to be reused.
268 setsockopt(CFSocketGetNative(socket_
), SOL_SOCKET
, SO_REUSEADDR
, &yes
, sizeof(BOOL));
270 // Schedule the socket on the run loop.
271 CFRunLoopSourceRef source
= CFSocketCreateRunLoopSource(kCFAllocatorDefault
, socket_
, 0);
272 CFRunLoopAddSource([runLoop_ getCFRunLoop
], source
, kCFRunLoopCommonModes
);
282 * Called by SocketWrapper after the connection is successful. This immediately calls
283 * -[SocketWrapper receive] to clear the way for communication, though the information
284 * could be useful server information that we don't use right now.
286 - (void)socketDidAccept
290 self.queuedWrites
= [NSMutableArray array
];
291 writeQueueLock_
= [NSRecursiveLock
new];
295 * Closes a socket and releases the ref.
300 CFRunLoopStop([runLoop_ getCFRunLoop
]);
303 // The socket goes down, so do the streams, which clean themselves up.
305 CFSocketInvalidate(socket_
);
308 self.queuedWrites
= nil;
309 [writeQueueLock_ release
];
313 * Notification that the socket disconnected.
315 - (void)socketDisconnected
318 [delegate_ connectionDidClose
:self];
322 * Writes a command into the write stream. If the stream is ready for writing,
323 * we do so immediately. If not, the command is queued and will be written
324 * when the stream is ready.
326 - (void)send
:(NSString
*)command
328 if (lastReadTransaction_
>= lastWrittenTransaction_
&& CFWriteStreamCanAcceptBytes(writeStream_
)) {
329 [self performSend
:command
];
331 [writeQueueLock_ lock
];
332 [queuedWrites_ addObject
:command
];
333 [writeQueueLock_ unlock
];
335 [self sendQueuedWrites
];
339 * This will send a command to the debugger engine. It will append the
340 * transaction ID automatically. It accepts a NSString command along with a
341 * a variable number of arguments to substitute into the command, a la
342 * +[NSString stringWithFormat:]. Returns the transaction ID as a NSNumber.
344 - (NSNumber
*)sendCommandWithFormat
:(NSString
*)format
, ...
346 // Collect varargs and format command.
348 va_start(args
, format
);
349 NSString
* command
= [[NSString alloc
] initWithFormat
:format arguments
:args
];
352 NSNumber
* callbackKey
= [NSNumber numberWithInt
:transactionID
++];
353 [self send
:[NSString stringWithFormat
:@
"%@ -i %@", [command autorelease
], callbackKey
]];
359 * Given a file path, this returns a file:// URI and escapes any spaces for the
362 - (NSString
*)escapedURIPath
:(NSString
*)path
364 // Custon GDBp paths are fine.
365 if ([[path substringToIndex
:4] isEqualToString
:@
"gdbp"])
368 // Create a temporary URL that will escape all the nasty characters.
369 NSURL
* url
= [NSURL fileURLWithPath
:path
];
370 NSString
* urlString
= [url absoluteString
];
372 // Remove the host because this is a file:// URL;
373 urlString
= [urlString stringByReplacingOccurrencesOfString
:[url host
] withString
:@
""];
375 // Escape % for use in printf-style NSString formatters.
376 urlString
= [urlString stringByReplacingOccurrencesOfString
:@
"%" withString
:@
"%%"];
381 * Returns the transaction_id from an NSXMLDocument.
383 - (NSInteger
)transactionIDFromResponse
:(NSXMLDocument
*)response
385 return [[[[response rootElement
] attributeForName
:@
"transaction_id"] stringValue
] intValue
];
389 * Scans a command string for the transaction ID component. If it is not found,
390 * returns NSNotFound.
392 - (NSInteger
)transactionIDFromCommand
:(NSString
*)command
394 NSRange occurrence
= [command rangeOfString
:@
"-i "];
395 if (occurrence.location
== NSNotFound
)
397 NSString
* transaction
= [command substringFromIndex
:occurrence.location
+ occurrence.length
];
398 return [transaction intValue
];
401 // Private /////////////////////////////////////////////////////////////////////
405 * Receives errors from the SocketWrapper and updates the display
407 - (void)errorEncountered
:(NSString
*)error
409 [delegate_ errorEncountered
:error
];
413 * Callback from the CFReadStream that there is data waiting to be read.
415 - (void)readStreamHasData
417 const NSUInteger kBufferSize
= 1024;
418 UInt8 buffer
[kBufferSize
];
419 CFIndex bufferOffset
= 0; // Starting point in |buffer| to work with.
420 CFIndex bytesRead
= CFReadStreamRead(readStream_
, buffer
, kBufferSize
);
421 const char* charBuffer
= (const char*)buffer
;
423 // The read loop works by going through the buffer until all the bytes have
425 while (bufferOffset
< bytesRead
)
427 // Find the NULL separator, or the end of the string.
428 NSUInteger partLength
= 0;
429 for (NSUInteger i
= bufferOffset
; i
< bytesRead
&& charBuffer
[i
] != '\0'; ++i
, ++partLength
) ;
431 // If there is not a current packet, set some state.
432 if (!self.currentPacket
)
434 // Read the message header: the size. This will be |partLength| bytes.
435 packetSize_
= atoi(charBuffer
+ bufferOffset
);
436 currentPacketIndex_
= 0;
437 self.currentPacket
= [NSMutableString stringWithCapacity
:packetSize_
];
438 bufferOffset
+= partLength
+ 1; // Pass over the NULL byte.
439 continue; // Spin the loop to begin reading actual data.
442 // Substring the byte stream and append it to the packet string.
443 CFStringRef bufferString
= CFStringCreateWithBytes(kCFAllocatorDefault
,
444 buffer
+ bufferOffset
, // Byte pointer, offset by start index.
445 partLength
, // Length.
446 kCFStringEncodingUTF8
,
448 [self.currentPacket appendString
:(NSString
*)bufferString
];
449 CFRelease(bufferString
);
452 currentPacketIndex_
+= partLength
;
453 bufferOffset
+= partLength
+ 1;
455 // If this read finished the packet, handle it and reset.
456 NSLog(@
"cpi %d ps %d br %d ds %d", currentPacketIndex_
, packetSize_
, bytesRead
, partLength
);
457 if (currentPacketIndex_
>= packetSize_
)
459 [self handlePacket
:[[currentPacket_ retain
] autorelease
]];
460 self.currentPacket
= nil;
462 currentPacketIndex_
= 0;
468 * Performs the packet handling of a raw string XML packet. From this point on,
469 * the packets are associated with a transaction and are then dispatched.
471 - (void)handlePacket
:(NSString
*)packet
473 // Test if we can convert it into an NSXMLDocument.
474 NSError
* error
= nil;
475 NSXMLDocument
* xmlTest
= [[NSXMLDocument alloc
] initWithXMLString
:currentPacket_ options
:NSXMLDocumentTidyXML error
:&error
];
477 // Try to recover if we encountered an error.
480 // We do not want to starve the write queue, so manually parse out the
482 NSRange location
= [currentPacket_ rangeOfString
:@
"transaction_id"];
483 if (location.location
!= NSNotFound
)
485 NSUInteger start
= location.location
+ location.length
;
486 NSUInteger end
= start
;
488 NSCharacterSet
* numericSet
= [NSCharacterSet decimalDigitCharacterSet
];
490 // Loop over the characters after the attribute name to extract the ID.
491 while (end
< [currentPacket_ length
])
493 unichar c
= [currentPacket_ characterAtIndex
:end
];
494 if ([numericSet characterIsMember
:c
])
496 // If this character is numeric, extend the range to substring.
503 // If this character is nonnumeric and we have nothing in the
504 // range, skip this character.
510 // We've moved past the numeric ID so we should stop searching.
516 // If we were able to extract the transaction ID, update the last read.
517 NSRange substringRange
= NSMakeRange(start
, end
- start
);
518 NSString
* transactionStr
= [currentPacket_ substringWithRange
:substringRange
];
519 if ([transactionStr length
])
520 lastReadTransaction_
= [transactionStr intValue
];
523 // Otherwise, assume +1 and hope it works.
524 ++lastReadTransaction_
;
528 // See if the transaction can be parsed out.
529 NSInteger transaction
= [self transactionIDFromResponse
:xmlTest
];
530 if (transaction
< lastReadTransaction_
)
532 NSLog(@
"tx = %d vs %d", transaction
, lastReadTransaction_
);
533 NSLog(@
"out of date transaction %@", packet
);
537 if (transaction
!= lastWrittenTransaction_
)
538 NSLog(@
"txn %d <> %d last written, %d last read", transaction
, lastWrittenTransaction_
, lastReadTransaction_
);
540 lastReadTransaction_
= transaction
;
543 // Log this receive event.
544 LoggingController
* logger
= [(AppDelegate
*)[NSApp delegate
] loggingController
];
545 LogEntry
* log
= [logger recordReceive
:currentPacket_
];
547 log.lastWrittenTransactionID
= lastWrittenTransaction_
;
548 log.lastReadTransactionID
= lastReadTransaction_
;
550 // Finally, dispatch the handler for this response.
551 [self handleResponse
:[xmlTest autorelease
]];
554 - (void)handleResponse
:(NSXMLDocument
*)response
556 // Check and see if there's an error.
557 NSArray
* error
= [[response rootElement
] elementsForName
:@
"error"];
558 if ([error count
] > 0)
560 NSLog(@
"Xdebug error: %@", error
);
561 [delegate_ errorEncountered
:[[[[error objectAtIndex
:0] children
] objectAtIndex
:0] stringValue
]];
564 if ([[[response rootElement
] name
] isEqualToString
:@
"init"])
566 [delegate_ handleInitialResponse
:response
];
570 if ([delegate_ respondsToSelector
:@selector(handleResponse
:)])
571 [(NSObject
*)delegate_ performSelectorOnMainThread
:@selector(handleResponse
:) withObject
:response waitUntilDone
:NO
];
573 [self sendQueuedWrites
];
577 * This performs a blocking send. This should ONLY be called when we know we
578 * have write access to the stream. We will busy wait in case we don't do a full
581 - (void)performSend
:(NSString
*)command
583 // If this is an out-of-date transaction, do not bother sending it.
584 NSInteger transaction
= [self transactionIDFromCommand
:command
];
585 if (transaction
!= NSNotFound
&& transaction
< lastWrittenTransaction_
)
590 char* string
= (char*)[command UTF8String
];
591 int stringLength
= strlen(string
);
593 // Busy wait while writing. BAADD. Should background this operation.
596 if (CFWriteStreamCanAcceptBytes(writeStream_
))
598 // Include the NULL byte in the string when we write.
599 int bytesWritten
= CFWriteStreamWrite(writeStream_
, (UInt8
*)string
, stringLength
+ 1);
600 if (bytesWritten
< 0)
602 NSLog(@
"write error");
605 else if (bytesWritten
< strlen(string
))
607 // Adjust the buffer and wait for another chance to write.
608 stringLength
-= bytesWritten
;
609 memmove(string
, string
+ bytesWritten
, stringLength
);
615 // We need to scan the string to find the transactionID.
616 if (transaction
== NSNotFound
)
618 NSLog(@
"sent %@ without a transaction ID", command
);
621 lastWrittenTransaction_
= transaction
;
626 // Log this trancation.
627 LoggingController
* logger
= [(AppDelegate
*)[NSApp delegate
] loggingController
];
628 LogEntry
* log
= [logger recordSend
:command
];
629 log.lastWrittenTransactionID
= lastWrittenTransaction_
;
630 log.lastReadTransactionID
= lastReadTransaction_
;
634 * Checks if there are unsent commands in the |queuedWrites_| queue and sends
635 * them if it's OK to do so. This will not block.
637 - (void)sendQueuedWrites
642 [writeQueueLock_ lock
];
643 if (lastReadTransaction_
>= lastWrittenTransaction_
&& [queuedWrites_ count
] > 0)
645 NSString
* command
= [queuedWrites_ objectAtIndex
:0];
647 // We don't want to block because this is called from the main thread.
648 // |-performSend:| busy waits when the stream is not ready. Bail out
649 // before we do that becuase busy waiting is BAD.
650 if (CFWriteStreamCanAcceptBytes(writeStream_
))
652 [self performSend
:command
];
653 [queuedWrites_ removeObjectAtIndex
:0];
656 [writeQueueLock_ unlock
];