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