1/* Profile PC and write result to FIFO.
2 Copyright (C) 1999-2022 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 <https://www.gnu.org/licenses/>. */
18
19#include <errno.h>
20#include <fcntl.h>
21#include <stdint.h>
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 (name: "PCPROFILE_OUTPUT");
39
40 if (outfile != NULL && *outfile != '\0')
41 {
42 fd = open (file: outfile, O_RDWR | O_CREAT, 0666);
43
44 if (fd != -1)
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: fd);
57 fd = -1;
58 active = 0;
59 }
60 }
61 }
62}
63
64
65static void
66__attribute__ ((destructor))
67uninstall (void)
68{
69 if (active)
70 close (fd: 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: fd, buf: buf, n: sizeof buf);
88}
89/* We don't handle entry and exit differently here. */
90strong_alias (__cyg_profile_func_enter, __cyg_profile_func_exit)
91

source code of glibc/debug/pcprofile.c