Blame debug/pcprofile.c

Packit 6c4009
/* Profile PC and write result to FIFO.
Packit 6c4009
   Copyright (C) 1999-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1999.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
/* Nonzero if we are actually doing something.  */
Packit 6c4009
static int active;
Packit 6c4009
Packit 6c4009
/* The file descriptor of the FIFO.  */
Packit 6c4009
static int fd;
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
__attribute__ ((constructor))
Packit 6c4009
install (void)
Packit 6c4009
{
Packit 6c4009
  /* See whether the environment variable `PCPROFILE_OUTPUT' is defined.
Packit 6c4009
     If yes, it should name a FIFO.  We open it and mark ourself as active.  */
Packit 6c4009
  const char *outfile = getenv ("PCPROFILE_OUTPUT");
Packit 6c4009
Packit 6c4009
  if (outfile != NULL && *outfile != '\0')
Packit 6c4009
    {
Packit 6c4009
      fd = open (outfile, O_RDWR | O_CREAT, 0666);
Packit 6c4009
Packit 6c4009
      if (fd != -1)
Packit 6c4009
	{
Packit 6c4009
	  uint32_t word;
Packit 6c4009
Packit 6c4009
	  active = 1;
Packit 6c4009
Packit 6c4009
	  /* Write a magic word which tells the reader about the byte
Packit 6c4009
	     order and the size of the following entries.  */
Packit 6c4009
	  word = 0xdeb00000 | sizeof (void *);
Packit 6c4009
	  if (TEMP_FAILURE_RETRY (write (fd, &word, 4)) != 4)
Packit 6c4009
	    {
Packit 6c4009
	      /* If even this fails we shouldn't try further.  */
Packit 6c4009
	      close (fd);
Packit 6c4009
	      fd = -1;
Packit 6c4009
	      active = 0;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
__attribute__ ((destructor))
Packit 6c4009
uninstall (void)
Packit 6c4009
{
Packit 6c4009
  if (active)
Packit 6c4009
    close (fd);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
__cyg_profile_func_enter (void *this_fn, void *call_site)
Packit 6c4009
{
Packit 6c4009
  void *buf[2];
Packit 6c4009
Packit 6c4009
  if (! active)
Packit 6c4009
    return;
Packit 6c4009
Packit 6c4009
  /* Now write out the current position and that of the caller.  We do
Packit 6c4009
     this now, and don't cache the because we want real-time output.  */
Packit 6c4009
  buf[0] = this_fn;
Packit 6c4009
  buf[1] = call_site;
Packit 6c4009
Packit 6c4009
  write (fd, buf, sizeof buf);
Packit 6c4009
}
Packit 6c4009
/* We don't handle entry and exit differently here.  */
Packit 6c4009
strong_alias (__cyg_profile_func_enter, __cyg_profile_func_exit)