1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
|
/*
** PipeDelegate.m
**
** Copyright (c) 2008-2016 Free Software Foundation
**
** Author: Gregory Casamento <greg.casamento@gmail.com>
** Riccardo Mottola <rm@gnu.org>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <sys/stat.h>
#include <signal.h>
#include <stdio.h> /* for stderr and perror*/
#include <errno.h> /* for int errno */
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#import "PipeDelegate.h"
#import "PCDebugger.h"
#ifndef NOTIFICATION_CENTER
#define NOTIFICATION_CENTER [NSNotificationCenter defaultCenter]
#endif
@implementation PipeDelegate
- (id)init
{
if ((self = [super init]))
{
userInputColor = [[NSColor blueColor] retain];
debuggerColor = [[NSColor blackColor] retain];
messageColor = [[NSColor brownColor] retain];
errorColor = [[NSColor redColor] retain];
promptColor = [[NSColor purpleColor] retain];
debuggerStarted = NO;
}
return self;
}
- (NSTextView *)textView
{
return tView;
}
- (void)setTextView: (NSTextView *)tv
{
if (tView != tv)
{
[tView release];
tView = tv;
[tView retain];
}
}
- (PCDebugger *)debugger
{
return debugger;
}
- (void)setDebugger:(PCDebugger *)dbg
{
if (debugger != dbg)
{
[debugger release];
debugger = dbg;
[debugger retain];
}
}
- (NSColor *)userInputColor
{
return userInputColor;
}
- (NSColor *)debuggerColor
{
return debuggerColor;
}
- (NSColor *)messageColor
{
return messageColor;
}
- (NSColor *)errorColor
{
return errorColor;
}
/**
* Log string to the view.
*/
- (void) logString:(NSString *)str
newLine:(BOOL)newLine
withColor:(NSColor *)color
{
NSMutableDictionary *textAttributes;
NSAttributedString *attrStr;
if (newLine)
{
str = [str stringByAppendingString:@"\n"];
}
textAttributes = [NSMutableDictionary dictionary];
[textAttributes setObject:[NSFont userFixedPitchFontOfSize:0] forKey:NSFontAttributeName];
if (color)
{
[textAttributes setObject:color forKey:NSForegroundColorAttributeName];
}
attrStr = [[NSAttributedString alloc] initWithString: str
attributes: textAttributes];
[[tView textStorage] appendAttributedString: attrStr];
[attrStr release];
[tView scrollRangeToVisible:NSMakeRange([[tView string] length], 0)];
[tView setNeedsDisplay:YES];
}
- (PCDebuggerOutputTypes) parseStringLine: (NSString *)stringInput
{
BOOL found = NO;
NSScanner *stringScanner;
NSString *prefix = NULL;
if ([stringInput length] == 0)
return PCDBEmptyRecord;
stringScanner = [NSScanner scannerWithString: stringInput];
//NSLog(@"parsing: |%@|", stringInput);
[stringScanner scanString: @"(gdb)" intoString: &prefix];
if(prefix != nil)
{
if(debuggerStarted == NO)
{
[NOTIFICATION_CENTER postNotificationName:PCDBDebuggerStartedNotification
object: nil];
debuggerStarted = YES;
}
return PCDBPromptRecord;
}
[stringScanner scanString: @"=" intoString: &prefix];
if(prefix != nil)
{
NSString *dictionaryName = NULL;
found = YES;
[stringScanner scanUpToString: @"," intoString: &dictionaryName];
if([dictionaryName isEqualToString: @"thread-group-started"])
{
NSLog(@"%@",dictionaryName);
}
if(dictionaryName != nil)
{
NSString *key = NULL;
NSString *value = NULL;
while([stringScanner isAtEnd] == NO)
{
[stringScanner scanString: @"," intoString: NULL];
[stringScanner scanUpToString: @"=" intoString: &key];
[stringScanner scanString: @"=" intoString: NULL];
[stringScanner scanString: @"\"" intoString: NULL];
[stringScanner scanUpToString: @"\"" intoString: &value];
[stringScanner scanString: @"\"" intoString: NULL];
if([key isEqualToString:@"pid"] &&
[dictionaryName isEqualToString: @"thread-group-started"])
{
[debugger setSubProcessId: [value intValue]];
}
}
}
return PCDBAsyncInfoRecord;
}
[stringScanner scanString: @"*" intoString: &prefix];
if(prefix != nil)
{
return PCDBAsyncStatusRecord;
}
[stringScanner scanString: @"<-" intoString: &prefix];
if(prefix != nil)
{
return PCDBBreakpointRecord;
}
[stringScanner scanString: @"->" intoString: &prefix];
if(prefix != nil)
{
return PCDBBreakpointRecord;
}
[stringScanner scanString: @"~" intoString: &prefix];
if(prefix != nil)
{
if ([debugger gdbVersion] == 0.0)
{
NSString *str1 = nil;
NSString *str2 = nil;
[stringScanner scanString: @"\"GNU gdb" intoString: &str1];
if (str1 != nil)
{
[stringScanner scanString: @" (GDB)" intoString: &str2];
}
if (str2 != nil || str1 != nil)
{
float v;
if ([stringScanner scanFloat:&v])
{
NSLog(@"GDB version string: %f", v);
[debugger setGdbVersion:v];
}
}
}
if (([debugger gdbVersion] < 7) && [debugger subProcessId] == 0)
{
NSString *str1;
// we attempt to parse: [New thread 6800.0x18ec]
[stringScanner scanString: @"\"[New thread" intoString: &str1];
if (str1 != nil)
{
int v;
if([stringScanner scanInt:&v])
{
NSLog(@"sub process id: %d", v);
[debugger setSubProcessId:v];
}
}
}
return PCDBConsoleStreamRecord;
}
[stringScanner scanString: @"@" intoString: &prefix];
if(prefix != nil)
{
return PCDBTargetStreamRecord;
}
[stringScanner scanString: @"&" intoString: &prefix];
if(prefix != nil)
{
return PCDBDebugStreamRecord;
}
[stringScanner scanString: @"^" intoString: &prefix];
if(prefix != nil)
{
NSString *result = nil;
[stringScanner scanString: @"done" intoString: &result];
if(result != nil)
{
[debugger setStatus: @"Done"];
return PCDBResultRecord;
}
[stringScanner scanString: @"running" intoString: &result];
if(result != nil)
{
[debugger setStatus: @"Running"];
return PCDBResultRecord;
}
[stringScanner scanString: @"connected" intoString: &result];
if(result != nil)
{
[debugger setStatus: @"Connected"];
return PCDBResultRecord;
}
[stringScanner scanString: @"error" intoString: &result];
if(result != nil)
{
[debugger setStatus: @"Error"];
return PCDBResultRecord;
}
[stringScanner scanString: @"exit" intoString: &result];
if(result != nil)
{
[debugger setStatus: @"Exit"];
return PCDBResultRecord;
}
return PCDBResultRecord;
}
NSLog(@"No match found parse: |%@|", stringInput);
return PCDBNotFoundRecord;
}
- (NSString *)unescapeOutputRecord: (NSString *)recordString
{
NSString *unescapedString = [recordString copy];
if ([unescapedString hasPrefix:@"~\""])
unescapedString = [unescapedString substringFromIndex:2];
if ([unescapedString hasSuffix:@"\""])\
unescapedString = [unescapedString substringToIndex: [unescapedString length] - 1];
unescapedString = [unescapedString stringByReplacingOccurrencesOfString: @"\\\"" withString: @"\""];
unescapedString = [unescapedString stringByReplacingOccurrencesOfString: @"\\n" withString: @"\n"];
unescapedString = [unescapedString stringByReplacingOccurrencesOfString: @"\\t" withString: @"\t"];
unescapedString = [unescapedString stringByReplacingOccurrencesOfString: @"\\032" withString: @" "];
return unescapedString;
}
- (void) parseString: (NSString *)inputString
{
NSArray *components;
NSEnumerator *en;
NSString *item = nil;
#if defined(__MINGW32__)
components = [inputString componentsSeparatedByString:@"\r\n"];
#else
components = [inputString componentsSeparatedByString:@"\n"];
#endif
en = [components objectEnumerator];
while((item = [en nextObject]) != nil)
{
PCDebuggerOutputTypes outtype = [self parseStringLine: item];
if(outtype == PCDBConsoleStreamRecord ||
outtype == PCDBTargetStreamRecord)
{
NSString *unescapedString = [self unescapeOutputRecord: item];
[self logString: unescapedString newLine: NO withColor:debuggerColor];
}
else if(outtype == PCDBPromptRecord)
{
[self logString: item newLine: NO withColor:promptColor];
}
/*
else if(outtype == PCDBNotFoundRecord)
{
[self logString: item newLine: NO withColor:promptColor];
}
*/
}
/*
stringRange = [inputString rangeOfString: "(gdb)" options: NULL];
if(stringRange.location == NSNotFound)
{
[self logString: inputString newLine: NO withColor:debuggerColor];
}
else
{
}
*/
}
/**
* Log standard out.
*/
- (void) logStdOut:(NSNotification *)aNotif
{
NSData *data;
NSFileHandle *handle = stdoutHandle;
if ((data = [handle availableData]) && [data length] > 0)
{
NSString *dataString;
dataString = [[NSString alloc]
initWithData:data
encoding:[NSString defaultCStringEncoding]];
// if( !
[self parseString: dataString]; // )
// {
// [self logString: dataString newLine: NO withColor:debuggerColor];
// }
RELEASE(dataString);
}
if (task)
{
[handle waitForDataInBackgroundAndNotify];
}
else
{
[NOTIFICATION_CENTER removeObserver: self
name: NSFileHandleDataAvailableNotification
object: handle];
}
}
/**
* Log error out.
*/
- (void) logErrOut:(NSNotification *)aNotif
{
NSData *data;
NSFileHandle *handle = error_handle;
if ((data = [handle availableData]) && [data length] > 0)
{
NSString *dataString;
dataString = [[NSString alloc]
initWithData:data
encoding:[NSString defaultCStringEncoding]];
// if(![self parseString: dataString])
{
[self logString: dataString newLine: NO withColor:errorColor];
}
RELEASE(dataString);
}
if (task)
{
[handle waitForDataInBackgroundAndNotify];
}
else
{
[NOTIFICATION_CENTER removeObserver:self
name: NSFileHandleDataAvailableNotification
object: handle];
}
}
/**
* Notified when the task is completed.
*/
- (void) taskDidTerminate: (NSNotification *)notif
{
NSLog(@"Task Terminated...");
[self logString: [self stopMessage]
newLine:YES
withColor:messageColor];
}
/**
* Message to print when the task starts
*/
- (NSString *) startMessage
{
return @"=== Task Started ===";
}
/**
* Message to print when the task stops
*/
- (NSString *) stopMessage
{
return @"\n=== Task Stopped ===";
}
/**
* Start the program.
*/
- (void) runProgram: (NSString *)path
inCurrentDirectory: (NSString *)directory
withArguments: (NSArray *)array
logStandardError: (BOOL)logError
{
NSPipe *inPipe;
NSPipe *outPipe;
task = [[NSTask alloc] init];
[task setArguments: array];
[task setCurrentDirectoryPath: directory];
[task setLaunchPath: path];
inPipe = [NSPipe pipe];
outPipe = [NSPipe pipe];
stdinHandle = [[inPipe fileHandleForWriting] retain];
stdoutHandle = [[outPipe fileHandleForReading] retain];
[task setStandardOutput: outPipe];
[task setStandardInput: inPipe];
[stdoutHandle waitForDataInBackgroundAndNotify];
// Log standard error, if requested.
if(logError)
{
[task setStandardError: [NSPipe pipe]];
error_handle = [[task standardError] fileHandleForReading];
[error_handle waitForDataInBackgroundAndNotify];
[NOTIFICATION_CENTER addObserver:self
selector:@selector(logErrOut:)
name:NSFileHandleDataAvailableNotification
object:error_handle];
}
// set up notifications to get data.
[NOTIFICATION_CENTER addObserver:self
selector:@selector(logStdOut:)
name:NSFileHandleDataAvailableNotification
object:stdoutHandle];
[NOTIFICATION_CENTER addObserver:self
selector:@selector(taskDidTerminate:)
name:NSTaskDidTerminateNotification
object:task];
// run the task...
NS_DURING
{
[self logString: [self startMessage]
newLine:YES
withColor:messageColor];
[task launch];
}
NS_HANDLER
{
NSRunAlertPanel(@"Problem Launching Debugger",
[localException reason],
@"OK", nil, nil, nil);
NSLog(@"Task Terminated Unexpectedly...");
[self logString: @"\n=== Task Terminated Unexpectedly ===\n"
newLine:NO
withColor:messageColor];
//Clean up after task is terminated
[[NSNotificationCenter defaultCenter]
postNotificationName: NSTaskDidTerminateNotification
object: task];
}
NS_ENDHANDLER
}
- (void) terminate
{
if(task)
{
[task terminate];
}
}
- (void) dealloc
{
[NOTIFICATION_CENTER removeObserver: self];
[self terminate];
[userInputColor release];
[debuggerColor release];
[messageColor release];
[errorColor release];
[tView release];
[super dealloc];
}
- (void) putString: (NSString *)string;
{
unichar *str = (unichar *)[string cStringUsingEncoding: [NSString defaultCStringEncoding]];
int len = strlen((char *)str);
NSData *data = [NSData dataWithBytes: str length: len];
[stdinHandle writeData: data];
[stdinHandle synchronizeFile];
}
/* for input as typed from the user */
- (void) typeString: (NSString *)string
{
NSUInteger strLen;
strLen = [string length];
[self putString:string];
// if we have a single backspace or delete character
if (strLen == 1 && [string characterAtIndex:strLen-1] == '\177')
{
NSUInteger textLen;
textLen = [[tView string] length];
[tView setSelectedRange:NSMakeRange(textLen-1, 1)];
[tView delete:nil];
return;
}
[self logString:string newLine:NO withColor:userInputColor];
}
/**
* Put a single character into the stream.
*/
- (void) putChar:(unichar)ch
{
NSData *data = [NSData dataWithBytes: &ch length: 1];
[stdinHandle writeData: data];
}
- (void) interrupt
{
[task interrupt];
}
/**
* Respond to key events and pipe them down to the debugger
*/
- (void) keyDown: (NSEvent*)theEvent
{
NSString *chars;
chars = [theEvent characters];
if ([chars length] == 0)
{
}
else if ([chars length] == 1)
{
unichar c;
c = [chars characterAtIndex: 0];
//NSLog(@"char: %d", c);
if (c == 3) // ETX, Control-C
{
[self interrupt]; // send the interrupt signal to the subtask
}
else if (c == 13) // CR
{
[self typeString: @"\n"];
}
else if (c == 127) // del (usually backspace)
{
NSString *tss = [[tView textStorage] string];
if (![tss hasSuffix:@"\n"] && ![tss hasSuffix:@"(gdb) "])
{
[self typeString: chars];
}
}
else
{
[self typeString: chars];
}
}
else
NSLog(@"characters: |%@|", chars);
}
- (void) setBreakpoints:(NSArray *)breakpoints
{
NSDictionary *bp;
NSEnumerator *e;
// TODO
e = [breakpoints objectEnumerator];
while ((bp = [e nextObject]))
{
NSString *bpType;
NSString *bpString;
bpType = [bp objectForKey:PCBreakTypeKey];
bpString = nil;
if ([bpType isEqualToString:PCBreakTypeByLine])
{
NSString *fileName;
NSNumber *lineNumber;
fileName = [bp objectForKey:PCBreakFilename];
lineNumber = [bp objectForKey:PCBreakLineNumber];
bpString = [NSString stringWithFormat:@"-break-insert -f %@:%@\n", fileName, lineNumber];
}
else if ([bpType isEqualToString:PCBreakTypeMethod])
{
NSString *methodName;
methodName = [bp objectForKey:PCBreakMethod];
bpString = [NSString stringWithFormat:@"-interpreter-exec console \"break %@\"\n", methodName];
}
else
{
NSLog(@"Unknown breakpoint type: %@", bpType);
}
if (bpString)
{
NSString *command;
/* TODO: split into a separate insert function */
command = bpString;
NSLog(@"gdb mi command is: %@", command);
[self putString: command];
}
}
}
- (void) debuggerSetup
{
NSString *command = @"set confirm off\n";
[self putString: command];
}
@end
|