]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/tracemunch
ncurses 6.1 - patch 20180324
[ncurses.git] / test / tracemunch
1 #!/usr/bin/env perl
2 # $Id: tracemunch,v 1.18 2018/03/22 08:50:15 tom Exp $
3 ##############################################################################
4 # Copyright (c) 1998-2005,2017 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\\({{ '(.)' = 0[0-7]+ }}\\) at \\(([0-9]+), ([0-9]+)\\)";
41 our $waddnstr =
42   "waddnstr\\(0x([[:xdigit:]]+),\"([^\"]+)\",[0-9]+\\) called {A_NORMAL}";
43
44 our $scr_nums = 0;
45 our $thr_nums = 0;
46 our $try_nums = 0;
47 our $win_nums = 0;
48 our $curscr   = "";
49 our $newscr   = "";
50 our $stdscr   = "";
51 our %scr_addr;
52 our %thr_addr;
53 our %try_addr;
54 our %win_addr;
55
56 sub transaddr {
57     my $n;
58     my $arg = $_[0];
59
60     $arg =~ s/\b$curscr\b/curscr/g if ($curscr);
61     $arg =~ s/\b$newscr\b/newscr/g if ($newscr);
62     $arg =~ s/\b$stdscr\b/stdscr/g if ($stdscr);
63     foreach my $addr ( keys %scr_addr ) {
64         $n = $scr_addr{$addr};
65         $arg =~ s/\b$addr\b/screen$n/g if ( defined $n );
66     }
67     foreach my $addr ( keys %thr_addr ) {
68         $n = $thr_addr{$addr};
69         $arg =~ s/\b$addr\b/thread$n/g if ( defined $n );
70     }
71     foreach my $addr ( keys %try_addr ) {
72         $n = $try_addr{$addr};
73         $arg =~ s/\b$addr\b/tries_$n/g if ( defined $n );
74     }
75     foreach my $addr ( keys %win_addr ) {
76         $n = $win_addr{$addr};
77         $arg =~ s/\b$addr\b/window$n/g if ( defined $n );
78     }
79     if ( $arg =~ /add_wch\((window\d+,)?0x[[:xdigit:]]+\)/i ) {
80         $arg =~ s/(0x[[:xdigit:]]+)[)]/\&wch)/i;
81     }
82     elsif ( $arg =~ /color_content\((screen\d+,)?\d+(,0x[[:xdigit:]]+){3}/i ) {
83         $arg =~ s/(,0x[[:xdigit:]]+){3}[)]/,\&r,\&g,\&b)/i;
84     }
85     elsif ( $arg =~ /pair_content\((screen\d+,)?\d+(,0x[[:xdigit:]]+){2}/i ) {
86         $arg =~ s/(,0x[[:xdigit:]]+){2}[)]/,\&fg,\&bg)/i;
87     }
88
89     return $arg;
90 }
91
92 while (<STDIN>) {
93     my $addr;
94     my $n;
95     my $awaiting = "";
96
97   CLASSIFY: {
98
99         my $thread = "";
100         if ( $_ =~ /^(0x[[:xdigit:]]+):/ ) {
101             $thr_addr{$1} = ++$thr_nums unless defined $thr_addr{$1};
102             $thread = "thread" . $thr_addr{$1} . ":";
103             $_ =~ s/^[^:]*://;
104         }
105
106         # Transform window pointer addresses so it's easier to compare logs
107         $awaiting = "curscr" if ( $_ =~ /creating curscr/ );
108         $awaiting = "newscr" if ( $_ =~ /creating newscr/ );
109         $awaiting = "stdscr" if ( $_ =~ /creating stdscr/ );
110         $awaiting = "screen" if ( $_ =~ /^(\+ )*called {new_prescr\(\)/ );
111         if ( $_ =~ /^create :window 0x([[:xdigit:]]+)/ ) {
112             $addr = "0x$1";
113             if ( $awaiting eq "curscr" ) {
114                 $curscr = $addr;
115             }
116             elsif ( $awaiting eq "newscr" ) {
117                 $newscr = $addr;
118             }
119             elsif ( $awaiting eq "stdscr" ) {
120                 $stdscr = $addr;
121             }
122             else {
123                 $win_addr{$addr} = $win_nums++;
124             }
125             $awaiting = "";
126         }
127         elsif ( $_ =~ /^(\+ )*called {_nc_add_to_try\((0x[[:xdigit:]]+),/ ) {
128             $try_addr{$2} = ++$try_nums unless defined $try_addr{$2};
129         }
130         elsif ( $_ =~ /^(\+ )*_nc_alloc_screen_sp 0x([[:xdigit:]]+)/ ) {
131             $addr = "0x$2";
132             $scr_addr{$addr} = ++$scr_nums unless ( $scr_addr{$addr} );
133             $awaiting = "";
134         }
135         elsif ( $_ =~ /^(\+ )*return }0x([[:xdigit:]]+)/ ) {
136             $addr = "0x$2";
137             if ( $awaiting eq "screen" ) {
138                 $scr_addr{$addr} = ++$scr_nums unless ( $scr_addr{$addr} );
139             }
140         }
141         elsif ( $_ =~ /^\.\.\.deleted win=0x([[:xdigit:]]+)/ ) {
142             $addr = "0x$1";
143             $_    = &transaddr($_);
144             if ( $addr eq $curscr ) {
145                 $curscr = "";
146             }
147             elsif ( $addr eq $newscr ) {
148                 $newscr = "";
149             }
150             elsif ( $addr eq $stdscr ) {
151                 $stdscr = "";
152             }
153             else {
154                 undef $win_addr{$addr};
155             }
156         }
157
158         # Compactify runs of PutAttrChar calls (TR_CHARPUT)
159         if ( $_ =~ /$putattr/ ) {
160             my $putattr_chars = $1;
161             my $starty        = $2;
162             my $startx        = $3;
163             while (<STDIN>) {
164                 if ( $_ =~ /$putattr/ ) {
165                     $putattr_chars .= $1;
166                 }
167                 else {
168                     last;
169                 }
170             }
171             print
172 "RUN of PutAttrChar()s: \"$putattr_chars\" from ${starty}, ${startx}\n";
173             redo CLASSIFY;
174         }
175
176         # Compactify runs of waddnstr calls (TR_CALLS)
177         if ( $_ =~ /$waddnstr/ ) {
178             my $waddnstr_chars = $2;
179             my $winaddr        = $1;
180             while (<STDIN>) {
181                 if ( $_ =~ /$waddnstr/ && $1 eq $winaddr ) {
182                     $waddnstr_chars .= $2;
183                 }
184                 else {
185                     last;
186                 }
187             }
188             my $winaddstr = &transaddr($winaddr);
189             print "RUN of waddnstr()s: $winaddr, \"$waddnstr_chars\"\n";
190             redo CLASSIFY;
191         }
192
193         # More transformations can go here
194
195         # Repeated runs of anything
196         my $anyline     = &transaddr($_);
197         my $repeatcount = 1;
198         while (<STDIN>) {
199             if ( &transaddr($_) eq $anyline ) {
200                 $repeatcount++;
201             }
202             else {
203                 last;
204             }
205         }
206         if ( $repeatcount > 1 ) {
207             print "${repeatcount} REPEATS OF $anyline";
208         }
209         else {
210             print $thread . $anyline;
211         }
212         redo CLASSIFY if $_;
213
214     }    # :CLASSIFY
215 }
216
217 # tracemunch ends here