File: Stacktrace.pm

package info (click to toggle)
libapp-stacktrace-perl 0.09-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 560 kB
  • sloc: perl: 297; ansic: 37; makefile: 3
file content (270 lines) | stat: -rw-r--r-- 5,765 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package App::Stacktrace;

=head1 NAME

App::Stacktrace - Stack trace

=head1 VERSION

version 0.09

=head1 SYNOPSIS

  perl-stacktrace [option] pid

    -m      Prints a gdb script
    -v      Verbose debugging
    -c      Additionally, prints C stacktrace
    --help  Show this help

    --exec  exec() into gdb

=head1 DESCRIPTION

perl-stacktrace prints Perl stack traces of Perl threads for a given
Perl process. For each Perl frame, the full file name and line number
are printed.

For example, a stack dump of a running perl program:

    $ ps x | grep cpan
    24077 pts/12   T      0:01 /usr/local/bin/perl /usr/local/bin/cpan
    24093 pts/12   S+     0:00 grep cpan

    $ perl-stacktrace 24077
    0x00d73416 in __kernel_vsyscall ()
    /usr/local/bin/cpan:11
    /usr/local/lib/perl5/5.12.2/App/Cpan.pm:364
    /usr/local/lib/perl5/5.12.2/App/Cpan.pm:295
    /usr/local/lib/perl5/5.12.2/CPAN.pm:339
    /usr/local/lib/perl5/5.12.2/Term/ReadLine.pm:198

=head1 API

There exists an internal API

=head2 new

This accepts the following parameters by applying them through
L<Getopt::Long>. This is actually just a front for the script
F<perl-stacktrace>'s command line handling.

  App::Stacktrace->new(
      $pid,       # The process to attach to
      'm',        # Dump the generated script
      'v',        # Verbose
      'exec',     # exec() into gdb
      '--noexec', # system() into gdb
  );

=head2 run

Runs the app program as configured by the C<< ->new(...) >> method.

    $obj = App::Stacktrace->new( ... );
    $obj->run;

=cut

use strict;
use Config ();
use English -no_match_vars;
use Getopt::Long ();
use Pod::Usage ();
use XSLoader ();
use File::Temp ();

our $VERSION = '0.09';

XSLoader::load(__PACKAGE__, $VERSION);

sub new {
    my $class = shift;
    my $self = {};
    bless $self, $class;

    $self->_read_arguments( @_ );

    return $self;
}

sub run {
    my $self = shift;

    if ($self->{help}) {
        Pod::Usage::pod2usage(
            -verbose => 2,
            -exitcode => 0,
        );
    }

    my $script = $self->_custom_generated_script;
    if ($self->{dump_script}) {
        print $script;
    }
    else {
        $self->_run_gdb($script);
    }

    return;
}

sub _read_arguments {
    my $self = shift;
    local @ARGV = @_;
    my %args;
    Getopt::Long::GetOptions(
         \ %args,
        'help',
        'm',
        'v',
        'c',
        'exec!',
    )
      or Pod::Usage::pod2usage(
        -verbose => 2,
        -exitcode => 2 );
    if (1 == @ARGV && $ARGV[0] =~ /^\d+$/) {
        $args{pid} = shift @ARGV;
    }
    if (@ARGV) {
        Pod::Usage::pod2usage( -verbose => 2, -exitcode => 2 );
    }

    $self->{help}        = $args{help};
    $self->{pid}         = $args{pid};
    $self->{dump_script} = $args{m};
    $self->{verbose}     = $args{v};
    $self->{c_backtrace} = $args{v} || $args{c};
    $self->{exec}        = $args{exec};

    return;
}


sub _custom_generated_script {
    my ($self) = @_;

    # TODO: generate this statically
    for my $dir ( @INC ) {
        my $file = "$dir/App/Stacktrace/perl_backtrace_raw.txt";
        if (-e $file) {
            return $self->_TODO_add_constants( $file );
        }
    }

    die "Can't locate perl-backtrace.txt in \@INC (\@INC contains: @INC)";
}

sub _TODO_add_constants {
    my ($self, $template_script) = @_;

    my $this_library = __FILE__;
    my $src = <<"TODO_preamble";
# !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
# This file is built by $this_library from its data.
# Any changes made here will be lost!
#
TODO_preamble

    if ($self->{verbose}) {
        $src .= <<VERBOSE;
set trace-commands on
set \$DEBUG = 1
VERBOSE
    }
    else {
        $src .= <<QUIET;
set \$DEBUG = 0
QUIET
    }

    my $offsets = App::Stacktrace::_perl_offsets();
    for my $name (sort keys %$offsets) {
        $src .= "set $name = $offsets->{$name}\n";
    }

    if ($Config::Config{usethreads}) {
        require threads;
        my $key = "threads::_pool$threads::VERSION";
        my $len = length $key;
        $src .= <<"THREADS";
set \$POOL_KEY = "$key"
set \$POOL_KEY_LEN = $len
THREADS
    }


    open my $template_fh, '<', $template_script
        or die "Can't open $template_script: $!";
    local $/;
    $src .= readline $template_fh;

    if ($self->{c_backtrace}) {
        $src .= <<"C_BACKTRACE";
backtrace
C_BACKTRACE
    }

    my $command = $self->_command_for_version;
    $src .= <<"INVOKE";
$command
detach
quit
INVOKE

    return $src;
}

sub _command_for_version {
    return
        $] >= 5.038     ? 'perl_backtrace_5_38_x' :
        $] >= 5.014     ? 'perl_backtrace_5_14_x' :
        $] >= 5.012     ? 'perl_backtrace_5_12_x' :
        $] >= 5.010     ? 'perl_backtrace_5_10_x' :
        $] >= 5.008_009 ? 'perl_backtrace_5_8_9'  :
        $] >= 5.008     ? 'perl_backtrace_5_8_x'  :
        die 'Support for perl-5.6 or earlier not implemented';
}

sub _run_gdb {
    my ($self, $src) = @_;

    # TODO: what are the failure modes of File::Temp?
    my $tmp = File::Temp->new(
        UNLINK => 0,
        SUFFIX => '.gdb',
    );
    my $file = $tmp->filename;

    print { $tmp } $src;
    $tmp->flush;
    $tmp->sync;

    if ($self->{verbose}) {
        print $src;
    }

    my @cmd = (
        'gdb',
            '-quiet',
            '-batch',
            '-nx',
            '-p', $self->{pid},
            '-x', $file,
    );
    if ($self->{exec}) {
        exec @cmd;
    }
    else {
        system @cmd;
        my $sig_num = $? & 127;
        my $core    = $? & 128;
        my $rc      = $? >> 8;

        warn "@cmd killed by signal $sig_num" if $sig_num;
        warn "@cmd core dumped" if $core;
    }
}

q{Bartender, I'll have a Gordon Freeman on the rocks, thanks.}