1 package Test::Valgrind::Session;
8 Test::Valgrind::Session - Test::Valgrind session object.
16 our $VERSION = '1.17';
20 This class supervises the execution of the C<valgrind> process.
21 It also acts as a dispatcher between the different components.
27 use ExtUtils::MM (); # MM->maybe_command()
30 use Fcntl (); # F_SETFD
32 use POSIX (); # SIGKILL, _exit()
34 use base qw<Test::Valgrind::Carp>;
36 use Test::Valgrind::Version;
42 my $tvs = Test::Valgrind::Session->new(
43 search_dirs => \@search_dirs,
44 valgrind => $valgrind, # One candidate
45 valgrind => \@valgrind, # Several candidates
46 min_version => $min_version,
47 regen_def_supp => $regen_def_supp,
48 no_def_supp => $no_def_supp,
49 allow_no_supp => $allow_no_supp,
50 extra_supps => \@extra_supps,
53 The package constructor, which takes several options :
59 All the directories from C<@search_dirs> will have F<valgrind> appended to create a list of candidates for the C<valgrind> executable.
61 Defaults to the current C<PATH> environment variable.
65 If a simple scalar C<$valgrind> is passed as the value to C<'valgrind'>, it will be the only candidate.
66 C<@search_dirs> will then be ignored.
68 If an array refernce C<\@valgrind> is passed, its values will be I<prepended> to the list of the candidates resulting from C<@search_dirs>.
72 C<$min_version> specifies the minimal C<valgrind> version required.
73 The constructor will croak if it's not able to find an adequate C<valgrind> from the supplied candidates list and search path.
79 If C<$regen_def_supp> is true, the default suppression file associated with the tool and the command will be forcefully regenerated.
85 If C<$no_def_supp> is true, C<valgrind> won't read the default suppression file associated with the tool and the command.
91 If C<$allow_no_supp> is true, the command will always be run into C<valgrind> even if no appropriate suppression file is available.
97 C<$extra_supps> is a reference to an array of optional suppression files that will be passed to C<valgrind>.
107 $class = ref($class) || $class;
112 my $vg = delete $args{valgrind};
113 if (defined $vg and not ref $vg) {
116 push @paths, @$vg if defined $vg and ref $vg eq 'ARRAY';
117 my $dirs = delete $args{search_dirs};
118 $dirs = [ File::Spec->path ] unless defined $dirs;
119 my $exe_name = 'valgrind';
120 $exe_name .= $Config::Config{exe_ext} if defined $Config::Config{exe_ext};
121 push @paths, map File::Spec->catfile($_, $exe_name), @$dirs
122 if ref $dirs eq 'ARRAY';
124 $class->_croak('Empty valgrind candidates list') unless @paths;
126 my $min_version = delete $args{min_version};
127 if (defined $min_version) {
128 $min_version = Test::Valgrind::Version->new(string => $min_version);
131 my ($valgrind, $version);
132 for my $path (@paths) {
133 next unless defined($path) and MM->maybe_command($path);
134 my $output = qx/$path --version/;
137 eval { Test::Valgrind::Version->new(command_output => $output) };
140 next if defined $min_version and $ver < $min_version;
146 $class->_croak('No appropriate valgrind executable could be found')
147 unless defined $valgrind;
149 my $extra_supps = delete $args{extra_supps};
150 $extra_supps = [ ] unless $extra_supps and ref $extra_supps eq 'ARRAY';
151 @$extra_supps = grep { defined && -f $_ && -r _ } @$extra_supps;
154 valgrind => $valgrind,
156 regen_def_supp => delete($args{regen_def_supp}),
157 no_def_supp => delete($args{no_def_supp}),
158 allow_no_supp => delete($args{allow_no_supp}),
159 extra_supps => $extra_supps,
165 my $valgrind_path = $tvs->valgrind;
167 The path to the selected C<valgrind> executable.
171 my $valgrind_version = $tvs->version;
173 The L<Test::Valgrind::Version> object associated to the selected C<valgrind>.
175 =head2 C<regen_def_supp>
177 my $regen_def_supp = $tvs->regen_def_supp;
179 Read-only accessor for the C<regen_def_supp> option.
183 =head2 C<no_def_supp>
185 my $no_def_supp = $tvs->no_def_supp;
187 Read-only accessor for the C<no_def_supp> option.
189 =head2 C<allow_no_supp>
191 my $allow_no_supp = $tvs->allow_no_supp;
193 Read-only accessor for the C<allow_no_supp> option.
197 eval "sub $_ { \$_[0]->{$_} }" for qw<
205 =head2 C<extra_supps>
207 my @extra_supps = $tvs->extra_supps;
209 Read-only accessor for the C<extra_supps> option.
213 sub extra_supps { @{$_[0]->{extra_supps} || []} }
223 Runs the command C<$command> through C<valgrind> with the tool C<$tool>, which will report to the action C<$action>.
225 If the command is a L<Test::Valgrind::Command::Aggregate> object, the action and the tool will be initialized once before running all the aggregated commands.
230 my ($self, %args) = @_;
232 for (qw<action tool command>) {
233 my $base = 'Test::Valgrind::' . ucfirst;
234 my $value = $args{$_};
235 $self->_croak("Invalid $_") unless Scalar::Util::blessed($value)
236 and $value->isa($base);
240 my $cmd = $self->command;
241 if ($cmd->isa('Test::Valgrind::Command::Aggregate')) {
242 for my $subcmd ($cmd->commands) {
243 $args{command} = $subcmd;
249 $self->report($self->report_class->new_diag(
250 'Using valgrind ' . $self->version . ' located at ' . $self->valgrind
253 my $env = $self->command->env($self);
256 if ($self->do_suppressions) {
257 push @supp_args, '--gen-suppressions=all';
259 if (!$self->no_def_supp) {
260 my $def_supp = $self->def_supp_file;
262 if ($self->regen_def_supp and -e $def_supp) {
263 1 while unlink $def_supp;
266 if (defined $def_supp and not -e $def_supp) {
267 $self->report($self->report_class->new_diag(
268 'Generating suppressions' . ($forced ? ' (forced)' : '') . '...'
270 require Test::Valgrind::Suppressions;
271 Test::Valgrind::Suppressions->generate(
273 command => $self->command,
276 $self->_croak('Couldn\'t generate suppressions') unless -e $def_supp;
277 $self->report($self->report_class->new_diag(
278 "Suppressions for this perl stored in $def_supp"
282 my @supp_files = grep {
283 -e $_ and $self->command->check_suppressions_file($_)
284 } $self->suppressions;
285 if (@supp_files > 1) {
286 my $files_list = join "\n", map " $_", @supp_files;
287 $self->report($self->report_class->new_diag(
288 "Using suppressions from:\n$files_list"
290 } elsif (@supp_files) {
291 $self->report($self->report_class->new_diag(
292 "Using suppressions from $supp_files[0]"
294 } elsif ($self->allow_no_supp) {
295 $self->report($self->report_class->new_diag("No suppressions used"));
297 $self->_croak("No compatible suppressions available");
299 @supp_args = map "--suppressions=$_", @supp_files;
304 my $guard = Test::Valgrind::Session::Guard->new(sub { $self->finish });
307 pipe my $vrdr, my $vwtr or $self->_croak("pipe(\$vrdr, \$vwtr): $!");
309 my $oldfh = select $vrdr;
314 pipe my $erdr, my $ewtr or $self->_croak("pipe(\$erdr, \$ewtr): $!");
316 my $oldfh = select $erdr;
322 $self->_croak("fork(): $!") unless defined $pid;
327 eval { setpgrp(0, 0) };
330 close $erdr or POSIX::_exit(255);
334 close $vrdr or $self->_croak("close(\$vrdr): $!");
336 fcntl $vwtr, Fcntl::F_SETFD(), 0
337 or $self->_croak("fcntl(\$vwtr, F_SETFD, 0): $!");
341 $self->tool->args($self),
343 $self->parser->args($self, $vwtr),
344 $self->command->args($self),
349 exec { $args[0] } @args;
351 $self->_croak("exec @args: $!");
362 local $SIG{INT} = sub {
363 die 'valgrind analysis was interrupted';
366 close $vwtr or $self->_croak("close(\$vwtr): $!");
367 close $ewtr or $self->_croak("close(\$ewtr): $!");
370 my $sel = IO::Select->new($vrdr, $erdr);
373 while (my @ready = $sel->can_read) {
374 last SEL if @ready == 1 and fileno $ready[0] == fileno $vrdr;
377 my $bytes_read = sysread $erdr, $buf, 4096;
378 if (not defined $bytes_read) {
379 $self->_croak("sysread(\$erdr): $!");
380 } elsif ($bytes_read) {
381 $sel->remove($vrdr) unless $child_err;
385 die $child_err if $child_err;
390 my $aborted = $self->parser->parse($self, $vrdr);
393 $self->report($self->report_class->new_diag("valgrind has aborted"));
400 kill -(POSIX::SIGKILL()) => $pid if kill 0 => $pid;
404 # Force the guard destructor to trigger now so that old perls don't lose $@
408 $self->{exit_code} = (waitpid($pid, 0) == $pid) ? $? >> 8 : 255;
410 close $erdr or $self->_croak("close(\$erdr): $!");
411 close $vrdr or $self->_croak("close(\$vrdr): $!");
416 die $error if $error;
421 sub Test::Valgrind::Session::Guard::new { bless \($_[1]), $_[0] }
423 sub Test::Valgrind::Session::Guard::DESTROY { ${$_[0]}->() }
427 Read-only accessor for the C<action> associated to the current run.
431 Read-only accessor for the C<tool> associated to the current run.
435 Read-only accessor for the C<parser> associated to the current tool.
439 Read-only accessor for the C<command> associated to the current run.
445 @members = qw<action tool command parser>;
447 eval "sub $_ { \@_ <= 1 ? \$_[0]->{$_} : (\$_[0]->{$_} = \$_[1]) }";
452 =head2 C<do_suppressions>
454 Forwards to C<< ->action->do_suppressions >>.
458 sub do_suppressions { $_[0]->action->do_suppressions }
460 =head2 C<parser_class>
462 Calls C<< ->tool->parser_class >> with the current session object as the unique argument.
466 sub parser_class { $_[0]->tool->parser_class($_[0]) }
468 =head2 C<report_class>
470 Calls C<< ->tool->report_class >> with the current session object as the unique argument.
474 sub report_class { $_[0]->tool->report_class($_[0]) }
476 =head2 C<def_supp_file>
478 Returns an absolute path to the default suppression file associated to the current session.
480 C<undef> will be returned as soon as any of C<< ->command->suppressions_tag >> or C<< ->tool->suppressions_tag >> are also C<undef>.
481 Otherwise, the file part of the name is builded by joining those two together, and the directory part is roughly F<< File::HomeDir->my_home / .perl / Test-Valgrind / suppressions / $VERSION >>.
488 my $tool_tag = $self->tool->suppressions_tag($self);
489 return unless defined $tool_tag;
491 my $cmd_tag = $self->command->suppressions_tag($self);
492 return unless defined $cmd_tag;
494 require File::HomeDir; # So that it's not needed at configure time.
496 return File::Spec->catfile(
497 File::HomeDir->my_home,
502 "$tool_tag-$cmd_tag.supp",
506 =head2 C<suppressions>
508 my @suppressions = $tvs->suppressions;
510 Returns the list of all the suppressions that will be passed to C<valgrind>.
511 Honors L</no_def_supp> and L</extra_supps>.
519 unless ($self->no_def_supp) {
520 my $def_supp = $self->def_supp_file;
521 push @supps, $def_supp if defined $def_supp;
523 push @supps, $self->extra_supps;
532 Starts the action and tool associated to the current run.
533 It's automatically called at the beginning of L</run>.
540 delete @{$self}{qw<last_status exit_code>};
542 $self->tool->start($self);
543 $self->parser($self->parser_class->new)->start($self);
544 $self->action->start($self);
553 Forwards to C<< ->action->abort >> after unshifting the session object to the argument list.
560 $self->action->abort($self, @_);
565 $tvs->report($report);
567 Forwards to C<< ->action->report >> after unshifting the session object to the argument list.
572 my ($self, $report) = @_;
574 return unless defined $report;
576 for my $handler (qw<tool command>) {
577 $report = $self->$handler->filter($self, $report);
578 return unless defined $report;
581 $self->action->report($self, $report);
588 Finishes the action and tool associated to the current run.
589 It's automatically called at the end of L</run>.
596 my $action = $self->action;
598 $action->finish($self);
599 $self->parser->finish($self);
600 $self->tool->finish($self);
602 my $status = $action->status($self);
603 $self->{last_status} = defined $status ? $status : $self->{exit_code};
605 $self->$_(undef) for @members;
612 my $status = $tvs->status;
614 Returns the status code of the last run of the session.
618 sub status { $_[0]->{last_status} }
622 L<Test::Valgrind>, L<Test::Valgrind::Action>, L<Test::Valgrind::Command>, L<Test::Valgrind::Tool>, L<Test::Valgrind::Parser>.
628 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
630 You can contact me by mail or on C<irc.perl.org> (vincent).
634 Please report any bugs or feature requests to C<bug-test-valgrind at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Valgrind>.
635 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
639 You can find documentation for this module with the perldoc command.
641 perldoc Test::Valgrind::Session
643 =head1 COPYRIGHT & LICENSE
645 Copyright 2009,2010,2011,2013,2015 Vincent Pit, all rights reserved.
647 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
651 1; # End of Test::Valgrind::Session