[comp.lang.c++] automatic tracing of function entry/exit

jhc@m2jhc.uucp (James H. Coombs) (10/07/90)

Has anyone come up with a way to automatically trace function entries
and exits?  We have a convention of wrapping our functions in hand coded
macros, e.g.:

	Object::Print()
	{
	  TRACEIN("Object::Print");
	  ....
	  TRACEOUT("Object::Print");
	}

This gives us a simple way of viewing the execution of a program, and
it works even when adb is confused.  But, it would be much better to
provide such tracing automatically.

I could write a filter to pre-process the source, but I am hoping to
find a better approach.  I also would like to avoid asking developers
to wrap their function definitions in macro invocations.

Thanks for any ideas.  --Jim

schemers@vela.acs.oakland.edu (Roland Schemers III) (10/07/90)

In article <52352@brunix.UUCP> jhc@iris.brown.edu writes:
>Has anyone come up with a way to automatically trace function entries
>and exits?  We have a convention of wrapping our functions in hand coded
>macros, e.g.:

One way is mentioned in the new C++ ARM. It is as follows:

//
// Class tracer will trace entering and leaving a function. Just
// Declare a Tracer variable at the beggining of the function.
//
// test() {
//    Tracer trace("test");
//    ...
// }

class Tracer {
	const char *mess;
public:
	Tracer(const char *m) { mess=m; cout << "Entering("<<m<<")" << endl; }
	~Tracer() { cout << "Exiting("<<mess<<")" << endl; }
};

This class traces entry and exit of the scope where it is declared.
It is a little easier then what you suggested, but still not generated
automatically.

Roland



-- 
Roland J. Schemers III                              Systems Programmer 
schemers@vela.acs.oakland.edu (Ultrix)              Oakland University 
schemers@argo.acs.oakland.edu (VMS)                 Rochester, MI 48309-4401
"So what's your pointer?"                            (313)-370-4323

jac@gandalf..llnl.gov (James Crotinger) (10/08/90)

  In the October '89 issue of Computer Language there was an article
"A Debugging Class for C++" by Marco Hyman which presented a Trace
class that I find quite useful. At the entry of every function you
declare an instance of the Trace class, passing the name of the function
as an argument:

   main()
   {
      Trace trace = "::main()";
      ...
   }

The trace class has a static member which keeps track of the number of
instances currently alive and indents its output accordingly. It also
overloads operator() to print out diagnostic messages. Through an
environment variable you can select whether you want "trace", "info",
"assert", or "error" information printed out (or any combo, of course). 
Here is some output from 

    ::main()
      AbsoluteFile::AbsoluteFile(...)
	info: name     = stats
	info: mode     = 2
	info: length   = 131072
	info: bufflen  = 2048
	info: size_inc = 0
	info: FREEUS returned unit = 1
	Opening file for read only.
	info: unit       = 1
	info: filename   = stats
	info: type       = 4
	info: filelength = 131072
	info: OPEN returned filelength = 9081
      OldStatFile::OldStatFile(...)
	info: name     = stats
	info: mode     = 2
	info: length   = 131072
	info: bufflen  = 2048
	info: size_inc = 0
	AbsoluteFile::Read( void * const, const long )
	  info: Reading 2 words from address 0...
	  info: ...read.
	info: ID = TYPE: STATS
	AbsoluteFile::Read( void * const, const long )
	  info: Reading 1 words from address 2...
	  info: ...read.
	AbsoluteFile::Read( void * const, const long )
	  info: Reading 1 words from address 3...
	  info: ...read.
	AbsoluteFile::Read( void * const, const long )
	  info: Reading 1 words from address 4...
	  info: ...read.
	AbsoluteFile::Read( void * const, const long )
	  info: Reading 1 words from address 5...
	  info: ...read.
	info: STATS_Version     = 0
	info: dataStartLocation = 16
	info: recordSize        = 45
	info: recordCount       = 201
	info: Checking for correct record size.
	info: recordSize             = 45
	info: 8 * sizeof(StatRecord) = 48
	AbsoluteFile::Read( void * const, const long )
	  info: Reading 45 words from address 16...
	  info: ...read.
      OldStatFile::NextRecord(...)
	info: Getting record 1; size = 48
	AbsoluteFile::Read( void * const, const long )
	  info: Reading 48 words from address 61...
	  info: ...read.
...
etc. We recently got CFRONT running on the CRAYS here which have
no source-level debuggers (not even for Fortran, let alone C++). 
This Trace class is proving immensely useful!

  Jim

P.S. If Marco Hyman reads this, thanks!

--
-----------------------------------------------------------------------------
James A. Crotinger   Lawrence Livermore Nat'l Lab // The above views 
jac@gandalf.llnl.gov P.O. Box 808;  L-630     \\ // are mine and are not 
(415) 422-0259       Livermore CA  94550       \\/ necessarily those of LLNL.

schmidt@indetech.com (Douglas C. Schmidt) (10/09/90)

In article <69413@lll-winken.LLNL.GOV> jac@gandalf..llnl.gov (James Crotinger) writes:
++ 
++   In the October '89 issue of Computer Language there was an article
++ "A Debugging Class for C++" by Marco Hyman which presented a Trace
++ class that I find quite useful. At the entry of every function you
++ declare an instance of the Trace class, passing the name of the function
++ as an argument.

	Does anyone have a machine-readable version of that class that they
could send me? 

		thanks,

			Doug
-- 
____*_  Douglas C. Schmidt          schmidt@indetech.com
\  / /  Independence Technologies   {sun,sharkey,pacbell}!indetech!schmidt
 \/ /   42705 Lawrence Place        FAX: 415 438-2034
  \/    Fremont, CA 94538           Voice: 415 438-2023