summaryrefslogtreecommitdiffstats
path: root/bin/trace
diff options
context:
space:
mode:
authorRobb Matzke <matzke@llnl.gov>1998-06-17 20:46:29 (GMT)
committerRobb Matzke <matzke@llnl.gov>1998-06-17 20:46:29 (GMT)
commit22538a0c799bcb82b40346849776e354fbc18fee (patch)
treedcefb55f7e1a6632c89fdd8592588747f16fa9e1 /bin/trace
parent53916f4e5935ae7c36d7dd6e04d1c5e51b7e78ea (diff)
downloadhdf5-22538a0c799bcb82b40346849776e354fbc18fee.zip
hdf5-22538a0c799bcb82b40346849776e354fbc18fee.tar.gz
hdf5-22538a0c799bcb82b40346849776e354fbc18fee.tar.bz2
[svn-r429] Changes since 19980616
---------------------- ./html/tracing.html NEW This entire update is to make it possible for the library to print the name, arguments, and return value of every API call without requiring any extra work from developers or app programmers. This file describes how this all works. ./configure.in Added the `--enable-tracing' switch. If you use it then the library will include code to print API function names, argument names and values, and function return values. However, you must then turn on the tracing by setting the HDF5_TRACE environment variable to a file descriptor number. The default is `--disable-tracing' since enabling it causes a slight increase in library size and a slowdown resulting from an extra function call for each API function call (I couldn't even measure the slowdown :-) ./bin/trace NEW A perl script that synchronizes the H5TRACE() macro calls in the *.c files with the function return type and formal argument names and types. If you use GNU make and gcc then this will be done automatically, otherwise just invoke this script with the names of one or more .c files. You could do it by hand to, but encoding argument types is a little tricky at first. ./config/commence.in Added the $(TRACE) macro, which defaults to the no-op. Added -D_POSIX_SOURCE to the compiler command line. ./src/Makefile.in Override the default for $(TRACE). ./config/depend.in Automatically calls $(TRACE) to synchronize the H5TRACE() macros in any source file that changed. As with makefile dependencies, one way to force synchronization of all files is to remove the `.depend' file. ./MANIFEST Added new files. ./src/H5Eprivate.h Modified HRETURN_ERROR() and HRETURN() for tracing. ./src/H5.c ./src/H5private.h This is where the real tracing work really happens, in H5_trace(). ./src/H5A.c ./src/H5D.c ./src/H5G.c ./src/H5P.c ./src/H5S.c ./src/H5Z.c Added H5TRACE() calls to all API functions. You don't really need these changes if you don't want to merge your stuff because they can be generated automatically by going to the hdf5/src directory and saying ../bin/trace *.c ./src/H5T.c Added H5TRACE() calls. Other stuff below. ./src/H5E.c ./src/H5Epublic.h Added H5TRACE() calls. Created a type H5E_auto_t for the `func' argument of H5Eset_auto() and H5Eget_auto() to make those arguments easier to parse for tracing. It should also make it clearer for users that don't know how to read complicated ANSI data types. ./src/H5F.c Added H5TRACE() calls. Changed a couple `uintn' argument types in API functions to `unsigned int' since `uintn' part of the API. Changed a few "can't" and "cant" error messages to "unable to". ./src/H5Ipublic.h Removed H5_DIRECTORY from the H5I_group_t enum. It wasn't used anywhere. ./src/H5Tconv.c Removed an unused label. ./src/H5Fistore.c ./src/H5Oattr.c ./src/H5Odtype.c ./src/H5T.c ./test/dsets.c ./test/dtypes.c Fixed a warning about a variable possibly used before it's initialized. Added __unused__ to turn off some unused argument warnings that pop up when debugging is turned off and optimizations are turned on.
Diffstat (limited to 'bin/trace')
-rwxr-xr-xbin/trace201
1 files changed, 201 insertions, 0 deletions
diff --git a/bin/trace b/bin/trace
new file mode 100755
index 0000000..a124d5c
--- /dev/null
+++ b/bin/trace
@@ -0,0 +1,201 @@
+#!/usr/bin/perl -w
+require 5.003;
+$Source = "";
+
+##############################################################################
+# A map from type name to type letter. We use this map for two reasons:
+# 1. We want the debugging stuff in the source code to be as unobtrusive as
+# possible, which means as compact as possible.
+# 2. It's easier (faster) to parse these one and two-letter types in the C
+# functions that display debugging results.
+#
+# All type strings are one or two characters. One-character strings
+# are always lower case and should be used for common types.
+# Two-character strings begin with an upper-case letter which is
+# usually the same as the package name.
+#
+%TypeString = ("hbool_t" => "b",
+ "double" => "d",
+ "H5D_layout_t" => "Dl",
+ "H5D_transfer_t" => "Dt",
+ "herr_t" => "e",
+ "H5E_direction_t" => "Ed",
+ "H5E_error_t*" => "Ee",
+ "H5G_link_t" => "Gl",
+ "H5G_stat_t*" => "Gs",
+ "hsize_t" => "h",
+ "hssize_t" => "Hs",
+ "hid_t" => "i",
+ "int" => "Is",
+ "unsigned" => "Iu",
+ "unsigned int" => "Iu",
+ "MPI_Comm" => "Mc",
+ "MPI_Info" => "Mi",
+ "off_t" => "o",
+ "H5P_class_t" => "p",
+ "char*" => "s",
+ "H5T_cset_t", => "Tc",
+ "H5T_norm_t" => "Tn",
+ "H5T_order_t" => "To",
+ "H5T_pad_t" => "Tp",
+ "H5T_sign_t" => "Ts",
+ "H5T_class_t" => "Tt",
+ "H5T_str_t" => "Tz",
+ "void*" => "x",
+ "FILE*" => "x",
+ "H5A_operator_t" => "x",
+ "H5E_auto_t" => "x",
+ "H5E_walk_t" => "x",
+ "H5G_iterate_t" => "x",
+ "H5T_conv_t" => "x",
+ "H5Z_func_t" => "x",
+ "size_t" => "z",
+ "H5Z_method_t" => "Zm",
+ "ssize_t" => "Zs",
+ );
+
+##############################################################################
+# Print an error message.
+#
+sub errmesg ($$@) {
+ my ($file, $func, @mesg) = @_;
+ my ($mesg) = join "", @mesg;
+ my ($lineno) = 1;
+ if ($Source =~ /(.*?\n)($func)/s) {
+ local $_ = $1;
+ $lineno = tr/\n/\n/;
+ }
+
+ print "$file: in function \`$func\':\n";
+ print "$file:$lineno: $mesg\n";
+}
+
+##############################################################################
+# Given a C data type return the type string that goes with it.
+#
+sub argstring ($$$) {
+ my ($file, $func, $atype) = @_;
+ my ($ptr, $tstr) = (0,"!");
+
+ # Normalize the data type by removing redundant white space,
+ # certain type qualifiers, and indirection.
+ $atype =~ s/^\bconst\b//;
+ $atype =~ s/\b__unused__\b//g;
+ $atype =~ s/\s+/ /g;
+ $ptr = length $1 if $atype =~ s/(\*+)//;
+ $atype =~ s/^\s+//;
+ $atype =~ s/\s+$//;
+
+ if ($ptr>0 && exists $TypeString{"$atype*"}) {
+ --$ptr;
+ $tstr = $TypeString{"$atype*"};
+ } elsif (!exists $TypeString{$atype}) {
+ errmesg $file, $func, "unknown type \`$atype", '*'x$ptr, "\'";
+ } else {
+ $tstr = $TypeString{$atype};
+ }
+ return ("*" x $ptr) . $tstr;
+}
+
+##############################################################################
+# Given information about an API function, rewrite that function with
+# updated tracing information.
+#
+sub rewrite_func ($$$$$) {
+ my ($file, $type, $name, $args, $body) = @_;
+ my ($arg,$trace);
+ my (@arg_name, @arg_str);
+ local $_;
+
+ # Parse return value
+ my $rettype = argstring $file, $name, $type;
+ goto error if $rettype =~ /!/;
+
+ # Parse arguments
+ if ($args eq "void") {
+ $trace = "H5TRACE0(\"$rettype\", \"\");\n";
+ } else {
+ my @args = split /,[\s\n]*/, $args;
+ for $arg (@args) {
+ unless ($arg=~/^(([a-z_A-Z]\w*\s+)+\**)
+ ([a-z_A-Z]\w*)(\[\])?
+ (\s*\/\*\s*(in|out|in,\s*out)\s*\*\/)?\s*$/x) {
+ errmesg $file, $name, "unable to parse \`$arg\'";
+ goto error;
+ } else {
+ my ($atype, $aname, $array, $adir) = ($1, $3, $4, $6);
+ $adir ||= "in";
+ next if $adir eq "out";
+ $atype =~ s/\s+$//;
+ $atype .= "*" if $array;
+ push @arg_name, $aname;
+ push @arg_str, argstring $file, $name, $atype;
+ }
+ }
+ $trace = "H5TRACE" . scalar(@arg_str) . "(\"$rettype\",\"";
+ $trace .= join("", @arg_str) . "\"";
+ my $len = 4 + length $trace;
+ for (@arg_name) {
+ if ($len + length >= 78) {
+ $trace .= ",\n $_";
+ $len = 13 + length;
+ } else {
+ $trace .= ",$_";
+ $len += 1 + length;
+ }
+ }
+ $trace .= ");\n";
+ }
+ goto error if grep {/!/} @arg_str;
+
+ # The H5TRACE() statement
+ if ($body =~ /\/\*[ \t]*NO[ \t]*TRACE[ \t]*\*\//) {
+ if ($body =~ /\s*H5TRACE\d+\s*\(/) {
+ errmesg $file, $name, "warning: trace info was not updated";
+ } else {
+ errmesg $file, $name, "warning: trace info was not inserted";
+ }
+ } elsif ($body =~ s/((\n[ \t]*)H5TRACE\d+\s*\(.*?\);)\n/"$2$trace"/es) {
+ # Replaced an H5TRACE macro
+ } elsif ($body=~s/((\n[ \t]*)FUNC_ENTER\s*\([ \t]*?\);)\n/"$1$2$trace"/es) {
+ # Added an H5TRACE macro after a FUNC_ENTER macro.
+ } else {
+ errmesg $file, $name, "unable to insert tracing information";
+ goto error;
+ }
+
+
+ error:
+ return "\n$type\n$name ($args)$body";
+}
+
+##############################################################################
+# Process each source file, rewriting API functions with updated
+# tracing information.
+#
+my $total_api = 0;
+for $file (@ARGV) {
+ # Snarf up the entire file
+ open SOURCE, $file or die "$file: $!\n";
+ $Source = join "", <SOURCE>;
+ close SOURCE;
+
+ # Make modifications
+ my $original = $Source;
+ my $napi = $Source =~ s/\n([a-z]\w*(\s+[a-z]\w*)*)\s*\n #type
+ (H5[A-Z]{1,2}[^_A-Z]\w*) #name
+ \s*\((.*?)\) #args
+ (.*?\n\}[^\n]*) #body
+ /rewrite_func($file,$1,$3,$4,$5)/segx;
+ $total_api += $napi;
+
+ # If the source changed then print out the new version
+ if ($original ne $Source) {
+ printf "%s: instrumented %d API function%s\n",
+ $file, $napi, 1==$napi?"":"s";
+ rename $file, "$file~" or die "unable to make backup";
+ open SOURCE, ">$file" or die "unable to modify source";
+ print SOURCE $Source;
+ close SOURCE;
+ }
+}