blib/lib/PostgreSQL/PLPerl/Trace.pm

filter: Statementstmt 100% 31 / 31 Branchbran 87.5% 14 / 16 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 1 / 1 Podpod 0.0% 0 / 1 totaltotal 94.2% 49 / 52 CC 8 SCAR 20.8
CC
8
Cov
96%
DB
8.0
CRAP 8.0
↑ Summary
1 package PostgreSQL::PLPerl::Trace;
2 2 our $VERSION = '1.001';
3  
4 =head1 NAME
5  
6 PostgreSQL::PLPerl::Trace - Simple way to trace execution of Perl statements in PL/Perl
7  
8 =head1 VERSION
9  
10 version 1.001
11  
12 =head1 SYNOPSIS
13  
14 Load via a line in your F<plperlinit.pl> file:
15  
16 use PostgreSQL::PLPerl::Trace;
17  
18 Load via the C<PERL5OPT> environment variable:
19  
20 $ PERL5OPT='-MPostgreSQL::PLPerl::Trace' pg_ctl ...
21  
22 =head1 DESCRIPTION
23  
24 Writes a line to the PostgreSQL log file for every PL/Perl statement executed.
25 This can generate truly I<massive> amounts of log data and also slows excution
26 of PL/Perl code by at least a couple of orders of magnitude.
27  
28 Why would you want to do this? Well, there are times when it's a simple and
29 effective way to see what PL/Perl code is I<actually> being executed.
30  
31 This module is based on L<Devel::Trace> but modified to work with PostgreSQL PL/Perl
32 for both the C<plperlu> language I<and>, more significantly, for the C<plperl>
33 language as well. It also shows the subroutine name whenever execution moves
34 from one subroutine to another.
35  
36 =head1 ENABLING
37  
38 In order to use this module you need to arrange for it to be loaded when
39 PostgreSQL initializes a Perl interpreter.
40  
41 Create a F<plperlinit.pl> file in the same directory as your
42 F<postgres.conf> file, if it doesn't exist already.
43  
44 In the F<plperlinit.pl> file write the code to load this module:
45  
46 use PostgreSQL::PLPerl::Trace;
47  
48 When it's no longer needed just comment it out by prefixing with a C<#>.
49  
50 =head2 PostgreSQL 8.x
51  
52 Set the C<PERL5OPT> before starting postgres, to something like this:
53  
54 PERL5OPT='-e "require q{plperlinit.pl}"'
55  
56 The code in the F<plperlinit.pl> should also include C<delete $ENV{PERL5OPT};>
57 to avoid any problems with nested invocations of perl, e.g., via a C<plperlu>
58 function.
59  
60 =head2 PostgreSQL 9.0
61  
62 For PostgreSQL 9.0 you can still use the C<PERL5OPT> method described above.
63 Alternatively, and preferably, you can use the C<plperl.on_init> configuration
64 variable in the F<postgres.conf> file.
65  
66 plperl.on_init='require q{plperlinit.pl};'
67  
68 =head2 Alternative Method
69  
70 It you're not already using the C<PERL5OPT> environment variable to load a
71 F<plperlinit.pl> file, as described above, then you can use it as a quick way
72 to load the module for ad-hoc use:
73  
74 $ PERL5OPT='-MPostgreSQL::PLPerl::Trace' pg_ctl ...
75  
76 =head1 AUTHOR
77  
78 Tim Bunce L<http://www.tim.bunce.name>
79  
80 Copyright (c) Tim Bunce, Ireland, 2010. All rights reserved.
81 You may use and distribute on the same terms as Perl 5.10.1.
82  
83 With thanks to L<http://www.TigerLead.com> for sponsoring development.
84  
85 =cut
86  
87 # these are currently undocumented but used by tests
88 2 ▶our $TRACE; $TRACE = 1 unless defined $TRACE;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $TRACE 1 1 2
89 2 ▶our $fh; $fh = \*STDERR unless defined $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 1 1 2
90  
91 2 my $main_glob = *{"main::"};
92 2 my $main_stash = \%{$main_glob}; # get ref to true main glob outside of Safe
93 2 my $file_sub_prev = '';
94  
95 # maybe move core of this to to a new Devel::TraceSafe module
96  
97 3132 ▶sub DB::DB { # magic sub
Subroutine DB: called CC=8 SCAR=20.8
Pod undocumented
98  
99 3132 ▶ return unless $TRACE;
Branch: 1 branch
BranchTrueFalseTotal
unless $TRACE 2927 205 3132
100  
101 205 my ($p, $f, $l) = caller();
102  
103 205 my $code = \@{"::_<$f"};
104 205 ▶ if (!@$code) { # probably inside Safe
Branch: 1 branch
BranchTrueFalseTotal
unless (@$code) 5 200 205
105 5 my $glob = $main_stash->{"_<$f"};
106 5 $code = \@{$glob};
107 }
108  
109 205 ▶ my $sub = (caller(1))[3] || '???';
Condition100%2/2
(caller 1)[3] || '???'
l!l
2032
MC/DC100%1/1
(caller 1)[3] || '???'
(caller 1)[3]
Truth table100%2/2
A
A= (caller 1)[3]
Aresult
11
00
110 205 my $linesrc = $code->[$l];
111 205 ▶ if (!$linesrc) { # should never happen
Branch: 1 branch
BranchTrueFalseTotal
unless ($linesrc) 2 203 205
112 2 ▶ my $submsg = $sub ? " for sub $sub" : "";
Branch: 1 branch
BranchTrueFalseTotal
$sub ? : 2 0 2
113 2 $linesrc = "\t(missing src$submsg in $p)";
114 }
115 205 chomp $linesrc;
116  
117 205 my $file_sub = "$f/$sub";
118 205 ▶ if ($file_sub ne $file_sub_prev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_sub ne $file_sub_prev) 14 191 205
119 14 ▶ print $fh "-- in $sub:\n" if $sub;
Branch: 1 branch
BranchTrueFalseTotal
if $sub 14 0 14
120 14 $file_sub_prev = $file_sub;
121 }
122  
123 205 print $fh ">> $f:$l: $linesrc\n";
124 }
125  
126  
127 2 $^P |= 0
128 | 0x002 # Line-by-line debugging & save src lines.
129 | 0x004 # Switch off optimizations.
130 # | 0x008 # Preserve more data for future interactive inspections.
131 # | 0x010 # Keep info about source lines on which a subroutine is defined.
132 | 0x020 # Start with single-step on.
133 | 0x100 # Provide informative "file" names for evals
134 | 0x200 # Provide informative names to anonymous subroutines
135 | 0x400 # Save source code lines into "@{"_<$filename"}".
136 ;
137  
138 2 1;
139  
140 # vim: ts=8:sw=4:et
↑ Summary