summaryrefslogtreecommitdiff
path: root/toolbin/vdb.pl
blob: 8555fc1cbe82bdd960c1ab0cc818290f0735d960 (plain)
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
#!/usr/bin/perl

# Perl script to ease the use of gdb with valgrind.
#
# Invoke as: vdb.pl <command to run>

use strict;
use warnings;
use IPC::Open3;
use IO::Select;

# Global variables
my $gdbkilled = 0;

sub killgdb() {
    if ($gdbkilled) {
        return;
    }
    print GDBSTDIN " \nkill\ny\nquit\n";
    $gdbkilled = 1;
}


# Store the args
my @args = @ARGV;

# Make the invocation args for valgrind
my @vgargs = (
    "valgrind",
    "--track-origins=yes",
    "--vgdb=yes",
    "--vgdb-error=0" );
push(@vgargs, @args);

# Make the invocation args for gdb
my @gdbargs = (
    "gdb",
    $args[0]);

# Fork the subprocesses
my $vgpid = open3(0, \*VGSTDOUT, 0, @vgargs);
my $gdbpid = open3(\*GDBSTDIN, \*GDBSTDOUT, 0, @gdbargs);

# Cope with Ctrl-C
sub my_sigint_handler() {
    print "Caught a SIGINT...\n";
    if ($gdbkilled) {
        kill 9, $vgpid;
        kill 9, $gdbpid;
        exit(1);
    }
    killgdb();
}

$SIG{'INT'} = \&my_sigint_handler;

my $sel = new IO::Select();
$sel->add(*VGSTDOUT);
$sel->add(*GDBSTDOUT);
$sel->add(*STDIN);

*STDOUT->autoflush();

my $scanning = 1;

sub print_lines($)
{
    my $buf=shift;

    while (1) {
	my $loc = index($buf, "\n");
	if ($loc < 0) {
	    last;
	}
	my $line = substr($buf, 0, $loc+1);
	print "$line";
	$buf = substr($buf, $loc+1);
    }
    return $buf;
}

my $vgpartial = '';
my $gdbpartial = '';
my $last2print = 0; # 0 = VG, 1 = GDB
while (my @ready = $sel->can_read())
{
    for my $fh (@ready) {
        # If valgrind says anything, just print it.
        if ($fh eq *VGSTDOUT) {
            my $vgbuf= '';
            if (sysread(VGSTDOUT, $vgbuf, 64*1024, length($vgbuf)) == 0) {
                # When valgrind hits EOF, exit.
                killgdb();
                waitpid($vgpid,0);
                waitpid($gdbpid,0);
                exit(0);
            }
            if ($scanning) {
                $vgbuf =~ m/(target remote \| .+ \-\-pid\=\d+)\s*/;
                if ($1) {
                    print GDBSTDIN "$1\n";
                    $scanning = 0;
                }
            }
	    # It definitely read something, so print it.
	    if ($last2print == 1) { # Last to print was GDB
                if ($gdbpartial ne "") { # We need a newline
		    print "\n";
		}
		# Better reprint any partial line we had
		print "$vgpartial";
	    }
            $vgpartial = print_lines($vgbuf);
	    print "$vgpartial";
	    $last2print = 0; # VG
        }
        # Don't say anything to or from gdb until after we've got the magic words from valgrind
        if ($scanning == 0) {
            # Anything the user says, should be parotted to gdb
            if ($fh eq *STDIN) {
                my $buf = '';
                if (sysread(STDIN, $buf, 64*1024, length($buf)) == 0) {
                    # When the user hits EOF, start to kill stuff.
                    killgdb();
                }
                print GDBSTDIN "$buf";
            }
            # Anything gdb says, should be parotted out.
            if ($fh eq *GDBSTDOUT) {
		my $gdbbuf='';
                if (sysread(GDBSTDOUT, $gdbbuf, 64*1024, length($gdbbuf)) == 0) {
                    # When gdb hits EOF start to kill stuff.
                    killgdb();
                }
		# It definite read something, so print it.
		if ($last2print == 0) { # Last to print was VG
		    if ($vgpartial ne "") { # We need a newline
			print "\n";
		    }
		    # Better reprint any partial line we had
		    print "$gdbpartial";
		}
		$gdbpartial = print_lines($gdbbuf);
		print "$gdbpartial";
		$last2print = 1; # GDB
            }
        }
    }
}