]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/posix/profil.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / sysdeps / posix / profil.c
CommitLineData
e9607dbe 1/* Low-level statistical profiling support function. Mostly POSIX.1 version.
dff8da6b 2 Copyright (C) 1996-2024 Free Software Foundation, Inc.
478b92f0
UD
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
41bdb6e2
AJ
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.
478b92f0
UD
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
41bdb6e2 13 Lesser General Public License for more details.
478b92f0 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6 16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
e9607dbe
RM
18
19#include <sys/types.h>
20#include <unistd.h>
21#include <errno.h>
22#include <signal.h>
23#include <sys/time.h>
a43565ac 24#include <stdint.h>
f2a2deef 25#include <libc-internal.h>
a992f506 26#include <sigsetops.h>
e9607dbe
RM
27
28#ifndef SIGPROF
29
2826ac7e 30#include <gmon/profil.c>
e9607dbe
RM
31
32#else
33
34static u_short *samples;
35static size_t nsamples;
36static size_t pc_offset;
37static u_int pc_scale;
38
39static inline void
a43565ac 40profil_count (uintptr_t pc)
e9607dbe 41{
a43565ac 42 size_t i = (pc - pc_offset) / 2;
b236e99d
UD
43
44 if (sizeof (unsigned long long int) > sizeof (size_t))
45 i = (unsigned long long int) i * pc_scale / 65536;
46 else
47 i = i / 65536 * pc_scale + i % 65536 * pc_scale / 65536;
48
e9607dbe
RM
49 if (i < nsamples)
50 ++samples[i];
51}
52
ea41469b 53/* Get the machine-dependent definition of `__profil_counter', the signal
e9607dbe
RM
54 handler for SIGPROF. It calls `profil_count' (above) with the PC of the
55 interrupted code. */
56#include "profil-counter.h"
57
58/* Enable statistical profiling, writing samples of the PC into at most
59 SIZE bytes of SAMPLE_BUFFER; every processor clock tick while profiling
60 is enabled, the system examines the user PC and increments
61 SAMPLE_BUFFER[((PC - OFFSET) / 2) * SCALE / 65536]. If SCALE is zero,
62 disable profiling. Returns zero on success, -1 on error. */
63
64int
9a0a462c 65__profil (u_short *sample_buffer, size_t size, size_t offset, u_int scale)
e9607dbe 66{
de7e6366
UD
67 struct sigaction act;
68 struct itimerval timer;
a3848485 69#if !IS_IN (rtld)
ce6e047f
UD
70 static struct sigaction oact;
71 static struct itimerval otimer;
72# define oact_ptr &oact
73# define otimer_ptr &otimer
e9607dbe
RM
74
75 if (sample_buffer == NULL)
76 {
77 /* Disable profiling. */
78 if (samples == NULL)
79 /* Wasn't turned on. */
80 return 0;
e9607dbe 81
50304ef0 82 if (__setitimer (ITIMER_PROF, &otimer, NULL) < 0)
e9607dbe 83 return -1;
b236e99d 84 samples = NULL;
c0b50509 85 return __sigaction (SIGPROF, &oact, NULL);
e9607dbe
RM
86 }
87
57ba7bb4
UD
88 if (samples)
89 {
90 /* Was already turned on. Restore old timer and signal handler
91 first. */
50304ef0 92 if (__setitimer (ITIMER_PROF, &otimer, NULL) < 0
c0b50509 93 || __sigaction (SIGPROF, &oact, NULL) < 0)
57ba7bb4
UD
94 return -1;
95 }
ce6e047f
UD
96#else
97 /* In ld.so profiling should never be disabled once it runs. */
98 //assert (sample_buffer != NULL);
99# define oact_ptr NULL
100# define otimer_ptr NULL
101#endif
57ba7bb4 102
e9607dbe
RM
103 samples = sample_buffer;
104 nsamples = size / sizeof *samples;
105 pc_offset = offset;
106 pc_scale = scale;
107
a43565ac
AZ
108#ifdef SA_SIGINFO
109 act.sa_sigaction = __profil_counter;
110 act.sa_flags = SA_SIGINFO;
111#else
112 act.sa_handler = __profil_counter;
113 act.sa_flags = 0;
114#endif
115 act.sa_flags |= SA_RESTART;
284128f6 116 __sigfillset (&act.sa_mask);
ce6e047f 117 if (__sigaction (SIGPROF, &act, oact_ptr) < 0)
e9607dbe
RM
118 return -1;
119
120 timer.it_value.tv_sec = 0;
f2a2deef 121 timer.it_value.tv_usec = 1000000 / __profile_frequency ();
e9607dbe 122 timer.it_interval = timer.it_value;
ce6e047f 123 return __setitimer (ITIMER_PROF, &timer, otimer_ptr);
e9607dbe 124}
9a0a462c 125weak_alias (__profil, profil)
e9607dbe
RM
126
127#endif