Begin making DebuggerConnection more threadsafe.
[macgdbp.git] / Source / DebuggerConnection.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 "DebuggerConnection.h"
18
19 #import <sys/socket.h>
20 #import <netinet/in.h>
21
22 #import "AppDelegate.h"
23 #import "LoggingController.h"
24
25 // DebuggerConnection (Private) ////////////////////////////////////////////////
26
27 @interface DebuggerConnection ()
28
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;
36
37 - (void)connectInternal;
38
39 - (void)socketDidAccept;
40 - (void)socketDisconnected;
41 - (void)readStreamHasData;
42
43 - (void)performSend:(NSString*)command;
44 - (void)sendQueuedWrites;
45
46 - (void)handleResponse:(NSXMLDocument*)response;
47 - (void)handlePacket:(NSString*)packet;
48
49 // Threadsafe wrappers for the delegate's methods.
50 - (void)errorEncountered:(NSString*)error;
51 - (LogEntry*)recordSend:(NSString*)command;
52 - (LogEntry*)recordReceive:(NSString*)command;
53
54 @end
55
56 // CFNetwork Callbacks /////////////////////////////////////////////////////////
57
58 void ReadStreamCallback(CFReadStreamRef stream, CFStreamEventType eventType, void* connectionRaw)
59 {
60 DebuggerConnection* connection = (DebuggerConnection*)connectionRaw;
61 switch (eventType)
62 {
63 case kCFStreamEventHasBytesAvailable:
64 [connection readStreamHasData];
65 break;
66
67 case kCFStreamEventErrorOccurred:
68 {
69 CFErrorRef error = CFReadStreamCopyError(stream);
70 CFReadStreamUnscheduleFromRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
71 CFReadStreamClose(stream);
72 CFRelease(stream);
73 [connection errorEncountered:[[(NSError*)error autorelease] description]];
74 break;
75 }
76
77 case kCFStreamEventEndEncountered:
78 CFReadStreamUnscheduleFromRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
79 CFReadStreamClose(stream);
80 CFRelease(stream);
81 [connection socketDisconnected];
82 break;
83 };
84 }
85
86 void WriteStreamCallback(CFWriteStreamRef stream, CFStreamEventType eventType, void* connectionRaw)
87 {
88 DebuggerConnection* connection = (DebuggerConnection*)connectionRaw;
89 switch (eventType)
90 {
91 case kCFStreamEventCanAcceptBytes:
92 [connection sendQueuedWrites];
93 break;
94
95 case kCFStreamEventErrorOccurred:
96 {
97 CFErrorRef error = CFWriteStreamCopyError(stream);
98 CFWriteStreamUnscheduleFromRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
99 CFWriteStreamClose(stream);
100 CFRelease(stream);
101 [connection errorEncountered:[[(NSError*)error autorelease] description]];
102 break;
103 }
104
105 case kCFStreamEventEndEncountered:
106 CFWriteStreamUnscheduleFromRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
107 CFWriteStreamClose(stream);
108 CFRelease(stream);
109 [connection socketDisconnected];
110 break;
111 }
112 }
113
114 void SocketAcceptCallback(CFSocketRef socket,
115 CFSocketCallBackType callbackType,
116 CFDataRef address,
117 const void* data,
118 void* connectionRaw)
119 {
120 assert(callbackType == kCFSocketAcceptCallBack);
121 NSLog(@"SocketAcceptCallback()");
122
123 DebuggerConnection* connection = (DebuggerConnection*)connectionRaw;
124
125 CFReadStreamRef readStream;
126 CFWriteStreamRef writeStream;
127
128 // Create the streams on the socket.
129 CFStreamCreatePairWithSocket(kCFAllocatorDefault,
130 *(CFSocketNativeHandle*)data, // Socket handle.
131 &readStream, // Read stream in-pointer.
132 &writeStream); // Write stream in-pointer.
133
134 // Create struct to register callbacks for the stream.
135 CFStreamClientContext context;
136 context.version = 0;
137 context.info = connection;
138 context.retain = NULL;
139 context.release = NULL;
140 context.copyDescription = NULL;
141
142 // Set the client of the read stream.
143 CFOptionFlags readFlags =
144 kCFStreamEventOpenCompleted |
145 kCFStreamEventHasBytesAvailable |
146 kCFStreamEventErrorOccurred |
147 kCFStreamEventEndEncountered;
148 if (CFReadStreamSetClient(readStream, readFlags, ReadStreamCallback, &context))
149 // Schedule in run loop to do asynchronous communication with the engine.
150 CFReadStreamScheduleWithRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
151 else
152 return;
153
154 // Open the stream now that it's scheduled on the run loop.
155 if (!CFReadStreamOpen(readStream))
156 {
157 CFStreamError error = CFReadStreamGetError(readStream);
158 NSLog(@"error! %@", error);
159 return;
160 }
161
162 // Set the client of the write stream.
163 CFOptionFlags writeFlags =
164 kCFStreamEventOpenCompleted |
165 kCFStreamEventCanAcceptBytes |
166 kCFStreamEventErrorOccurred |
167 kCFStreamEventEndEncountered;
168 if (CFWriteStreamSetClient(writeStream, writeFlags, WriteStreamCallback, &context))
169 // Schedule it in the run loop to receive error information.
170 CFWriteStreamScheduleWithRunLoop(writeStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
171 else
172 return;
173
174 // Open the write stream.
175 if (!CFWriteStreamOpen(writeStream))
176 {
177 CFStreamError error = CFWriteStreamGetError(writeStream);
178 NSLog(@"error! %@", error);
179 return;
180 }
181
182 connection.readStream = readStream;
183 connection.writeStream = writeStream;
184 [connection socketDidAccept];
185 }
186
187 ////////////////////////////////////////////////////////////////////////////////
188
189 @implementation DebuggerConnection
190
191 @synthesize port = port_;
192 @synthesize connected = connected_;
193 @synthesize delegate = delegate_;
194
195 @synthesize socket = socket_;
196 @synthesize readStream = readStream_;
197 @synthesize lastReadTransaction = lastReadTransaction_;
198 @synthesize currentPacket = currentPacket_;
199 @synthesize writeStream = writeStream_;
200 @synthesize lastWrittenTransaction = lastWrittenTransaction_;
201 @synthesize queuedWrites = queuedWrites_;
202
203 - (id)initWithPort:(NSUInteger)aPort
204 {
205 if (self = [super init])
206 {
207 port_ = aPort;
208 }
209 return self;
210 }
211
212 - (void)dealloc
213 {
214 self.currentPacket = nil;
215 [super dealloc];
216 }
217
218 /**
219 * Kicks off the socket on another thread.
220 */
221 - (void)connect
222 {
223 [NSThread detachNewThreadSelector:@selector(connectInternal) toTarget:self withObject:nil];
224 }
225
226 /**
227 * Creates, connects to, and schedules a CFSocket.
228 */
229 - (void)connectInternal
230 {
231 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
232
233 thread_ = [NSThread currentThread];
234 runLoop_ = [NSRunLoop currentRunLoop];
235
236 // Pass ourselves to the callback so we don't have to use ugly globals.
237 CFSocketContext context;
238 context.version = 0;
239 context.info = self;
240 context.retain = NULL;
241 context.release = NULL;
242 context.copyDescription = NULL;
243
244 // Create the address structure.
245 struct sockaddr_in address;
246 memset(&address, 0, sizeof(address));
247 address.sin_len = sizeof(address);
248 address.sin_family = AF_INET;
249 address.sin_port = htons(port_);
250 address.sin_addr.s_addr = htonl(INADDR_ANY);
251
252 // Create the socket signature.
253 CFSocketSignature signature;
254 signature.protocolFamily = PF_INET;
255 signature.socketType = SOCK_STREAM;
256 signature.protocol = IPPROTO_TCP;
257 signature.address = (CFDataRef)[NSData dataWithBytes:&address length:sizeof(address)];
258
259 socket_ = CFSocketCreateWithSocketSignature(kCFAllocatorDefault,
260 &signature, // Socket signature.
261 kCFSocketAcceptCallBack, // Callback types.
262 SocketAcceptCallback, // Callout function pointer.
263 &context); // Context to pass to callout.
264 if (!socket_)
265 {
266 [self errorEncountered:@"Could not open socket."];
267 return;
268 }
269
270 // Allow old, yet-to-be recycled sockets to be reused.
271 BOOL yes = YES;
272 setsockopt(CFSocketGetNative(socket_), SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(BOOL));
273
274 // Schedule the socket on the run loop.
275 CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(kCFAllocatorDefault, socket_, 0);
276 CFRunLoopAddSource([runLoop_ getCFRunLoop], source, kCFRunLoopCommonModes);
277 CFRelease(source);
278
279 [runLoop_ run];
280
281 thread_ = nil;
282 runLoop_ = nil;
283
284 [pool release];
285 }
286
287 /**
288 * Called by SocketWrapper after the connection is successful. This immediately calls
289 * -[SocketWrapper receive] to clear the way for communication, though the information
290 * could be useful server information that we don't use right now.
291 */
292 - (void)socketDidAccept
293 {
294 connected_ = YES;
295 transactionID = 1;
296 self.queuedWrites = [NSMutableArray array];
297 writeQueueLock_ = [NSRecursiveLock new];
298 }
299
300 /**
301 * Closes a socket and releases the ref.
302 */
303 - (void)close
304 {
305 if (runLoop_) {
306 CFRunLoopStop([runLoop_ getCFRunLoop]);
307 }
308
309 // The socket goes down, so do the streams, which clean themselves up.
310 if (socket_) {
311 CFSocketInvalidate(socket_);
312 CFRelease(socket_);
313 }
314 self.queuedWrites = nil;
315 [writeQueueLock_ release];
316 }
317
318 /**
319 * Notification that the socket disconnected.
320 */
321 - (void)socketDisconnected
322 {
323 [self close];
324 [delegate_ connectionDidClose:self];
325 }
326
327 /**
328 * Writes a command into the write stream. If the stream is ready for writing,
329 * we do so immediately. If not, the command is queued and will be written
330 * when the stream is ready.
331 */
332 - (void)send:(NSString*)command
333 {
334 if (lastReadTransaction_ >= lastWrittenTransaction_ && CFWriteStreamCanAcceptBytes(writeStream_)) {
335 [self performSend:command];
336 } else {
337 [writeQueueLock_ lock];
338 [queuedWrites_ addObject:command];
339 [writeQueueLock_ unlock];
340 }
341 [self sendQueuedWrites];
342 }
343
344 /**
345 * This will send a command to the debugger engine. It will append the
346 * transaction ID automatically. It accepts a NSString command along with a
347 * a variable number of arguments to substitute into the command, a la
348 * +[NSString stringWithFormat:]. Returns the transaction ID as a NSNumber.
349 */
350 - (NSNumber*)sendCommandWithFormat:(NSString*)format, ...
351 {
352 // Collect varargs and format command.
353 va_list args;
354 va_start(args, format);
355 NSString* command = [[NSString alloc] initWithFormat:format arguments:args];
356 va_end(args);
357
358 NSNumber* callbackKey = [NSNumber numberWithInt:transactionID++];
359 NSString* taggedCommand = [NSString stringWithFormat:@"%@ -i %@", [command autorelease], callbackKey];
360 [self performSelector:@selector(send:)
361 onThread:thread_
362 withObject:taggedCommand
363 waitUntilDone:YES];
364
365 return callbackKey;
366 }
367
368 /**
369 * Given a file path, this returns a file:// URI and escapes any spaces for the
370 * debugger engine.
371 */
372 - (NSString*)escapedURIPath:(NSString*)path
373 {
374 // Custon GDBp paths are fine.
375 if ([[path substringToIndex:4] isEqualToString:@"gdbp"])
376 return path;
377
378 // Create a temporary URL that will escape all the nasty characters.
379 NSURL* url = [NSURL fileURLWithPath:path];
380 NSString* urlString = [url absoluteString];
381
382 // Remove the host because this is a file:// URL;
383 urlString = [urlString stringByReplacingOccurrencesOfString:[url host] withString:@""];
384
385 // Escape % for use in printf-style NSString formatters.
386 urlString = [urlString stringByReplacingOccurrencesOfString:@"%" withString:@"%%"];
387 return urlString;
388 }
389
390 /**
391 * Returns the transaction_id from an NSXMLDocument.
392 */
393 - (NSInteger)transactionIDFromResponse:(NSXMLDocument*)response
394 {
395 return [[[[response rootElement] attributeForName:@"transaction_id"] stringValue] intValue];
396 }
397
398 /**
399 * Scans a command string for the transaction ID component. If it is not found,
400 * returns NSNotFound.
401 */
402 - (NSInteger)transactionIDFromCommand:(NSString*)command
403 {
404 NSRange occurrence = [command rangeOfString:@"-i "];
405 if (occurrence.location == NSNotFound)
406 return NSNotFound;
407 NSString* transaction = [command substringFromIndex:occurrence.location + occurrence.length];
408 return [transaction intValue];
409 }
410
411 // Private /////////////////////////////////////////////////////////////////////
412 #pragma mark Private
413
414 // Delegate Thread-Safe Wrappers ///////////////////////////////////////////////
415
416 /**
417 * Receives errors from the SocketWrapper and updates the display
418 */
419 - (void)errorEncountered:(NSString*)error
420 {
421 [delegate_ performSelectorOnMainThread:@selector(errorEncountered:)
422 withObject:error
423 waitUntilDone:NO];
424 }
425
426 - (LogEntry*)recordSend:(NSString*)command
427 {
428 LoggingController* logger = [(AppDelegate*)[NSApp delegate] loggingController];
429 [logger performSelectorOnMainThread:@selector(recordSend:)
430 withObject:command
431 waitUntilDone:NO];
432 return [logger.logEntries lastObject];
433 }
434
435 - (LogEntry*)recordReceive:(NSString*)command
436 {
437 LoggingController* logger = [(AppDelegate*)[NSApp delegate] loggingController];
438 [logger performSelectorOnMainThread:@selector(recordReceive:)
439 withObject:command
440 waitUntilDone:NO];
441 return [logger.logEntries lastObject];
442 }
443
444 // Stream Managers /////////////////////////////////////////////////////////////
445
446 /**
447 * Callback from the CFReadStream that there is data waiting to be read.
448 */
449 - (void)readStreamHasData
450 {
451 const NSUInteger kBufferSize = 1024;
452 UInt8 buffer[kBufferSize];
453 CFIndex bufferOffset = 0; // Starting point in |buffer| to work with.
454 CFIndex bytesRead = CFReadStreamRead(readStream_, buffer, kBufferSize);
455 const char* charBuffer = (const char*)buffer;
456
457 // The read loop works by going through the buffer until all the bytes have
458 // been processed.
459 while (bufferOffset < bytesRead)
460 {
461 // Find the NULL separator, or the end of the string.
462 NSUInteger partLength = 0;
463 for (NSUInteger i = bufferOffset; i < bytesRead && charBuffer[i] != '\0'; ++i, ++partLength) ;
464
465 // If there is not a current packet, set some state.
466 if (!self.currentPacket)
467 {
468 // Read the message header: the size. This will be |partLength| bytes.
469 packetSize_ = atoi(charBuffer + bufferOffset);
470 currentPacketIndex_ = 0;
471 self.currentPacket = [NSMutableString stringWithCapacity:packetSize_];
472 bufferOffset += partLength + 1; // Pass over the NULL byte.
473 continue; // Spin the loop to begin reading actual data.
474 }
475
476 // Substring the byte stream and append it to the packet string.
477 CFStringRef bufferString = CFStringCreateWithBytes(kCFAllocatorDefault,
478 buffer + bufferOffset, // Byte pointer, offset by start index.
479 partLength, // Length.
480 kCFStringEncodingUTF8,
481 true);
482 [self.currentPacket appendString:(NSString*)bufferString];
483 CFRelease(bufferString);
484
485 // Advance counters.
486 currentPacketIndex_ += partLength;
487 bufferOffset += partLength + 1;
488
489 // If this read finished the packet, handle it and reset.
490 NSLog(@"cpi %d ps %d br %d ds %d", currentPacketIndex_, packetSize_, bytesRead, partLength);
491 if (currentPacketIndex_ >= packetSize_)
492 {
493 [self handlePacket:[[currentPacket_ retain] autorelease]];
494 self.currentPacket = nil;
495 packetSize_ = 0;
496 currentPacketIndex_ = 0;
497 }
498 }
499 }
500
501 /**
502 * Performs the packet handling of a raw string XML packet. From this point on,
503 * the packets are associated with a transaction and are then dispatched.
504 */
505 - (void)handlePacket:(NSString*)packet
506 {
507 // Test if we can convert it into an NSXMLDocument.
508 NSError* error = nil;
509 NSXMLDocument* xmlTest = [[NSXMLDocument alloc] initWithXMLString:currentPacket_ options:NSXMLDocumentTidyXML error:&error];
510
511 // Try to recover if we encountered an error.
512 if (!xmlTest)
513 {
514 // We do not want to starve the write queue, so manually parse out the
515 // transaction ID.
516 NSRange location = [currentPacket_ rangeOfString:@"transaction_id"];
517 if (location.location != NSNotFound)
518 {
519 NSUInteger start = location.location + location.length;
520 NSUInteger end = start;
521
522 NSCharacterSet* numericSet = [NSCharacterSet decimalDigitCharacterSet];
523
524 // Loop over the characters after the attribute name to extract the ID.
525 while (end < [currentPacket_ length])
526 {
527 unichar c = [currentPacket_ characterAtIndex:end];
528 if ([numericSet characterIsMember:c])
529 {
530 // If this character is numeric, extend the range to substring.
531 ++end;
532 }
533 else
534 {
535 if (start == end)
536 {
537 // If this character is nonnumeric and we have nothing in the
538 // range, skip this character.
539 ++start;
540 ++end;
541 }
542 else
543 {
544 // We've moved past the numeric ID so we should stop searching.
545 break;
546 }
547 }
548 }
549
550 // If we were able to extract the transaction ID, update the last read.
551 NSRange substringRange = NSMakeRange(start, end - start);
552 NSString* transactionStr = [currentPacket_ substringWithRange:substringRange];
553 if ([transactionStr length])
554 lastReadTransaction_ = [transactionStr intValue];
555 }
556
557 // Otherwise, assume +1 and hope it works.
558 ++lastReadTransaction_;
559 }
560 else
561 {
562 // See if the transaction can be parsed out.
563 NSInteger transaction = [self transactionIDFromResponse:xmlTest];
564 if (transaction < lastReadTransaction_)
565 {
566 NSLog(@"tx = %d vs %d", transaction, lastReadTransaction_);
567 NSLog(@"out of date transaction %@", packet);
568 return;
569 }
570
571 if (transaction != lastWrittenTransaction_)
572 NSLog(@"txn %d <> %d last written, %d last read", transaction, lastWrittenTransaction_, lastReadTransaction_);
573
574 lastReadTransaction_ = transaction;
575 }
576
577 // Log this receive event.
578 LogEntry* log = [self recordReceive:currentPacket_];
579 log.error = error;
580 log.lastWrittenTransactionID = lastWrittenTransaction_;
581 log.lastReadTransactionID = lastReadTransaction_;
582
583 // Finally, dispatch the handler for this response.
584 [self handleResponse:[xmlTest autorelease]];
585 }
586
587 - (void)handleResponse:(NSXMLDocument*)response
588 {
589 // Check and see if there's an error.
590 NSArray* error = [[response rootElement] elementsForName:@"error"];
591 if ([error count] > 0)
592 {
593 NSLog(@"Xdebug error: %@", error);
594 NSString* errorMessage = [[[[error objectAtIndex:0] children] objectAtIndex:0] stringValue];
595 [self errorEncountered:errorMessage];
596 }
597
598 if ([[[response rootElement] name] isEqualToString:@"init"])
599 {
600 [delegate_ performSelectorOnMainThread:@selector(handleInitialResponse:)
601 withObject:response
602 waitUntilDone:NO];
603 return;
604 }
605
606 if ([delegate_ respondsToSelector:@selector(handleResponse:)])
607 [delegate_ performSelectorOnMainThread:@selector(handleResponse:)
608 withObject:response
609 waitUntilDone:NO];
610
611 [self sendQueuedWrites];
612 }
613
614 /**
615 * This performs a blocking send. This should ONLY be called when we know we
616 * have write access to the stream. We will busy wait in case we don't do a full
617 * send.
618 */
619 - (void)performSend:(NSString*)command
620 {
621 // If this is an out-of-date transaction, do not bother sending it.
622 NSInteger transaction = [self transactionIDFromCommand:command];
623 if (transaction != NSNotFound && transaction < lastWrittenTransaction_)
624 return;
625
626 BOOL done = NO;
627
628 char* string = (char*)[command UTF8String];
629 int stringLength = strlen(string);
630
631 // Busy wait while writing. BAADD. Should background this operation.
632 while (!done)
633 {
634 if (CFWriteStreamCanAcceptBytes(writeStream_))
635 {
636 // Include the NULL byte in the string when we write.
637 int bytesWritten = CFWriteStreamWrite(writeStream_, (UInt8*)string, stringLength + 1);
638 if (bytesWritten < 0)
639 {
640 NSLog(@"write error");
641 }
642 // Incomplete write.
643 else if (bytesWritten < strlen(string))
644 {
645 // Adjust the buffer and wait for another chance to write.
646 stringLength -= bytesWritten;
647 memmove(string, string + bytesWritten, stringLength);
648 }
649 else
650 {
651 done = YES;
652
653 // We need to scan the string to find the transactionID.
654 if (transaction == NSNotFound)
655 {
656 NSLog(@"sent %@ without a transaction ID", command);
657 continue;
658 }
659 lastWrittenTransaction_ = transaction;
660 }
661 }
662 }
663
664 // Log this trancation.
665 LogEntry* log = [self recordSend:command];
666 log.lastWrittenTransactionID = lastWrittenTransaction_;
667 log.lastReadTransactionID = lastReadTransaction_;
668 }
669
670 /**
671 * Checks if there are unsent commands in the |queuedWrites_| queue and sends
672 * them if it's OK to do so. This will not block.
673 */
674 - (void)sendQueuedWrites
675 {
676 if (!connected_)
677 return;
678
679 [writeQueueLock_ lock];
680 if (lastReadTransaction_ >= lastWrittenTransaction_ && [queuedWrites_ count] > 0)
681 {
682 NSString* command = [queuedWrites_ objectAtIndex:0];
683
684 // We don't want to block because this is called from the main thread.
685 // |-performSend:| busy waits when the stream is not ready. Bail out
686 // before we do that becuase busy waiting is BAD.
687 if (CFWriteStreamCanAcceptBytes(writeStream_))
688 {
689 [self performSend:command];
690 [queuedWrites_ removeObjectAtIndex:0];
691 }
692 }
693 [writeQueueLock_ unlock];
694 }
695
696 @end