2 # $Id: tracemunch,v 1.30 2020/08/29 18:59:58 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:
81 _nc_mingw_console_read 1
88 assume_default_colors 1
382 $result = 1 if ( $value =~ /\b0x[[:xdigit:]]+\b/i );
390 $arg =~ s/\b$curscr\b/curscr/g if ($curscr);
391 $arg =~ s/\b$newscr\b/newscr/g if ($newscr);
392 $arg =~ s/\b$stdscr\b/stdscr/g if ($stdscr);
393 if ( &has_addr($arg) ) {
394 foreach my $addr ( keys %scr_addr ) {
395 $n = $scr_addr{$addr};
396 $arg =~ s/\b$addr\b/screen$n/g if ( defined $n );
399 if ( &has_addr($arg) ) {
400 foreach my $addr ( keys %thr_addr ) {
401 $n = $thr_addr{$addr};
402 $arg =~ s/\b$addr\b/thread$n/g if ( defined $n );
405 if ( &has_addr($arg) ) {
406 foreach my $addr ( keys %trm_addr ) {
407 $n = $trm_addr{$addr};
408 $arg =~ s/\b$addr\b/terminal$n/g if ( defined $n );
411 if ( &has_addr($arg) ) {
412 foreach my $addr ( keys %try_addr ) {
413 $n = $try_addr{$addr};
414 $arg =~ s/\b$addr\b/tries_$n/g if ( defined $n );
417 if ( &has_addr($arg) ) {
418 foreach my $addr ( keys %win_addr ) {
419 $n = $win_addr{$addr};
420 $arg =~ s/\b$addr\b/window$n/g if ( defined $n );
423 if ( &has_addr($arg) ) {
424 if ( $arg =~ /add_wch\((window\d+,)?0x[[:xdigit:]]+\)/i ) {
425 $arg =~ s/(0x[[:xdigit:]]+)[)]/\&wch)/i;
428 $arg =~ /color_content\((screen\d+,)?\d+(,0x[[:xdigit:]]+){3}/i )
430 $arg =~ s/(,0x[[:xdigit:]]+){3}[)]/,\&r,\&g,\&b)/i;
432 elsif ( $arg =~ /pair_content\((screen\d+,)?\d+(,0x[[:xdigit:]]+){2}/i )
434 $arg =~ s/(,0x[[:xdigit:]]+){2}[)]/,\&fg,\&bg)/i;
437 if ( &has_addr($arg) and $arg =~ /called\s+\{/ ) {
440 $func =~ s/^.*called\s+\{([[:alnum:]_]+)\(.*$/$1/;
441 if ( defined $known_p1{$func} ) {
443 my $type = $known_p1{$func};
445 $addr =~ s/^[^(]+\((0x[[:xdigit:]]+).*/$1/i;
446 if ( $addr !~ /^0x[[:xdigit:]]+$/i ) {
447 printf "OOPS - expected type #$type, skipping\n>>$addr\n";
449 elsif ( $type == 1 ) {
450 $scr_addr{$addr} = ++$scr_nums;
451 $arg = &transaddr($arg);
453 elsif ( $type == 2 ) {
454 $win_addr{$addr} = ++$win_nums;
455 $arg = &transaddr($arg);
457 elsif ( $type == 4 ) {
458 $trm_addr{$addr} = ++$trm_nums;
459 $arg = &transaddr($arg);
477 # just in case someone tries a file with cr/lf line-endings:
482 /^TRACING NCURSES version.*\(tracelevel=(0x[[:xdigit:]]+)\)/ )
484 $tracelevel = hex $1;
490 if ( $_ =~ /^(0x[[:xdigit:]]+):/ ) {
491 $thr_addr{$1} = ++$thr_nums unless defined $thr_addr{$1};
492 $thread = "thread" . $thr_addr{$1} . ":";
496 # Transform window pointer addresses so it's easier to compare logs
497 $awaiting = "curscr" if ( $_ =~ /creating curscr/ );
498 $awaiting = "newscr" if ( $_ =~ /creating newscr/ );
499 $awaiting = "stdscr" if ( $_ =~ /creating stdscr/ );
500 $awaiting = "screen" if ( $_ =~ /^(\+ )*called \{new_prescr\(\)/ );
501 if ( $_ =~ /^create :window 0x([[:xdigit:]]+)/ ) {
503 if ( $awaiting eq "curscr" ) {
506 elsif ( $awaiting eq "newscr" ) {
509 elsif ( $awaiting eq "stdscr" ) {
513 $win_addr{$addr} = $win_nums++;
517 elsif ( $_ =~ /^(\+ )*called \{set_curterm\((0x[[:xdigit:]]+)\)/ ) {
518 $trm_addr{$2} = ++$trm_nums unless defined $trm_addr{$2};
520 elsif ( $_ =~ /^(\+ )*called \{_nc_add_to_try\((0x[[:xdigit:]]+),/ )
522 $try_addr{$2} = ++$try_nums unless defined $try_addr{$2};
524 elsif ( $_ =~ /^(\+ )*_nc_alloc_screen_sp 0x([[:xdigit:]]+)/ ) {
526 $scr_addr{$addr} = ++$scr_nums unless ( $scr_addr{$addr} );
529 elsif ( $_ =~ /^(\+ )*return }0x([[:xdigit:]]+)/ ) {
531 if ( $awaiting eq "screen" ) {
532 $scr_addr{$addr} = ++$scr_nums unless ( $scr_addr{$addr} );
535 elsif ( $_ =~ /^\.\.\.deleted win=0x([[:xdigit:]]+)/ ) {
538 if ( $addr eq $curscr ) {
541 elsif ( $addr eq $newscr ) {
544 elsif ( $addr eq $stdscr ) {
548 undef $win_addr{$addr};
552 # Compactify runs of PutAttrChar
553 if ( ( ( $tracelevel & $TR{CHARPUT} ) != 0 ) and $_ =~ /$putattr/ )
555 my $putattr_chars = $1;
559 if ( $_ =~ /$putattr/ ) {
560 $putattr_chars .= $1;
563 next if ( $_ =~ /^PUTC 0x[[:xdigit:]]+.*/ );
564 next if ( $_ =~ /^\.\.\.skip.*/ );
565 next if ( $_ =~ /^forced to blank.*/ );
569 print "RUN of PutAttrChar()s:"
570 . " \"$putattr_chars\" from ${starty}, ${startx}\n";
574 # Compactify runs of waddnstr calls
575 if ( ( ( $tracelevel & $TR{CALLS} ) != 0 ) and $_ =~ /$waddnstr/ ) {
576 my $waddnstr_chars = $2;
579 next if ( $_ =~ /^return \}0/ );
580 if ( $_ =~ /$waddnstr/ && $1 eq $winaddr ) {
581 $waddnstr_chars .= $2;
587 my $winaddstr = &transaddr($winaddr);
588 print "RUN of waddnstr()s:"
589 . " $winaddstr, \"$waddnstr_chars\"\n";
593 # More transformations can go here
595 # Repeated runs of anything
596 my $anyline = &transaddr($_);
599 if ( &transaddr($_) eq $anyline ) {
606 if ( $repeatcount > 1 ) {
607 print "${repeatcount} REPEATS OF $anyline";
610 print $thread . $anyline;
618 for my $tr ( keys %TR ) {
619 $TR{$tr} = hex $TR{$tr};
623 while ( $#ARGV >= 0 ) {
624 my $file = shift @ARGV;
625 open my $ifh, "<", $file or die $!;
633 # tracemunch ends here