Back to index

glibc  2.9
pcprofile.c
Go to the documentation of this file.
00001 /* Profile PC and write result to FIFO.
00002    Copyright (C) 1999, 2000 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1999.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <errno.h>
00022 #include <fcntl.h>
00023 #include <stdint.h>
00024 #include <stdlib.h>
00025 #include <unistd.h>
00026 #include <libc-internal.h>
00027 
00028 /* Nonzero if we are actually doing something.  */
00029 static int active;
00030 
00031 /* The file descriptor of the FIFO.  */
00032 static int fd;
00033 
00034 
00035 static void
00036 __attribute__ ((constructor))
00037 install (void)
00038 {
00039   /* See whether the environment variable `PCPROFILE_OUTPUT' is defined.
00040      If yes, it should name a FIFO.  We open it and mark ourself as active.  */
00041   const char *outfile = getenv ("PCPROFILE_OUTPUT");
00042 
00043   if (outfile != NULL && *outfile != '\0')
00044     {
00045       fd = open (outfile, O_RDWR | O_CREAT, 0666);
00046 
00047       if (fd != -1)
00048        {
00049          uint32_t word;
00050 
00051          active = 1;
00052 
00053          /* Write a magic word which tells the reader about the byte
00054             order and the size of the following entries.  */
00055          word = 0xdeb00000 | sizeof (void *);
00056          if (TEMP_FAILURE_RETRY (write (fd, &word, 4)) != 4)
00057            {
00058              /* If even this fails we shouldn't try further.  */
00059              close (fd);
00060              fd = -1;
00061              active = 0;
00062            }
00063        }
00064     }
00065 }
00066 
00067 
00068 static void
00069 __attribute__ ((destructor))
00070 uninstall (void)
00071 {
00072   if (active)
00073     close (fd);
00074 }
00075 
00076 
00077 void
00078 __cyg_profile_func_enter (void *this_fn, void *call_site)
00079 {
00080   void *buf[2];
00081 
00082   if (! active)
00083     return;
00084 
00085   /* Now write out the current position and that of the caller.  We do
00086      this now, and don't cache the because we want real-time output.  */
00087   buf[0] = this_fn;
00088   buf[1] = call_site;
00089 
00090   write (fd, buf, sizeof buf);
00091 }
00092 /* We don't handle entry and exit differently here.  */
00093 strong_alias (__cyg_profile_func_enter, __cyg_profile_func_exit)