Back to index

glibc  2.9
gmon-start.c
Go to the documentation of this file.
00001 /* Code to enable profiling at program startup.
00002    Copyright (C) 1995,1996,1997,2000,2001,2002 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <sys/types.h>
00021 #include <sys/gmon.h>
00022 #include <stdlib.h>
00023 #include <unistd.h>
00024 #define __ASSEMBLY__
00025 #include <entry.h>
00026 
00027 /* Beginning and end of our code segment. We cannot declare them
00028    as the external functions since we want the addresses of those
00029    labels. Taking the address of a function may have different
00030    meanings on different platforms. */
00031 #ifdef ENTRY_POINT_DECL
00032 ENTRY_POINT_DECL(extern)
00033 #else
00034 extern void ENTRY_POINT;
00035 #endif
00036 extern void etext;
00037 
00038 #ifndef TEXT_START
00039 # ifdef ENTRY_POINT_DECL
00040 #  define TEXT_START ENTRY_POINT
00041 # else
00042 #  define TEXT_START &ENTRY_POINT
00043 # endif
00044 #endif
00045 
00046 #ifndef HAVE_INITFINI
00047 /* This function gets called at startup by the normal constructor
00048    mechanism.  We link this file together with start.o to produce gcrt1.o,
00049    so this constructor will be first in the list.  */
00050 
00051 extern void __gmon_start__ (void) __attribute__ ((constructor));
00052 #else
00053 /* In ELF and COFF, we cannot use the normal constructor mechanism to call
00054    __gmon_start__ because gcrt1.o appears before crtbegin.o in the link.
00055    Instead crti.o calls it specially (see initfini.c).  */
00056 extern void __gmon_start__ (void);
00057 #endif
00058 
00059 void
00060 __gmon_start__ (void)
00061 {
00062 #ifdef HAVE_INITFINI
00063   /* Protect from being called more than once.  Since crti.o is linked
00064      into every shared library, each of their init functions will call us.  */
00065   static int called;
00066 
00067   if (called)
00068     return;
00069 
00070   called = 1;
00071 #endif
00072 
00073   /* Start keeping profiling records.  */
00074   __monstartup ((u_long) TEXT_START, (u_long) &etext);
00075 
00076   /* Call _mcleanup before exiting; it will write out gmon.out from the
00077      collected data.  */
00078   atexit (&_mcleanup);
00079 }