File Coverage

blib/lib/Debug/Runopt.pm
Criterion Covered Total %
statement 9 49 18.3
branch 0 26 0.0
condition 0 33 0.0
subroutine 3 7 42.8
pod 0 4 0.0
total 12 119 10.0


line stmt bran cond sub pod time code
1             package Debug::Runopt;
2              
3 1     1   37075 use strict;
  1         3  
  1         43  
4 1     1   4 use warnings;
  1         2  
  1         34  
5 1     1   6 use Carp qw/croak/;
  1         6  
  1         1233  
6              
7             our $VERSION = '1.01';
8             our $RCFileLoc;
9              
10             sub init {
11 0     0 0   my ($opts) = @_;
12              
13             ## Source file for debug commands, if provided
14 0 0 0       my $src = ($opts && $opts->{src})?$opts->{src}:undef;
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 0         if ($cmdLine[1] !~ /-d/) {
21 0           my $rcFile;
22              
23 0 0 0       unless((open $rcFile, ">.perldb") && do {$RCFileLoc = '.perldb';}) {
  0            
24 0 0 0       (open $rcFile, ">$ENV{HOME}/.perldb") && do {$RCFileLoc = "$ENV{HOME}/.perldb";}
  0            
25             || croak "Could not open .perldb for writing";
26             }
27              
28 0           setParseOptions($rcFile,$opts);
29              
30 0 0         if ($src) {
31 0           my $srcFile;
32 0   0       open $srcFile, "<$src" || croak "Could not open $src for writing";
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             sub setParseOptions {
49 0     0 0   my ($rc, $opt) = @_;
50              
51             ## All the rc file content provided as free form text
52 0 0 0       if ($opt && defined $opt->{freecontent}) {
53 0           print $rc $opt->{freecontent};
54             }
55             else {
56             ## parse_options string provided verbatim
57 0 0 0       if ($opt && defined $opt->{parseoptions}) {
58 0           print $rc "parse_options($$opt{parseoptions});\n";
59             }
60             else {
61             ## User specified or default
62 0 0 0       my $interActive = ($opt && defined $opt->{interactive})?$opt->{interactive}:1;
63 0 0 0       my $outputFile = ($opt && defined $opt->{outputfile})?$opt->{outputfile}:'db.out';
64 0 0 0       my $autoTrace = ($opt && defined $opt->{autotrace})?$opt->{autotrace}:1;
65 0 0 0       my $frame = ($opt && defined $opt->{frame})?$opt->{frame}:6;
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             sub setSourceFile {
77 0     0 0   my ($rc, $src) = @_;
78              
79             ## Read from source file line by line
80 0           my $srcCmdStr;
81 0           while (my $line = <$src>) {
82 0           chomp $line;
83 0           $srcCmdStr .= "'$line',";
84             }
85              
86             ## RC file directive to feed to @DB::typeahead
87 0 0         if($srcCmdStr) {
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             sub end {
95             ## Clean up of RC files - call end() optionally
96 0     0 0   foreach my $rcFile ("$ENV{HOME}/.perldb",".perldb") {
97 0 0         if (-f $rcFile) {
98 0           print "Warning :: Removing $rcFile\n";
99 0   0       unlink $rcFile || croak "Error :: Could not remove $rcFile\n";
100             }
101             }
102             }
103              
104             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