This file is indexed.

/usr/bin/eztrace_plugin_generator is in eztrace 1.1-2-1ubuntu2.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/perl -w

# Copyright (C) CNRS, INRIA, Université Bordeaux 1, Télécom SudParis
# See COPYING in top-level directory.

use File::Temp qw/ tempfile tempdir /;
use File::Copy;

my $prefix;
my $exec_prefix;
my $libdir;
my $includedir;
my $bindir;

BEGIN {
    $prefix="/usr";
    $exec_prefix="${prefix}";
    $libdir="${prefix}/lib/x86_64-linux-gnu";
    $includedir="${prefix}/include";
    $bindir="${exec_prefix}/bin";
}

my $indent_fortran="$prefix/bin/eztrace_indent_fortran";

# format a source file so that function prototypes
# are properly formatted (eg. everything on line line)
sub format_source_file( $ ) {
    my $fname = shift;

    my $fh;
    my $filename;

    my $suffix;
    if( $fname =~ m/.+\.c$/) {
	$suffix=".c";
    } elsif( $fname =~ m/.+\.cpp$/) {
	$suffix=".cpp";
    } elsif( $fname =~ m/.+\.f$/) {
	# todo .f77, .f90, .F, etc.
	$suffix=".f";
    }
    ($fh, $filename) = tempfile(SUFFIX=>$suffix);

    copy($fname, $filename) or die "Copy failed: $!";

    if($suffix eq ".c") {
	`indent -npsl --line-length10000  $filename`;
    }

    if($suffix eq ".cpp") {
	`indent -npsl --line-length10000  $filename`;
    }

    if($suffix eq ".f") {
	`$indent_fortran $fname $filename`;
    }

    return $filename;
}

