develooper Front page | perl.perl5.porters | Postings from September 2023

Re: Native stack traces?

Thread Previous | Thread Next
Dave Mitchell
September 8, 2023 16:52
Re: Native stack traces?
Message ID:
On Fri, Sep 08, 2023 at 04:52:16PM +0100, Paul "LeoNerd" Evans wrote:
> At least for me, stacktraces fall below the criteria to bother spending
> a lot of time on just at the moment, when there's so many other things
> to be done. But of course that's not to stop anyone else who wishes to
> have a go.

On the other hand, the current 'package DB; @args = @DB::args' is a
horrible hack. Perhaps once signatures have been properly implemented
(with the original args kept on the stack rather than quietly being copied
into @_ and accessed from there) it might be time to revisit the official
API that perl provides to access the stack args, and come up up with
something better? For example something that can extract the names of
named parameters.

But I think creating exception objects which can have stack trace info
attached to them or similar, is a tad ambitious.


This quick-n-dirty sample code creates a full stack trace about 75,000
times per second on my laptop. It sanitises the output by just listing the
class or type of the arg, such as,


It shows that such code can be small and fast.

Here goes:

    package Foo;

    f(bless({}, 'Foo'), [], qw(here's some args for f));

    sub f { my $self = shift; $self->g(qw(args for g)) }
    sub g { my $self = shift; $self->h(qw(h's args))   }
    sub h {
        my $self = shift;
        for my $i (1..100_000) {

    sub dump_stack {
        my @c;
        my $i = 0;
        my @frames;
        package DB;
        while (@c = caller(++$i)) {
            my @args = map ref ? ref : ref \$_, @DB::args;
            push @frames, "$c[1]:$c[2] $c[3](" . join(', ', @args) .  ")\n";

The Enterprise successfully ferries an alien VIP from one place to another
without serious incident.
    -- Things That Never Happen in "Star Trek" #7

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About