1 package Test::Valgrind::Session;
8 Test::Valgrind::Session - Test::Valgrind session object.
16 our $VERSION = '1.14';
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
29 use POSIX (); # SIGKILL
33 use base qw<Test::Valgrind::Carp>;
39 my $tvs = Test::Valgrind::Session->new(
40 search_dirs => \@search_dirs,
41 valgrind => $valgrind, # One candidate
42 valgrind => \@valgrind, # Several candidates
43 min_version => $min_version,
44 regen_def_supp => $regen_def_supp,
45 no_def_supp => $no_def_supp,
46 extra_supps => \@extra_supps,
49 The package constructor, which takes several options :
55 All the directories from C<@search_dirs> will have F<valgrind> appended to create a list of candidates for the C<valgrind> executable.
57 Defaults to the current C<PATH> environment variable.
61 If a simple scalar C<$valgrind> is passed as the value to C<'valgrind'>, it will be the only candidate.
62 C<@search_dirs> will then be ignored.
64 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>.
68 C<$min_version> specifies the minimal C<valgrind> version required.
69 The constructor will croak if it's not able to find an adequate C<valgrind> from the supplied candidates list and search path.
75 If C<$regen_def_supp> is true, the default suppression file associated with the tool and the command will be forcefully regenerated.
81 If C<$no_def_supp> is true, C<valgrind> won't read the default suppression file associated with the tool and the command.
87 C<$extra_supps> is a reference to an array of optional suppression files that will be passed to C<valgrind>.
97 $class = ref($class) || $class;
102 my $vg = delete $args{valgrind};
103 if (defined $vg and not ref $vg) {
106 push @paths, @$vg if $vg and ref $vg eq 'ARRAY';
107 my $dirs = delete $args{search_dirs};
108 $dirs = [ File::Spec->path ] unless $dirs;
109 push @paths, map File::Spec->catfile($_, 'valgrind'), @$dirs
110 if ref $dirs eq 'ARRAY';
112 $class->_croak('Empty valgrind candidates list') unless @paths;
114 my $min_version = delete $args{min_version};
115 defined and not ref and $_ = version->new($_) for $min_version;
117 my ($valgrind, $version);
120 my $ver = qx/$_ --version/;
121 if ($ver =~ /^valgrind-(\d+(\.\d+)*)/) {
123 $version = version->new($1);
124 next if $version < $min_version;
132 $class->_croak('No appropriate valgrind executable could be found')
133 unless defined $valgrind;
135 my $extra_supps = delete $args{extra_supps};
136 $extra_supps = [ ] unless $extra_supps and ref $extra_supps eq 'ARRAY';
137 @$extra_supps = grep { defined && -f $_ && -r _ } @$extra_supps;
140 valgrind => $valgrind,
142 regen_def_supp => delete($args{regen_def_supp}),
143 no_def_supp => delete($args{no_def_supp}),
144 extra_supps => $extra_supps,
150 my $valgrind_path = $tvs->valgrind;
152 The path to the selected C<valgrind> executable.
156 my $valgrind_version = $tvs->version;
158 The L<version> object associated to the selected C<valgrind>.
165 my $version = $self->{version};
166 $self->{version} = $version = version->new($version) unless ref $version;
171 =head2 C<regen_def_supp>
173 my $regen_def_supp = $tvs->regen_def_supp;
175 Read-only accessor for the C<regen_def_supp> option.
179 =head2 C<no_def_supp>
181 my $no_def_supp = $tvs->no_def_supp;
183 Read-only accessor for the C<no_def_supp> option.
187 eval "sub $_ { \$_[0]->{$_} }" for qw<valgrind regen_def_supp no_def_supp>;
189 =head2 C<extra_supps>
191 my @extra_supps = $tvs->extra_supps;
193 Read-only accessor for the C<extra_supps> option.
197 sub extra_supps { @{$_[0]->{extra_supps} || []} }
207 Runs the command C<$command> through C<valgrind> with the tool C<$tool>, which will report to the action C<$action>.
209 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.
219 my $guard = Test::Valgrind::Session::Guard->new(sub { $self->finish });
221 $self->_run($args{command});
225 my ($self, $cmd) = @_;
227 if ($cmd->isa('Test::Valgrind::Command::Aggregate')) {
228 $self->_run($_) for $cmd->commands;
232 $self->command($cmd);
234 $self->report($self->report_class->new_diag(
235 'Using valgrind ' . $self->version . ' located at ' . $self->valgrind
238 my $env = $self->command->env($self);
241 if ($self->do_suppressions) {
242 push @supp_args, '--gen-suppressions=all';
243 } elsif (not $self->no_def_supp) {
244 my $def_supp = $self->def_supp_file;
246 if ($self->regen_def_supp and -e $def_supp) {
247 1 while unlink $def_supp;
250 if (defined $def_supp and not -e $def_supp) {
251 $self->report($self->report_class->new_diag(
252 'Generating suppressions' . ($forced ? ' (forced)' : '') . '...'
254 require Test::Valgrind::Suppressions;
255 Test::Valgrind::Suppressions->generate(
257 command => $self->command,
260 $self->_croak('Couldn\'t generate suppressions') unless -e $def_supp;
261 $self->report($self->report_class->new_diag(
262 "Suppressions for this perl stored in $def_supp"
265 for ($self->suppressions) {
267 $self->report($self->report_class->new_diag("Using suppression file $_"));
268 push @supp_args, "--suppressions=$_";
272 pipe my $vrdr, my $vwtr or $self->_croak("pipe(\$vrdr, \$vwtr): $!");
274 my $oldfh = select $vrdr;
280 $self->_croak("fork(): $!") unless defined $pid;
284 close $vrdr or $self->_croak("close(\$vrdr): $!");
285 fcntl $vwtr, Fcntl::F_SETFD(), 0
286 or $self->_croak("fcntl(\$vwtr, F_SETFD, 0): $!");
290 $self->tool->args($self),
292 $self->parser->args($self, $vwtr),
293 $self->command->args($self),
296 # $self->report($self->report_class->new_diag("@args"));
298 exec { $args[0] } @args or $self->_croak("exec @args: $!");
301 local $SIG{INT} = sub {
302 kill -(POSIX::SIGKILL()) => $pid;
307 close $vwtr or $self->_croak("close(\$vwtr): $!");
309 $self->parser->parse($self, $vrdr);
311 $self->{exit_code} = (waitpid($pid, 0) == $pid) ? $? >> 8 : 255;
313 close $vrdr or $self->_croak("close(\$vrdr): $!");
318 sub Test::Valgrind::Session::Guard::new { bless \($_[1]), $_[0] }
320 sub Test::Valgrind::Session::Guard::DESTROY { ${$_[0]}->() }
324 Read-only accessor for the C<action> associated to the current run.
328 Read-only accessor for the C<tool> associated to the current run.
332 Read-only accessor for the C<parser> associated to the current tool.
336 Read-only accessor for the C<command> associated to the current run.
342 @members = qw<action tool command parser>;
344 eval "sub $_ { \@_ <= 1 ? \$_[0]->{$_} : (\$_[0]->{$_} = \$_[1]) }";
349 =head2 C<do_suppressions>
351 Forwards to C<< ->action->do_suppressions >>.
355 sub do_suppressions { $_[0]->action->do_suppressions }
357 =head2 C<parser_class>
359 Calls C<< ->tool->parser_class >> with the current session object as the unique argument.
363 sub parser_class { $_[0]->tool->parser_class($_[0]) }
365 =head2 C<report_class>
367 Calls C<< ->tool->report_class >> with the current session object as the unique argument.
371 sub report_class { $_[0]->tool->report_class($_[0]) }
373 =head2 C<def_supp_file>
375 Returns an absolute path to the default suppression file associated to the current session.
377 C<undef> will be returned as soon as any of C<< ->command->suppressions_tag >> or C<< ->tool->suppressions_tag >> are also C<undef>.
378 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 >>.
385 my $tool_tag = $self->tool->suppressions_tag($self);
386 return unless defined $tool_tag;
388 my $cmd_tag = $self->command->suppressions_tag($self);
389 return unless defined $cmd_tag;
391 require File::HomeDir; # So that it's not needed at configure time.
393 return File::Spec->catfile(
394 File::HomeDir->my_home,
399 "$tool_tag-$cmd_tag.supp",
403 =head2 C<suppressions>
405 my @suppressions = $tvs->suppressions;
407 Returns the list of all the suppressions that will be passed to C<valgrind>.
408 Honors L</no_def_supp> and L</extra_supps>.
416 unless ($self->no_def_supp) {
417 my $def_supp = $self->def_supp_file;
418 push @supps, $def_supp if defined $def_supp;
420 push @supps, $self->extra_supps;
433 Starts the action and tool associated to the current run.
434 It's automatically called at the beginning of L</run>.
443 for (qw<action tool command>) {
444 my $base = 'Test::Valgrind::' . ucfirst;
445 my $value = $args{$_};
446 $self->_croak("Invalid $_") unless Scalar::Util::blessed($value)
447 and $value->isa($base);
451 delete @{$self}{qw<last_status exit_code>};
453 $self->tool->start($self);
454 $self->parser($self->parser_class->new)->start($self);
455 $self->action->start($self);
464 Forwards to C<< ->action->abort >> after unshifting the session object to the argument list.
471 $self->action->abort($self, @_);
476 $tvs->report($report);
478 Forwards to C<< ->action->report >> after unshifting the session object to the argument list.
483 my ($self, $report) = @_;
485 return unless defined $report;
487 for my $handler (qw<tool command>) {
488 $report = $self->$handler->filter($self, $report);
489 return unless defined $report;
492 $self->action->report($self, $report);
499 Finishes the action and tool associated to the current run.
500 It's automatically called at the end of L</run>.
507 my $action = $self->action;
509 $action->finish($self);
510 $self->parser->finish($self);
511 $self->tool->finish($self);
513 my $status = $action->status($self);
514 $self->{last_status} = defined $status ? $status : $self->{exit_code};
516 $self->$_(undef) for @members;
523 my $status = $tvs->status;
525 Returns the status code of the last run of the session.
529 sub status { $_[0]->{last_status} }
533 L<Test::Valgrind>, L<Test::Valgrind::Action>, L<Test::Valgrind::Command>, L<Test::Valgrind::Tool>, L<Test::Valgrind::Parser>.
535 L<version>, L<File::HomeDir>.
539 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
541 You can contact me by mail or on C<irc.perl.org> (vincent).
545 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>.
546 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
550 You can find documentation for this module with the perldoc command.
552 perldoc Test::Valgrind::Session
554 =head1 COPYRIGHT & LICENSE
556 Copyright 2009,2010,2011,2013,2015 Vincent Pit, all rights reserved.
558 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
562 1; # End of Test::Valgrind::Session