# return the prototype of function $fname extracted from $filename
sub extract_prototype( $$ ) {
    my $fname=shift;
    my $filename=shift;

    # in some cases, fname main contain (). Remove these parenthesis
    $fname =~ s/\(\s*\)//;

    # don't instrument the main function
    if($fname eq "main") {
	return "";
    }

    # format the source code
    my $new_filename=format_source_file($filename);

    # extract symbols
    my $program_language;
    my $symbols="";
    if ($new_filename =~ m/.\.c/) {
	$program_language="C";
	$symbols=`ctags --c-kinds=pf --fields=+S -x $new_filename`;
    } elsif ($new_filename =~ m/.\.cpp/) {
	$program_language="CPP";
	$symbols=`ctags --c++-kinds=pf --fields=+S -x $new_filename`;
    } elsif ($new_filename =~ m/.\.f/) {
	$prototype = "void ".$fname."()";
	$symbols=`ctags --fortran-kinds=pfis --fields=+S -x $new_filename`;
    } else {
	# example: f90, c++, etc.
	# not implemented
	printf "Cannot extract information about function %s: file format not supported\n", $fname;
	printf "\tAssuming the function prototype is void %s()\n", $fname;
	$prototype = "void ".$fname."()";
	unlink($new_filename);
	return $prototype;
    }

    my $fortran_fname=$fname;
    $fortran_fname=~ s/_$//;
    my @symbol_list = split('\n', $symbols);

    foreach $symbol(@symbol_list) {
	# each line should look like this:
	# <foo_func> function 22 <source_file.c> <void foo_func (int len)>
	if($symbol =~ m/^($fname)\s+(function)\s+\d+\s+\S+\s+(.+)/) {
	    # fname was found. return the corresponding prototype
	    ($prototype) = ($symbol =~ m/$fname\s+function\s+\d+\s+\S+\s+(.+)$/);
	    unlink($new_filename);
	    # remove any /* ... */ comment that we might have found
	    $prototype =~ s/\/\*.*\*\///g;
	    return $prototype;
	}

# for fortran files, each line looks like this
# <foo_func>      subroutine   95 <source_file.f> subroutine <foo_func (arg1, arg2)>
	if($symbol =~ m/^($fortran_fname)\s+subroutine/) {
	    # fname was found. return the corresponding prototype
	    printf "$fname found in a subroutine\n";
	    ($prototype) = ($symbol =~ m/$fortran_fname\s+subroutine\s+\d+\s+\S+\s+(.+)$/);
	    unlink($new_filename);
	    # remove any /* ... */ comment that we might have found
	    $prototype =~ s/\/\*.*\*\///g;

	    # replace ^subroutine with void
	    $prototype =~ s/^subroutine $fortran_fname/void $fname/;

	    $prototype =~ s/>//g;

	    # cast each parameter to int*
	    $prototype =~ s/\(\s*(\S+)/\(int *$1/;
	    $prototype =~ s/,/, int */g;
	    if($prototype !~ m/\(/) {
		printf("Proto doesnt contain parenthesis!\n");
		$prototype .= "( )";
	    }
	    return $prototype;
	}
    }

    # not found
    unlink($new_filename);
    return "";
}

if (@ARGV<1) {
    printf "Usage: eztrace_plugin_generator <binary_file>\n";
    exit 1;
}
my $input_file=$ARGV[0];
my $input_file_parsed=$input_file;
$input_file_parsed =~ s/\//_/g;
$input_file_parsed =~ s/-/_/g;
$input_file_parsed =~ s/\./_/g;

my $output_file=$input_file_parsed.".tpl";
my $plugin_dir="plugin_".$input_file_parsed;
my $module_name=$input_file_parsed;
$module_name =~ s/\./_/g;

open($output_handle, '>', $output_file)
    or die "Couldn't open $output_file for writing!";

printf $output_handle "BEGIN_MODULE\n";
printf $output_handle "NAME %s\n", $module_name;
printf $output_handle "DESC \"Module for the %s program\"\n", $input_file;
printf $output_handle "\n";
printf $output_handle "# In this section, insert the #include/#define directives that are needed for\n";
printf $output_handle "# compiling\n";
printf $output_handle "BEGIN_INCLUDE\n";
printf $output_handle "\n";
printf $output_handle "END_INCLUDE\n\n";
printf $output_handle "\n";
printf $output_handle "# In this section, insert the CFLAGS that are needed for compiling\n";
printf $output_handle "BEGIN_CFLAGS\n";
printf $output_handle "\n";
printf $output_handle "END_CFLAGS\n";
printf $output_handle "\n";
printf $output_handle "# In this section, insert the LDFLAGS that are needed for compiling\n";
printf $output_handle "BEGIN_LDFLAGS\n";
printf $output_handle "\n";
printf $output_handle "END_LDFLAGS\n";
printf $output_handle "\n";


my $symbols=`nm --demangle --defined -l $input_file`;
my @symbol_list = split('\n', $symbols);
my $nb_symbols=0;

printf "Creating the plugin script %s\n", $output_file;
foreach $symbol(@symbol_list) {
    if( $symbol =~ m/\s+T\s+(.+)\s+(.+)/ ) {
	($fname, $file) = ( $symbol =~ m/\s+T\s+(\S+)\s+(.+):/ );
	my $proto=extract_prototype($fname, $file);
	if ($proto ne "") {
	    $nb_symbols++;
	    printf "\tFound '%s'\n", $proto;
	    printf $output_handle "%s\n\n", $proto;
	}
    }
}

printf $output_handle "END_MODULE\n";
close($output_handle);

if ( $nb_symbols == 0) {
	# No symbol found
	printf "No symbol found!\n";
	printf "Make sure the program contains debugging symbols (ie. compile it with -the -g option)\n";
	exit ;
}
printf "%d symbols found\n", $nb_symbols;

printf "\nGenerating the plugin...\n";
printf "\t\$ %s/eztrace_create_plugin -o %s %s\n", $bindir, $plugin_dir, $output_file;
`$bindir/eztrace_create_plugin -o $plugin_dir $output_file`;
if ( $? != 0) {
    die "Plugin creation failed !\n";
}

printf "\nCompiling the plugin...\n";
printf "\t\$ make -C %s\n", $plugin_dir;
`make -C $plugin_dir`;
if ( $? != 0) {
    die "Compilation failed ! Please fix the ".$output_file." template file and re-run the eztrace_create_plugin command\n";
}


printf "\nYou can now use the generated plugin by setting the following environment variables:\n";
printf "\t\$ export EZTRACE_LIBRARY_PATH=\$EZTRACE_LIBRARY_PATH:%s\n", `readlink -f $plugin_dir`;