Update copyright notices with scripts/update-copyrights
[glibc.git] / debug / pcprofile.c
blob827f413154efedbdc8d101740711be908416bc6b
1 /* Profile PC and write result to FIFO.
2 Copyright (C) 1999-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1999.
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.
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.
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/>. */
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <libc-internal.h>
27 /* Nonzero if we are actually doing something. */
28 static int active;
30 /* The file descriptor of the FIFO. */
31 static int fd;
34 static void
35 __attribute__ ((constructor))
36 install (void)
38 /* See whether the environment variable `PCPROFILE_OUTPUT' is defined.
39 If yes, it should name a FIFO. We open it and mark ourself as active. */
40 const char *outfile = getenv ("PCPROFILE_OUTPUT");
42 if (outfile != NULL && *outfile != '\0')
44 fd = open (outfile, O_RDWR | O_CREAT, 0666);
46 if (fd != -1)
48 uint32_t word;
50 active = 1;
52 /* Write a magic word which tells the reader about the byte
53 order and the size of the following entries. */
54 word = 0xdeb00000 | sizeof (void *);
55 if (TEMP_FAILURE_RETRY (write (fd, &word, 4)) != 4)
57 /* If even this fails we shouldn't try further. */
58 close (fd);
59 fd = -1;
60 active = 0;
67 static void
68 __attribute__ ((destructor))
69 uninstall (void)
71 if (active)
72 close (fd);
76 void
77 __cyg_profile_func_enter (void *this_fn, void *call_site)
79 void *buf[2];
81 if (! active)
82 return;
84 /* Now write out the current position and that of the caller. We do
85 this now, and don't cache the because we want real-time output. */
86 buf[0] = this_fn;
87 buf[1] = call_site;
89 write (fd, buf, sizeof buf);
91 /* We don't handle entry and exit differently here. */
92 strong_alias (__cyg_profile_func_enter, __cyg_profile_func_exit)