Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 9597cfe818
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 213 lines (186 sloc) 5.886 kb
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
# -*- coding: utf-8 -*-
# Copyright (C) 2011 Rocky Bernstein <rocky@cpan.org>
use strict; use warnings;
use lib '../../..';
use Devel::Trepan::DB::Sub;
use Devel::Trepan::Complete;

package Devel::Trepan::CmdProcessor;
use English;

sub adjust_frame($$$)
{
    my ($self, $frame_num, $absolute_pos) = @_;
    my $frame;
    ($frame, $frame_num) = $self->get_frame($frame_num, $absolute_pos);
    if ($frame) {
        $self->{frame} = $frame;
        $self->{frame_index} = $frame_num;
        unless ($self->{settings}{traceprint}) {
my $opts = {
basename => $self->{settings}{basename},
current_pos => $frame_num,
maxwidth => $self->{settings}{maxwidth},
};
$self->print_stack_trace_from_to($frame_num, $frame_num, $self->{frames}, $opts);
$self->print_location ;
}
$self->{list_line} = $self->line();
$self->{list_filename} = $self->filename();
        $self->{frame};
    } else {
        undef
    }
}

sub frame_complete($$;$)
{
    my ($self, $prefix, $direction) = @_;
    $direction //= 1;
    my ($low, $high) = $self->frame_low_high($direction);
    my @ary = ($low..$high);
    Devel::Trepan::Complete::complete_token(\@ary, $prefix);
}

sub frame_low_high($;$)
{
    my ($self, $direction) = @_;
    $direction //= 1;
    my $stack_size = $self->{stack_size};
    my ($low, $high) = (-$stack_size, $stack_size-1);
    ($low, $high) = ($high, $low) if ($direction < 0);
    return ($low, $high);
}

sub frame_setup($$$)
{
    my ($self, $frame_ary) = @_;

    my $stack_size = $DB::stack_depth;
    my $i=0;
    while (my ($pkg, $file, $line, $fn) = caller($i++)) {
last if 'DB::DB' eq $fn or ('DB' eq $pkg && 'DB' eq $fn);
    }
    if ($stack_size <= 0) {
# Dynamic debugging didn't set $DB::stack_depth correctly.
my $j=$i;
while (caller($j++)) {
$stack_size++;
}
$stack_size++;
$DB::stack_depth = $j;
    } else {
$stack_size -= ($i-3);
    }
    $self->{stack_size} = $stack_size;
    

    $self->{frames} = []; # place to cache frames
    $#{$self->{frames}} = $stack_size-1;

    my @frames = $self->{dbgr}->backtrace(1);
    $self->{frame_index} = 0;
    $self->{hide_level} = 0;
    $self->{frame} = $frames[0];
    $self->{list_line} = $self->line();
    $self->{list_filename} = $self->filename();
}

sub filename($)
{
    my $self = shift;
    $self->{frame}{file};
}

sub get_frame($$$)
{
    my ($self, $frame_num, $absolute_pos) = @_;
    my $stack_size = $self->{stack_size};

    if ($absolute_pos) {
        $frame_num += $stack_size if $frame_num < 0;
    } else {
        $frame_num += $self->{frame_index};
    }

    if ($frame_num < 0) {
        $self->errmsg('Adjusting would put us beyond the newest frame.');
        return (undef, undef);
    } elsif ($frame_num >= $stack_size) {
        $self->errmsg('Adjusting would put us beyond the oldest frame.');
        return (undef, undef);
    }

    my $frames = $self->{frames};
    unless ($frames->[$frame_num]) {
my @new_frames = $self->{dbgr}->backtrace(0);
$self->{frames}[$frame_num] = $new_frames[$frame_num];
    }
    $self->{frame} = $frames->[$frame_num];
    return ($self->{frame}, $frame_num);
}

sub line($)
{
    my $self = shift;
    $self->{frame}{line};
}

sub print_stack_entry()
{
    my ($self, $frame, $i, $prefix, $opts) = @_;
    $opts->{maxstack} //= 1e9;
    # Set the separator so arrays print nice.
    local $LIST_SEPARATOR = ', ';

    # Get the file name.
    my $file = $self->canonic_file($frame->{file});

    # Put in a filename header if short is off.
    $file = ($file eq '-e') ? $file : "file `$file'" unless $opts->{short};
    
    my $not_last_frame = $i != ($self->{stack_size}-1);
    my $s;
    my $args =
defined $frame->{args}
    ? "(@{ $frame->{args} })"
: '';
    if ($not_last_frame) {
# Grab and stringify the arguments if they are there.

# Shorten them up if $opts->{maxwidth} says they're too long.
$args = substr($args, 0, $opts->{maxwidth}-3) . '...'
if length($args) > $opts->{maxwidth};

# Get the actual sub's name, and shorten to $maxwidth's requirement.
$s = $frame->{fn};
$s = substr($s, 0, $opts->{maxwidth}-3) . '...'
if length($s) > $opts->{maxwidth};
    }
    
    # Short report uses trimmed file and sub names.
    my $wa = $frame->{wantarray};
    if ($opts->{short}) {
my $fn = $s; # @_ >= 4 ? $_[3] : $s;
$self->msg("$wa=$fn$args from $file:$frame->{line}");
    } else {
# Non-short report includes full names.
# Lose the DB::DB hook call if frame is 0.
my $call_str = $not_last_frame ?
"$frame->{wantarray} = $s$args in " : '';
my $prefix_call = "$prefix$call_str";
my $file_line = $self->canonic_file($file) . " at line $frame->{line}";

if (length($prefix_call) + length($file_line) <= $opts->{maxwidth}) {
$self->msg($prefix_call . $file_line);
} else {
$self->msg($prefix_call);
$self->msg("\t" . $file_line);
}
    }
}
    
sub print_stack_trace_from_to($$$$$)
{
    my ($self, $from, $to, $frames, $opts) = @_;
    for (my $i=$from; $i <= $to; $i++) {
my $prefix = ($i == $opts->{current_pos}) ? '-->' : ' ';
$prefix .= sprintf ' #%d ', $i;
$self->print_stack_entry($frames->[$i], $i, $prefix, $opts);
    }
}

# Print `count' frame entries
sub print_stack_trace($$$)
{
    my ($self, $frame, $opts)=@_;
    $opts ||= {maxstack=>1e9, count=>1e9};
    # $opts = DEFAULT_STACK_TRACE_SETTINGS.merge(opts);
    my $halfstack = $opts->{maxstack} / 2;
    my $n = scalar @{$frame};
    $n = $opts->{count} if $opts->{count} < $n;
    if ($n > ($halfstack * 2)) {
        $self->print_stack_trace_from_to(0, $halfstack-1, $frame, $opts);
my $msg = sprintf "... %d levels ...", ($n - $halfstack*2);
        $self->msg($msg);
$self->print_stack_trace_from_to($n - $halfstack, $n-1, $frame, $opts);
    } else {
$self->print_stack_trace_from_to(0, $n-1, $frame, $opts);
    }
}

1;
Something went wrong with that request. Please try again.