438{
439 int pid;
440 int in_fd[2];
441 int out_fd[2];
442 fd_set fdset;
443 fd_set readset;
444 struct timeval tv;
445 int sel,
index, state;
446 char buffer[256];
447 char c;
448
449 if ((pipe (in_fd) == -1) || (pipe (out_fd) == -1))
450 {
451 perror ("could open pipe");
453 }
454
455 pid = fork ();
456 if (pid == 0)
457 {
458 si_close (0); si_dup2 (in_fd[0],0);
459 si_close (1); si_dup2 (out_fd[1],1);
460 si_close (2); si_dup2 (out_fd[1],2);
461
462 execvp (args[0], args);
463 perror ("exec failed");
465 }
466 else if (pid == -1)
467 {
468 perror ("could not fork");
470 }
471
472 FD_ZERO (&fdset);
473 FD_SET (out_fd[0], &fdset);
474
475 si_write (in_fd[1], "backtrace\n", 10);
476 si_write (in_fd[1], "p si_stop_stack_trace_x = 0\n", 28);
477 si_write (in_fd[1], "quit\n", 5);
478
480 state = 0;
481
483 {
484 readset = fdset;
485 tv.tv_sec = 1;
486 tv.tv_usec = 0;
487
488 sel = si_select (FD_SETSIZE, &readset,
NULL,
NULL, &tv);
489 if (sel == -1)
490 break;
491
492 if ((sel > 0) && (FD_ISSET (out_fd[0], &readset)))
493 {
494 if (si_read (out_fd[0], &c, 1))
495 {
496 switch (state)
497 {
498 case 0:
499 if (c == '#')
500 {
501 state = 1;
504 }
505 break;
506 case 1:
508 if ((c == '\n') || (c == '\r'))
509 {
511 fputs (buffer,stderr);
512 state = 0;
514 }
515 break;
516 default:
517 break;
518 }
519 }
520 }
522 break;
523 }
524
525 si_close (in_fd[0]);
526 si_close (in_fd[1]);
527 si_close (out_fd[0]);
528 si_close (out_fd[1]);
530}
static int index(p_Length length, p_Ord ord)