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.
28 use Fcntl (); # F_SETFD
30 use POSIX (); # SIGKILL, _exit()
32 use base qw<Test::Valgrind::Carp>;
34 use Test::Valgrind::Version;
40 my $tvs = Test::Valgrind::Session->new(
41 search_dirs => \@search_dirs,
42 valgrind => $valgrind, # One candidate
43 valgrind => \@valgrind, # Several candidates
44 min_version => $min_version,
45 regen_def_supp => $regen_def_supp,
46 no_def_supp => $no_def_supp,
47 allow_no_supp => $allow_no_supp,
48 extra_supps => \@extra_supps,
51 The package constructor, which takes several options :
57 All the directories from C<@search_dirs> will have F<valgrind> appended to create a list of candidates for the C<valgrind> executable.
59 Defaults to the current C<PATH> environment variable.
63 If a simple scalar C<$valgrind> is passed as the value to C<'valgrind'>, it will be the only candidate.
64 C<@search_dirs> will then be ignored.
66 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>.
70 C<$min_version> specifies the minimal C<valgrind> version required.
71 The constructor will croak if it's not able to find an adequate C<valgrind> from the supplied candidates list and search path.
77 If C<$regen_def_supp> is true, the default suppression file associated with the tool and the command will be forcefully regenerated.
83 If C<$no_def_supp> is true, C<valgrind> won't read the default suppression file associated with the tool and the command.
89 If C<$allow_no_supp> is true, the command will always be run into C<valgrind> even if no appropriate suppression file is available.
95 C<$extra_supps> is a reference to an array of optional suppression files that will be passed to C<valgrind>.
105 $class = ref($class) || $class;
110 my $vg = delete $args{valgrind};
111 if (defined $vg and not ref $vg) {
114 push @paths, @$vg if $vg and ref $vg eq 'ARRAY';
115 my $dirs = delete $args{search_dirs};
116 $dirs = [ File::Spec->path ] unless $dirs;
117 push @paths, map File::Spec->catfile($_, 'valgrind'), @$dirs
118 if ref $dirs eq 'ARRAY';
120 $class->_croak('Empty valgrind candidates list') unless @paths;
122 my $min_version = delete $args{min_version};
123 if (defined $min_version) {
124 $min_version = Test::Valgrind::Version->new(string => $min_version);
127 my ($valgrind, $version);
130 my $output = qx/$_ --version/;
133 eval { Test::Valgrind::Version->new(command_output => $output) };
135 if (defined $version) {
136 next if defined $min_version and $version < $min_version;
141 $class->_croak('No appropriate valgrind executable could be found')
142 unless defined $valgrind;
144 my $extra_supps = delete $args{extra_supps};
145 $extra_supps = [ ] unless $extra_supps and ref $extra_supps eq 'ARRAY';
146 @$extra_supps = grep { defined && -f $_ && -r _ } @$extra_supps;
149 valgrind => $valgrind,
151 regen_def_supp => delete($args{regen_def_supp}),
152 no_def_supp => delete($args{no_def_supp}),
153 allow_no_supp => delete($args{allow_no_supp}),
154 extra_supps => $extra_supps,
160 my $valgrind_path = $tvs->valgrind;
162 The path to the selected C<valgrind> executable.
166 my $valgrind_version = $tvs->version;
168 The L<Test::Valgrind::Version> object associated to the selected C<valgrind>.
170 =head2 C<regen_def_supp>
172 my $regen_def_supp = $tvs->regen_def_supp;
174 Read-only accessor for the C<regen_def_supp> option.
178 =head2 C<no_def_supp>
180 my $no_def_supp = $tvs->no_def_supp;
182 Read-only accessor for the C<no_def_supp> option.
184 =head2 C<allow_no_supp>
186 my $allow_no_supp = $tvs->allow_no_supp;
188 Read-only accessor for the C<allow_no_supp> option.
192 eval "sub $_ { \$_[0]->{$_} }" for qw<
200 =head2 C<extra_supps>
202 my @extra_supps = $tvs->extra_supps;
204 Read-only accessor for the C<extra_supps> option.
208 sub extra_supps { @{$_[0]->{extra_supps} || []} }
218 Runs the command C<$command> through C<valgrind> with the tool C<$tool>, which will report to the action C<$action>.
220 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.
225 my ($self, %args) = @_;
227 for (qw<action tool command>) {
228 my $base = 'Test::Valgrind::' . ucfirst;
229 my $value = $args{$_};
230 $self->_croak("Invalid $_") unless Scalar::Util::blessed($value)
231 and $value->isa($base);
235 my $cmd = $self->command;
236 if ($cmd->isa('Test::Valgrind::Command::Aggregate')) {
237 for my $subcmd ($cmd->commands) {
238 $args{command} = $subcmd;
244 $self->report($self->report_class->new_diag(
245 'Using valgrind ' . $self->version . ' located at ' . $self->valgrind
248 my $env = $self->command->env($self);
251 if ($self->do_suppressions) {
252 push @supp_args, '--gen-suppressions=all';
254 if (!$self->no_def_supp) {
255 my $def_supp = $self->def_supp_file;
257 if ($self->regen_def_supp and -e $def_supp) {
258 1 while unlink $def_supp;
261 if (defined $def_supp and not -e $def_supp) {
262 $self->report($self->report_class->new_diag(
263 'Generating suppressions' . ($forced ? ' (forced)' : '') . '...'
265 require Test::Valgrind::Suppressions;
266 Test::Valgrind::Suppressions->generate(
268 command => $self->command,
271 $self->_croak('Couldn\'t generate suppressions') unless -e $def_supp;
272 $self->report($self->report_class->new_diag(
273 "Suppressions for this perl stored in $def_supp"
277 my @supp_files = grep {
278 -e $_ and $self->command->check_suppressions_file($_)
279 } $self->suppressions;
280 if (@supp_files > 1) {
281 my $files_list = join "\n", map " $_", @supp_files;
282 $self->report($self->report_class->new_diag(
283 "Using suppressions from:\n$files_list"
285 } elsif (@supp_files) {
286 $self->report($self->report_class->new_diag(
287 "Using suppressions from $supp_files[0]"
289 } elsif ($self->allow_no_supp) {
290 $self->report($self->report_class->new_diag("No suppressions used"));
292 $self->_croak("No compatible suppressions available");
294 @supp_args = map "--suppressions=$_", @supp_files;
299 my $guard = Test::Valgrind::Session::Guard->new(sub { $self->finish });
302 pipe my $vrdr, my $vwtr or $self->_croak("pipe(\$vrdr, \$vwtr): $!");
304 my $oldfh = select $vrdr;
309 pipe my $erdr, my $ewtr or $self->_croak("pipe(\$erdr, \$ewtr): $!");
311 my $oldfh = select $erdr;
317 $self->_croak("fork(): $!") unless defined $pid;
322 eval { setpgrp(0, 0) };
325 close $erdr or POSIX::_exit(255);
329 close $vrdr or $self->_croak("close(\$vrdr): $!");
331 fcntl $vwtr, Fcntl::F_SETFD(), 0
332 or $self->_croak("fcntl(\$vwtr, F_SETFD, 0): $!");
336 $self->tool->args($self),
338 $self->parser->args($self, $vwtr),
339 $self->command->args($self),
344 exec { $args[0] } @args;
346 $self->_croak("exec @args: $!");
357 local $SIG{INT} = sub {
358 die 'valgrind analysis was interrupted';
361 close $vwtr or $self->_croak("close(\$vwtr): $!");
362 close $ewtr or $self->_croak("close(\$ewtr): $!");
365 my $sel = IO::Select->new($vrdr, $erdr);
368 while (my @ready = $sel->can_read) {
369 last SEL if @ready == 1 and fileno $ready[0] == fileno $vrdr;
372 my $bytes_read = sysread $erdr, $buf, 4096;
373 if (not defined $bytes_read) {
374 $self->_croak("sysread(\$erdr): $!");
375 } elsif ($bytes_read) {
376 $sel->remove($vrdr) unless $child_err;
380 die $child_err if $child_err;
385 my $aborted = $self->parser->parse($self, $vrdr);
388 $self->report($self->report_class->new_diag("valgrind has aborted"));
395 kill -(POSIX::SIGKILL()) => $pid if kill 0 => $pid;
399 # Force the guard destructor to trigger now so that old perls don't lose $@
403 $self->{exit_code} = (waitpid($pid, 0) == $pid) ? $? >> 8 : 255;
405 close $erdr or $self->_croak("close(\$erdr): $!");
406 close $vrdr or $self->_croak("close(\$vrdr): $!");
411 die $error if $error;
416 sub Test::Valgrind::Session::Guard::new { bless \($_[1]), $_[0] }
418 sub Test::Valgrind::Session::Guard::DESTROY { ${$_[0]}->() }
422 Read-only accessor for the C<action> associated to the current run.
426 Read-only accessor for the C<tool> associated to the current run.
430 Read-only accessor for the C<parser> associated to the current tool.
434 Read-only accessor for the C<command> associated to the current run.
440 @members = qw<action tool command parser>;
442 eval "sub $_ { \@_ <= 1 ? \$_[0]->{$_} : (\$_[0]->{$_} = \$_[1]) }";
447 =head2 C<do_suppressions>
449 Forwards to C<< ->action->do_suppressions >>.
453 sub do_suppressions { $_[0]->action->do_suppressions }
455 =head2 C<parser_class>
457 Calls C<< ->tool->parser_class >> with the current session object as the unique argument.
461 sub parser_class { $_[0]->tool->parser_class($_[0]) }
463 =head2 C<report_class>
465 Calls C<< ->tool->report_class >> with the current session object as the unique argument.
469 sub report_class { $_[0]->tool->report_class($_[0]) }
471 =head2 C<def_supp_file>
473 Returns an absolute path to the default suppression file associated to the current session.
475 C<undef> will be returned as soon as any of C<< ->command->suppressions_tag >> or C<< ->tool->suppressions_tag >> are also C<undef>.
476 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 >>.
483 my $tool_tag = $self->tool->suppressions_tag($self);
484 return unless defined $tool_tag;
486 my $cmd_tag = $self->command->suppressions_tag($self);
487 return unless defined $cmd_tag;
489 require File::HomeDir; # So that it's not needed at configure time.
491 return File::Spec->catfile(
492 File::HomeDir->my_home,
497 "$tool_tag-$cmd_tag.supp",
501 =head2 C<suppressions>
503 my @suppressions = $tvs->suppressions;
505 Returns the list of all the suppressions that will be passed to C<valgrind>.
506 Honors L</no_def_supp> and L</extra_supps>.
514 unless ($self->no_def_supp) {
515 my $def_supp = $self->def_supp_file;
516 push @supps, $def_supp if defined $def_supp;
518 push @supps, $self->extra_supps;
527 Starts the action and tool associated to the current run.
528 It's automatically called at the beginning of L</run>.
535 delete @{$self}{qw<last_status exit_code>};
537 $self->tool->start($self);
538 $self->parser($self->parser_class->new)->start($self);
539 $self->action->start($self);
548 Forwards to C<< ->action->abort >> after unshifting the session object to the argument list.
555 $self->action->abort($self, @_);
560 $tvs->report($report);
562 Forwards to C<< ->action->report >> after unshifting the session object to the argument list.
567 my ($self, $report) = @_;
569 return unless defined $report;
571 for my $handler (qw<tool command>) {
572 $report = $self->$handler->filter($self, $report);
573 return unless defined $report;
576 $self->action->report($self, $report);
583 Finishes the action and tool associated to the current run.
584 It's automatically called at the end of L</run>.
591 my $action = $self->action;
593 $action->finish($self);
594 $self->parser->finish($self);
595 $self->tool->finish($self);
597 my $status = $action->status($self);
598 $self->{last_status} = defined $status ? $status : $self->{exit_code};
600 $self->$_(undef) for @members;
607 my $status = $tvs->status;
609 Returns the status code of the last run of the session.
613 sub status { $_[0]->{last_status} }
617 L<Test::Valgrind>, L<Test::Valgrind::Action>, L<Test::Valgrind::Command>, L<Test::Valgrind::Tool>, L<Test::Valgrind::Parser>.
623 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
625 You can contact me by mail or on C<irc.perl.org> (vincent).
629 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>.
630 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
634 You can find documentation for this module with the perldoc command.
636 perldoc Test::Valgrind::Session
638 =head1 COPYRIGHT & LICENSE
640 Copyright 2009,2010,2011,2013,2015 Vincent Pit, all rights reserved.
642 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
646 1; # End of Test::Valgrind::Session