1 package Test::Valgrind::Session;
8 Test::Valgrind::Session - Test::Valgrind session object.
16 our $VERSION = '1.13';
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>;
37 =head2 C<< new search_dirs => \@search_dirs, valgrind => [ $valgrind | \@valgrind ], min_version => $min_version, no_def_supp => $no_def_supp, extra_supps => \@extra_supps >>
39 The package constructor, which takes several options :
45 All the directories from C<@search_dirs> will have F<valgrind> appended to create a list of candidates for the C<valgrind> executable.
47 Defaults to the current C<PATH> environment variable.
51 If a simple scalar C<$valgrind> is passed as the value to C<'valgrind'>, it will be the only candidate.
52 C<@search_dirs> will then be ignored.
54 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>.
58 C<$min_version> specifies the minimal C<valgrind> version required.
59 The constructor will croak if it's not able to find an adequate C<valgrind> from the supplied candidates list and search path.
65 If C<$no_def_supp> is false, C<valgrind> won't read the default suppression file associated with the tool and the command.
71 C<$extra_supps> is a reference to an array of optional suppression files that will be passed to C<valgrind>.
81 $class = ref($class) || $class;
86 my $vg = delete $args{valgrind};
87 if (defined $vg and not ref $vg) {
90 push @paths, @$vg if $vg and ref $vg eq 'ARRAY';
91 my $dirs = delete $args{search_dirs};
92 $dirs = [ File::Spec->path ] unless $dirs;
93 push @paths, map File::Spec->catfile($_, 'valgrind'), @$dirs
94 if ref $dirs eq 'ARRAY';
96 $class->_croak('Empty valgrind candidates list') unless @paths;
98 my $min_version = delete $args{min_version};
99 defined and not ref and $_ = version->new($_) for $min_version;
101 my ($valgrind, $version);
104 my $ver = qx/$_ --version/;
105 if ($ver =~ /^valgrind-(\d+(\.\d+)*)/) {
107 $version = version->new($1);
108 next if $version < $min_version;
116 $class->_croak('No appropriate valgrind executable could be found')
117 unless defined $valgrind;
119 my $extra_supps = delete $args{extra_supps};
120 $extra_supps = [ ] unless $extra_supps and ref $extra_supps eq 'ARRAY';
121 @$extra_supps = grep { defined && -f $_ && -r _ } @$extra_supps;
124 valgrind => $valgrind,
126 no_def_supp => delete($args{no_def_supp}),
127 extra_supps => $extra_supps,
133 The path to the selected C<valgrind> executable.
137 The L<version> object associated to the selected C<valgrind>.
144 my $version = $self->{version};
145 $self->{version} = $version = version->new($version) unless ref $version;
150 =head2 C<no_def_supp>
152 Read-only accessor for the C<no_def_supp> option.
156 eval "sub $_ { \$_[0]->{$_} }" for qw<valgrind no_def_supp>;
158 =head2 C<extra_supps>
160 Read-only accessor for the C<extra_supps> option.
164 sub extra_supps { @{$_[0]->{extra_supps} || []} }
166 =head2 C<< run action => $action, tool => $tool, command => $command >>
168 Runs the command C<$command> through C<valgrind> with the tool C<$tool>, which will report to the action C<$action>.
170 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.
180 my $guard = Test::Valgrind::Session::Guard->new(sub { $self->finish });
182 $self->_run($args{command});
186 my ($self, $cmd) = @_;
188 if ($cmd->isa('Test::Valgrind::Command::Aggregate')) {
189 $self->_run($_) for $cmd->commands;
193 $self->command($cmd);
195 $self->report($self->report_class->new_diag(
196 'Using valgrind ' . $self->version . ' located at ' . $self->valgrind
199 my $env = $self->command->env($self);
202 if ($self->do_suppressions) {
203 push @supp_args, '--gen-suppressions=all';
204 } elsif (not $self->no_def_supp) {
205 my $def_supp = $self->def_supp_file;
206 if (defined $def_supp and not -e $def_supp) {
207 $self->report($self->report_class->new_diag(
208 "Generating suppressions..."
210 require Test::Valgrind::Suppressions;
211 Test::Valgrind::Suppressions->generate(
213 command => $self->command,
216 $self->_croak('Couldn\'t generate suppressions') unless -e $def_supp;
217 $self->report($self->report_class->new_diag(
218 "Suppressions for this perl stored in $def_supp"
221 for ($self->suppressions) {
223 $self->report($self->report_class->new_diag("Using suppression file $_"));
224 push @supp_args, "--suppressions=$_";
228 pipe my $vrdr, my $vwtr or $self->_croak("pipe(\$vrdr, \$vwtr): $!");
230 my $oldfh = select $vrdr;
236 $self->_croak("fork(): $!") unless defined $pid;
240 close $vrdr or $self->_croak("close(\$vrdr): $!");
241 fcntl $vwtr, Fcntl::F_SETFD(), 0
242 or $self->_croak("fcntl(\$vwtr, F_SETFD, 0): $!");
246 $self->tool->args($self),
248 $self->parser->args($self, $vwtr),
249 $self->command->args($self),
252 # $self->report($self->report_class->new_diag("@args"));
254 exec { $args[0] } @args or $self->_croak("exec @args: $!");
257 local $SIG{INT} = sub {
258 kill -(POSIX::SIGKILL()) => $pid;
263 close $vwtr or $self->_croak("close(\$vwtr): $!");
265 $self->parser->parse($self, $vrdr);
267 $self->{exit_code} = (waitpid($pid, 0) == $pid) ? $? >> 8 : 255;
269 close $vrdr or $self->_croak("close(\$vrdr): $!");
274 sub Test::Valgrind::Session::Guard::new { bless \($_[1]), $_[0] }
276 sub Test::Valgrind::Session::Guard::DESTROY { ${$_[0]}->() }
280 Read-only accessor for the C<action> associated to the current run.
284 Read-only accessor for the C<tool> associated to the current run.
288 Read-only accessor for the C<parser> associated to the current tool.
292 Read-only accessor for the C<command> associated to the current run.
298 @members = qw<action tool command parser>;
300 eval "sub $_ { \@_ <= 1 ? \$_[0]->{$_} : (\$_[0]->{$_} = \$_[1]) }";
305 =head2 C<do_suppressions>
307 Forwards to C<< ->action->do_suppressions >>.
311 sub do_suppressions { $_[0]->action->do_suppressions }
313 =head2 C<parser_class>
315 Calls C<< ->tool->parser_class >> with the current session object as the unique argument.
319 sub parser_class { $_[0]->tool->parser_class($_[0]) }
321 =head2 C<report_class>
323 Calls C<< ->tool->report_class >> with the current session object as the unique argument.
327 sub report_class { $_[0]->tool->report_class($_[0]) }
329 =head2 C<def_supp_file>
331 Returns an absolute path to the default suppression file associated to the current session.
333 C<undef> will be returned as soon as any of C<< ->command->suppressions_tag >> or C<< ->tool->suppressions_tag >> are also C<undef>.
334 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 >>.
341 my $tool_tag = $self->tool->suppressions_tag($self);
342 return unless defined $tool_tag;
344 my $cmd_tag = $self->command->suppressions_tag($self);
345 return unless defined $cmd_tag;
347 require File::HomeDir; # So that it's not needed at configure time.
349 return File::Spec->catfile(
350 File::HomeDir->my_home,
355 "$tool_tag-$cmd_tag.supp",
359 =head2 C<suppressions>
361 Returns the list of all the suppressions that will be passed to C<valgrind>.
362 Honors L</no_def_supp> and L</extra_supps>.
370 unless ($self->no_def_supp) {
371 my $def_supp = $self->def_supp_file;
372 push @supps, $def_supp if defined $def_supp;
374 push @supps, $self->extra_supps;
381 Starts the action and tool associated to the current run.
382 It's automatically called at the beginning of L</run>.
391 for (qw<action tool command>) {
392 my $base = 'Test::Valgrind::' . ucfirst;
393 my $value = $args{$_};
394 $self->_croak("Invalid $_") unless Scalar::Util::blessed($value)
395 and $value->isa($base);
399 delete @{$self}{qw<last_status exit_code>};
401 $self->tool->start($self);
402 $self->parser($self->parser_class->new)->start($self);
403 $self->action->start($self);
410 Forwards to C<< ->action->abort >> after unshifting the session object to the argument list.
417 $self->action->abort($self, @_);
420 =head2 C<report $report>
422 Forwards to C<< ->action->report >> after unshifting the session object to the argument list.
427 my ($self, $report) = @_;
429 return unless defined $report;
431 for my $handler (qw<tool command>) {
432 $report = $self->$handler->filter($self, $report);
433 return unless defined $report;
436 $self->action->report($self, $report);
441 Finishes the action and tool associated to the current run.
442 It's automatically called at the end of L</run>.
449 my $action = $self->action;
451 $action->finish($self);
452 $self->parser->finish($self);
453 $self->tool->finish($self);
455 my $status = $action->status($self);
456 $self->{last_status} = defined $status ? $status : $self->{exit_code};
458 $self->$_(undef) for @members;
465 Returns the status code of the last run of the session.
469 sub status { $_[0]->{last_status} }
473 L<Test::Valgrind>, L<Test::Valgrind::Action>, L<Test::Valgrind::Command>, L<Test::Valgrind::Tool>, L<Test::Valgrind::Parser>.
475 L<version>, L<File::HomeDir>.
479 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
481 You can contact me by mail or on C<irc.perl.org> (vincent).
485 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>.
486 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
490 You can find documentation for this module with the perldoc command.
492 perldoc Test::Valgrind::Session
494 =head1 COPYRIGHT & LICENSE
496 Copyright 2009,2010,2011 Vincent Pit, all rights reserved.
498 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
502 1; # End of Test::Valgrind::Session