2 # $Id: tracemunch,v 1.27 2020/02/02 23:34:34 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.
41 'PutAttrChar\(\{\{ ' . "'(.)'"
42 . ' = 0[0-7]+ \}\}\) at \(([0-9]+), ([0-9]+)\)';
44 'waddnstr\(0x([[:xdigit:]]+),"([^\"]+)",[0-9]+\) called \{A_NORMAL\}';
46 # If the trace is complete, we can infer addresses using the return value from
47 # newwin, etc. But if it is incomplete, we can still check for special cases
48 # such as SCREEN* and WINDOW* parameters. In this table, the type for the
49 # first parameter is encoded, relying upon an ncurses programming convention:
58 _nc_mingw_console_read 1
65 assume_default_colors 1
359 $result = 1 if ( $value =~ /\b0x[[:xdigit:]]+\b/i );
367 $arg =~ s/\b$curscr\b/curscr/g if ($curscr);
368 $arg =~ s/\b$newscr\b/newscr/g if ($newscr);
369 $arg =~ s/\b$stdscr\b/stdscr/g if ($stdscr);
370 if ( &has_addr($arg) ) {
371 foreach my $addr ( keys %scr_addr ) {
372 $n = $scr_addr{$addr};
373 $arg =~ s/\b$addr\b/screen$n/g if ( defined $n );
376 if ( &has_addr($arg) ) {
377 foreach my $addr ( keys %thr_addr ) {
378 $n = $thr_addr{$addr};
379 $arg =~ s/\b$addr\b/thread$n/g if ( defined $n );
382 if ( &has_addr($arg) ) {
383 foreach my $addr ( keys %trm_addr ) {
384 $n = $trm_addr{$addr};
385 $arg =~ s/\b$addr\b/terminal$n/g if ( defined $n );
388 if ( &has_addr($arg) ) {
389 foreach my $addr ( keys %try_addr ) {
390 $n = $try_addr{$addr};
391 $arg =~ s/\b$addr\b/tries_$n/g if ( defined $n );
394 if ( &has_addr($arg) ) {
395 foreach my $addr ( keys %win_addr ) {
396 $n = $win_addr{$addr};
397 $arg =~ s/\b$addr\b/window$n/g if ( defined $n );
400 if ( &has_addr($arg) ) {
401 if ( $arg =~ /add_wch\((window\d+,)?0x[[:xdigit:]]+\)/i ) {
402 $arg =~ s/(0x[[:xdigit:]]+)[)]/\&wch)/i;
405 $arg =~ /color_content\((screen\d+,)?\d+(,0x[[:xdigit:]]+){3}/i )
407 $arg =~ s/(,0x[[:xdigit:]]+){3}[)]/,\&r,\&g,\&b)/i;
409 elsif ( $arg =~ /pair_content\((screen\d+,)?\d+(,0x[[:xdigit:]]+){2}/i )
411 $arg =~ s/(,0x[[:xdigit:]]+){2}[)]/,\&fg,\&bg)/i;
414 if ( &has_addr($arg) and $arg =~ /called\s+\{/ ) {
417 $func =~ s/^.*called\s+\{([[:alnum:]_]+)\(.*$/$1/;
418 if ( defined $known_p1{$func} ) {
420 my $type = $known_p1{$func};
422 $addr =~ s/^[^(]+\((0x[[:xdigit:]]+).*/$1/i;
424 $scr_addr{$addr} = ++$scr_nums;
425 $arg = &transaddr($arg);
427 elsif ( $type == 2 ) {
428 $win_addr{$addr} = ++$win_nums;
429 $arg = &transaddr($arg);
431 elsif ( $type == 4 ) {
432 $trm_addr{$addr} = ++$trm_nums;
433 $arg = &transaddr($arg);
451 # just in case someone tries a file with cr/lf line-endings:
456 if ( $_ =~ /^(0x[[:xdigit:]]+):/ ) {
457 $thr_addr{$1} = ++$thr_nums unless defined $thr_addr{$1};
458 $thread = "thread" . $thr_addr{$1} . ":";
462 # Transform window pointer addresses so it's easier to compare logs
463 $awaiting = "curscr" if ( $_ =~ /creating curscr/ );
464 $awaiting = "newscr" if ( $_ =~ /creating newscr/ );
465 $awaiting = "stdscr" if ( $_ =~ /creating stdscr/ );
466 $awaiting = "screen" if ( $_ =~ /^(\+ )*called \{new_prescr\(\)/ );
467 if ( $_ =~ /^create :window 0x([[:xdigit:]]+)/ ) {
469 if ( $awaiting eq "curscr" ) {
472 elsif ( $awaiting eq "newscr" ) {
475 elsif ( $awaiting eq "stdscr" ) {
479 $win_addr{$addr} = $win_nums++;
483 elsif ( $_ =~ /^(\+ )*called \{set_curterm\((0x[[:xdigit:]]+)\)/ ) {
484 $trm_addr{$2} = ++$trm_nums unless defined $trm_addr{$2};
486 elsif ( $_ =~ /^(\+ )*called \{_nc_add_to_try\((0x[[:xdigit:]]+),/ )
488 $try_addr{$2} = ++$try_nums unless defined $try_addr{$2};
490 elsif ( $_ =~ /^(\+ )*_nc_alloc_screen_sp 0x([[:xdigit:]]+)/ ) {
492 $scr_addr{$addr} = ++$scr_nums unless ( $scr_addr{$addr} );
495 elsif ( $_ =~ /^(\+ )*return }0x([[:xdigit:]]+)/ ) {
497 if ( $awaiting eq "screen" ) {
498 $scr_addr{$addr} = ++$scr_nums unless ( $scr_addr{$addr} );
501 elsif ( $_ =~ /^\.\.\.deleted win=0x([[:xdigit:]]+)/ ) {
504 if ( $addr eq $curscr ) {
507 elsif ( $addr eq $newscr ) {
510 elsif ( $addr eq $stdscr ) {
514 undef $win_addr{$addr};
518 # Compactify runs of PutAttrChar calls (TR_CHARPUT)
519 if ( $_ =~ /$putattr/ ) {
520 my $putattr_chars = $1;
524 if ( $_ =~ /$putattr/ ) {
525 $putattr_chars .= $1;
532 "RUN of PutAttrChar()s: \"$putattr_chars\" from ${starty}, ${startx}\n";
536 # Compactify runs of waddnstr calls (TR_CALLS)
537 if ( $_ =~ /$waddnstr/ ) {
538 my $waddnstr_chars = $2;
541 if ( $_ =~ /$waddnstr/ && $1 eq $winaddr ) {
542 $waddnstr_chars .= $2;
548 my $winaddstr = &transaddr($winaddr);
549 print "RUN of waddnstr()s: $winaddr, \"$waddnstr_chars\"\n";
553 # More transformations can go here
555 # Repeated runs of anything
556 my $anyline = &transaddr($_);
559 if ( &transaddr($_) eq $anyline ) {
566 if ( $repeatcount > 1 ) {
567 print "${repeatcount} REPEATS OF $anyline";
570 print $thread . $anyline;
579 while ( $#ARGV >= 0 ) {
580 my $file = shift @ARGV;
581 open my $ifh, "<", $file or die $!;
589 # tracemunch ends here