citrun

watch C/C++ source code execute
Log | Files | Refs | LICENSE

commit 889d763c327bc197ed0dc5afad862b24ffed4acc
parent 2d5f2f40e717b86297690a441687a709c2df7a90
Author: kyle <kyle@0x30.net>
Date:   Wed,  9 Nov 2016 00:20:48 -0700

t: move shm.pm into t/

Diffstat:
Mt/rt_exectotals.t | 4++--
Mt/rt_header.t | 4++--
Mt/rt_size.t | 4++--
Mt/rt_translunit.t | 4++--
At/shm.pm | 98+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dtlib/shm.pm | 98-------------------------------------------------------------------------------
6 files changed, 106 insertions(+), 106 deletions(-)

diff --git a/t/rt_exectotals.t b/t/rt_exectotals.t @@ -6,7 +6,7 @@ use warnings; use Test::More tests => 100; use Time::HiRes qw( usleep ); use tlib::program; -use tlib::shm; +use t::shm; my $child_pid = fork(); if ($child_pid == 0) { @@ -16,7 +16,7 @@ if ($child_pid == 0) { # Give the runtime time to set up. sleep 1; -my $shm = tlib::shm->new(); +my $shm = t::shm->new(); my $last_total = 0; for (0..99) { diff --git a/t/rt_header.t b/t/rt_header.t @@ -5,12 +5,12 @@ use strict; use warnings; use Test::More tests => 11; use tlib::program; -use tlib::shm; +use t::shm; my $ret = system('tlib/program/program 1'); is $ret >> 8, 0, "is program exit code 0"; -my $shm = tlib::shm->new(); +my $shm = t::shm->new(); is $shm->{magic}, "citrun", "is file magic correct"; is $shm->{major}, 0, "is major correct"; is $shm->{minor}, 0, "is minor correct"; diff --git a/t/rt_size.t b/t/rt_size.t @@ -6,11 +6,11 @@ use warnings; use POSIX; use Test::More tests => 1; use tlib::program; -use tlib::shm; +use t::shm; system("tlib/program/program 1"); -my $procfile = tlib::shm->new(); +my $procfile = t::shm->new(); my $pagesize = POSIX::sysconf(POSIX::_SC_PAGESIZE); is($procfile->{size}, $pagesize * 4, "is memory file 4 pages long"); diff --git a/t/rt_translunit.t b/t/rt_translunit.t @@ -5,12 +5,12 @@ use strict; use warnings; use Test::More tests => 7; use tlib::program; -use tlib::shm; +use t::shm; my $ret = system('tlib/program/program 10'); is $ret >> 8, 0, "is program exit code 0"; -my $shm = tlib::shm->new(); +my $shm = t::shm->new(); my ($tu1, $tu2, $tu3) = @{ $shm->{translation_units} }; is $tu1->{size}, 9, "is translation unit 1 9 lines"; diff --git a/t/shm.pm b/t/shm.pm @@ -0,0 +1,98 @@ +package t::shm; +use strict; +use warnings; +use POSIX; + +# Triggers runtime to use alternate shm path. +$ENV{CITRUN_TOOLS} = 1; + +my $pagesize = POSIX::sysconf(POSIX::_SC_PAGESIZE); + +sub new { + my ($class) = @_; + + my $self = {}; + bless($self, $class); + + open(my $fh, "<:mmap", "procfile.shm") or die $!; + + $self->{fh} = $fh; + $self->{size} = (stat "procfile.shm")[7]; + + ( $self->{magic}, $self->{major}, $self->{minor}, + $self->{pids}[0], $self->{pids}[1], $self->{pids}[2], + $self->{progname}, $self->{cwd} + ) = unpack("Z6CCLLLZ" . PATH_MAX . "Z" . PATH_MAX, xread($fh, $pagesize)); + + my @translation_units; + while (tell $fh < $self->{size}) { + my %tu; + + ($tu{size}, $tu{comp_file_name}, $tu{abs_file_path}) = + unpack("LZ" . PATH_MAX . "Z" . PATH_MAX, xread($fh, 4 + 2 * 1024 + 4 + 8)); + + $tu{exec_buf_pos} = tell $fh; + xread($fh, $tu{size} * 8); + $self->next_page(); + + push @translation_units, (\%tu); + } + $self->{translation_units} = \@translation_units; + + return $self; +} + +sub next_page { + my ($self) = @_; + + my $cur_pos = tell $self->{fh}; + xread($self->{fh}, $pagesize - ($cur_pos % $pagesize)); +} + +sub execs_for { + my ($self, $tu_num) = @_; + + my $tu = $self->{translation_units}->[$tu_num]; + seek $self->{fh}, $tu->{exec_buf_pos}, 0; + my @execs = unpack("Q$tu->{size}", xread($self->{fh}, $tu->{size} * 8)); + + return \@execs; +} + +sub print_tus { + my ($self) = @_; + + my $transl_units = $self->{translation_units}; + for (@$transl_units) { + my %tu = %$_; + + print "$tu{comp_file_name} $tu{size}\n"; + } +} + +# +# Read an exact amount of bytes. +# +sub xread { + my ($fh, $bytes_total) = @_; + + my $data; + my $bytes_read = 0; + while ($bytes_total > 0) { + my $read = read($fh, $data, $bytes_total, $bytes_read); + + die "read failed: $!" if (!defined $read); + die "end of file\n" if ($read == 0); + + $bytes_total -= $read; + $bytes_read += $read; + } + + return $data; +} + +sub DESTROY { + unlink "procfile.shm"; +} + +1; diff --git a/tlib/shm.pm b/tlib/shm.pm @@ -1,98 +0,0 @@ -package tlib::shm; -use strict; -use warnings; -use POSIX; - -# Triggers runtime to use alternate shm path. -$ENV{CITRUN_TOOLS} = 1; - -my $pagesize = POSIX::sysconf(POSIX::_SC_PAGESIZE); - -sub new { - my ($class) = @_; - - my $self = {}; - bless($self, $class); - - open(my $fh, "<:mmap", "procfile.shm") or die $!; - - $self->{fh} = $fh; - $self->{size} = (stat "procfile.shm")[7]; - - ( $self->{magic}, $self->{major}, $self->{minor}, - $self->{pids}[0], $self->{pids}[1], $self->{pids}[2], - $self->{progname}, $self->{cwd} - ) = unpack("Z6CCLLLZ" . PATH_MAX . "Z" . PATH_MAX, xread($fh, $pagesize)); - - my @translation_units; - while (tell $fh < $self->{size}) { - my %tu; - - ($tu{size}, $tu{comp_file_name}, $tu{abs_file_path}) = - unpack("LZ" . PATH_MAX . "Z" . PATH_MAX, xread($fh, 4 + 2 * 1024 + 4 + 8)); - - $tu{exec_buf_pos} = tell $fh; - xread($fh, $tu{size} * 8); - $self->next_page(); - - push @translation_units, (\%tu); - } - $self->{translation_units} = \@translation_units; - - return $self; -} - -sub next_page { - my ($self) = @_; - - my $cur_pos = tell $self->{fh}; - xread($self->{fh}, $pagesize - ($cur_pos % $pagesize)); -} - -sub execs_for { - my ($self, $tu_num) = @_; - - my $tu = $self->{translation_units}->[$tu_num]; - seek $self->{fh}, $tu->{exec_buf_pos}, 0; - my @execs = unpack("Q$tu->{size}", xread($self->{fh}, $tu->{size} * 8)); - - return \@execs; -} - -sub print_tus { - my ($self) = @_; - - my $transl_units = $self->{translation_units}; - for (@$transl_units) { - my %tu = %$_; - - print "$tu{comp_file_name} $tu{size}\n"; - } -} - -# -# Read an exact amount of bytes. -# -sub xread { - my ($fh, $bytes_total) = @_; - - my $data; - my $bytes_read = 0; - while ($bytes_total > 0) { - my $read = read($fh, $data, $bytes_total, $bytes_read); - - die "read failed: $!" if (!defined $read); - die "end of file\n" if ($read == 0); - - $bytes_total -= $read; - $bytes_read += $read; - } - - return $data; -} - -sub DESTROY { - unlink "procfile.shm"; -} - -1;