2 # $Id: tracemunch,v 1.29 2020/04/18 23:52:24 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 '^called \{waddnstr\((0x[[:xdigit:]]+|window\d+),"((\\.|[^\"]*))",[-]?[0-9]+\)';
68 # If the trace is complete, we can infer addresses using the return value from
69 # newwin, etc. But if it is incomplete, we can still check for special cases
70 # such as SCREEN* and WINDOW* parameters. In this table, the type for the
71 # first parameter is encoded, relying upon an ncurses programming convention:
80 _nc_mingw_console_read 1
87 assume_default_colors 1
381 $result = 1 if ( $value =~ /\b0x[[:xdigit:]]+\b/i );
389 $arg =~ s/\b$curscr\b/curscr/g if ($curscr);
390 $arg =~ s/\b$newscr\b/newscr/g if ($newscr);
391 $arg =~ s/\b$stdscr\b/stdscr/g if ($stdscr);
392 if ( &has_addr($arg) ) {
393 foreach my $addr ( keys %scr_addr ) {
394 $n = $scr_addr{$addr};
395 $arg =~ s/\b$addr\b/screen$n/g if ( defined $n );
398 if ( &has_addr($arg) ) {
399 foreach my $addr ( keys %thr_addr ) {
400 $n = $thr_addr{$addr};
401 $arg =~ s/\b$addr\b/thread$n/g if ( defined $n );
404 if ( &has_addr($arg) ) {
405 foreach my $addr ( keys %trm_addr ) {
406 $n = $trm_addr{$addr};
407 $arg =~ s/\b$addr\b/terminal$n/g if ( defined $n );
410 if ( &has_addr($arg) ) {
411 foreach my $addr ( keys %try_addr ) {
412 $n = $try_addr{$addr};
413 $arg =~ s/\b$addr\b/tries_$n/g if ( defined $n );
416 if ( &has_addr($arg) ) {
417 foreach my $addr ( keys %win_addr ) {
418 $n = $win_addr{$addr};
419 $arg =~ s/\b$addr\b/window$n/g if ( defined $n );
422 if ( &has_addr($arg) ) {
423 if ( $arg =~ /add_wch\((window\d+,)?0x[[:xdigit:]]+\)/i ) {
424 $arg =~ s/(0x[[:xdigit:]]+)[)]/\&wch)/i;
427 $arg =~ /color_content\((screen\d+,)?\d+(,0x[[:xdigit:]]+){3}/i )
429 $arg =~ s/(,0x[[:xdigit:]]+){3}[)]/,\&r,\&g,\&b)/i;
431 elsif ( $arg =~ /pair_content\((screen\d+,)?\d+(,0x[[:xdigit:]]+){2}/i )
433 $arg =~ s/(,0x[[:xdigit:]]+){2}[)]/,\&fg,\&bg)/i;
436 if ( &has_addr($arg) and $arg =~ /called\s+\{/ ) {
439 $func =~ s/^.*called\s+\{([[:alnum:]_]+)\(.*$/$1/;
440 if ( defined $known_p1{$func} ) {
442 my $type = $known_p1{$func};
444 $addr =~ s/^[^(]+\((0x[[:xdigit:]]+).*/$1/i;
445 if ( $addr !~ /^0x[[:xdigit:]]+$/i ) {
446 printf "OOPS - expected type #$type, skipping\n>>$addr\n";
448 elsif ( $type == 1 ) {
449 $scr_addr{$addr} = ++$scr_nums;
450 $arg = &transaddr($arg);
452 elsif ( $type == 2 ) {
453 $win_addr{$addr} = ++$win_nums;
454 $arg = &transaddr($arg);
456 elsif ( $type == 4 ) {
457 $trm_addr{$addr} = ++$trm_nums;
458 $arg = &transaddr($arg);
476 # just in case someone tries a file with cr/lf line-endings:
481 /^TRACING NCURSES version.*\(tracelevel=(0x[[:xdigit:]]+)\)/ )
483 $tracelevel = hex $1;
489 if ( $_ =~ /^(0x[[:xdigit:]]+):/ ) {
490 $thr_addr{$1} = ++$thr_nums unless defined $thr_addr{$1};
491 $thread = "thread" . $thr_addr{$1} . ":";
495 # Transform window pointer addresses so it's easier to compare logs
496 $awaiting = "curscr" if ( $_ =~ /creating curscr/ );
497 $awaiting = "newscr" if ( $_ =~ /creating newscr/ );
498 $awaiting = "stdscr" if ( $_ =~ /creating stdscr/ );
499 $awaiting = "screen" if ( $_ =~ /^(\+ )*called \{new_prescr\(\)/ );
500 if ( $_ =~ /^create :window 0x([[:xdigit:]]+)/ ) {
502 if ( $awaiting eq "curscr" ) {
505 elsif ( $awaiting eq "newscr" ) {
508 elsif ( $awaiting eq "stdscr" ) {
512 $win_addr{$addr} = $win_nums++;
516 elsif ( $_ =~ /^(\+ )*called \{set_curterm\((0x[[:xdigit:]]+)\)/ ) {
517 $trm_addr{$2} = ++$trm_nums unless defined $trm_addr{$2};
519 elsif ( $_ =~ /^(\+ )*called \{_nc_add_to_try\((0x[[:xdigit:]]+),/ )
521 $try_addr{$2} = ++$try_nums unless defined $try_addr{$2};
523 elsif ( $_ =~ /^(\+ )*_nc_alloc_screen_sp 0x([[:xdigit:]]+)/ ) {
525 $scr_addr{$addr} = ++$scr_nums unless ( $scr_addr{$addr} );
528 elsif ( $_ =~ /^(\+ )*return }0x([[:xdigit:]]+)/ ) {
530 if ( $awaiting eq "screen" ) {
531 $scr_addr{$addr} = ++$scr_nums unless ( $scr_addr{$addr} );
534 elsif ( $_ =~ /^\.\.\.deleted win=0x([[:xdigit:]]+)/ ) {
537 if ( $addr eq $curscr ) {
540 elsif ( $addr eq $newscr ) {
543 elsif ( $addr eq $stdscr ) {
547 undef $win_addr{$addr};
551 # Compactify runs of PutAttrChar
552 if ( ( ( $tracelevel & $TR{CHARPUT} ) != 0 ) and $_ =~ /$putattr/ )
554 my $putattr_chars = $1;
558 if ( $_ =~ /$putattr/ ) {
559 $putattr_chars .= $1;
562 next if ( $_ =~ /^PUTC 0x[[:xdigit:]]+.*/ );
563 next if ( $_ =~ /^\.\.\.skip.*/ );
564 next if ( $_ =~ /^forced to blank.*/ );
568 print "RUN of PutAttrChar()s:"
569 . " \"$putattr_chars\" from ${starty}, ${startx}\n";
573 # Compactify runs of waddnstr calls
574 if ( ( ( $tracelevel & $TR{CALLS} ) != 0 ) and $_ =~ /$waddnstr/ ) {
575 my $waddnstr_chars = $2;
578 next if ( $_ =~ /^return \}0/ );
579 if ( $_ =~ /$waddnstr/ && $1 eq $winaddr ) {
580 $waddnstr_chars .= $2;
586 my $winaddstr = &transaddr($winaddr);
587 print "RUN of waddnstr()s:"
588 . " $winaddstr, \"$waddnstr_chars\"\n";
592 # More transformations can go here
594 # Repeated runs of anything
595 my $anyline = &transaddr($_);
598 if ( &transaddr($_) eq $anyline ) {
605 if ( $repeatcount > 1 ) {
606 print "${repeatcount} REPEATS OF $anyline";
609 print $thread . $anyline;
617 for my $tr ( keys %TR ) {
618 $TR{$tr} = hex $TR{$tr};
622 while ( $#ARGV >= 0 ) {
623 my $file = shift @ARGV;
624 open my $ifh, "<", $file or die $!;
632 # tracemunch ends here