2 # $Id: tracemunch,v 1.26 2019/12/21 22:33:35 tom Exp $
3 ##############################################################################
4 # Copyright (c) 1998-2018,2019 Free Software Foundation, Inc. #
6 # Permission is hereby granted, free of charge, to any person obtaining a #
7 # copy of this software and associated documentation files (the "Software"), #
8 # to deal in the Software without restriction, including without limitation #
9 # the rights to use, copy, modify, merge, publish, distribute, distribute #
10 # with modifications, sublicense, and/or sell copies of the Software, and to #
11 # permit persons to whom the Software is furnished to do so, subject to the #
12 # following conditions: #
14 # The above copyright notice and this permission notice shall be included in #
15 # all copies or substantial portions of the Software. #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL #
20 # THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING #
22 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER #
23 # DEALINGS IN THE SOFTWARE. #
25 # Except as contained in this notice, the name(s) of the above copyright #
26 # holders shall not be used in advertising or otherwise to promote the sale, #
27 # use or other dealings in this Software without prior written #
29 ##############################################################################
30 # tracemunch -- compactify ncurses trace logs
32 # The error logs produced by ncurses with tracing enabled can be very tedious
33 # to wade through. This script helps by compacting runs of log lines that
34 # can be conveniently expressed as higher-level operations.
40 'PutAttrChar\(\{\{ ' . "'(.)'"
41 . ' = 0[0-7]+ \}\}\) at \(([0-9]+), ([0-9]+)\)';
43 'waddnstr\(0x([[:xdigit:]]+),"([^\"]+)",[0-9]+\) called \{A_NORMAL\}';
45 # If the trace is complete, we can infer addresses using the return value from
46 # newwin, etc. But if it is incomplete, we can still check for special cases
47 # such as SCREEN* and WINDOW* parameters. In this table, the type for the
48 # first parameter is encoded, relying upon an ncurses programming convention:
57 _nc_mingw_console_read 1
64 assume_default_colors 1
358 $result = 1 if ( $value =~ /\b0x[[:xdigit:]]+\b/i );
366 $arg =~ s/\b$curscr\b/curscr/g if ($curscr);
367 $arg =~ s/\b$newscr\b/newscr/g if ($newscr);
368 $arg =~ s/\b$stdscr\b/stdscr/g if ($stdscr);
369 if ( &has_addr($arg) ) {
370 foreach my $addr ( keys %scr_addr ) {
371 $n = $scr_addr{$addr};
372 $arg =~ s/\b$addr\b/screen$n/g if ( defined $n );
375 if ( &has_addr($arg) ) {
376 foreach my $addr ( keys %thr_addr ) {
377 $n = $thr_addr{$addr};
378 $arg =~ s/\b$addr\b/thread$n/g if ( defined $n );
381 if ( &has_addr($arg) ) {
382 foreach my $addr ( keys %trm_addr ) {
383 $n = $trm_addr{$addr};
384 $arg =~ s/\b$addr\b/terminal$n/g if ( defined $n );
387 if ( &has_addr($arg) ) {
388 foreach my $addr ( keys %try_addr ) {
389 $n = $try_addr{$addr};
390 $arg =~ s/\b$addr\b/tries_$n/g if ( defined $n );
393 if ( &has_addr($arg) ) {
394 foreach my $addr ( keys %win_addr ) {
395 $n = $win_addr{$addr};
396 $arg =~ s/\b$addr\b/window$n/g if ( defined $n );
399 if ( &has_addr($arg) ) {
400 if ( $arg =~ /add_wch\((window\d+,)?0x[[:xdigit:]]+\)/i ) {
401 $arg =~ s/(0x[[:xdigit:]]+)[)]/\&wch)/i;
404 $arg =~ /color_content\((screen\d+,)?\d+(,0x[[:xdigit:]]+){3}/i )
406 $arg =~ s/(,0x[[:xdigit:]]+){3}[)]/,\&r,\&g,\&b)/i;
408 elsif ( $arg =~ /pair_content\((screen\d+,)?\d+(,0x[[:xdigit:]]+){2}/i )
410 $arg =~ s/(,0x[[:xdigit:]]+){2}[)]/,\&fg,\&bg)/i;
413 if ( &has_addr($arg) and $arg =~ /called\s+\{/ ) {
416 $func =~ s/^.*called\s+\{([[:alnum:]_]+)\(.*$/$1/;
417 if ( defined $known_p1{$func} ) {
419 my $type = $known_p1{$func};
421 $addr =~ s/^[^(]+\((0x[[:xdigit:]]+).*/$1/i;
423 $scr_addr{$addr} = ++$scr_nums;
424 $arg = &transaddr($arg);
426 elsif ( $type == 2 ) {
427 $win_addr{$addr} = ++$win_nums;
428 $arg = &transaddr($arg);
430 elsif ( $type == 4 ) {
431 $trm_addr{$addr} = ++$trm_nums;
432 $arg = &transaddr($arg);
450 # just in case someone tries a file with cr/lf line-endings:
455 if ( $_ =~ /^(0x[[:xdigit:]]+):/ ) {
456 $thr_addr{$1} = ++$thr_nums unless defined $thr_addr{$1};
457 $thread = "thread" . $thr_addr{$1} . ":";
461 # Transform window pointer addresses so it's easier to compare logs
462 $awaiting = "curscr" if ( $_ =~ /creating curscr/ );
463 $awaiting = "newscr" if ( $_ =~ /creating newscr/ );
464 $awaiting = "stdscr" if ( $_ =~ /creating stdscr/ );
465 $awaiting = "screen" if ( $_ =~ /^(\+ )*called \{new_prescr\(\)/ );
466 if ( $_ =~ /^create :window 0x([[:xdigit:]]+)/ ) {
468 if ( $awaiting eq "curscr" ) {
471 elsif ( $awaiting eq "newscr" ) {
474 elsif ( $awaiting eq "stdscr" ) {
478 $win_addr{$addr} = $win_nums++;
482 elsif ( $_ =~ /^(\+ )*called \{set_curterm\((0x[[:xdigit:]]+)\)/ ) {
483 $trm_addr{$2} = ++$trm_nums unless defined $trm_addr{$2};
485 elsif ( $_ =~ /^(\+ )*called \{_nc_add_to_try\((0x[[:xdigit:]]+),/ )
487 $try_addr{$2} = ++$try_nums unless defined $try_addr{$2};
489 elsif ( $_ =~ /^(\+ )*_nc_alloc_screen_sp 0x([[:xdigit:]]+)/ ) {
491 $scr_addr{$addr} = ++$scr_nums unless ( $scr_addr{$addr} );
494 elsif ( $_ =~ /^(\+ )*return }0x([[:xdigit:]]+)/ ) {
496 if ( $awaiting eq "screen" ) {
497 $scr_addr{$addr} = ++$scr_nums unless ( $scr_addr{$addr} );
500 elsif ( $_ =~ /^\.\.\.deleted win=0x([[:xdigit:]]+)/ ) {
503 if ( $addr eq $curscr ) {
506 elsif ( $addr eq $newscr ) {
509 elsif ( $addr eq $stdscr ) {
513 undef $win_addr{$addr};
517 # Compactify runs of PutAttrChar calls (TR_CHARPUT)
518 if ( $_ =~ /$putattr/ ) {
519 my $putattr_chars = $1;
523 if ( $_ =~ /$putattr/ ) {
524 $putattr_chars .= $1;
531 "RUN of PutAttrChar()s: \"$putattr_chars\" from ${starty}, ${startx}\n";
535 # Compactify runs of waddnstr calls (TR_CALLS)
536 if ( $_ =~ /$waddnstr/ ) {
537 my $waddnstr_chars = $2;
540 if ( $_ =~ /$waddnstr/ && $1 eq $winaddr ) {
541 $waddnstr_chars .= $2;
547 my $winaddstr = &transaddr($winaddr);
548 print "RUN of waddnstr()s: $winaddr, \"$waddnstr_chars\"\n";
552 # More transformations can go here
554 # Repeated runs of anything
555 my $anyline = &transaddr($_);
558 if ( &transaddr($_) eq $anyline ) {
565 if ( $repeatcount > 1 ) {
566 print "${repeatcount} REPEATS OF $anyline";
569 print $thread . $anyline;
578 while ( $#ARGV >= 0 ) {
579 my $file = shift @ARGV;
580 open my $ifh, "<", $file or die $!;
588 # tracemunch ends here