| 1 | # Copyright (C) 2011-2015 Rocky Bernstein <rocky@cpan.org> | ||
| 2 | use strict; | ||
| 3 | use Exporter; | ||
| 4 | use warnings; | ||
| 5 | no warnings 'redefine'; no warnings 'once'; | ||
| 6 | use rlib '../../..'; | ||
| 7 | # require_relative '../app/default' | ||
| 8 | |||
| 9 | package Devel::Trepan::CmdProcessor; | ||
| 10 | |||
| 11 | # Because we use Exporter we want to silence: | ||
| 12 | # Use of inherited AUTOLOAD for non-method ... is deprecated | ||
| 13 | # if ($] < 5.022) { | ||
| 14 | # eval { | ||
| 15 | # sub AUTOLOAD | ||
| 16 | # { | ||
| 17 | # my $name = our $AUTOLOAD; | ||
| 18 | # $name =~ s/.*:://; # lose package name | ||
| 19 | # my $target = "DynaLoader::$name"; | ||
| 20 | # goto &$target; | ||
| 21 | # }; | ||
| 22 | # } | ||
| 23 | # } | ||
| 24 | |||
| 25 | sub DESTROY{} | ||
| 26 | |||
| 27 | use English qw( -no_match_vars ); | ||
| 28 | use Cwd 'abs_path'; | ||
| 29 | |||
| 30 | use File::Basename; | ||
| 31 | use File::Spec; | ||
| 32 | use Devel::Trepan::DB::LineCache; | ||
| 33 | |||
| 34 | our $EVENT2ICON = { | ||
| 35 | 'brkpt' => 'xx', | ||
| 36 | 'call' => '->', | ||
| 37 | 'debugger-call' => ':o', | ||
| 38 | 'end' => '-|', | ||
| 39 | 'interrupt' => 'oo', | ||
| 40 | 'line' => '--', | ||
| 41 | 'post-mortem' => 'XX', | ||
| 42 | 'return' => '<-', | ||
| 43 | 'signal' => '!!', | ||
| 44 | 'tbrkpt' => 'x1', | ||
| 45 | 'terminated' => ':x', | ||
| 46 | 'trace' => '==', | ||
| 47 | 'unknown' => '?!', | ||
| 48 | 'watch' => 'wa' | ||
| 49 | }; | ||
| 50 | |||
| 51 | sub canonic_file($$;$) | ||
| 52 | { | ||
| 53 | my ($self, $filename, $resolve) = @_; | ||
| 54 | return undef unless defined $filename; | ||
| 55 | $resolve = 1 unless defined $resolve; | ||
| 56 | |||
| 57 | # For now we want resolved filenames | ||
| 58 | if ($self->{settings}{basename}) { | ||
| 59 | my $is_eval = filename_is_eval($filename); | ||
| 60 | return $is_eval ? $filename : (basename($filename) || $filename); | ||
| 61 | } elsif ($resolve) { | ||
| 62 | my $mapped_filename = map_file($filename); | ||
| 63 | $filename = $mapped_filename if defined($mapped_filename); | ||
| 64 | my $is_eval = filename_is_eval($filename); | ||
| 65 | return $is_eval ? $filename : (abs_path($filename) || $filename); | ||
| 66 | } else { | ||
| 67 | return $filename; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | sub min($$) { | ||
| 72 | my ($a, $b) = @_; | ||
| 73 | return $a < $b ? $a : $b; | ||
| 74 | } | ||
| 75 | |||
| 76 | # Return the text to the current source line. We use trace line | ||
| 77 | # information to try to retrieve all of the current source line up | ||
| 78 | # to some limit of lines. The lines returned may be colorized. | ||
| 79 | # Devel::Trepan::DB:LineCache::getline actually does the retrieval. | ||
| 80 | sub current_source_text(;$) | ||
| 81 | { | ||
| 82 | my ($self, $opts) = @_; | ||
| 83 | $opts = {maxlines => 5} unless defined $opts; | ||
| 84 | my $filename = $self->{frame}{file}; | ||
| 85 | my $line_number = $self->{frame}{line}; | ||
| 86 | my $text = (getline($filename, $line_number, $opts)) || ''; | ||
| 87 | chomp($text); | ||
| 88 | return $text; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub resolve_file_with_dir($$) | ||
| 92 | { | ||
| 93 | my ($self, $path_suffix) = @_; | ||
| 94 | my @dirs = @$self->{settings}{directory}; | ||
| 95 | for my $dir (split(/:/, @dirs)) { | ||
| 96 | if ('$cwd' eq $dir) { | ||
| 97 | $dir = `pwd`; | ||
| 98 | } elsif ('$cdir' eq $dir) { | ||
| 99 | $dir = $DB::OS_STARTUP_DIR; | ||
| 100 | } | ||
| 101 | next unless $dir && !-d ($dir); | ||
| 102 | my $try_file = File::Spec->catfile($dir, $path_suffix); | ||
| 103 | return $try_file if -f $try_file; | ||
| 104 | } | ||
| 105 | return undef; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub text_at($;$) | ||
| 109 | { | ||
| 110 | my ($self, $opts) = @_; | ||
| 111 | $opts = { | ||
| 112 | reload_on_change => $self->{settings}{reload}, | ||
| 113 | output => $self->{settings}{highlight}, | ||
| 114 | } unless defined $opts; | ||
| 115 | |||
| 116 | my $line_no = $self->line(); | ||
| 117 | my $text; | ||
| 118 | my $filename = $self->filename(); | ||
| 119 | if (filename_is_eval($filename)) { | ||
| 120 | if ($DB::filename eq $filename) { | ||
| 121 | { | ||
| 122 | # Some lines in @DB::line might not be defined. | ||
| 123 | # So we have to turn off strict here. | ||
| 124 | no warnings; | ||
| 125 | my $string = join("\n", @DB::dbline); | ||
| 126 | use warnings; | ||
| 127 | $filename = map_script($filename, $string); | ||
| 128 | $text = getline($filename, $line_no, $opts); | ||
| 129 | } | ||
| 130 | } | ||
| 131 | } else { | ||
| 132 | $text = line_at($filename, $line_no, $opts); | ||
| 133 | my ($map_file, $map_line) = | ||
| 134 | map_file_line($filename, $line_no); | ||
| 135 | } | ||
| 136 | $text; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub format_location($;$$$) | ||
| 140 | { | ||
| 141 | my ($self, $event, $frame, $frame_index) = @_; | ||
| 142 | $event = $self->{event} unless defined $event; | ||
| 143 | $frame = $self->{frame} unless defined $frame; | ||
| 144 | $frame_index = $self->{frame_index} unless defined $frame_index; | ||
| 145 | my $text = undef; | ||
| 146 | my $ev = ' '; | ||
| 147 | if (defined($self->{event}) && 0 == $frame_index) { | ||
| 148 | $ev = $EVENT2ICON->{$self->{event}}; | ||
| 149 | } | ||
| 150 | |||
| 151 | $self->{line_no} = $self->{frame}{line}; | ||
| 152 | |||
| 153 | my $loc = $self->source_location_info; | ||
| 154 | my $suffix = ($event eq 'return' && defined($DB::_[0])) ? " $DB::_[0]" : ''; | ||
| 155 | my $pkg = $self->{frame}{pkg} || '??' ; | ||
| 156 | "${ev} ${pkg}::(${loc})$suffix"; | ||
| 157 | } | ||
| 158 | |||
| 159 | sub print_location($;$) | ||
| 160 | { | ||
| 161 | my ($self,$opts) = @_; | ||
| 162 | $opts = { | ||
| 163 | output => $self->{settings}{highlight}, | ||
| 164 | max_continue => $self->{settings}{lines}, | ||
| 165 | } unless defined $opts; | ||
| 166 | my $loc = $self->format_location; | ||
| 167 | $self->msg(${loc}); | ||
| 168 | |||
| 169 | my $text = $self->current_source_text($opts); | ||
| 170 | if ($text) { | ||
| 171 | $self->msg($text, {unlimited => 1}); | ||
| 172 | } | ||
| 173 | } | ||
| 174 | |||
| 175 | sub source_location_info($) | ||
| 176 | { | ||
| 177 | my $self = shift; | ||
| 178 | # if (@frame.eval?) | ||
| 179 | my $canonic_filename; | ||
| 180 | # 'eval ' + safe_repr(@frame.eval_string.gsub("\n", ';').inspect, 20) | ||
| 181 | # else | ||
| 182 | my $filename = $self->{frame}{file}; | ||
| 183 | my $line_number = $self->line() || 0; | ||
| 184 | |||
| 185 | my $op_addr_str = ''; | ||
| 186 | $self->{op_addr} = undef; | ||
| 187 | if ($self->{settings}{displayop}) { | ||
| 188 | my $frame_index = $self->{frame_index}; | ||
| 189 | if ($DB::OP_addr && $frame_index == 0) { | ||
| 190 | $self->{op_addr} = $DB::OP_addr; | ||
| 191 | } else { | ||
| 192 | my $skip = DB::caller_levels_skip(); | ||
| 193 | my $addr = Devel::Callsite::callsite($frame_index + $skip); | ||
| 194 | $self->{op_addr} = $addr if defined $addr and $addr > 0; | ||
| 195 | } | ||
| 196 | $op_addr_str = sprintf(" \@0x%x", $self->{op_addr}) if $self->{op_addr} | ||
| 197 | } | ||
| 198 | if (filename_is_eval($filename)) { | ||
| 199 | ### FIXME: put this all into DB::LineCache | ||
| 200 | if ($DB::filename eq $filename) { | ||
| 201 | # Some lines in @DB::line might not be defined. | ||
| 202 | # So we have to turn off strict here. | ||
| 203 | if ($filename ne '-e') { | ||
| 204 | my $string = undef; | ||
| 205 | if (@DB::dbline) { | ||
| 206 | no warnings; | ||
| 207 | $string = join('', @DB::dbline); | ||
| 208 | use warnings; | ||
| 209 | } elsif ($filename =~/^sub (\S+)/) { | ||
| 210 | my $func = $1; | ||
| 211 | if (%SelfLoader::Cache) { | ||
| 212 | $string = $SelfLoader::Cache{$func}; | ||
| 213 | $string =~ s/^\n#line 1.+\n//; | ||
| 214 | } | ||
| 215 | } | ||
| 216 | unless (defined($string)) { | ||
| 217 | return "${filename}:${line_number}$op_addr_str"; | ||
| 218 | } | ||
| 219 | my $try_filename = map_script($filename, $string); | ||
| 220 | $filename = $try_filename if defined($try_filename); | ||
| 221 | } | ||
| 222 | return $self->filename() . | ||
| 223 | " remapped $filename:$line_number$op_addr_str"; | ||
| 224 | } | ||
| 225 | } | ||
| 226 | $canonic_filename = $self->canonic_file($self->filename(), 0) | ||
| 227 | || $filename; | ||
| 228 | return "${canonic_filename}:${line_number}$op_addr_str"; | ||
| 229 | } | ||
| 230 | |||
| 231 | unless (caller()) { | ||
| 232 | # Demo it. | ||
| 233 | require Devel::Trepan::CmdProcessor; | ||
| 234 | my $proc = Devel::Trepan::CmdProcessor->new; | ||
| 235 | eval <<'EOE'; | ||
| 236 | sub create_frame() { | ||
| 237 | my ($pkg, $file, $line, $fn) = caller(0); | ||
| 238 | return [ | ||
| 239 | { | ||
| 240 | file => $file, | ||
| 241 | fn => $fn, | ||
| 242 | line => $line, | ||
| 243 | pkg => $pkg, | ||
| 244 | }]; | ||
| 245 | } | ||
| 246 | EOE | ||
| 247 | my $frame_ary = create_frame(); | ||
| 248 | $proc->frame_setup($frame_ary); | ||
| 249 | $proc->{event} = 'return'; | ||
| 250 | print $proc->format_location, "\n"; | ||
| 251 | print $proc->current_source_text({output=>'plain'}), "\n"; | ||
| 252 | print $proc->current_source_text({output=>'term'}), "\n"; | ||
| 253 | # See if cached line is the same | ||
| 254 | print $proc->current_source_text({output=>'term'}), "\n"; | ||
| 255 | # Try unhighlighted line. | ||
| 256 | print $proc->current_source_text, "\n"; | ||
| 257 | |||
| 258 | # Now try an eval | ||
| 259 | $DB::filename = ''; | ||
| 260 | $frame_ary = eval "create_frame()"; | ||
| 261 | $proc->frame_setup($frame_ary); | ||
| 262 | $proc->{event} = 'line'; | ||
| 263 | print $proc->format_location, "\n"; | ||
| 264 | print $proc->current_source_text({output=>'plain'}), "\n"; | ||
| 265 | } | ||
| 266 | |||
| 267 | 1; |