Blame sysdeps/posix/profil.c

Packit 6c4009
/* Low-level statistical profiling support function.  Mostly POSIX.1 version.
Packit 6c4009
   Copyright (C) 1996-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
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 <sys/types.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <signal.h>
Packit 6c4009
#include <sys/time.h>
Packit 6c4009
#include <libc-internal.h>
Packit 6c4009
#include <sigsetops.h>
Packit 6c4009
Packit 6c4009
#ifndef SIGPROF
Packit 6c4009
Packit 6c4009
#include <gmon/profil.c>
Packit 6c4009
Packit 6c4009
#else
Packit 6c4009
Packit 6c4009
static u_short *samples;
Packit 6c4009
static size_t nsamples;
Packit 6c4009
static size_t pc_offset;
Packit 6c4009
static u_int pc_scale;
Packit 6c4009
Packit 6c4009
static inline void
Packit 6c4009
profil_count (void *pc)
Packit 6c4009
{
Packit 6c4009
  size_t i = (pc - pc_offset - (void *) 0) / 2;
Packit 6c4009
Packit 6c4009
  if (sizeof (unsigned long long int) > sizeof (size_t))
Packit 6c4009
    i = (unsigned long long int) i * pc_scale / 65536;
Packit 6c4009
  else
Packit 6c4009
    i = i / 65536 * pc_scale + i % 65536 * pc_scale / 65536;
Packit 6c4009
Packit 6c4009
  if (i < nsamples)
Packit 6c4009
    ++samples[i];
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Get the machine-dependent definition of `__profil_counter', the signal
Packit 6c4009
   handler for SIGPROF.  It calls `profil_count' (above) with the PC of the
Packit 6c4009
   interrupted code.  */
Packit 6c4009
#include "profil-counter.h"
Packit 6c4009
Packit 6c4009
/* Enable statistical profiling, writing samples of the PC into at most
Packit 6c4009
   SIZE bytes of SAMPLE_BUFFER; every processor clock tick while profiling
Packit 6c4009
   is enabled, the system examines the user PC and increments
Packit 6c4009
   SAMPLE_BUFFER[((PC - OFFSET) / 2) * SCALE / 65536].  If SCALE is zero,
Packit 6c4009
   disable profiling.  Returns zero on success, -1 on error.  */
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__profil (u_short *sample_buffer, size_t size, size_t offset, u_int scale)
Packit 6c4009
{
Packit 6c4009
  struct sigaction act;
Packit 6c4009
  struct itimerval timer;
Packit 6c4009
#if !IS_IN (rtld)
Packit 6c4009
  static struct sigaction oact;
Packit 6c4009
  static struct itimerval otimer;
Packit 6c4009
# define oact_ptr &oact
Packit 6c4009
# define otimer_ptr &otimer
Packit 6c4009
Packit 6c4009
  if (sample_buffer == NULL)
Packit 6c4009
    {
Packit 6c4009
      /* Disable profiling.  */
Packit 6c4009
      if (samples == NULL)
Packit 6c4009
	/* Wasn't turned on.  */
Packit 6c4009
	return 0;
Packit 6c4009
Packit 6c4009
      if (__setitimer (ITIMER_PROF, &otimer, NULL) < 0)
Packit 6c4009
	return -1;
Packit 6c4009
      samples = NULL;
Packit 6c4009
      return __sigaction (SIGPROF, &oact, NULL);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
 if (samples)
Packit 6c4009
    {
Packit 6c4009
      /* Was already turned on.  Restore old timer and signal handler
Packit 6c4009
	 first.  */
Packit 6c4009
      if (__setitimer (ITIMER_PROF, &otimer, NULL) < 0
Packit 6c4009
	  || __sigaction (SIGPROF, &oact, NULL) < 0)
Packit 6c4009
	return -1;
Packit 6c4009
    }
Packit 6c4009
#else
Packit 6c4009
 /* In ld.so profiling should never be disabled once it runs.  */
Packit 6c4009
 //assert (sample_buffer != NULL);
Packit 6c4009
# define oact_ptr NULL
Packit 6c4009
# define otimer_ptr NULL
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  samples = sample_buffer;
Packit 6c4009
  nsamples = size / sizeof *samples;
Packit 6c4009
  pc_offset = offset;
Packit 6c4009
  pc_scale = scale;
Packit 6c4009
Packit 6c4009
  act.sa_handler = (sighandler_t) &__profil_counter;
Packit 6c4009
  act.sa_flags = SA_RESTART;
Packit 6c4009
  __sigfillset (&act.sa_mask);
Packit 6c4009
  if (__sigaction (SIGPROF, &act, oact_ptr) < 0)
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  timer.it_value.tv_sec = 0;
Packit 6c4009
  timer.it_value.tv_usec = 1000000 / __profile_frequency ();
Packit 6c4009
  timer.it_interval = timer.it_value;
Packit 6c4009
  return __setitimer (ITIMER_PROF, &timer, otimer_ptr);
Packit 6c4009
}
Packit 6c4009
weak_alias (__profil, profil)
Packit 6c4009
Packit 6c4009
#endif