| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Low-level statistical profiling support function.  Mostly POSIX.1 version. | 
|  | 2 | Copyright (C) 1996-2016 Free Software Foundation, Inc. | 
|  | 3 | This file is part of the GNU C Library. | 
|  | 4 |  | 
|  | 5 | The GNU C Library is free software; you can redistribute it and/or | 
|  | 6 | modify it under the terms of the GNU Lesser General Public | 
|  | 7 | License as published by the Free Software Foundation; either | 
|  | 8 | version 2.1 of the License, or (at your option) any later version. | 
|  | 9 |  | 
|  | 10 | The GNU C Library is distributed in the hope that it will be useful, | 
|  | 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|  | 13 | Lesser General Public License for more details. | 
|  | 14 |  | 
|  | 15 | You should have received a copy of the GNU Lesser General Public | 
|  | 16 | License along with the GNU C Library; if not, see | 
|  | 17 | <http://www.gnu.org/licenses/>.  */ | 
|  | 18 |  | 
|  | 19 | #include <sys/types.h> | 
|  | 20 | #include <unistd.h> | 
|  | 21 | #include <errno.h> | 
|  | 22 | #include <signal.h> | 
|  | 23 | #include <sys/time.h> | 
|  | 24 | #include <libc-internal.h> | 
|  | 25 |  | 
|  | 26 | #ifndef SIGPROF | 
|  | 27 |  | 
|  | 28 | #include <gmon/profil.c> | 
|  | 29 |  | 
|  | 30 | #else | 
|  | 31 |  | 
|  | 32 | static u_short *samples; | 
|  | 33 | static size_t nsamples; | 
|  | 34 | static size_t pc_offset; | 
|  | 35 | static u_int pc_scale; | 
|  | 36 |  | 
|  | 37 | static inline void | 
|  | 38 | profil_count (void *pc) | 
|  | 39 | { | 
|  | 40 | size_t i = (pc - pc_offset - (void *) 0) / 2; | 
|  | 41 |  | 
|  | 42 | if (sizeof (unsigned long long int) > sizeof (size_t)) | 
|  | 43 | i = (unsigned long long int) i * pc_scale / 65536; | 
|  | 44 | else | 
|  | 45 | i = i / 65536 * pc_scale + i % 65536 * pc_scale / 65536; | 
|  | 46 |  | 
|  | 47 | if (i < nsamples) | 
|  | 48 | ++samples[i]; | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | /* Get the machine-dependent definition of `__profil_counter', the signal | 
|  | 52 | handler for SIGPROF.  It calls `profil_count' (above) with the PC of the | 
|  | 53 | interrupted code.  */ | 
|  | 54 | #include "profil-counter.h" | 
|  | 55 |  | 
|  | 56 | /* Enable statistical profiling, writing samples of the PC into at most | 
|  | 57 | SIZE bytes of SAMPLE_BUFFER; every processor clock tick while profiling | 
|  | 58 | is enabled, the system examines the user PC and increments | 
|  | 59 | SAMPLE_BUFFER[((PC - OFFSET) / 2) * SCALE / 65536].  If SCALE is zero, | 
|  | 60 | disable profiling.  Returns zero on success, -1 on error.  */ | 
|  | 61 |  | 
|  | 62 | int | 
|  | 63 | __profil (u_short *sample_buffer, size_t size, size_t offset, u_int scale) | 
|  | 64 | { | 
|  | 65 | struct sigaction act; | 
|  | 66 | struct itimerval timer; | 
|  | 67 | #if !IS_IN (rtld) | 
|  | 68 | static struct sigaction oact; | 
|  | 69 | static struct itimerval otimer; | 
|  | 70 | # define oact_ptr &oact | 
|  | 71 | # define otimer_ptr &otimer | 
|  | 72 |  | 
|  | 73 | if (sample_buffer == NULL) | 
|  | 74 | { | 
|  | 75 | /* Disable profiling.  */ | 
|  | 76 | if (samples == NULL) | 
|  | 77 | /* Wasn't turned on.  */ | 
|  | 78 | return 0; | 
|  | 79 |  | 
|  | 80 | if (__setitimer (ITIMER_PROF, &otimer, NULL) < 0) | 
|  | 81 | return -1; | 
|  | 82 | samples = NULL; | 
|  | 83 | return __sigaction (SIGPROF, &oact, NULL); | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | if (samples) | 
|  | 87 | { | 
|  | 88 | /* Was already turned on.  Restore old timer and signal handler | 
|  | 89 | first.  */ | 
|  | 90 | if (__setitimer (ITIMER_PROF, &otimer, NULL) < 0 | 
|  | 91 | || __sigaction (SIGPROF, &oact, NULL) < 0) | 
|  | 92 | return -1; | 
|  | 93 | } | 
|  | 94 | #else | 
|  | 95 | /* In ld.so profiling should never be disabled once it runs.  */ | 
|  | 96 | //assert (sample_buffer != NULL); | 
|  | 97 | # define oact_ptr NULL | 
|  | 98 | # define otimer_ptr NULL | 
|  | 99 | #endif | 
|  | 100 |  | 
|  | 101 | samples = sample_buffer; | 
|  | 102 | nsamples = size / sizeof *samples; | 
|  | 103 | pc_offset = offset; | 
|  | 104 | pc_scale = scale; | 
|  | 105 |  | 
|  | 106 | act.sa_handler = (sighandler_t) &__profil_counter; | 
|  | 107 | act.sa_flags = SA_RESTART; | 
|  | 108 | __sigfillset (&act.sa_mask); | 
|  | 109 | if (__sigaction (SIGPROF, &act, oact_ptr) < 0) | 
|  | 110 | return -1; | 
|  | 111 |  | 
|  | 112 | timer.it_value.tv_sec = 0; | 
|  | 113 | timer.it_value.tv_usec = 1000000 / __profile_frequency (); | 
|  | 114 | timer.it_interval = timer.it_value; | 
|  | 115 | return __setitimer (ITIMER_PROF, &timer, otimer_ptr); | 
|  | 116 | } | 
|  | 117 | weak_alias (__profil, profil) | 
|  | 118 |  | 
|  | 119 | #endif |