Previous: Programming in M4sugar, Up: Programming in M4

8.4 Debugging via autom4te

At times, it is desirable to see what was happening inside m4, to see why output was not matching expectations. However, post-processing done by autom4te means that directly using the m4 builtin m4_traceon is likely to interfere with operation. Also, frequent diversion changes and the concept of forbidden tokens make it difficult to use m4_defn to generate inline comments in the final output.

There are a couple of tools to help with this. One is the use of the --trace option provided by autom4te (as well as each of the programs that wrap autom4te, such as autoconf), in order to inspect when a macro is called and with which arguments. For example, when this paragraph was written, the autoconf version could be found by:

     $ autoconf --trace=AC_INIT
     $ autoconf --trace='AC_INIT:version is $2'
     version is 2.63b.95-3963

Another trick is to print out the expansion of various m4 expressions to standard error or to an independent file, with no further m4 expansion, and without interfering with diversion changes or the post-processing done to standard output. m4_errprintn shows a given expression on standard error. For example, if you want to see the expansion of an autoconf primitive or of one of your autoconf macros, you can do it like this:

     $ cat <<\EOF >
     m4_errprintn([The definition of AC_DEFINE_UNQUOTED:])
     $ autoconf
     error-->The definition of AC_DEFINE_UNQUOTED:
     error-->_AC_DEFINE_Q([], $@)