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
;
41 // CFNetwork Callbacks /////////////////////////////////////////////////////////
43 void ReadStreamCallback(CFReadStreamRef stream
, CFStreamEventType eventType
, void* connectionRaw
)
45 DebuggerConnection
* connection
= (DebuggerConnection
*)connectionRaw
;
48 case kCFStreamEventHasBytesAvailable
:
49 [connection readStreamHasData
];
52 case kCFStreamEventErrorOccurred
:
54 CFErrorRef error
= CFReadStreamCopyError(stream
);
55 CFReadStreamUnscheduleFromRunLoop(stream
, CFRunLoopGetCurrent(), kCFRunLoopCommonModes
);
56 CFReadStreamClose(stream
);
58 [connection errorEncountered
:[[(NSError
*)error autorelease
] description
]];
62 case kCFStreamEventEndEncountered
:
63 CFReadStreamUnscheduleFromRunLoop(stream
, CFRunLoopGetCurrent(), kCFRunLoopCommonModes
);
64 CFReadStreamClose(stream
);
66 [connection socketDisconnected
];
71 void WriteStreamCallback(CFWriteStreamRef stream
, CFStreamEventType eventType
, void* connectionRaw
)
73 DebuggerConnection
* connection
= (DebuggerConnection
*)connectionRaw
;
76 case kCFStreamEventCanAcceptBytes
:
77 [connection sendQueuedWrites
];
80 case kCFStreamEventErrorOccurred
:
82 CFErrorRef error
= CFWriteStreamCopyError(stream
);
83 CFWriteStreamUnscheduleFromRunLoop(stream
, CFRunLoopGetCurrent(), kCFRunLoopCommonModes
);
84 CFWriteStreamClose(stream
);
86 [connection errorEncountered
:[[(NSError
*)error autorelease
] description
]];
90 case kCFStreamEventEndEncountered
:
91 CFWriteStreamUnscheduleFromRunLoop(stream
, CFRunLoopGetCurrent(), kCFRunLoopCommonModes
);
92 CFWriteStreamClose(stream
);
94 [connection socketDisconnected
];
99 void SocketAcceptCallback(CFSocketRef socket
,
100 CFSocketCallBackType callbackType
,
105 assert(callbackType
== kCFSocketAcceptCallBack
);
106 NSLog(@
"SocketAcceptCallback()");
108 DebuggerConnection
* connection
= (DebuggerConnection
*)connectionRaw
;
110 CFReadStreamRef readStream
;
111 CFWriteStreamRef writeStream
;
113 // Create the streams on the socket.
114 CFStreamCreatePairWithSocket(kCFAllocatorDefault
,
115 *(CFSocketNativeHandle
*)data
, // Socket handle.
116 &readStream
, // Read stream in-pointer.
117 &writeStream
); // Write stream in-pointer.
119 // Create struct to register callbacks for the stream.
120 CFStreamClientContext context
;
122 context.info
= connection
;
123 context.retain
= NULL
;
124 context.release
= NULL
;
125 context.copyDescription
= NULL
;
127 // Set the client of the read stream.
128 CFOptionFlags readFlags
=
129 kCFStreamEventOpenCompleted |
130 kCFStreamEventHasBytesAvailable |
131 kCFStreamEventErrorOccurred |
132 kCFStreamEventEndEncountered
;
133 if (CFReadStreamSetClient(readStream
, readFlags
, ReadStreamCallback
, &context
))
134 // Schedule in run loop to do asynchronous communication with the engine.
135 CFReadStreamScheduleWithRunLoop(readStream
, CFRunLoopGetCurrent(), kCFRunLoopCommonModes
);
139 // Open the stream now that it's scheduled on the run loop.
140 if (!CFReadStreamOpen(readStream
))
142 CFStreamError error
= CFReadStreamGetError(readStream
);
143 NSLog(@
"error! %@", error
);
147 // Set the client of the write stream.
148 CFOptionFlags writeFlags
=
149 kCFStreamEventOpenCompleted |
150 kCFStreamEventCanAcceptBytes |
151 kCFStreamEventErrorOccurred |
152 kCFStreamEventEndEncountered
;
153 if (CFWriteStreamSetClient(writeStream
, writeFlags
, WriteStreamCallback
, &context
))
154 // Schedule it in the run loop to receive error information.
155 CFWriteStreamScheduleWithRunLoop(writeStream
, CFRunLoopGetCurrent(), kCFRunLoopCommonModes
);
159 // Open the write stream.
160 if (!CFWriteStreamOpen(writeStream
))
162 CFStreamError error
= CFWriteStreamGetError(writeStream
);
163 NSLog(@
"error! %@", error
);
167 connection.readStream
= readStream
;
168 connection.writeStream
= writeStream
;
169 [connection socketDidAccept
];
172 ////////////////////////////////////////////////////////////////////////////////
174 @implementation DebuggerConnection
176 @synthesize port
= port_
;
177 @synthesize connected
= connected_
;
178 @synthesize delegate
= delegate_
;
180 @synthesize socket
= socket_
;
181 @synthesize readStream
= readStream_
;
182 @synthesize lastReadTransaction
= lastReadTransaction_
;
183 @synthesize currentPacket
= currentPacket_
;
184 @synthesize writeStream
= writeStream_
;
185 @synthesize lastWrittenTransaction
= lastWrittenTransaction_
;
186 @synthesize queuedWrites
= queuedWrites_
;
188 - (id)initWithPort
:(NSUInteger
)aPort
190 if (self = [super init
])
199 self.currentPacket
= nil;
204 * Called by SocketWrapper after the connection is successful. This immediately calls
205 * -[SocketWrapper receive] to clear the way for communication, though the information
206 * could be useful server information that we don't use right now.
208 - (void)socketDidAccept
212 self.queuedWrites
= [NSMutableArray array
];
213 writeQueueLock_
= [NSRecursiveLock
new];
217 * Receives errors from the SocketWrapper and updates the display
219 - (void)errorEncountered
:(NSString
*)error
221 [delegate_ errorEncountered
:error
];
225 * Kicks off the socket on another thread.
229 [NSThread detachNewThreadSelector
:@selector(connectInternal
) toTarget
:self withObject
:nil];
233 * Creates, connects to, and schedules a CFSocket.
235 - (void)connectInternal
237 runLoop_
= [NSRunLoop currentRunLoop
];
239 // Pass ourselves to the callback so we don't have to use ugly globals.
240 CFSocketContext context
;
243 context.retain
= NULL
;
244 context.release
= NULL
;
245 context.copyDescription
= NULL
;
247 // Create the address structure.
248 struct sockaddr_in address
;
249 memset(&address
, 0, sizeof(address
));
250 address.sin_len
= sizeof(address
);
251 address.sin_family
= AF_INET
;
252 address.sin_port
= htons(port_
);
253 address.sin_addr.s_addr
= htonl(INADDR_ANY
);
255 // Create the socket signature.
256 CFSocketSignature signature
;
257 signature.protocolFamily
= PF_INET
;
258 signature.socketType
= SOCK_STREAM
;
259 signature.protocol
= IPPROTO_TCP
;
260 signature.address
= (CFDataRef
)[NSData dataWithBytes
:&address length
:sizeof(address
)];
262 socket_
= CFSocketCreateWithSocketSignature(kCFAllocatorDefault
,
263 &signature
, // Socket signature.
264 kCFSocketAcceptCallBack
, // Callback types.
265 SocketAcceptCallback
, // Callout function pointer.
266 &context
); // Context to pass to callout.
269 [self errorEncountered
:@
"Could not open socket."];
273 // Allow old, yet-to-be recycled sockets to be reused.
275 setsockopt(CFSocketGetNative(socket_
), SOL_SOCKET
, SO_REUSEADDR
, &yes
, sizeof(BOOL));
277 // Schedule the socket on the run loop.
278 CFRunLoopSourceRef source
= CFSocketCreateRunLoopSource(kCFAllocatorDefault
, socket_
, 0);
279 CFRunLoopAddSource([runLoop_ getCFRunLoop
], source
, kCFRunLoopCommonModes
);
286 * Closes a socket and releases the ref.
290 CFRunLoopStop([runLoop_ getCFRunLoop
]);
292 // The socket goes down, so do the streams, which clean themselves up.
294 CFSocketInvalidate(socket_
);
297 self.queuedWrites
= nil;
298 [writeQueueLock_ release
];
302 * Notification that the socket disconnected.
304 - (void)socketDisconnected
307 [delegate_ connectionDidCose
:self];
311 * Callback from the CFReadStream that there is data waiting to be read.
313 - (void)readStreamHasData
315 const NSUInteger kBufferSize
= 1024;
316 UInt8 buffer
[kBufferSize
];
317 CFIndex bufferOffset
= 0; // Starting point in |buffer| to work with.
318 CFIndex bytesRead
= CFReadStreamRead(readStream_
, buffer
, kBufferSize
);
319 const char* charBuffer
= (const char*)buffer
;
321 // The read loop works by going through the buffer until all the bytes have
323 while (bufferOffset
< bytesRead
)
325 // Find the NULL separator, or the end of the string.
326 NSUInteger partLength
= 0;
327 for (NSUInteger i
= bufferOffset
; i
< bytesRead
&& charBuffer
[i
] != '\0'; ++i
, ++partLength
) ;
329 // If there is not a current packet, set some state.
330 if (!self.currentPacket
)
332 // Read the message header: the size. This will be |partLength| bytes.
333 packetSize_
= atoi(charBuffer
+ bufferOffset
);
334 currentPacketIndex_
= 0;
335 self.currentPacket
= [NSMutableString stringWithCapacity
:packetSize_
];
336 bufferOffset
+= partLength
+ 1; // Pass over the NULL byte.
337 continue; // Spin the loop to begin reading actual data.
340 // Substring the byte stream and append it to the packet string.
341 CFStringRef bufferString
= CFStringCreateWithBytes(kCFAllocatorDefault
,
342 buffer
+ bufferOffset
, // Byte pointer, offset by start index.
343 partLength
, // Length.
344 kCFStringEncodingUTF8
,
346 [self.currentPacket appendString
:(NSString
*)bufferString
];
347 CFRelease(bufferString
);
350 currentPacketIndex_
+= partLength
;
351 bufferOffset
+= partLength
+ 1;
353 // If this read finished the packet, handle it and reset.
354 NSLog(@
"cpi %d ps %d br %d ds %d", currentPacketIndex_
, packetSize_
, bytesRead
, partLength
);
355 if (currentPacketIndex_
>= packetSize_
)
357 [self handlePacket
:[[currentPacket_ retain
] autorelease
]];
358 self.currentPacket
= nil;
360 currentPacketIndex_
= 0;
366 * Performs the packet handling of a raw string XML packet. From this point on,
367 * the packets are associated with a transaction and are then dispatched.
369 - (void)handlePacket
:(NSString
*)packet
371 // Test if we can convert it into an NSXMLDocument.
372 NSError
* error
= nil;
373 NSXMLDocument
* xmlTest
= [[NSXMLDocument alloc
] initWithXMLString
:currentPacket_ options
:NSXMLDocumentTidyXML error
:&error
];
375 // Try to recover if we encountered an error.
378 // We do not want to starve the write queue, so manually parse out the
380 NSRange location
= [currentPacket_ rangeOfString
:@
"transaction_id"];
381 if (location.location
!= NSNotFound
)
383 NSUInteger start
= location.location
+ location.length
;
384 NSUInteger end
= start
;
386 NSCharacterSet
* numericSet
= [NSCharacterSet decimalDigitCharacterSet
];
388 // Loop over the characters after the attribute name to extract the ID.
389 while (end
< [currentPacket_ length
])
391 unichar c
= [currentPacket_ characterAtIndex
:end
];
392 if ([numericSet characterIsMember
:c
])
394 // If this character is numeric, extend the range to substring.
401 // If this character is nonnumeric and we have nothing in the
402 // range, skip this character.
408 // We've moved past the numeric ID so we should stop searching.
414 // If we were able to extract the transaction ID, update the last read.
415 NSRange substringRange
= NSMakeRange(start
, end
- start
);
416 NSString
* transactionStr
= [currentPacket_ substringWithRange
:substringRange
];
417 if ([transactionStr length
])
418 lastReadTransaction_
= [transactionStr intValue
];
421 // Otherwise, assume +1 and hope it works.
422 ++lastReadTransaction_
;
426 // See if the transaction can be parsed out.
427 NSInteger transaction
= [self transactionIDFromResponse
:xmlTest
];
428 if (transaction
< lastReadTransaction_
)
430 NSLog(@
"tx = %d vs %d", transaction
, lastReadTransaction_
);
431 NSLog(@
"out of date transaction %@", packet
);
435 if (transaction
!= lastWrittenTransaction_
)
436 NSLog(@
"txn %d <> %d last written, %d last read", transaction
, lastWrittenTransaction_
, lastReadTransaction_
);
438 lastReadTransaction_
= transaction
;
441 // Log this receive event.
442 LoggingController
* logger
= [(AppDelegate
*)[NSApp delegate
] loggingController
];
443 LogEntry
* log
= [logger recordReceive
:currentPacket_
];
445 log.lastWrittenTransactionID
= lastWrittenTransaction_
;
446 log.lastReadTransactionID
= lastReadTransaction_
;
448 // Finally, dispatch the handler for this response.
449 [self handleResponse
:[xmlTest autorelease
]];
453 * Writes a command into the write stream. If the stream is ready for writing,
454 * we do so immediately. If not, the command is queued and will be written
455 * when the stream is ready.
457 - (void)send
:(NSString
*)command
459 if (lastReadTransaction_
>= lastWrittenTransaction_
&& CFWriteStreamCanAcceptBytes(writeStream_
))
460 [self performSend
:command
];
462 [queuedWrites_ addObject
:command
];
463 [self sendQueuedWrites
];
467 * This performs a blocking send. This should ONLY be called when we know we
468 * have write access to the stream. We will busy wait in case we don't do a full
471 - (void)performSend
:(NSString
*)command
473 // If this is an out-of-date transaction, do not bother sending it.
474 NSInteger transaction
= [self transactionIDFromCommand
:command
];
475 if (transaction
!= NSNotFound
&& transaction
< lastWrittenTransaction_
)
480 char* string
= (char*)[command UTF8String
];
481 int stringLength
= strlen(string
);
483 // Busy wait while writing. BAADD. Should background this operation.
486 if (CFWriteStreamCanAcceptBytes(writeStream_
))
488 // Include the NULL byte in the string when we write.
489 int bytesWritten
= CFWriteStreamWrite(writeStream_
, (UInt8
*)string
, stringLength
+ 1);
490 if (bytesWritten
< 0)
492 NSLog(@
"write error");
495 else if (bytesWritten
< strlen(string
))
497 // Adjust the buffer and wait for another chance to write.
498 stringLength
-= bytesWritten
;
499 memmove(string
, string
+ bytesWritten
, stringLength
);
505 // We need to scan the string to find the transactionID.
506 if (transaction
== NSNotFound
)
508 NSLog(@
"sent %@ without a transaction ID", command
);
511 lastWrittenTransaction_
= transaction
;
516 // Log this trancation.
517 LoggingController
* logger
= [(AppDelegate
*)[NSApp delegate
] loggingController
];
518 LogEntry
* log
= [logger recordSend
:command
];
519 log.lastWrittenTransactionID
= lastWrittenTransaction_
;
520 log.lastReadTransactionID
= lastReadTransaction_
;
523 - (void)handleResponse
:(NSXMLDocument
*)response
525 // Check and see if there's an error.
526 NSArray
* error
= [[response rootElement
] elementsForName
:@
"error"];
527 if ([error count
] > 0)
529 NSLog(@
"Xdebug error: %@", error
);
530 [delegate_ errorEncountered
:[[[[error objectAtIndex
:0] children
] objectAtIndex
:0] stringValue
]];
533 if ([[[response rootElement
] name
] isEqualToString
:@
"init"])
535 [delegate_ handleInitialResponse
:response
];
539 if ([delegate_ respondsToSelector
:@selector(handleResponse
:)])
540 [(NSObject
*)delegate_ performSelectorOnMainThread
:@selector(handleResponse
:) withObject
:response waitUntilDone
:NO
];
542 [self sendQueuedWrites
];
548 * This will send a command to the debugger engine. It will append the
549 * transaction ID automatically. It accepts a NSString command along with a
550 * a variable number of arguments to substitute into the command, a la
551 * +[NSString stringWithFormat:]. Returns the transaction ID as a NSNumber.
553 - (NSNumber
*)sendCommandWithFormat
:(NSString
*)format
, ...
555 // Collect varargs and format command.
557 va_start(args
, format
);
558 NSString
* command
= [[NSString alloc
] initWithFormat
:format arguments
:args
];
561 NSNumber
* callbackKey
= [NSNumber numberWithInt
:transactionID
++];
562 [self send
:[NSString stringWithFormat
:@
"%@ -i %@", [command autorelease
], callbackKey
]];
568 * Checks if there are unsent commands in the |queuedWrites_| queue and sends
569 * them if it's OK to do so. This will not block.
571 - (void)sendQueuedWrites
576 [writeQueueLock_ lock
];
577 if (lastReadTransaction_
>= lastWrittenTransaction_
&& [queuedWrites_ count
] > 0)
579 NSString
* command
= [queuedWrites_ objectAtIndex
:0];
581 // We don't want to block because this is called from the main thread.
582 // |-performSend:| busy waits when the stream is not ready. Bail out
583 // before we do that becuase busy waiting is BAD.
584 if (CFWriteStreamCanAcceptBytes(writeStream_
))
586 [self performSend
:command
];
587 [queuedWrites_ removeObjectAtIndex
:0];
590 [writeQueueLock_ unlock
];
595 * Returns the transaction_id from an NSXMLDocument.
597 - (NSInteger
)transactionIDFromResponse
:(NSXMLDocument
*)response
599 return [[[[response rootElement
] attributeForName
:@
"transaction_id"] stringValue
] intValue
];
603 * Scans a command string for the transaction ID component. If it is not found,
604 * returns NSNotFound.
606 - (NSInteger
)transactionIDFromCommand
:(NSString
*)command
608 NSRange occurrence
= [command rangeOfString
:@
"-i "];
609 if (occurrence.location
== NSNotFound
)
611 NSString
* transaction
= [command substringFromIndex
:occurrence.location
+ occurrence.length
];
612 return [transaction intValue
];
616 * Given a file path, this returns a file:// URI and escapes any spaces for the
619 - (NSString
*)escapedURIPath
:(NSString
*)path
621 // Custon GDBp paths are fine.
622 if ([[path substringToIndex
:4] isEqualToString
:@
"gdbp"])
625 // Create a temporary URL that will escape all the nasty characters.
626 NSURL
* url
= [NSURL fileURLWithPath
:path
];
627 NSString
* urlString
= [url absoluteString
];
629 // Remove the host because this is a file:// URL;
630 urlString
= [urlString stringByReplacingOccurrencesOfString
:[url host
] withString
:@
""];
632 // Escape % for use in printf-style NSString formatters.
633 urlString
= [urlString stringByReplacingOccurrencesOfString
:@
"%" withString
:@
"%%"];