[comp.lang.c++] AT&T C++ 2.0 -- profiling option fix on Sun

newsuser@lth.se (LTH network news server) (09/13/89)

PROBLEM

Compiling with `-p' (profiling) option causes link time compilation
error; exit() multiply defined.

PROBLEM CONDITIONS

AT&T C++ Translator, Release 2.0, on Sun-3 running SunOS 3.5; uses Sun
cc as back end.  Probably applicable to other Sun systems.

DESCRIPTION

The problem arises from the use of exit(3) for two different purposes:

1.  AT&T C++ (cfront) uses exit() to call a function (__dtors) which
    in turn calls the destructors of all static objects.  To be
    "compatible" with existing exit() functions, the cfront exit() also
    calls cleanup() and lastly _exit().

2.  The `-p' (profiling) option will make the program gather profiling
    statistics.  The statistics is then written to a file (mon.out)
    with a specially modified exit() that is automatically linked with
    the program by the Sun cc compiler.

The solution presented in the context diff below solves this problem by
using a Sun-specific function called on_exit().

1.  The _main() function which calls the initialization routines for
    static objects has been modified to "register" __dtors() as a
    clean-up function to be run at program termination.

2.  The exit() provided with C++ 2.0 has been dropped.

When the patches have been applied, a new run-time library (libC.a) must
be built and installed.  The new library can be used both with and without
the `-p' option.

SEE ALSO

exit(3) and on_exit(3).

NOTES

The on_exit() call is specific to Sun Unix and should not be used if
portability is a concern.

It is still not possible to run programs compiled with the `-a' option
to gather profiling information on the "basic block" level.  The
termination routine cannot find the `.d' files that correspond to the
temporary C programs generated by cfront.

The patches below apply to the "patch" version of the compiler.  A similar
solution is probably applicable to the "munch" version.

A similar patch is applicable to AT&T C++, Release 1.2.

This has been tested to some degree, but I cannot guarantee the code is
error free, nor that it doesn't interact with something else.  Comments are
gratefully accepted.

Dag Michael Bruck
--
Department of Automatic Control		Internet:  dag@control.lth.se
Lund Institute of Technology
P. O. Box 118				Phone:	+46 46-108779
S-221 00 Lund, SWEDEN			Fax:    +46 46-138118


================== context diff follows =====================================

*** Patch/OLD_main.c	Tue Jun 13 17:16:57 1989
--- Patch/_main.c	Wed Sep 13 13:30:01 1989
***************
*** 1,4 ****
--- 1,5 ----
  /*ident	"@(#)ctrans:Patch/_main.c	1.1.2.2" */
+ /* Modified for SunOS and `-p' option */
  /**************************************************************************
  			Copyright (c) 1984 AT&T
  	  		  All Rights Reserved  	
***************
*** 21,27 ****
--- 22,30 ----
  
  extern "C" {
  extern void _main();
+ extern void on_exit(void (*)(), int); /* not quite right, but works */
  };
+ extern void __dtors();
  
  extern void _main()
  {
***************
*** 41,44 ****
--- 44,48 ----
      }
  
      __head = previous;
+     on_exit(__dtors, 0);
  }
*** lib/static/OLDexit.c	Tue Jun 13 17:20:17 1989
--- lib/static/exit.c	Wed Sep 13 13:36:31 1989
***************
*** 1,4 ****
--- 1,5 ----
  /*ident	"@(#)ctrans:lib/static/exit.c	1.1.2.2" */
+ /* Modified for SunOS and `-p' option */
  /**************************************************************************
  			Copyright (c) 1984 AT&T
  	  		  All Rights Reserved  	
***************
*** 20,25 ****
--- 21,27 ----
  extern void exit(int i);
  }
  
+ #if 0
  extern void exit(int i)
  {
  	__dtors();
***************
*** 26,28 ****
--- 28,31 ----
  	_cleanup();
  	_exit(i);
  }
+ #endif
-- 
Department of Automatic Control		Internet:  dag@control.lth.se
Lund Institute of Technology
P. O. Box 118				Phone:	+46 46-108779
S-221 00 Lund, SWEDEN			Fax:    +46 46-138118