2 # ======================================================================
3 # log2xml - This script translates cacao -verbosecall output into a
4 # more readable XML format. It also separates the output of
5 # different threads and is able to ignore commonly called
6 # (uninteresting) methods.
9 # cacao -verbose -verbosecall ... | log2xml.pl
11 # For each thread the script writes a file LOG_{threadid}.xml.
13 # You can use any XML editor to browse the logs. If you want to use
14 # vim, which works well, take a look at log2xml.vim in this
17 # You may want to edit the opt_ignore options below.
19 # Author : Edwin Steiner
20 # Revision: $Id: log2xml.pl 2307 2005-04-15 09:06:54Z edwin $
23 # Revision 1.4 2005/04/15 09:06:54 edwin
24 # output more valid xml
26 # Revision 1.3 2005/04/14 20:11:04 edwin
29 # Revision 1.2 2005/04/14 20:10:20 edwin
30 # disabled debug print, added vim boilerplate
32 # Revision 1.1 2005/04/14 19:44:00 edwin
33 # added log2xml.pl and log2xml.vim
35 # ======================================================================
40 my @opt_ignorelist = (
41 'java.lang.Character.toLowerCase(C)C',
42 'java.lang.Character.toUpperCase(C)C',
43 'java.lang.String.endsWith(Ljava/lang/String;)Z',
44 'java.lang.String.equalsIgnoreCase(Ljava/lang/String;)Z',
45 'java.lang.String.equals(Ljava/lang/Object;)Z',
46 'java.lang.String.indexOf(I)I',
47 'java.lang.String.indexOf(II)I',
48 'java.lang.String.indexOf(Ljava/lang/String;)I',
49 'java.lang.String.indexOf(Ljava/lang/String;I)I',
50 'java.lang.String.lastIndexOf(Ljava/lang/String;)I',
51 'java.lang.String.lastIndexOf(Ljava/lang/String;I)I',
52 'java.lang.String.regionMatches(ILjava/lang/String;II)Z',
53 'java.lang.String.regionMatches(ZILjava/lang/String;II)Z',
54 'java.lang.String.replace(CC)Ljava/lang/String;',
55 'java.lang.String.startsWith(Ljava/lang/String;)Z',
56 'java.lang.String.substring(II)Ljava/lang/String;',
57 'java.lang.String.toLowerCase()Ljava/lang/String;',
58 'java.util.HashMap.get(Ljava/lang/Object;)Ljava/lang/Object;',
59 'java.util.HashMap.put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;',
60 'java.util.Hashtable.clone()Ljava/lang/Object;',
61 'java.util.Hashtable.get(Ljava/lang/Object;)Ljava/lang/Object;',
62 'java.util.Hashtable.put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;',
63 'java.util.jar.JarFile$EntryInputStream.read([BII)I',
66 my @opt_ignore_regexps = (
67 qr/^java\.lang\.Character\./,
68 qr/^java\.lang\.String\./,
69 qr/^java\.util\.jar\./,
70 qr/^java\.util\.Locale\./,
71 qr/^java\.util\.zip\./,
76 my $opt_no_text_for_ignored = 0;
77 my $opt_fileprefix = 'LOG_';
84 my %ignorehash = map { $_=>1 } @opt_ignorelist;
86 # ======================================================================
88 # ======================================================================
90 # Quote the given text for putting it in an XML file.
102 my ($thread,$xml) = @_;
103 my $file = $files{$thread};
107 sub write_xml_call_info ( $$ )
109 my ($thread,$node) = @_;
110 my $file = $files{$thread};
111 print $file ' return="' . quote($node->{RETURN}) . '"' if defined($node->{RETURN});
112 print $file ' except="' . quote($node->{EXCEPTION}) . '"' if defined($node->{EXCEPTION});
114 print $file ' details="' . quote($node->{DETAILS}) . '"' if defined($node->{DETAILS});
118 sub write_xml_frame ( $$ )
120 my ($thread,$node) = @_;
122 my $file = $files{$thread};
123 print $file '<frame name="' . quote($node->{NAME}) . '"';
124 write_xml_call_info($thread,$node);
128 sub write_xml_enter ( $$ )
130 my ($thread,$node) = @_;
132 my $file = $files{$thread};
133 print $file '<call name="' . quote($node->{NAME}) . '"';
134 write_xml_call_info($thread,$node);
138 sub write_xml_leave ( $$ )
140 my ($thread,$node) = @_;
142 my $file = $files{$thread};
143 print $file '<leave';
144 write_xml_call_info($thread,$node);
145 print $file ' name="' . quote($node->{NAME}) . '"';
147 print $file "</call>\n";
150 sub write_xml_never_returns ( $$ )
152 my ($thread,$node) = @_;
154 my $file = $files{$thread};
155 print $file '<never_returns';
156 print $file ' name="' . quote($node->{NAME}) . '"';
158 print $file "</call>\n";
161 sub write_xml_node ( $$ )
163 my ($thread,$node) = @_;
165 write_xml_enter($thread,$node);
166 write_xml_body($thread,$node);
167 write_xml_leave($thread,$node);
170 sub write_xml_body ( $$ )
172 my ($thread,$node) = @_;
174 for my $x (@{$node->{BODY}}) {
176 write_xml_node($thread,$x);
179 my $file = $files{$thread};
185 sub write_xml_header ( $ )
188 print $file '<?xml version="1.0"?>'."\n";
189 print $file "<thread>\n";
192 sub write_xml_end ( $ )
195 print $file "</thread>\n";
198 # ======================================================================
200 # ======================================================================
202 # Return true if functions with this name are ignored.
207 return 1 if $ignorehash{$name};
209 for my $re (@opt_ignore_regexps) {
210 return 1 if $name =~ $re;
216 # ======================================================================
217 # STACK RECONSTRUCTION
218 # ======================================================================
222 return {NAME => 'root',
231 sub thread_register ( $ )
234 unless (exists $stacks{$thread}) {
235 $stacks{$thread} = [make_root];
236 my $filename = $opt_fileprefix . $thread . '.xml';
237 $files{$thread} = IO::File->new(">$filename");
238 $ignore_level{$thread} = 0;
239 write_xml_header($files{$thread});
246 return $stacks{$thread}->[-1] or die "stack underflow";
252 return pop @{$stacks{$thread}} or die "stack underflow";
255 sub stack_push ( $$ )
257 my ($thread,$value) = @_;
258 push @{$stacks{$thread}},$value;
261 sub stack_slot ( $$ )
263 my ($thread,$index) = @_;
264 return $stacks{$thread}->[$index] or die "invalid stack index";
267 sub stack_write ( $ )
271 for (@{$stacks{$thread}}) {
272 print STDERR "\t".quote($_->{NAME})."\n";
276 sub process_call ( $$$ )
278 my ($thread,$keyword,$rest) = @_;
280 my $top = stack_top($thread);
282 $rest =~ /(\S+?) \( ([^)]*) \) ([^ \t(]+) (.*) $/x or die "could not match call log: $rest";
283 my ($n,$args,$rettype,$details) = ($1,$2,$3,$4);
284 my $name = "$n($args)$rettype";
286 my $call = {NAME => $name,SHORTNAME => $n,DETAILS => $details,BODY => [],LINE => $.};
288 $call->{FIRST} = 1 if $keyword eq '1st_call';
290 stack_push($thread,$call);
292 if ($ignore_level{$thread} == 0) {
293 write_xml_enter($thread,$call);
295 if (is_ignored($name)) {
296 $ignore_level{$thread}++;
300 $ignore_level{$thread}++;
304 sub process_leave ( $$$ )
306 my ($thread,$rest,$exception) = @_;
308 my $top = stack_pop($thread);
311 $top->{EXCEPTION} = $exception;
314 $rest =~ /(\S+?) (\([^)]*\))? (->(.*))?$/x or die "could not match finished log: $rest";
315 my ($name,$return) = ($1,$4);
317 $name eq $top->{NAME} or die "warning: mismatched leave:\n"
318 ."\t(line $.) $name from\n"
319 ."\t(line ".$top->{LINE}.") ".$top->{NAME}."\n";
321 $top->{RETURN} = defined($return) ? $return : 'void';
324 --$ignore_level{$thread} if $ignore_level{$thread} > 0;
326 if ($ignore_level{$thread} == 0) {
327 write_xml_leave($thread,$top);
331 sub process_exception ( $$$ )
333 my ($thread,$exception,$rest) = @_;
336 my $top = stack_top($thread);
338 if ($rest =~ /(\S+?) \([^)]*\) \((0x[^)]+)\)/x) {
339 ($name,$entry) = ($1,$2);
341 elsif ($rest =~ /call_java_method/) {
342 $name = "call_java_method";
346 die "could not match exception: $exception in $rest";
349 $top->{ENTRYPOINT} = $entry unless defined($top->{ENTRYPOINT});
351 if ($name eq $top->{SHORTNAME} && $entry eq $top->{ENTRYPOINT}) {
352 if ($ignore_level{$thread} == 0) {
353 my $handled = '<exception name="'.$exception.'"/>'."\n";
354 write_xml($thread,$handled);
356 return; # exception handled locally
359 # unwind a stack frame
361 while ($name ne 'call_java_method' && $name ne stack_slot($thread,-2)->{SHORTNAME}) {
362 stack_write($thread);
363 print STDERR "exception : $exception\n";
364 print STDERR "method : $name\n";
365 print STDERR "entrypoint: $entry\n";
366 print STDERR "scope : ".$top->{SHORTNAME}."\n";
367 print STDERR "scopeentry: ".$top->{ENTRYPOINT}."\n";
368 print STDERR "outer : ".stack_slot($thread,-2)->{SHORTNAME}."\n";
369 print STDERR "outerentry: ".stack_slot($thread,-2)->{ENTRYPOINT}."\n";
370 print STDERR "unwinding stack...\n";
372 warn "heuristic unwind: $exception in $rest";
373 process_leave($thread,$rest,$exception);
376 process_leave($thread,$rest,$exception);
379 sub process_call_log ( $$$ )
381 my ($thread,$keyword,$rest) = @_;
383 if ($keyword eq 'called' || $keyword eq '1st_call') {
384 process_call($thread,$keyword,$rest);
386 elsif ($keyword eq 'finished') {
387 process_leave($thread,$rest,undef);
390 die "invalid log keyword: $keyword";
394 sub process_text ( $$ )
396 my ($thread,$text) = @_;
398 # print STDERR "$.: $text\n";
400 if ($opt_no_text_for_ignored && $ignore_level{$thread} > 0) {
404 my $top = stack_top($thread);
406 my $file = $files{$thread};
407 print $file quote($text)."\n";
410 # ======================================================================
412 # ======================================================================
420 if (/LOG: \[(\S+)\]\s+(.*)/) {
421 my ($thread,$log) = ($1,$2);
422 thread_register($thread);
423 $lastthread = $thread;
424 if ($log =~ /(1st_call|called|finished):\s*(.*)/) {
425 process_call_log($thread,$1,$2);
427 elsif ($log =~ /Exception\s+(\S+)\s+thrown in\s+(.*)/) {
428 process_exception($thread,$1,$2);
431 process_text($thread,$log);
435 unless (defined($lastthread)) {
436 $lastthread = '(nil)';
437 thread_register($lastthread);
439 process_text($lastthread,$_);
446 warn "warning: omitting the rest of the input";
449 for my $thread (keys %stacks) {
450 my $ign = $ignore_level{$thread};
452 warn "warning: ignore_level not reset to 0 at end of input";
453 write_xml($thread,"\n<!-- LOG ENDS IN IGNORED METHOD - STACKTRACE: -->\n\n");
455 my $top = $stacks{$thread}->[-$ign];
456 write_xml_frame($thread,$top);
459 pop @{$stacks{$thread}} for 1..$ignore_level{$thread};
461 while (scalar @{$stacks{$thread}} > 1) {
462 my $top = $stacks{$thread}->[-1];
463 write_xml_never_returns($thread,$top);
464 pop @{$stacks{$thread}};
466 write_xml_end($files{$thread});
467 $files{$thread}->close();
469 print STDERR "processed $. lines\n";
474 # vim: noet ts=4 sw=4 ai