]> git.ipfire.org Git - thirdparty/glibc.git/blame - debug/pcprofile.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / debug / pcprofile.c
CommitLineData
87843f15 1/* Profile PC and write result to FIFO.
6d7e8eda 2 Copyright (C) 1999-2023 Free Software Foundation, Inc.
87843f15 3 This file is part of the GNU C Library.
87843f15
UD
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.
87843f15
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.
87843f15 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/>. */
87843f15 18
cab30d75 19#include <errno.h>
87843f15 20#include <fcntl.h>
cab30d75 21#include <stdint.h>
87843f15
UD
22#include <stdlib.h>
23#include <unistd.h>
24
25/* Nonzero if we are actually doing something. */
26static int active;
27
28/* The file descriptor of the FIFO. */
29static int fd;
30
31
32static void
33__attribute__ ((constructor))
34install (void)
35{
36 /* See whether the environment variable `PCPROFILE_OUTPUT' is defined.
37 If yes, it should name a FIFO. We open it and mark ourself as active. */
38 const char *outfile = getenv ("PCPROFILE_OUTPUT");
39
40 if (outfile != NULL && *outfile != '\0')
41 {
cab30d75 42 fd = open (outfile, O_RDWR | O_CREAT, 0666);
87843f15
UD
43
44 if (fd != -1)
cab30d75
UD
45 {
46 uint32_t word;
47
48 active = 1;
49
50 /* Write a magic word which tells the reader about the byte
51 order and the size of the following entries. */
52 word = 0xdeb00000 | sizeof (void *);
53 if (TEMP_FAILURE_RETRY (write (fd, &word, 4)) != 4)
54 {
55 /* If even this fails we shouldn't try further. */
56 close (fd);
57 fd = -1;
58 active = 0;
59 }
60 }
87843f15
UD
61 }
62}
63
64
65static void
66__attribute__ ((destructor))
67uninstall (void)
68{
69 if (active)
70 close (fd);
71}
72
73
74void
75__cyg_profile_func_enter (void *this_fn, void *call_site)
76{
77 void *buf[2];
78
79 if (! active)
80 return;
81
82 /* Now write out the current position and that of the caller. We do
83 this now, and don't cache the because we want real-time output. */
84 buf[0] = this_fn;
85 buf[1] = call_site;
86
87 write (fd, buf, sizeof buf);
88}
89/* We don't handle entry and exit differently here. */
90strong_alias (__cyg_profile_func_enter, __cyg_profile_func_exit)