blib/lib/Debug/Runopt.pm

filter: Statementstmt 23.0% 12 / 52 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 31 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 42.8% 3 / 7 Podpod 0.0% 0 / 4 totaltotal 10.5% 15 / 142 CC 27 SCAR 46.9
CC
27
Cov
11%
setParseOptions
182.0
init
110.0
end
20.0
CRAP 108.6
↑ Summary Runopt.c »
1 package Debug::Runopt;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '1.01';
8 1 our $RCFileLoc;
9  
10 0 ▶sub init {
Subroutine init: not called CC=10 SCAR=47.0
Pod undocumented
11 0 my ($opts) = @_;
12  
13 ## Source file for debug commands, if provided
14 0 ▶ my $src = ($opts && $opts->{src})?$opts->{src}:undef;
Branch: 1 branch
BranchTrueFalseTotal
$opts && $opts->{'src'} ? : 0 0 0
Condition0%0/3
$opts and $opts->{"src"}
!ll&&!rl&&r
000
MC/DC0%0/2
$opts and $opts->{"src"}
$opts $opts->{"src"}
Truth table0%0/3
A and B
A= $optsB= $opts->{"src"}
ABresult
0X0
100
111
15  
16 ## Get the command line - basically to check if already running under debugger
17 0 my @cmdLine = `ps -o args $$`;
18  
19 ## Invoke debugger only when not already running under one
20 0 ▶ if ($cmdLine[1] !~ /-d/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $cmdLine[1] =~ /-d/) 0 0 0
21 0 my $rcFile;
22  
23 0 ▶ unless((open $rcFile, ">.perldb") && do {$RCFileLoc = '.perldb';}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $rcFile, ">.perldb" and do { $RCFileLoc = ".perldb" }) 0 0 0
Condition0%0/3
open $rcFile, ">.perldb" and do { $RCFileLoc = ".perldb" }
!ll&&!rl&&r
000
MC/DC0%0/2
open $rcFile, ">.perldb" and do { $RCFileLoc = ".perldb" }
open $rcFile, ">.perldb" do { $RCFileLoc = ".perldb" }
Truth table0%0/3
A and B
A= open $rcFile, ">.perldb"B= do { $RCFileLoc = ".perldb" }
ABresult
0X0
100
111
24 0 ▶ (open $rcFile, ">$ENV{HOME}/.perldb") && do {$RCFileLoc = "$ENV{HOME}/.perldb";}
Branch: 1 branch
BranchTrueFalseTotal
open $rcFile, qq[>$ENV{"HOME"}/.perldb] and do { $RCFileLoc = qq[$ENV{"HOME"}/.perldb] } or croak("Could not open .perldb for writing") 0 0 0
Condition0%0/3
open $rcFile, qq[>$ENV{"HOME"}/.perldb] and do { $RCFileLoc = qq[$ENV{"HOME"}/.perldb] }
!ll&&!rl&&r
000
MC/DC0%0/2
open $rcFile, qq[>$ENV{"HOME"}/.perldb] and do { $RCFileLoc = qq[$ENV{"HOME"}/.perldb] }
open $rcFile, qq[>$ENV{"HOME"}/.perldb] do { $RCFileLoc = qq[$ENV{"HOME"}/.perldb] }
Truth table0%0/3
A and B
A= open $rcFile, qq[>$ENV{"HOME"}/.perldb]B= do { $RCFileLoc = qq[$ENV{"HOME"}/.perldb] }
ABresult
0X0
100
111
25 || croak "Could not open .perldb for writing";
26 }
27  
28 0 setParseOptions($rcFile,$opts);
29  
30 0 ▶ if ($src) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src) 0 0 0
31 0 my $srcFile;
32 0 ▶ open $srcFile, "<$src" || croak "Could not open $src for writing";
Condition0%0/2
"<$src" || croak("Could not open $src for writing")
l!l
00
MC/DC0%0/1
"<$src" || croak("Could not open $src for writing")
"<$src"
Truth table0%0/2
A
A= "<$src"
Aresult
11
00
33  
34 0 setSourceFile($rcFile, $srcFile);
35  
36 0 close $srcFile;
37 }
38
39 0 close($rcFile);
40  
41 ## All settings done, run with debugger now
42 0 exec "$^X -d $0 @ARGV";
43 }
44 }
45  
46 ## Set parse_options parameters in rc file.
47 ## Can be used for writing free form debug customizations also
48 0 ▶sub setParseOptions {
Subroutine setParseOptions: not called CC=13 SCAR=52.0
Pod undocumented
49 0 my ($rc, $opt) = @_;
50  
51 ## All the rc file content provided as free form text
52 0 ▶ if ($opt && defined $opt->{freecontent}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt and defined $opt->{'freecontent'}) { } 0 0 0
Condition0%0/3
$opt and defined $opt->{"freecontent"}
!ll&&!rl&&r
000
MC/DC0%0/2
$opt and defined $opt->{"freecontent"}
$opt defined $opt->{"freecontent"}
Truth table0%0/3
A and B
A= $optB= defined $opt->{"freecontent"}
ABresult
0X0
100
111
53 0 print $rc $opt->{freecontent};
54 }
55 else {
56 ## parse_options string provided verbatim
57 0 ▶ if ($opt && defined $opt->{parseoptions}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt and defined $opt->{'parseoptions'}) { } 0 0 0
Condition0%0/3
$opt and defined $opt->{"parseoptions"}
!ll&&!rl&&r
000
MC/DC0%0/2
$opt and defined $opt->{"parseoptions"}
$opt defined $opt->{"parseoptions"}
Truth table0%0/3
A and B
A= $optB= defined $opt->{"parseoptions"}
ABresult
0X0
100
111
58 0 print $rc "parse_options($$opt{parseoptions});\n";
59 }
60 else {
61 ## User specified or default
62 0 ▶ my $interActive = ($opt && defined $opt->{interactive})?$opt->{interactive}:1;
Branch: 1 branch
BranchTrueFalseTotal
$opt && defined $opt->{'interactive'} ? : 0 0 0
Condition0%0/3
$opt and defined $opt->{"interactive"}
!ll&&!rl&&r
000
MC/DC0%0/2
$opt and defined $opt->{"interactive"}
$opt defined $opt->{"interactive"}
Truth table0%0/3
A and B
A= $optB= defined $opt->{"interactive"}
ABresult
0X0
100
111
63 0 ▶ my $outputFile = ($opt && defined $opt->{outputfile})?$opt->{outputfile}:'db.out';
Branch: 1 branch
BranchTrueFalseTotal
$opt && defined $opt->{'outputfile'} ? : 0 0 0
Condition0%0/3
$opt and defined $opt->{"outputfile"}
!ll&&!rl&&r
000
MC/DC0%0/2
$opt and defined $opt->{"outputfile"}
$opt defined $opt->{"outputfile"}
Truth table0%0/3
A and B
A= $optB= defined $opt->{"outputfile"}
ABresult
0X0
100
111
64 0 ▶ my $autoTrace = ($opt && defined $opt->{autotrace})?$opt->{autotrace}:1;
Branch: 1 branch
BranchTrueFalseTotal
$opt && defined $opt->{'autotrace'} ? : 0 0 0
Condition0%0/3
$opt and defined $opt->{"autotrace"}
!ll&&!rl&&r
000
MC/DC0%0/2
$opt and defined $opt->{"autotrace"}
$opt defined $opt->{"autotrace"}
Truth table0%0/3
A and B
A= $optB= defined $opt->{"autotrace"}
ABresult
0X0
100
111
65 0 ▶ my $frame = ($opt && defined $opt->{frame})?$opt->{frame}:6;
Branch: 1 branch
BranchTrueFalseTotal
$opt && defined $opt->{'frame'} ? : 0 0 0
Condition0%0/3
$opt and defined $opt->{"frame"}
!ll&&!rl&&r
000
MC/DC0%0/2
$opt and defined $opt->{"frame"}
$opt defined $opt->{"frame"}
Truth table0%0/3
A and B
A= $optB= defined $opt->{"frame"}
ABresult
0X0
100
111
66  
67 0 print "Info :: Debug outputs can be obtained in $outputFile\n";
68  
69 0 print $rc "parse_options(\"NonStop=$interActive LineInfo=$outputFile AutoTrace=$autoTrace frame=$frame\");","\n";
70 }
71 }
72 }
73  
74 ## Sets debug commands to a file
75 ## to be fed to debugger while running
76 0 ▶sub setSourceFile {
Subroutine setSourceFile: not called CC=3 SCAR=24.8
Pod undocumented
77 0 my ($rc, $src) = @_;
78  
79 ## Read from source file line by line
80 0 my $srcCmdStr;
81 0 ▶ while (my $line = <$src>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $src)) 0 0 0
82 0 chomp $line;
83 0 $srcCmdStr .= "'$line',";
84 }
85  
86 ## RC file directive to feed to @DB::typeahead
87 0 ▶ if($srcCmdStr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($srcCmdStr) 0 0 0
88 0 $srcCmdStr =~ s/,$//;
89 0 print $rc "sub afterinit { push \@DB::typeahead,$srcCmdStr;}","\n";
90 }
91 }
92  
93 ## Call this if you want to clean up the rc files
94 0 ▶sub end {
Subroutine end: not called CC=4 SCAR=30.0
Pod undocumented
95 ## Clean up of RC files - call end() optionally
96 0 foreach my $rcFile ("$ENV{HOME}/.perldb",".perldb") {
97 0 ▶ if (-f $rcFile) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $rcFile) 0 0 0
98 0 print "Warning :: Removing $rcFile\n";
99 0 ▶ unlink $rcFile || croak "Error :: Could not remove $rcFile\n";
Condition0%0/2
$rcFile || croak("Error :: Could not remove $rcFile\n")
l!l
00
MC/DC0%0/1
$rcFile || croak("Error :: Could not remove $rcFile\n")
$rcFile
Truth table0%0/2
A
A= $rcFile
Aresult
11
00
100 }
101 }
102 }
103  
104 1 1;
105  
106 =head1 NAME
107  
108 Debug::Runopt - Customize how to run debugger
109 Specify configurable debug options as part of rc file ie .perldb or ~/.perldb under Unix.
110 Specify runtime debug commands into a file and source to debugger
111 - works for interactive/non interactive both modes
112
113 =head1 SYNOPSIS
114  
115 use Debug::Runopt;
116  
117 Debug::Runopt::init();
118  
119 - Initializes debugger with a few default parse options eg.
120 NonStop=1 LineInfo=db.out AutoTrace=1 frame=6
121 No source command file given, debugger goes through normal execution flow.
122  
123 Debug::Runopt::init({'src'=>'tmp.cmd'});
124  
125 - Default parameters for parse_options, commands read from tmp.cmd
126  
127 Debug::Runopt::init({'src'=>'tmp.cmd', 'interactive' => 0, 'outputfile' => 'debug.out',
128 'autotrace' => 0, 'frame' => 2});
129  
130 - Sets parse_options as NonStop=0 LineInfo=debug.out AutoTrace=0 frame=2
131  
132 Debug::Runopt::init({'src'=>'tmp.cmd','parseoptions' => 'blah blah'});
133  
134 - Sets parse_options("blah blah");
135  
136 Debug::Runopt::init({'freecontent' => 'free form text blah blah....'});
137
138 - Writes 'free form text blah blah' to rc file as is.
139 Care should be taken while passing content like this.
140  
141 Debug::Runopt::end();
142  
143 - This can be optionally called at the end of the debuuged program
144 if rc files created needs to be cleaned up
145
146 =head1 ABSTRACT
147  
148 This module tries to make debugging easy by letting user specify configurable
149 options particulary when running in non-interactive mode.
150  
151 Apart from the configurable options, a source can be created on the run with user
152 specified contents and run with debugger.
153  
154 =head1 METHODS
155  
156 init() :: public
157  
158 - Initializes configurable options and rc file if any.
159  
160 end() :: public
161  
162 - Cleans up rc files created during execution if any.
163  
164 setParseOptions :: private
165  
166 - Sets up parse_options and other configurable directives.
167  
168 setSourceFile :: private
169  
170 - Creates rc file if required.
171  
172 =head1 CAVEATS
173  
174 It works only for a few versions of Unix/Linux.
175 Further improvement plan involves avoiding creating of rc file and using debug hooks.
176  
177 =head1 Similar Modules
178  
179 Debug::Simple
180  
181 =head1 SUPPORT
182  
183 debashish@cpan.org
184  
185 =head1 ACKNOWLEDGEMENTS
186  
187 =head1 COPYRIGHT & LICENSE
188  
189 Copyright 2013 Debashish Parasar, all rights reserved.
190  
191 This program is free software; you can redistribute it and/or modify it
192 under the same terms as Perl itself.
193  
194 =cut
195
↑ Summary Runopt.c »