]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/tracemunch
ncurses 6.1 - patch 20191019
[ncurses.git] / test / tracemunch
1 #!/usr/bin/env perl
2 # $Id: tracemunch,v 1.24 2018/12/29 22:20:06 tom Exp $
3 ##############################################################################
4 # Copyright (c) 1998-2017,2018 Free Software Foundation, Inc.                #
5 #                                                                            #
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:                                                      #
13 #                                                                            #
14 # The above copyright notice and this permission notice shall be included in #
15 # all copies or substantial portions of the Software.                        #
16 #                                                                            #
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.                                                  #
24 #                                                                            #
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               #
28 # authorization.                                                             #
29 ##############################################################################
30 # tracemunch -- compactify ncurses trace logs
31 #
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.
35
36 use strict;
37 use warnings;
38
39 our $putattr =
40     'PutAttrChar\(\{\{ ' . "'(.)'"
41   . ' = 0[0-7]+ \}\}\) at \(([0-9]+), ([0-9]+)\)';
42 our $waddnstr =
43   'waddnstr\(0x([[:xdigit:]]+),"([^\"]+)",[0-9]+\) called \{A_NORMAL\}';
44
45 our $scr_nums = 0;
46 our $thr_nums = 0;
47 our $try_nums = 0;
48 our $win_nums = 0;
49 our $curscr   = "";
50 our $newscr   = "";
51 our $stdscr   = "";
52 our %scr_addr;
53 our %thr_addr;
54 our %try_addr;
55 our %win_addr;
56
57 sub transaddr {
58     my $n;
59     my $arg = $_[0];
60
61     $arg =~ s/\b$curscr\b/curscr/g if ($curscr);
62     $arg =~ s/\b$newscr\b/newscr/g if ($newscr);
63     $arg =~ s/\b$stdscr\b/stdscr/g if ($stdscr);
64     foreach my $addr ( keys %scr_addr ) {
65         $n = $scr_addr{$addr};
66         $arg =~ s/\b$addr\b/screen$n/g if ( defined $n );
67     }
68     foreach my $addr ( keys %thr_addr ) {
69         $n = $thr_addr{$addr};
70         $arg =~ s/\b$addr\b/thread$n/g if ( defined $n );
71     }
72     foreach my $addr ( keys %try_addr ) {
73         $n = $try_addr{$addr};
74         $arg =~ s/\b$addr\b/tries_$n/g if ( defined $n );
75     }
76     foreach my $addr ( keys %win_addr ) {
77         $n = $win_addr{$addr};
78         $arg =~ s/\b$addr\b/window$n/g if ( defined $n );
79     }
80     if ( $arg =~ /add_wch\((window\d+,)?0x[[:xdigit:]]+\)/i ) {
81         $arg =~ s/(0x[[:xdigit:]]+)[)]/\&wch)/i;
82     }
83     elsif ( $arg =~ /color_content\((screen\d+,)?\d+(,0x[[:xdigit:]]+){3}/i ) {
84         $arg =~ s/(,0x[[:xdigit:]]+){3}[)]/,\&r,\&g,\&b)/i;
85     }
86     elsif ( $arg =~ /pair_content\((screen\d+,)?\d+(,0x[[:xdigit:]]+){2}/i ) {
87         $arg =~ s/(,0x[[:xdigit:]]+){2}[)]/,\&fg,\&bg)/i;
88     }
89
90     return $arg;
91 }
92
93 sub muncher($) {
94     my $STDIN = shift;
95
96     while (<$STDIN>) {
97         my $addr;
98         my $n;
99         my $awaiting = "";
100
101       CLASSIFY: {
102
103             # just in case someone tries a file with cr/lf line-endings:
104             $_ =~ s/\r\n/\n/g;
105             $_ =~ s/\r/\n/g;
106
107             my $thread = "";
108             if ( $_ =~ /^(0x[[:xdigit:]]+):/ ) {
109                 $thr_addr{$1} = ++$thr_nums unless defined $thr_addr{$1};
110                 $thread = "thread" . $thr_addr{$1} . ":";
111                 $_ =~ s/^[^:]*://;
112             }
113
114             # Transform window pointer addresses so it's easier to compare logs
115             $awaiting = "curscr" if ( $_ =~ /creating curscr/ );
116             $awaiting = "newscr" if ( $_ =~ /creating newscr/ );
117             $awaiting = "stdscr" if ( $_ =~ /creating stdscr/ );
118             $awaiting = "screen" if ( $_ =~ /^(\+ )*called \{new_prescr\(\)/ );
119             if ( $_ =~ /^create :window 0x([[:xdigit:]]+)/ ) {
120                 $addr = "0x$1";
121                 if ( $awaiting eq "curscr" ) {
122                     $curscr = $addr;
123                 }
124                 elsif ( $awaiting eq "newscr" ) {
125                     $newscr = $addr;
126                 }
127                 elsif ( $awaiting eq "stdscr" ) {
128                     $stdscr = $addr;
129                 }
130                 else {
131                     $win_addr{$addr} = $win_nums++;
132                 }
133                 $awaiting = "";
134             }
135             elsif ( $_ =~ /^(\+ )*called \{_nc_add_to_try\((0x[[:xdigit:]]+),/ )
136             {
137                 $try_addr{$2} = ++$try_nums unless defined $try_addr{$2};
138             }
139             elsif ( $_ =~ /^(\+ )*_nc_alloc_screen_sp 0x([[:xdigit:]]+)/ ) {
140                 $addr = "0x$2";
141                 $scr_addr{$addr} = ++$scr_nums unless ( $scr_addr{$addr} );
142                 $awaiting = "";
143             }
144             elsif ( $_ =~ /^(\+ )*return }0x([[:xdigit:]]+)/ ) {
145                 $addr = "0x$2";
146                 if ( $awaiting eq "screen" ) {
147                     $scr_addr{$addr} = ++$scr_nums unless ( $scr_addr{$addr} );
148                 }
149             }
150             elsif ( $_ =~ /^\.\.\.deleted win=0x([[:xdigit:]]+)/ ) {
151                 $addr = "0x$1";
152                 $_    = &transaddr($_);
153                 if ( $addr eq $curscr ) {
154                     $curscr = "";
155                 }
156                 elsif ( $addr eq $newscr ) {
157                     $newscr = "";
158                 }
159                 elsif ( $addr eq $stdscr ) {
160                     $stdscr = "";
161                 }
162                 else {
163                     undef $win_addr{$addr};
164                 }
165             }
166
167             # Compactify runs of PutAttrChar calls (TR_CHARPUT)
168             if ( $_ =~ /$putattr/ ) {
169                 my $putattr_chars = $1;
170                 my $starty        = $2;
171                 my $startx        = $3;
172                 while (<$STDIN>) {
173                     if ( $_ =~ /$putattr/ ) {
174                         $putattr_chars .= $1;
175                     }
176                     else {
177                         last;
178                     }
179                 }
180                 print
181 "RUN of PutAttrChar()s: \"$putattr_chars\" from ${starty}, ${startx}\n";
182                 redo CLASSIFY;
183             }
184
185             # Compactify runs of waddnstr calls (TR_CALLS)
186             if ( $_ =~ /$waddnstr/ ) {
187                 my $waddnstr_chars = $2;
188                 my $winaddr        = $1;
189                 while (<$STDIN>) {
190                     if ( $_ =~ /$waddnstr/ && $1 eq $winaddr ) {
191                         $waddnstr_chars .= $2;
192                     }
193                     else {
194                         last;
195                     }
196                 }
197                 my $winaddstr = &transaddr($winaddr);
198                 print "RUN of waddnstr()s: $winaddr, \"$waddnstr_chars\"\n";
199                 redo CLASSIFY;
200             }
201
202             # More transformations can go here
203
204             # Repeated runs of anything
205             my $anyline     = &transaddr($_);
206             my $repeatcount = 1;
207             while (<$STDIN>) {
208                 if ( &transaddr($_) eq $anyline ) {
209                     $repeatcount++;
210                 }
211                 else {
212                     last;
213                 }
214             }
215             if ( $repeatcount > 1 ) {
216                 print "${repeatcount} REPEATS OF $anyline";
217             }
218             else {
219                 print $thread . $anyline;
220             }
221             redo CLASSIFY if $_;
222
223         }    # :CLASSIFY
224     }
225 }
226
227 if ( $#ARGV >= 0 ) {
228     while ( $#ARGV >= 0 ) {
229         my $file = shift @ARGV;
230         open my $ifh, "<", $file or die $!;
231         &muncher($ifh);
232     }
233 }
234 else {
235     &muncher( \*STDIN );
236 }
237
238 # tracemunch ends here