blib/lib/B/DeparseTree/Fragment.pm untested

filter: Statementstmt 0.0% 0 / 247 Branchbran 0.0% 0 / 100 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 435 CC 50 SCAR 56.2
CC
50
Cov
0%
extract_node_info
702.0
dump
30.0
dump_relations
30.0
CRAP 274.6
1 package B::DeparseTree::Fragment;
2  
3 use B::DeparseTree;
4 use strict; use warnings;
5 use Data::Printer;
6 use vars qw(@ISA @EXPORT);
7 @ISA = ('Exporter');
8 @EXPORT = qw(deparse_offset
9 dump
10 extract_node_info
11 get_addr_info
12 get_parent_addr_info get_parent_op
13 get_prev_addr_info
14 get_prev_info
15 trim_line_pair
16 underline_parent
17 );
18  
19 sub deparse_offset
20 {
21 my ($funcname, $address) = @_;
22  
23 my $deparse = B::DeparseTree->new();
24 if ($funcname eq "DB::DB") {
25 $deparse->main2info;
26 } else {
27 $deparse->coderef2info(\&$funcname);
28 }
29 get_addr_info($deparse, $address);
30 }
31  
32 sub get_addr($$)
33 {
34 my ($deparse, $addr) = @_;
35 return undef unless $addr;
36 return $deparse->{optree}{$addr};
37 }
38  
39 sub get_addr_info($$)
40 {
41 my ($deparse, $addr) = @_;
42 my $op_info = get_addr($deparse, $addr);
43 return $op_info;
44 }
45  
46 sub get_parent_op($)
47 {
48 my ($op_info) = @_;
49 return undef unless $op_info;
50 my $deparse = $op_info->{deparse};
51  
52 # FIXME:
53 return $deparse->{ops}{$op_info->{addr}}{parent};
54 }
55  
56 sub get_parent_addr_info($)
57 {
58 my ($op_info) = @_;
59 my $deparse = $op_info->{deparse};
60 # FIXME
61 # my $parent_op = get_parent_op($op_info);
62 my $parent_addr = $op_info->{parent};
63 return undef unless $parent_addr;
64 return $deparse->{optree}{$parent_addr};
65 }
66  
67 sub get_prev_info($);
68 sub get_prev_info($)
69 {
70 my ($op_info) = @_;
71 return undef unless $op_info;
72 return $op_info->{prev_expr}
73 }
74  
75 sub get_prev_addr_info($);
76 sub get_prev_addr_info($)
77 {
78 my ($op_info) = @_;
79 return undef unless $op_info;
80 if (!exists $op_info->{prev_expr}) {
81 my $parent_info = get_parent_addr_info($op_info);
82 if ($parent_info) {
83 return get_prev_addr_info($parent_info);
84 } else {
85 return undef;
86 }
87 }
88 return $op_info->{prev_expr}
89 }
90  
91 sub underline_parent($$$) {
92 my ($child_text, $parent_text, $char) = @_;
93 my $start_pos = index($parent_text, $child_text);
94 return (' ' x $start_pos) . ($char x length($child_text));
95  
96 }
97 # Return either 2 or 3 strings in an array reference.
98 # There are various cases to consider.
99 # 1. Child and parent texts are no more than a single line:
100 # return and the underline, two entries. For example:
101 # my ($a, $b) = (5, 6);
102 # -----
103 # 2. The parent spans more than a line but the child is
104 # on that line. Return an array of the first line of the parent
105 # with elision and the child underline, two entries. Example
106 # if the child is $a in:
107 # if ($a) {
108 # $b
109 # }
110 # return:
111 # if ($a) {...
112 # --
113 # -----
114 # 3. The parent spans more than a line and the child is
115 # not that line. Return an array of the first line of the parent
116 # with elision, then the line containing the child and the child underline,
117 # three entries. Example:
118 # if the child is $b in:
119 # if ($a) {
120 # $b;
121 # $c;
122 # }
123 # return:
124 # if ($a) {...
125 # $b;
126 # --
127  
128 # 4. The parent spans more than a line and the child is
129 # not that line and also spans more than a single line.
130 # Do the same as 3. but add eplises to the underline.
131 # Example:
132 # if the child is "\$b;\n \$c" in:
133 # if ($a) {
134 # $b;
135 # $c;
136 # }
137 # return:
138 # if ($a) {...
139 # $b;
140 # ---...
141 # 5. Like 4, but the child is on the first line. A cross between
142 # 3 and 4. No elipses for the first line is needed, just one on the
143 # underline
144 #
145 sub trim_line_pair($$$$) {
146 my ($parent_text, $child_text, $parent_underline, $start_pos) = @_;
147 # If the parent text is longer than a line, use just the line.
148 # The underline indicator adds an elipsis to show it is elided.
149 my @parent_lines = split(/\n/, $parent_text);
150 my $i = 0;
151 if (scalar(@parent_lines) > 1) {
152 for ($i=0; $start_pos > length($parent_lines[$i]); $i++) {
153 my $l = length($parent_lines[$i]);
154 $start_pos -= ($l+1);
155 $parent_underline = substr($parent_underline, $l+1);
156 }
157 }
158 my @result = ();
159 if ($i > 0) {
160 push @result, $parent_lines[0] . '...';
161 }
162 my $stripped_parent = $parent_lines[$i];
163 my @child_lines = split(/\n/, $child_text);
164 if (scalar(@child_lines) > 1) {
165 $parent_underline = substr($parent_underline, 0, length($child_lines[0])+1) . '...';
166 }
167  
168 push @result, $stripped_parent, $parent_underline;
169 return \@result;
170 }
171  
172 # FIXME: this is a mess
173  
174 # return an ARRAY ref to strings that can be joined with "\n" to
175 # give a position in the text. Here are some examples
176 # after joining
177 #
178 # my ($x, $y) = (1, 2)
179 # ------
180 # my ($x, $y) = (1, 2)
181 # ~~~~~
182 # my ($x, $y) = (1, 2)
183 # -
184 # (1, 2)
185 #
186 # if ($x) { ...
187 # my ($x, $y = (1, 2)
188 # -------------------
189 #
190 # if ($x) { ...
191 # my ($x, $y = (1, 2)...
192 # -------------------
193  
194 # When we can, we text in the context of the surrounding
195 # text which is obtained by going up levels of the tree
196 # form the instruction. We might have to go a few levels
197 # up the tree before we find a text that spans more than
198 # a single line. In the fourth case where we don't
199 # have an underline but simply have "(1, 2)" that means
200 # were unable to get the parent text.
201  
202 # We hope that in the normal case, using the place holders in the
203 # format specifiers, we can know for sure where a child fits in as
204 # that child's node is stored in the parent as some "texts"
205 # entry. However this isn't always possible right now. So in the
206 # second example where "~" was used instead of "-", to
207 # indicate that the result was obtained the result by string matching
208 # rather than by exact node matching inside the parent.
209 # We can also use the "|" instead for instructions that really
210 # don't have an equivalent concept in the source code, so we've
211 # artificially tagged a location that is reasonable. "pushmark"
212 # and "padrange" instructions would be in this category.
213 #
214 # In the last two examples, we show how we do elision. The ...
215 # in the parent text means that we have only given the first line
216 # of the parent text along with the line that the child fits in.
217 # if there is an elision in the child text it means that that
218 # spans more than one line.
219  
220 sub extract_node_info($)
221 {
222 my ($info) = @_;
223  
224 my $child_text = $info->{text};
225 my $parent_text = undef;
226 my $candidate_pair = undef;
227 my $marked_position = undef;
228  
229 # Some opcodes like pushmark , padrange, and null,
230 # don't have an well-defined correspondence to a string in the
231 # source code, so we have made a somewhat arbitrary association fitting
232 # into the parent string. Examples of such artificial associations are
233 # the function name part of call, or an open brace of a scope.
234 # You can tell these nodes because they have a "position" field.
235 if (exists $info->{position}) {
236 my $found_pos = $info->{position};
237 $marked_position = $found_pos;
238 $parent_text = $child_text;
239 $child_text = substr($parent_text,
240 $found_pos->[0], $found_pos->[1]);
241 my $parent_underline = ' ' x $found_pos->[0];
242 $parent_underline .= '|' x $found_pos->[1];
243 $candidate_pair = trim_line_pair($parent_text, $child_text,
244 $parent_underline,
245 $found_pos->[0]);
246  
247 }
248  
249 my $parent = $info->{parent} ? $info->{parent} : undef;
250 unless ($parent) {
251 return $candidate_pair ? $candidate_pair : [$child_text];
252 }
253  
254 my $child_addr = $info->{addr};
255 my $deparsed = $info->{deparse};
256 my $parent_info = $deparsed->{optree}{$parent};
257  
258 unless ($parent_info) {
259 return $candidate_pair ? $candidate_pair : [$child_text];
260 }
261  
262 my $separator = exists $parent_info->{sep} ? $parent_info->{sep} : '';
263 my @texts = exists $parent_info->{texts} ? @{$parent_info->{texts}} : ($parent_info->{text});
264 my $parent_line = '';
265 my $text_len = $#texts;
266 my $result = '';
267  
268 if (!exists $parent_info->{fmt}
269 and scalar(@texts) == 1
270 and eval{$texts[0]->isa("B::DeparseTree::TreeNode")}) {
271 $parent_info = $texts[0];
272 }
273 if (exists $parent_info->{fmt} || exists $parent_info->{position}) {
274 # If the child text is the same as the parent's, go up the parent
275 # chain until we find something different.
276 while ($parent_info->{text} eq $child_text
277 && $parent_info->{parent}
278 && $deparsed->{optree}{$parent_info->{parent}}
279 ) {
280 last if ! exists $deparsed->{optree}{$parent_info->{parent}};
281 $parent_info = $deparsed->{optree}{$parent_info->{parent}};
282 }
283 my $fmt = $parent_info->{fmt};
284 my $indexes = $parent_info->{indexes};
285 my $args = $parent_info->{texts};
286 my ($str, $found_pos) = $deparsed->template_engine($fmt, $indexes, $args,
287 $child_addr);
288  
289 # Keep gathering parent text until we have at least one full line.
290 while (index($str, "\n") == -1 && $parent_info->{parent}) {
291 $child_addr = $parent_info->{addr};
292 last if ! exists $deparsed->{optree}{$parent_info->{parent}};
293 $parent_info = $deparsed->{optree}{$parent_info->{parent}};
294 $fmt = $parent_info->{fmt};
295 $indexes = $parent_info->{indexes};
296 $args = $parent_info->{texts};
297 my ($next_str, $next_found_pos) = $deparsed->template_engine($fmt,
298 $indexes, $args,
299 $child_addr);
300 last unless $next_found_pos;
301 my $nl_pos = index($next_str, "\n");
302 while ($nl_pos >= 0 and $nl_pos < $next_found_pos->[0]) {
303 $next_str = substr($next_str, $nl_pos+1);
304 $next_found_pos->[0] -= ($nl_pos+1);
305 $nl_pos = index($next_str, "\n");
306 }
307 $str = $next_str;
308 if ($found_pos) {
309 $found_pos->[0] += $next_found_pos->[0];
310 } else {
311 $found_pos = $next_found_pos;
312 }
313 }
314  
315 if (defined($found_pos)) {
316 my $parent_underline;
317 if ($marked_position) {
318 $parent_underline = ' ' x ($found_pos->[0] + $marked_position->[0]);
319 $parent_underline .= '|' x $marked_position->[1];
320 } else {
321 $parent_underline = ' ' x $found_pos->[0];
322 $parent_underline .= '-' x $found_pos->[1];
323 }
324 return trim_line_pair($str, $child_text, $parent_underline, $found_pos->[0]);
325 }
326 $result = $str;
327 } else {
328 for (my $i=0; $i <= $text_len; $i++) {
329 my $text = $texts[$i];
330 $result .= $separator if $result;
331  
332 if (ref($text)) {
333 if (ref($text) eq 'ARRAY' and (scalar(@$text) == 2)) {
334 if ($text->[1] == $child_addr) {
335 $child_text = $text->[0];
336 my $parent_underline = ' ' x length($result);
337 $result .= $text->[0];
338 $parent_underline .= '-' x length($text->[0]);
339 if ($i < $text_len) {
340 $result .= $separator;
341 my @remain_texts = @texts[$i+1..$#texts];
342 my $tail = $deparsed->combine2str($separator, \@remain_texts);
343 $result .= $tail;
344 }
345 return trim_line_pair($result, $child_text, $parent_underline, 0);
346 } else {
347 $result .= $text->[0];
348 }
349 } elsif ($text->{addr} == $child_addr) {
350 my $parent_underline = ' ' x length($result);
351 $result .= $text->{text};
352 $parent_underline .= '-' x length($text->{text});
353 if ($i < $text_len) {
354 $result .= $separator;
355 my @remain_texts = @texts[$i+1..$#texts];
356 my $tail = $deparsed->combine2str($separator, \@remain_texts);
357 $result .= $tail;
358 }
359 return trim_line_pair($result, $child_text, $parent_underline, 0);
360 } else {
361 $result .= $text->{text};
362 }
363 } else {
364 $result .= $text;
365 }
366 }
367 }
368 # Can't find by node address info, so just try to find the string
369 # inside of the parent.
370 $parent_text = $parent_info->{text};
371 my $start_index = index($parent_text, $child_text);
372 if ($start_index >= 0) {
373 if (index($parent_text, $child_text, $start_index+1) < 0) {
374 # It is in there *uniquely*!
375 my $parent_underline = underline_parent($child_text, $parent_text, '~');
376 return trim_line_pair($parent_text, $child_text, $parent_underline, $start_index);
377 }
378 }
379 }
380  
381 # Dump out full information of a node in relation to its
382 # parent
383 sub dump($) {
384 my ($deparse_tree) = @_;
385 my @addrs = sort keys %{$deparse_tree->{optree}};
386 for (my $i=0; $i < $#addrs; $i++) {
387 printf("%d: %s\n", $i, ('=' x 50));
388 my $info = get_addr_info($deparse_tree, $addrs[$i]);
389 if ($info) {
390 printf "0x%0x\n", $addrs[$i];
391 p $info ;
392 }
393 if ($info->{parent}) {
394 my $parent = get_parent_addr_info($info);
395 if ($parent) {
396 p $parent ;
397 my $texts = extract_node_info($info);
398 if ($texts) {
399 print join("\n", @$texts), "\n";
400 }
401 }
402 }
403 printf("%d: %s\n", $i, ('=' x 50));
404 }
405 }
406  
407 # Dump out essention information of a node in relation to its
408 # parent
409 sub dump_relations($) {
410 my ($deparse_tree) = @_;
411 my @addrs = sort keys %{$deparse_tree->{optree}};
412 for (my $i=0; $i < $#addrs; $i++) {
413 my $info = get_addr_info($deparse_tree, $addrs[$i]);
414 next unless $info && $info->{parent};
415 my $parent = get_parent_addr_info($info);
416 next unless $parent;
417 printf("%d: %s\n", $i, ('=' x 50));
418 print "Child info:\n";
419 printf "\taddr: 0x%0x, parent: 0x%0x\n", $addrs[$i], $parent->{addr};
420 printf "\top: %s\n", $info->{op}->can('name') ? $info->{op}->name : $info->{op} ;
421 printf "\ttext: %s\n\n", $info->{text};
422 # p $parent ;
423 my $texts = extract_node_info($info);
424 if ($texts) {
425 print join("\n", @$texts), "\n";
426 }
427 printf("%d: %s\n", $i, ('=' x 50));
428 }
429 }
430  
431 sub dump_tree($$);
432  
433 # Dump out the entire texts in tree format
434 sub dump_tree($$) {
435 my ($deparse_tree, $info) = @_;
436 if (ref($info) and (ref($info->{texts}) eq 'ARRAY')) {
437 foreach my $child_info (@{$info->{texts}}) {
438 if (ref($child_info)) {
439 if (ref($child_info) eq 'ARRAY') {
440 p $child_info;
441 } elsif (ref($child_info) eq 'B::DeparseTree::TreeNode') {
442 dump_tree($deparse_tree, $child_info)
443 } else {
444 printf "Unknown child_info type %s\n", ref($child_info);
445 p $child_info;
446 }
447 }
448 }
449 print '-' x 50, "\n";
450 }
451 p $info ;
452 print '=' x 50, "\n";
453 }
454  
455 unless (caller) {
456 sub bug() {
457 my ($a, $b) = @_;
458 ($a, $b) = ($b, $a) if ($a > $b);
459 # -((1, 2) x 2);
460 # no strict;
461 # for ( $i=0; $i;) {};
462 # my ($a, $b, $c);
463 # CORE::exec($foo $bar);
464 # exec $foo $bar;
465 # exec $foo $bar;
466 }
467  
468 my $child_text = '$foo $bar';
469 my $result = 'exec $foo $bar';
470 my $parent_underline = " ---------";
471 my $start_pos = 0;
472 my $lines = trim_line_pair($result, $child_text, $parent_underline,
473 $start_pos);
474 print join("\n", @$lines), "\n";
475  
476 my $deparse = B::DeparseTree->new();
477 use B;
478 $deparse->pessimise(B::main_root, B::main_start);
479 # my @addrs = sort keys %{$deparse->{ops}}, "\n";
480 # use Data::Printer;
481 # p @addrs;
482  
483 # my @info_addrs = sort keys %{$deparse->{optree}}, "\n";
484 # print '-' x 40, "\n";
485 # p @info_addrs;
486  
487 # $deparse->init();
488 # my $svref = B::svref_2object(\&bug);
489 # my $x = $deparse->deparse_sub($svref, $addrs[9]);
490 # my $x = $deparse->deparse_sub($svref);
491 # dump_tree($deparse, $x);
492  
493 # # my @info_addrs = sort keys %{$deparse->{optree}}, "\n";
494 # # print '-' x 40, "\n";
495 # # p @info_addrs;
496  
497 # #my $info = get_addr_info($deparse, $addrs[10]);
498 # # p $info;
499 # exit 0;
500  
501 $deparse->coderef2info(\&bug);
502 # $deparse->coderef2info(\&get_addr_info);
503 my @addrs = sort keys %{$deparse->{optree}}, "\n";
504 B::DeparseTree::Fragment::dump($deparse);
505  
506 # my ($parent_text, $pu);
507 # $parent_text = "now is the time";
508 # $child_text = 'is';
509 # $start_pos = index($parent_text, $child_text);
510 # $pu = underline_parent($child_text, $parent_text, '-');
511 # print join("\n", @{trim_line_pair($parent_text, $child_text,
512 # $pu, $start_pos)}), "\n";
513 # $parent_text = "if (\$a) {\n\$b\n}";
514 # $child_text = '$b';
515 # $start_pos = index($parent_text, $child_text);
516 # $pu = underline_parent($child_text, $parent_text, '-');
517 # print join("\n", @{trim_line_pair($parent_text, $child_text,
518 # $pu, $start_pos)}), "\n";
519  
520 # $parent_text = "if (\$a) {\n \$b;\n \$c}";
521 # $child_text = '$b';
522 # $start_pos = index($parent_text, $child_text);
523 # $pu = underline_parent($child_text, $parent_text, '-');
524 # print join("\n", @{trim_line_pair($parent_text, $child_text,
525 # $pu, $start_pos)}), "\n";
526 # $parent_text = "if (\$a) {\n \$b;\n \$c}";
527 # $child_text = "\$b;\n \$c";
528 # $start_pos = index($parent_text, $child_text);
529 # $pu = underline_parent($child_text, $parent_text, '-');
530 # print join("\n", @{trim_line_pair($parent_text, $child_text,
531 # $pu, $start_pos)}), "\n";
532 }
533  
534 1;