blib/lib/Devel/Trepan/CmdProcessor/Command/Disassemble.pm untested

filter: Statementstmt 0.0% 0 / 297 Branchbran 0.0% 0 / 116 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 0.0% 0 / 33 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 515 CC 57 SCAR 47.6
CC
57
Cov
0%
markup_basic
240.0
markup_tree_terse
182.0
run
182.0
CRAP 117.2
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2011-2012, 2015 Rocky Bernstein <rocky@cpan.org>
3 use warnings; no warnings 'redefine';
4  
5 use rlib '../../../..';
6  
7 # Our local modules
8  
9 package Devel::Trepan::CmdProcessor::Command::Disassemble;
10  
11 ## FIXME:: Make conditional
12 use Syntax::Highlight::Perl::Improved ':FULL';
13 use Devel::Trepan::DB::Colors;
14 use Devel::Trepan::DB::LineCache;
15  
16 my $perl_formatter = Devel::Trepan::DB::Colors::setup();
17  
18 use Getopt::Long qw(GetOptionsFromArray);
19 use B::Concise qw(set_style);
20  
21 use if !@ISA, Devel::Trepan::CmdProcessor::Command ;
22  
23 use constant ALIASES => qw(disasm);
24 use constant CATEGORY => 'data';
25 use constant SHORT_HELP => 'Disassemble subroutine(s)';
26 use constant MIN_ARGS => 0; # Need at least this many
27 use constant MAX_ARGS => undef; # Need at most this many - undef -> unlimited.
28 use constant NEED_STACK => 0;
29  
30 # OPf_ flags from Perl's op.h
31 # Note weird bit combiniation 3, 'want list', we have to handle separately
32  
33 sub interpret_flags($)
34 {
35 my $op_flags = shift;
36 my @OP_FLAGS = (
37 'want void',
38 'want scalar',
39 'want kids',
40 'parenthesized',
41 'reference',
42 'modify lvalue',
43 'arg stacked',
44 'special',
45 );
46 my @flags_str = ();
47 if ( ($op_flags & 0b11) == 0b11 ) {
48 push @flags_str, 'want list';
49 $op_flags ^= 0b11;
50 }
51 while ($op_flags) {
52 my $flag_str = shift @OP_FLAGS;
53 unshift(@flags_str, $flag_str) if $op_flags & 1;
54 $op_flags /= 2;
55 }
56 return @flags_str ? (': ' . join(', ', @flags_str)) : '';
57 }
58  
59 use strict;
60  
61 use vars qw(@ISA $DEFAULT_OPTIONS);
62 @ISA = qw(Devel::Trepan::CmdProcessor::Command);
63  
64 use vars @CMD_VARS; # Value inherited from parent
65  
66 $DEFAULT_OPTIONS = {
67 line_style => 'terse',
68 order => '-basic',
69 tree_style => '-ascii',
70 };
71  
72 our $NAME = set_name();
73 =head2 Synopsis
74  
75 =cut
76 our $HELP = <<'HELP';
77 =pod
78  
79 B<disassemble> [I<options>] [I<subroutine>|I<package-name> ...]
80  
81 I<options>:
82  
83 [-]-no-highlight
84 [-]-highight={plain|dark|light}
85 [-]-concise
86 [-]-basic
87 [-]-terse
88 [-]-linenoise
89 [-]-debug
90 [-]-compact
91 [-]-exec
92 [-]-tree
93 [-]-loose
94 [-]-vt
95 [-]-ascii
96 [-]-from <line-number>
97 [-]-to <line-number>
98  
99 Disassembles the Perl interpreter OP tree using L<B::Concise>.
100  
101 Flags C<-from> and C<-to> respectively exclude lines less than or
102 greater that the supplied line number. If no C<-to> value is given
103 and a subroutine or package is not given then the C<-to> value is
104 taken from the "listsize" value as a count, and the C<-from> value is
105 the current line.
106  
107 Use L<C<set max list>|Devel::Trepan::CmdProcessor::Set::Max::List> or
108 L<C<show max list>|Devel::Trepan::CmdProcessor::Show::Max::List> to
109 see or set the number of lines to list.
110  
111 C<-no-highlight> will turn off syntax highlighting. C<-highlight=dark> sets for a dark
112 background, C<light> for a light background and C<plain> is the same as C<-no-highlight>.
113  
114  
115 Other flags are are the corresponding I<B::Concise> flags and that
116 should be consulted for their meaning.
117  
118 =head2 Examples:
119  
120 disassemble # disassemble the curren tline for listsize lines
121 dissasm # default alias; same as above
122 disasm -from 15 -to 19 # disassmble lines 15-19
123 disasm main::fib # disassemble the main::fib() subroutine
124 disasm fib.pl # disassemble file fib.pl
125 disassm -debug -exec # block style, rather than tree, like B::Debug
126  
127  
128 =head2 See also:
129  
130 L<C<list>|Devel::Trepan::CmdProcessor::Command::List>, and
131 L<C<deparse>|Devel::Trepan::CmdProcessor::Command::Deparse>, L<C<set
132 highlight>|Devel::Trepan::CmdProcessor::Set::Highlight>, L<C<set max
133 list>|Devel::Trepan::CmdProcessor::Set::Max::List>, and L<C<show max
134 list>|Devel::Trepan::CmdProcessor::Show::Max::List>.
135  
136 =cut
137 HELP
138  
139 sub complete($$)
140 {
141 no warnings 'once';
142 my ($self, $prefix) = @_;
143 my @subs = keys %DB::sub;
144 my @opts = (qw(-concise -terse -linenoise -debug -basic -exec -tree
145 -compact -loose -vt -ascii -from -to),
146 @subs);
147 Devel::Trepan::Complete::complete_token(\@opts, $prefix) ;
148 }
149  
150 sub parse_options($$)
151 {
152 my ($self, $args) = @_;
153 my $opts = $DEFAULT_OPTIONS;
154 my $result = &GetOptionsFromArray(
155 $args,
156 '-concise' => sub { $opts->{line_style} = 'concise'},
157 '-terse' => sub { $opts->{line_style} = 'terse'},
158 '-linenoise' => sub { $opts->{line_style} = 'linenoise'},
159 '-debug' => sub { $opts->{line_style} = 'debug'},
160 # FIXME: would need to check that ENV vars B_CONCISE_FORMAT, B_CONCISE_TREE_FORMAT
161 # and B_CONCISE_GOTO_FORMAT are set
162 # '-env' => sub { $opts->{line_style} = 'env'},
163  
164 '-basic' => sub { $opts->{order} = '-basic'; },
165 '-exec' => sub { $opts->{order} = '-exec'; },
166 '-tree' => sub { $opts->{order} = '-tree'; },
167  
168 '-compact' => sub { $opts->{tree_style} = '-compact'; },
169 '-loose' => sub { $opts->{tree_style} = '-loose'; },
170 '-vt' => sub { $opts->{tree_style} = '-vt'; },
171 '-ascii' => sub { $opts->{tree_style} = '-ascii'; },
172 '-highlight=s' => \$opts->{highlight},
173 '-no-highlight' => sub { $opts->{highlight} = 0; },
174 'from=i' => \$opts->{from},
175 'to=i' => \$opts->{to},
176 #'addr=s' => \$opts->{addr},
177 );
178 $opts->{highlight} = 0 if defined($opts->{highlight}) && $opts->{highlight} eq 'plain';
179 $opts;
180 }
181  
182 sub highlight_string($)
183 {
184 my ($string) = shift;
185 $perl_formatter->reset();
186 $string = $perl_formatter->format_string($string);
187 chomp $string;
188 $string;
189 }
190  
191 sub markup_basic($$$$$)
192 {
193 my ($lines, $highlight, $proc, $from, $to) = @_;
194 my @lines = split /\n/, $lines;
195 my $current_line = 0;
196 my @newlines = ();
197 # use Enbugger 'trepan'; Enbugger->stop;
198 my $check_hex_str;
199 if ($proc->{frame}{addr}) {
200 $check_hex_str = sprintf "0x%x", $proc->{frame}{addr};
201 }
202 my $filename = $proc->{frame}{file};
203 foreach (@lines) {
204 my $marker = ' ';
205 if (/^#(\s+)(\d+):(\s+)(.+)$/) {
206 my ($space1, $lineno, $space2, $perl_code) = ($1, $2, $3, $4);
207 $current_line = $lineno;
208 my $marked = $perl_code;
209 if ($perl_code eq '-src not supported for -e' ||
210 $perl_code eq '-src unavailable under -e') {
211 my $opts = {
212 output => $highlight,
213 max_continue => 5,
214 };
215 $marked = getline($filename, $lineno, $opts);
216 $_ = "#${space1}${lineno}:${space2}$marked" if $marked;
217 } else {
218 # print "FOUND line $lineno\n";
219 if ($highlight) {
220 $marked = highlight_string($perl_code);
221 $_ = "#${space1}${lineno}:${space2}$marked";
222 }
223 }
224 ## FIXME: move into DB::Breakpoint and adjust List.pm
225 if (exists($DB::dbline{$lineno}) and
226 my $brkpts = $DB::dbline{$lineno}) {
227 my $found = 0;
228 for my $bp (@{$brkpts}) {
229 if (defined($bp)) {
230 $marker = sprintf('%s%02d ', $bp->icon_char, $bp->id);
231 $found = 1;
232 last;
233 }
234 }
235 }
236 ## FIXME move above code
237  
238 } elsif (/^(\s+op_flags\s+)(\d+)$/) {
239 # Interpret flag string
240 my $flag = $2;
241 my $bin_flag_str = sprintf '%07b', $flag;
242 $bin_flag_str = $perl_formatter->format_token($bin_flag_str, 'Number') if
243 $highlight;
244 $_ = sprintf "%s%s%s", $1, $bin_flag_str, interpret_flags($flag);
245 } elsif (/^([A-Z]+) \((0x[0-9a-f]+)\)/) {
246 my ($op, $hex_str) = ($1, $2);
247 # print "FOUND $op, $hex_str\n";
248 if ($check_hex_str && $check_hex_str eq $hex_str) {
249 $marker = '=> ';
250 $marker = $proc->bolden($marker) if $highlight;
251 }
252 if ($highlight) {
253 $op = $perl_formatter->format_token($op, 'Subroutine');
254 $hex_str = $perl_formatter->format_token($hex_str, 'Label');
255 $_ = "$op ($hex_str)";
256 }
257  
258 }
259 $_ = $marker . $_;
260 next if $current_line < $from or $current_line > $to;
261 push @newlines, $_;
262 }
263 return join("\n", @newlines);
264 }
265  
266 sub markup_tree($$$$$)
267 {
268 my ($lines, $highlight, $proc, $from, $to) = @_;
269 my @lines = split /\n/, $lines;
270 my $current_line = 0;
271 my @newlines = ();
272 # use Enbugger 'trepan'; Enbugger->stop;
273 my $addr = $proc->{frame}{addr};
274 my $filename = $proc->{frame}{file};
275 my $check_hex_str;
276 if ($proc->{frame}{addr}) {
277 $check_hex_str = sprintf "0x%x", $proc->{frame}{addr};
278 }
279 foreach (@lines) {
280 my $marker = ' ';
281 if (/^(.*)\|-#(\s+)(\d+):(.+)$/) {
282 my ($prefix, $space, $lineno, $perl_code) = ($1, $2, $3, $4);
283 my $marked = $perl_code;
284 # FIXME: DRY code with markup_basic
285 if ($perl_code =~
286 /-src (?:(?:not supported for)|(?:unavailable under)) -e/) {
287 my $opts = {
288 output => $highlight,
289 max_continue => 5,
290 };
291 my $filename = $proc->{frame}{file};
292 $marked = DB::LineCache::getline($filename, $lineno, $opts);
293 $_ = "${prefix}|-#${space}${lineno}: $marked";
294 } else {
295 # print "FOUND line $lineno\n";
296 if ($highlight) {
297 $marked = highlight_string($perl_code);
298 $_ = "${prefix}|-#${space}${lineno}: $marked";
299 }
300 }
301 ## END above FIXME
302 ## FIXME: move into DB::Breakpoint and adjust List.pm
303 if (exists($DB::dbline{$lineno}) and
304 my $brkpts = $DB::dbline{$lineno}) {
305 my $found = 0;
306 for my $bp (@{$brkpts}) {
307 if (defined($bp)) {
308 $marker = sprintf('%s%02d ', $bp->icon_char, $bp->id);
309 $found = 1;
310 last;
311 }
312 }
313 }
314 ## FIXME move above code
315 } elsif (/^((?:[ |`])*-?)(0x[0-9a-f]+)(.*)$/) {
316 my ($space, $hex_str, $rest) = ($1, $2, $3);
317 if ($check_hex_str && /$check_hex_str/) {
318 $marker = '=> ';
319 $marker = $proc->bolden($marker) if $highlight;
320 }
321 if ($highlight) {
322 $hex_str = $perl_formatter->format_token($hex_str, 'Label');
323 $_ = "${space}${hex_str}${rest}";
324 }
325 }
326 $_ = $marker . $_;
327 next if $current_line < $from or $current_line > $to;
328 push @newlines, $_;
329 }
330 return join("\n", @newlines);
331 }
332  
333 sub markup_tree_terse($$$$$)
334 {
335 my ($lines, $highlight, $proc, $from, $to) = @_;
336 my @lines = split /\n/, $lines;
337 my $current_line = 0;
338 my @newlines = ();
339 # use Enbugger; Enbugger->stop;
340 my $addr = $proc->{frame}{addr};
341 my $check_hex_str;
342 if ($proc->{frame}{addr}) {
343 $check_hex_str = sprintf "0x%x", $proc->{frame}{addr};
344 }
345 my $filename = $proc->{frame}{file};
346 foreach (@lines) {
347 my $marker = ' ';
348 if (/^(\s*)# (\d+):(.+)$/) {
349 my ($space, $lineno, $perl_code) = ($1, $2, $3, $4);
350 $current_line = $lineno;
351 my $marked = $perl_code;
352 # FIXME: DRY code with markup_basic
353 if ($perl_code =~
354 /-src (?:(?:not supported for)|(?:unavailable under)) -e/) {
355 my $opts = {
356 output => $highlight,
357 max_continue => 5,
358 };
359 my $filename = $proc->{frame}{file};
360 $marked = DB::LineCache::getline($filename, $lineno, $opts);
361 } else {
362 # print "FOUND line $lineno\n";
363 $marked = highlight_string($perl_code) if $highlight;
364 }
365 my $lineno_str = $highlight ?
366 $perl_formatter->format_token($lineno, 'Number') : $lineno ;
367 $_ = "${space}#${lineno_str}:$marked";
368 ## END above FIXME
369 ## FIXME: move into DB::Breakpoint and adjust List.pm
370 if (exists($DB::dbline{$lineno}) and
371 my $brkpts = $DB::dbline{$lineno}) {
372 my $found = 0;
373 for my $bp (@{$brkpts}) {
374 if (defined($bp)) {
375 $marker = sprintf('%s%02d ', $bp->icon_char, $bp->id);
376 $found = 1;
377 last;
378 }
379 }
380 }
381 ## FIXME move above code
382 } elsif (/^(\s*)([A-Z]+) \((0x[0-9a-f]+)\) (\w+) (.*)$/) {
383 my ($space, $op, $hex_str, $name, $rest) = ($1, $2, $3, $4, $5);
384 if ($check_hex_str && $check_hex_str eq $hex_str) {
385 $marker = '=> ';
386 $marker = $proc->bolden($marker) if $highlight;
387 }
388 if ($highlight) {
389 $hex_str = $perl_formatter->format_token($hex_str, 'Label');
390 $op = $perl_formatter->format_token($op, 'Subroutine');
391 $name = $perl_formatter->format_token($name, 'Builtin_Function');
392 }
393 $_ = "${space}${op} ($hex_str) $name ${rest}";
394 }
395  
396 $_ = $marker . $_;
397 next if $current_line < $from or $current_line > $to;
398 push @newlines, $_;
399  
400 }
401 return join("\n", @newlines);
402 }
403  
404 sub do_one($$$$)
405 {
406 my ($proc, $title, $options, $args) = @_;
407 no strict 'refs';
408 $proc->section($title);
409 my $walker = B::Concise::compile($options->{order}, '-src', @{$args});
410 B::Concise::set_style_standard($options->{line_style});
411 B::Concise::walk_output(\my $buf);
412 $walker->(); # walks and renders into $buf;
413 if ('terse' eq $options->{line_style}) {
414 $buf = markup_tree_terse($buf, $options->{highlight}, $proc,
415 $options->{from}, $options->{to});
416 } elsif ('-tree' eq $options->{order}) {
417 $buf = markup_tree($buf, $options->{highlight}, $proc, $options->{from},
418 $options->{to});
419 } elsif ('-basic' eq $options->{order}) {
420 $buf = markup_basic($buf, $options->{highlight}, $proc, $options->{from},
421 $options->{to});
422 }
423 $proc->msg($buf);
424 }
425  
426 sub run($$)
427 {
428 my ($self, $args) = @_;
429 my @args = @$args;
430 shift @args;
431 my $proc = $self->{proc};
432 $DEFAULT_OPTIONS->{from} = $DEFAULT_OPTIONS->{to} =
433 $DEFAULT_OPTIONS->{highlight} = undef;
434 my $options = parse_options($self, \@args);
435 $options->{highlight} = $proc->{settings}{highlight} unless
436 defined($options->{highlight});
437 $perl_formatter = Devel::Trepan::DB::Colors::setup($options->{highlight})
438 if $options->{highlight};
439  
440 if (scalar(@args)) {
441 $options->{from} = 0 unless defined($options->{from});
442 $options->{to} = 100000 unless defined($options->{to});
443 } else {
444 $options->{from} = $proc->{frame}{line} unless defined($options->{from});
445 $options->{to} = $DEFAULT_OPTIONS->{from} + $proc->{settings}{maxlist} unless
446 defined($options->{to});
447 if ($proc->funcname && $proc->funcname ne 'DB::DB') {
448 push @args, $proc->funcname;
449 } else {
450 do_one($proc, "Package Main", $options, ['-main']);
451 }
452 }
453 unless (scalar(@args)) {
454 }
455  
456 for my $disasm_unit (@args) {
457 no strict 'refs';
458 if (%{$disasm_unit.'::'}) {
459 do_one($proc, "Package $disasm_unit", $options,
460 ["-stash=$disasm_unit"]);
461 } elsif ($proc->is_method($disasm_unit)) {
462 do_one($proc, "Subroutine $disasm_unit", $options, [$disasm_unit]);
463 } elsif (-r $disasm_unit) {
464 do_one($proc, "File $disasm_unit", $options, [$disasm_unit]);
465 } else {
466 $proc->errmsg("Don't know $disasm_unit as a package or function");
467 }
468 }
469 }
470  
471  
472 # Demo it
473 unless (caller) {
474  
475 for my $flags (0, 1, 2, 3, 0b1101, 0b100011) {
476 printf "%07b: %s\n", $flags, interpret_flags($flags);
477 }
478 require Devel::Trepan::CmdProcessor;
479 eval { use Devel::Callsite; use B; };
480 my $proc = Devel::Trepan::CmdProcessor->new(undef, 'bogus');
481 my $root_cv = B::main_root;
482 $proc->{frame} = {
483 line => __LINE__ - 1,
484 file => __FILE__,
485 fn => 'DB::DB',
486 pkg => __PACKAGE__,
487 addr => $$root_cv,
488 };
489 $proc->{settings}{highlight} = 1;
490 my $cmd = __PACKAGE__->new($proc);
491 $cmd->run([$NAME, '-terse', '--highlight=dark']);
492 print '=' x 50, "\n";
493 # print '=' x 50, "\n";
494 # $cmd->run([$NAME, '-basic', '--highlight']);
495 # print '=' x 50, "\n";
496 # $cmd->run([$NAME, '-basic', '--highlight', '-from', 10, '-to', 20]);
497 # print '=' x 50, "\n";
498 # $cmd->run([$NAME, '-basic', '--no-highlight', '-to', 5]);
499 # print '=' x 50, "\n";
500 # $cmd->run([$NAME, '-basic', '--highlight', '-from', __LINE__-25]);
501 }
502  
503 1;