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