2 # $Id: tracemunch,v 1.28 2020/03/08 12:22:49 tom Exp $
3 ##############################################################################
4 # Copyright 2018-2019,2020 Thomas E. Dickey #
5 # Copyright 1998-2005,2017 Free Software Foundation, Inc. #
7 # Permission is hereby granted, free of charge, to any person obtaining a #
8 # copy of this software and associated documentation files (the "Software"), #
9 # to deal in the Software without restriction, including without limitation #
10 # the rights to use, copy, modify, merge, publish, distribute, distribute #
11 # with modifications, sublicense, and/or sell copies of the Software, and to #
12 # permit persons to whom the Software is furnished to do so, subject to the #
13 # following conditions: #
15 # The above copyright notice and this permission notice shall be included in #
16 # all copies or substantial portions of the Software. #
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
19 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
20 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL #
21 # THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
22 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING #
23 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER #
24 # DEALINGS IN THE SOFTWARE. #
26 # Except as contained in this notice, the name(s) of the above copyright #
27 # holders shall not be used in advertising or otherwise to promote the sale, #
28 # use or other dealings in this Software without prior written #
30 ##############################################################################
31 # tracemunch -- compactify ncurses trace logs
33 # The error logs produced by ncurses with tracing enabled can be very tedious
34 # to wade through. This script helps by compacting runs of log lines that
35 # can be conveniently expressed as higher-level operations.
43 'PutAttrChar\(\{\{ ' . "'(.)'"
44 . ' = 0[0-7]+ \}\}\) at \(([0-9]+), ([0-9]+)\)';
46 'waddnstr\(0x([[:xdigit:]]+),"([^\"]+)",[0-9]+\) called \{A_NORMAL\}';
48 # If the trace is complete, we can infer addresses using the return value from
49 # newwin, etc. But if it is incomplete, we can still check for special cases
50 # such as SCREEN* and WINDOW* parameters. In this table, the type for the
51 # first parameter is encoded, relying upon an ncurses programming convention:
60 _nc_mingw_console_read 1
67 assume_default_colors 1
361 $result = 1 if ( $value =~ /\b0x[[:xdigit:]]+\b/i );
369 $arg =~ s/\b$curscr\b/curscr/g if ($curscr);
370 $arg =~ s/\b$newscr\b/newscr/g if ($newscr);
371 $arg =~ s/\b$stdscr\b/stdscr/g if ($stdscr);
372 if ( &has_addr($arg) ) {
373 foreach my $addr ( keys %scr_addr ) {
374 $n = $scr_addr{$addr};
375 $arg =~ s/\b$addr\b/screen$n/g if ( defined $n );
378 if ( &has_addr($arg) ) {
379 foreach my $addr ( keys %thr_addr ) {
380 $n = $thr_addr{$addr};
381 $arg =~ s/\b$addr\b/thread$n/g if ( defined $n );
384 if ( &has_addr($arg) ) {
385 foreach my $addr ( keys %trm_addr ) {
386 $n = $trm_addr{$addr};
387 $arg =~ s/\b$addr\b/terminal$n/g if ( defined $n );
390 if ( &has_addr($arg) ) {
391 foreach my $addr ( keys %try_addr ) {
392 $n = $try_addr{$addr};
393 $arg =~ s/\b$addr\b/tries_$n/g if ( defined $n );
396 if ( &has_addr($arg) ) {
397 foreach my $addr ( keys %win_addr ) {
398 $n = $win_addr{$addr};
399 $arg =~ s/\b$addr\b/window$n/g if ( defined $n );
402 if ( &has_addr($arg) ) {
403 if ( $arg =~ /add_wch\((window\d+,)?0x[[:xdigit:]]+\)/i ) {
404 $arg =~ s/(0x[[:xdigit:]]+)[)]/\&wch)/i;
407 $arg =~ /color_content\((screen\d+,)?\d+(,0x[[:xdigit:]]+){3}/i )
409 $arg =~ s/(,0x[[:xdigit:]]+){3}[)]/,\&r,\&g,\&b)/i;
411 elsif ( $arg =~ /pair_content\((screen\d+,)?\d+(,0x[[:xdigit:]]+){2}/i )
413 $arg =~ s/(,0x[[:xdigit:]]+){2}[)]/,\&fg,\&bg)/i;
416 if ( &has_addr($arg) and $arg =~ /called\s+\{/ ) {
419 $func =~ s/^.*called\s+\{([[:alnum:]_]+)\(.*$/$1/;
420 if ( defined $known_p1{$func} ) {
422 my $type = $known_p1{$func};
424 $addr =~ s/^[^(]+\((0x[[:xdigit:]]+).*/$1/i;
425 if ( $addr !~ /^0x[[:xdigit:]]+$/i ) {
426 printf "OOPS - expected type #$type, skipping\n>>$addr\n";
428 elsif ( $type == 1 ) {
429 $scr_addr{$addr} = ++$scr_nums;
430 $arg = &transaddr($arg);
432 elsif ( $type == 2 ) {
433 $win_addr{$addr} = ++$win_nums;
434 $arg = &transaddr($arg);
436 elsif ( $type == 4 ) {
437 $trm_addr{$addr} = ++$trm_nums;
438 $arg = &transaddr($arg);
456 # just in case someone tries a file with cr/lf line-endings:
461 if ( $_ =~ /^(0x[[:xdigit:]]+):/ ) {
462 $thr_addr{$1} = ++$thr_nums unless defined $thr_addr{$1};
463 $thread = "thread" . $thr_addr{$1} . ":";
467 # Transform window pointer addresses so it's easier to compare logs
468 $awaiting = "curscr" if ( $_ =~ /creating curscr/ );
469 $awaiting = "newscr" if ( $_ =~ /creating newscr/ );
470 $awaiting = "stdscr" if ( $_ =~ /creating stdscr/ );
471 $awaiting = "screen" if ( $_ =~ /^(\+ )*called \{new_prescr\(\)/ );
472 if ( $_ =~ /^create :window 0x([[:xdigit:]]+)/ ) {
474 if ( $awaiting eq "curscr" ) {
477 elsif ( $awaiting eq "newscr" ) {
480 elsif ( $awaiting eq "stdscr" ) {
484 $win_addr{$addr} = $win_nums++;
488 elsif ( $_ =~ /^(\+ )*called \{set_curterm\((0x[[:xdigit:]]+)\)/ ) {
489 $trm_addr{$2} = ++$trm_nums unless defined $trm_addr{$2};
491 elsif ( $_ =~ /^(\+ )*called \{_nc_add_to_try\((0x[[:xdigit:]]+),/ )
493 $try_addr{$2} = ++$try_nums unless defined $try_addr{$2};
495 elsif ( $_ =~ /^(\+ )*_nc_alloc_screen_sp 0x([[:xdigit:]]+)/ ) {
497 $scr_addr{$addr} = ++$scr_nums unless ( $scr_addr{$addr} );
500 elsif ( $_ =~ /^(\+ )*return }0x([[:xdigit:]]+)/ ) {
502 if ( $awaiting eq "screen" ) {
503 $scr_addr{$addr} = ++$scr_nums unless ( $scr_addr{$addr} );
506 elsif ( $_ =~ /^\.\.\.deleted win=0x([[:xdigit:]]+)/ ) {
509 if ( $addr eq $curscr ) {
512 elsif ( $addr eq $newscr ) {
515 elsif ( $addr eq $stdscr ) {
519 undef $win_addr{$addr};
523 # Compactify runs of PutAttrChar calls (TR_CHARPUT)
524 if ( $_ =~ /$putattr/ ) {
525 my $putattr_chars = $1;
529 if ( $_ =~ /$putattr/ ) {
530 $putattr_chars .= $1;
537 "RUN of PutAttrChar()s: \"$putattr_chars\" from ${starty}, ${startx}\n";
541 # Compactify runs of waddnstr calls (TR_CALLS)
542 if ( $_ =~ /$waddnstr/ ) {
543 my $waddnstr_chars = $2;
546 if ( $_ =~ /$waddnstr/ && $1 eq $winaddr ) {
547 $waddnstr_chars .= $2;
553 my $winaddstr = &transaddr($winaddr);
554 print "RUN of waddnstr()s: $winaddr, \"$waddnstr_chars\"\n";
558 # More transformations can go here
560 # Repeated runs of anything
561 my $anyline = &transaddr($_);
564 if ( &transaddr($_) eq $anyline ) {
571 if ( $repeatcount > 1 ) {
572 print "${repeatcount} REPEATS OF $anyline";
575 print $thread . $anyline;
584 while ( $#ARGV >= 0 ) {
585 my $file = shift @ARGV;
586 open my $ifh, "<", $file or die $!;
594 # tracemunch ends here