1#!/usr/bin/env perl
2# Copyright (C) 2013 Red Hat, Inc.
3#
4# Authors:
5#  Markus Armbruster <armbru@redhat.com>
6#
7# This work is licensed under the terms of the GNU GPL, version 2 or
8# later.  See the COPYING file in the top-level directory.
9
10# Usage: cleanup-trace-events.pl trace-events
11#
12# Print cleaned up trace-events to standard output.
13
14use warnings;
15use strict;
16use File::Basename;
17
18my $buf = '';
19my %seen = ();
20
21sub out {
22    print $buf;
23    $buf = '';
24    %seen = ();
25}
26
27$#ARGV == 0 or die "usage: $0 FILE";
28my $in = $ARGV[0];
29my $dir = dirname($in);
30open(IN, $in) or die "open $in: $!";
31chdir($dir) or die "chdir $dir: $!";
32
33while (<IN>) {
34    if (/^(disable |(tcg) |vcpu )*([a-z_0-9]+)\(/i) {
35        my $pat = "trace_$3";
36        $pat .= '_tcg' if (defined $2);
37        open GREP, '-|', 'git', 'grep', '-lw', '--max-depth', '1', $pat
38            or die "run git grep: $!";
39        while (my $fname = <GREP>) {
40            chomp $fname;
41            next if $seen{$fname} || $fname eq 'trace-events';
42            $seen{$fname} = 1;
43            $buf = "# $fname\n" . $buf;
44        }
45        unless (close GREP) {
46            die "close git grep: $!"
47                if $!;
48            next;
49        }
50    } elsif (/^# ([^ ]*\.[ch])$/) {
51        out;
52        next;
53    } elsif (!/^#|^$/) {
54        warn "unintelligible line";
55    }
56    $buf .= $_;
57}
58
59out;
60close(IN) or die "close $in: $!";
61