platform.texi: Move LAM after LAHF64_SAHF64
[glibc.git] / debug / pcprofile.c
blobbb7800628093fa47081d4e4c6a301b85f115d38c
1 /* Profile PC and write result to FIFO.
2 Copyright (C) 1999-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <unistd.h>
25 /* Nonzero if we are actually doing something. */
26 static int active;
28 /* The file descriptor of the FIFO. */
29 static int fd;
32 static void
33 __attribute__ ((constructor))
34 install (void)
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");
40 if (outfile != NULL && *outfile != '\0')
42 fd = open (outfile, O_RDWR | O_CREAT, 0666);
44 if (fd != -1)
46 uint32_t word;
48 active = 1;
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)
55 /* If even this fails we shouldn't try further. */
56 close (fd);
57 fd = -1;
58 active = 0;
65 static void
66 __attribute__ ((destructor))
67 uninstall (void)
69 if (active)
70 close (fd);
74 void
75 __cyg_profile_func_enter (void *this_fn, void *call_site)
77 void *buf[2];
79 if (! active)
80 return;
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;
87 write (fd, buf, sizeof buf);
89 /* We don't handle entry and exit differently here. */
90 strong_alias (__cyg_profile_func_enter, __cyg_profile_func_exit)