2 # ======================================================================
3 # log2xml - This script translates cacao -verbosecall ouput 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 2304 2005-04-14 19:44:00Z edwin $
23 # Revision 1.1 2005/04/14 19:44:00 edwin
24 # added log2xml.pl and log2xml.vim
26 # ======================================================================
31 my @opt_ignorelist = (
32 'java.lang.Character.toLowerCase(C)C',
33 'java.lang.Character.toUpperCase(C)C',
34 'java.lang.String.endsWith(Ljava/lang/String;)Z',
35 'java.lang.String.equalsIgnoreCase(Ljava/lang/String;)Z',
36 'java.lang.String.equals(Ljava/lang/Object;)Z',
37 'java.lang.String.indexOf(I)I',
38 'java.lang.String.indexOf(II)I',
39 'java.lang.String.indexOf(Ljava/lang/String;)I',
40 'java.lang.String.indexOf(Ljava/lang/String;I)I',
41 'java.lang.String.lastIndexOf(Ljava/lang/String;)I',
42 'java.lang.String.lastIndexOf(Ljava/lang/String;I)I',
43 'java.lang.String.regionMatches(ILjava/lang/String;II)Z',
44 'java.lang.String.regionMatches(ZILjava/lang/String;II)Z',
45 'java.lang.String.replace(CC)Ljava/lang/String;',
46 'java.lang.String.startsWith(Ljava/lang/String;)Z',
47 'java.lang.String.substring(II)Ljava/lang/String;',
48 'java.lang.String.toLowerCase()Ljava/lang/String;',
49 'java.util.HashMap.get(Ljava/lang/Object;)Ljava/lang/Object;',
50 'java.util.HashMap.put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;',
51 'java.util.Hashtable.clone()Ljava/lang/Object;',
52 'java.util.Hashtable.get(Ljava/lang/Object;)Ljava/lang/Object;',
53 'java.util.Hashtable.put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;',
54 'java.util.jar.JarFile$EntryInputStream.read([BII)I',
57 my @opt_ignore_regexps = (
58 qr/^java\.lang\.Character\./,
59 qr/^java\.lang\.String\./,
60 qr/^java\.util\.jar\./,
61 qr/^java\.util\.Locale\./,
62 qr/^java\.util\.zip\./,
67 my $opt_no_text_for_ignored = 0;
68 my $opt_fileprefix = 'LOG_';
75 my %ignorehash = map { $_=>1 } @opt_ignorelist;
77 # ======================================================================
79 # ======================================================================
81 # Quote the given text for putting it in an XML file.
93 my ($thread,$xml) = @_;
94 my $file = $files{$thread};
98 sub write_xml_call_info ( $$ )
100 my ($thread,$node) = @_;
101 my $file = $files{$thread};
102 print $file ' return="' . quote($node->{RETURN}) . '"' if defined($node->{RETURN});
103 print $file ' except="' . quote($node->{EXCEPTION}) . '"' if defined($node->{EXCEPTION});
105 print $file ' details="' . quote($node->{DETAILS}) . '"' if defined($node->{DETAILS});
109 sub write_xml_frame ( $$ )
111 my ($thread,$node) = @_;
113 my $file = $files{$thread};
114 print $file '<frame name="' . quote($node->{NAME}) . '"';
115 write_xml_call_info($thread,$node);
119 sub write_xml_enter ( $$ )
121 my ($thread,$node) = @_;
123 my $file = $files{$thread};
124 print $file '<call name="' . quote($node->{NAME}) . '"';
125 write_xml_call_info($thread,$node);
129 sub write_xml_leave ( $$ )
131 my ($thread,$node) = @_;
133 my $file = $files{$thread};
134 print $file '<leave';
135 write_xml_call_info($thread,$node);
136 print $file ' name="' . quote($node->{NAME}) . '"';
138 print $file "</call>\n";
141 sub write_xml_node ( $$ )
143 my ($thread,$node) = @_;
145 write_xml_enter($thread,$node);
146 write_xml_body($thread,$node);
147 write_xml_leave($thread,$node);
150 sub write_xml_body ( $$ )
152 my ($thread,$node) = @_;
154 for my $x (@{$node->{BODY}}) {
156 write_xml_node($thread,$x);
159 my $file = $files{$thread};
165 sub write_xml_header ( $ )
168 print $file '<?xml version="1.0"?>'."\n";
171 sub write_xml_end ( $ )
176 # ======================================================================
178 # ======================================================================
180 # Return true if functions with this name are ignored.
185 return 1 if $ignorehash{$name};
187 for my $re (@opt_ignore_regexps) {
188 return 1 if $name =~ $re;
194 # ======================================================================
195 # STACK RECONSTRUCTION
196 # ======================================================================
200 return {NAME => 'root',
209 sub thread_register ( $ )
212 unless (exists $stacks{$thread}) {
213 $stacks{$thread} = [make_root];
214 my $filename = $opt_fileprefix . $thread . '.xml';
215 $files{$thread} = IO::File->new(">$filename");
216 $ignore_level{$thread} = 0;
217 write_xml_header($files{$thread});
224 return $stacks{$thread}->[-1] or die "stack underflow";
230 return pop @{$stacks{$thread}} or die "stack underflow";
233 sub stack_push ( $$ )
235 my ($thread,$value) = @_;
236 push @{$stacks{$thread}},$value;
239 sub stack_slot ( $$ )
241 my ($thread,$index) = @_;
242 return $stacks{$thread}->[$index] or die "invalid stack index";
245 sub stack_write ( $ )
249 for (@{$stacks{$thread}}) {
250 print STDERR "\t".quote($_->{NAME})."\n";
254 sub process_call ( $$$ )
256 my ($thread,$keyword,$rest) = @_;
258 my $top = stack_top($thread);
260 $rest =~ /(\S+?) \( ([^)]*) \) ([^ \t(]+) (.*) $/x or die "could not match call log: $rest";
261 my ($n,$args,$rettype,$details) = ($1,$2,$3,$4);
262 my $name = "$n($args)$rettype";
264 my $call = {NAME => $name,SHORTNAME => $n,DETAILS => $details,BODY => [],LINE => $.};
266 $call->{FIRST} = 1 if $keyword eq '1st_call';
268 stack_push($thread,$call);
270 if ($ignore_level{$thread} == 0) {
271 write_xml_enter($thread,$call);
273 if (is_ignored($name)) {
274 $ignore_level{$thread}++;
278 $ignore_level{$thread}++;
282 sub process_leave ( $$$ )
284 my ($thread,$rest,$exception) = @_;
286 my $top = stack_pop($thread);
289 $top->{EXCEPTION} = $exception;
292 $rest =~ /(\S+?) (\([^)]*\))? (->(.*))?$/x or die "could not match finished log: $rest";
293 my ($name,$return) = ($1,$4);
295 $name eq $top->{NAME} or die "warning: mismatched leave:\n"
296 ."\t(line $.) $name from\n"
297 ."\t(line ".$top->{LINE}.") ".$top->{NAME}."\n";
299 $top->{RETURN} = defined($return) ? $return : 'void';
302 --$ignore_level{$thread} if $ignore_level{$thread} > 0;
304 if ($ignore_level{$thread} == 0) {
305 write_xml_leave($thread,$top);
309 sub process_exception ( $$$ )
311 my ($thread,$exception,$rest) = @_;
314 my $top = stack_top($thread);
316 if ($rest =~ /(\S+?) \([^)]*\) \((0x[^)]+)\)/x) {
317 ($name,$entry) = ($1,$2);
319 elsif ($rest =~ /call_java_method/) {
320 $name = "call_java_method";
324 die "could not match exception: $exception in $rest";
327 $top->{ENTRYPOINT} = $entry unless defined($top->{ENTRYPOINT});
329 if ($name eq $top->{SHORTNAME} && $entry eq $top->{ENTRYPOINT}) {
330 if ($ignore_level{$thread} == 0) {
331 my $handled = '<exception name="'.$exception.'"/>'."\n";
332 write_xml($thread,$handled);
334 return; # exception handled locally
337 # unwind a stack frame
339 while ($name ne 'call_java_method' && $name ne stack_slot($thread,-2)->{SHORTNAME}) {
340 stack_write($thread);
341 print STDERR "exception : $exception\n";
342 print STDERR "method : $name\n";
343 print STDERR "entrypoint: $entry\n";
344 print STDERR "scope : ".$top->{SHORTNAME}."\n";
345 print STDERR "scopeentry: ".$top->{ENTRYPOINT}."\n";
346 print STDERR "outer : ".stack_slot($thread,-2)->{SHORTNAME}."\n";
347 print STDERR "outerentry: ".stack_slot($thread,-2)->{ENTRYPOINT}."\n";
348 print STDERR "unwinding stack...\n";
350 warn "heuristic unwind: $exception in $rest";
351 process_leave($thread,$rest,$exception);
354 process_leave($thread,$rest,$exception);
357 sub process_call_log ( $$$ )
359 my ($thread,$keyword,$rest) = @_;
361 if ($keyword eq 'called' || $keyword eq '1st_call') {
362 process_call($thread,$keyword,$rest);
364 elsif ($keyword eq 'finished') {
365 process_leave($thread,$rest,undef);
368 die "invalid log keyword: $keyword";
372 sub process_text ( $$ )
374 my ($thread,$text) = @_;
376 print STDERR "$.: $text\n";
378 if ($opt_no_text_for_ignored && $ignore_level{$thread} > 0) {
382 my $top = stack_top($thread);
384 my $file = $files{$thread};
385 print $file quote($text)."\n";
388 # ======================================================================
390 # ======================================================================
398 if (/LOG: \[(\S+)\]\s+(.*)/) {
399 my ($thread,$log) = ($1,$2);
400 thread_register($thread);
401 $lastthread = $thread;
402 if ($log =~ /(1st_call|called|finished):\s*(.*)/) {
403 process_call_log($thread,$1,$2);
405 elsif ($log =~ /Exception\s+(\S+)\s+thrown in\s+(.*)/) {
406 process_exception($thread,$1,$2);
409 process_text($thread,$log);
413 unless (defined($lastthread)) {
414 $lastthread = '(nil)';
415 thread_register($lastthread);
417 process_text($lastthread,$_);
424 warn "warning: omitting the rest of the input";
427 for my $thread (keys %stacks) {
428 my $ign = $ignore_level{$thread};
430 warn "warning: ignore_level not reset to 0 at end of input";
431 write_xml($thread,"\n<!-- LOG ENDS IN IGNORED METHOD - STACKTRACE: -->\n\n");
433 my $top = $stacks{$thread}->[-$ign];
434 write_xml_frame($thread,$top);
438 $files{$thread}->close();
440 print STDERR "processed $. lines\n";