]>
Commit | Line | Data |
---|---|---|
0f5219d4 | 1 | /* Dump information generated by PC profiling. |
04277e02 | 2 | Copyright (C) 1999-2019 Free Software Foundation, Inc. |
cab30d75 UD |
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 | |
41bdb6e2 AJ |
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. | |
cab30d75 UD |
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 | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
cab30d75 | 15 | |
41bdb6e2 | 16 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
17 | License along with the GNU C Library; if not, see |
18 | <http://www.gnu.org/licenses/>. */ | |
cab30d75 | 19 | |
30224e2b | 20 | /* This is mainly an example. It shows how programs which want to use |
cab30d75 UD |
21 | the information should read the file. */ |
22 | #ifdef HAVE_CONFIG_H | |
23 | # include <config.h> | |
24 | #endif | |
25 | ||
26 | #include <argp.h> | |
27 | #include <byteswap.h> | |
28 | #include <errno.h> | |
29 | #include <error.h> | |
30 | #include <fcntl.h> | |
31 | #include <inttypes.h> | |
32 | #include <libintl.h> | |
33 | #include <stdlib.h> | |
34 | #include <string.h> | |
35 | #include <unistd.h> | |
e054f494 | 36 | #include <stdint.h> |
cab30d75 UD |
37 | |
38 | #include "../version.h" | |
39 | ||
45dc3ad7 | 40 | #define PACKAGE _libc_intl_domainname |
cab30d75 UD |
41 | |
42 | #ifndef _ | |
43 | # define _(Str) gettext (Str) | |
44 | #endif | |
45 | ||
46 | #ifndef N_ | |
47 | # define N_(Str) Str | |
48 | #endif | |
49 | ||
50 | /* Definitions of arguments for argp functions. */ | |
51 | static const struct argp_option options[] = | |
52 | { | |
30224e2b | 53 | { "unbuffered", 'u', NULL, 0, N_("Don't buffer output") }, |
cab30d75 UD |
54 | { NULL, 0, NULL, 0, NULL } |
55 | }; | |
56 | ||
57 | /* Short description of program. */ | |
0f5219d4 | 58 | static const char doc[] = N_("Dump information generated by PC profiling."); |
cab30d75 UD |
59 | |
60 | /* Strings for arguments in help texts. */ | |
61 | static const char args_doc[] = N_("[FILE]"); | |
62 | ||
63 | /* Function to print some extra text in the help message. */ | |
64 | static char *more_help (int key, const char *text, void *input); | |
65 | ||
30224e2b UD |
66 | /* Prototype for option handler. */ |
67 | static error_t parse_opt (int key, char *arg, struct argp_state *state); | |
68 | ||
cbbcaf23 UD |
69 | /* Name and version of program. */ |
70 | static void print_version (FILE *stream, struct argp_state *state); | |
71 | void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version; | |
72 | ||
cab30d75 UD |
73 | /* Data structure to communicate with argp functions. */ |
74 | static struct argp argp = | |
75 | { | |
30224e2b | 76 | options, parse_opt, args_doc, doc, NULL, more_help |
cab30d75 UD |
77 | }; |
78 | ||
79 | ||
80 | int | |
81 | main (int argc, char *argv[]) | |
82 | { | |
45dc3ad7 UD |
83 | /* Set locale via LC_ALL. */ |
84 | setlocale (LC_ALL, ""); | |
85 | ||
86 | /* Set the text message domain. */ | |
87 | textdomain (PACKAGE); | |
cab30d75 UD |
88 | |
89 | /* Parse and process arguments. */ | |
45dc3ad7 | 90 | int remaining; |
cab30d75 UD |
91 | argp_parse (&argp, argc, argv, 0, &remaining, NULL); |
92 | ||
45dc3ad7 | 93 | int fd; |
cab30d75 UD |
94 | if (remaining == argc) |
95 | fd = STDIN_FILENO; | |
96 | else if (remaining + 1 != argc) | |
97 | { | |
98 | argp_help (&argp, stdout, ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR, | |
99 | program_invocation_short_name); | |
100 | exit (1); | |
101 | } | |
102 | else | |
103 | { | |
104 | /* Open the given file. */ | |
105 | fd = open (argv[remaining], O_RDONLY); | |
106 | ||
107 | if (fd == -1) | |
108 | error (EXIT_FAILURE, errno, _("cannot open input file")); | |
109 | } | |
110 | ||
111 | /* Read the first 4-byte word. It contains the information about | |
112 | the word size and the endianess. */ | |
45dc3ad7 | 113 | uint32_t word; |
cab30d75 UD |
114 | if (TEMP_FAILURE_RETRY (read (fd, &word, 4)) != 4) |
115 | error (EXIT_FAILURE, errno, _("cannot read header")); | |
116 | ||
117 | /* Check whether we have to swap the byte order. */ | |
99d67918 | 118 | int must_swap = (word & 0x0fffffff) == bswap_32 (0xdeb00000); |
cab30d75 UD |
119 | if (must_swap) |
120 | word = bswap_32 (word); | |
121 | ||
122 | /* We have two loops, one for 32 bit pointers, one for 64 bit pointers. */ | |
123 | if (word == 0xdeb00004) | |
124 | { | |
125 | union | |
126 | { | |
127 | uint32_t ptrs[2]; | |
128 | char bytes[8]; | |
129 | } pair; | |
130 | ||
131 | while (1) | |
132 | { | |
133 | size_t len = sizeof (pair); | |
134 | size_t n; | |
135 | ||
136 | while (len > 0 | |
137 | && (n = TEMP_FAILURE_RETRY (read (fd, &pair.bytes[8 - len], | |
138 | len))) != 0) | |
139 | len -= n; | |
140 | ||
141 | if (len != 0) | |
142 | /* Nothing to read. */ | |
143 | break; | |
144 | ||
145 | printf ("this = %#010" PRIx32 ", caller = %#010" PRIx32 "\n", | |
146 | must_swap ? bswap_32 (pair.ptrs[0]) : pair.ptrs[0], | |
147 | must_swap ? bswap_32 (pair.ptrs[1]) : pair.ptrs[1]); | |
148 | } | |
149 | } | |
150 | else if (word == 0xdeb00008) | |
151 | { | |
152 | union | |
153 | { | |
154 | uint64_t ptrs[2]; | |
155 | char bytes[16]; | |
156 | } pair; | |
157 | ||
158 | while (1) | |
159 | { | |
160 | size_t len = sizeof (pair); | |
161 | size_t n; | |
162 | ||
163 | while (len > 0 | |
164 | && (n = TEMP_FAILURE_RETRY (read (fd, &pair.bytes[8 - len], | |
165 | len))) != 0) | |
166 | len -= n; | |
167 | ||
168 | if (len != 0) | |
169 | /* Nothing to read. */ | |
170 | break; | |
171 | ||
172 | printf ("this = %#018" PRIx64 ", caller = %#018" PRIx64 "\n", | |
173 | must_swap ? bswap_64 (pair.ptrs[0]) : pair.ptrs[0], | |
174 | must_swap ? bswap_64 (pair.ptrs[1]) : pair.ptrs[1]); | |
175 | } | |
176 | } | |
177 | else | |
178 | /* This should not happen. */ | |
179 | error (EXIT_FAILURE, 0, _("invalid pointer size")); | |
180 | ||
181 | /* Clean up. */ | |
182 | close (fd); | |
183 | ||
184 | return 0; | |
185 | } | |
186 | ||
30224e2b UD |
187 | static error_t |
188 | parse_opt (int key, char *arg, struct argp_state *state) | |
189 | { | |
190 | switch (key) | |
191 | { | |
192 | case 'u': | |
193 | setbuf (stdout, NULL); | |
194 | break; | |
195 | default: | |
196 | return ARGP_ERR_UNKNOWN; | |
197 | } | |
198 | return 0; | |
199 | } | |
200 | ||
cab30d75 UD |
201 | static char * |
202 | more_help (int key, const char *text, void *input) | |
203 | { | |
8b748aed | 204 | char *tp = NULL; |
cab30d75 UD |
205 | switch (key) |
206 | { | |
207 | case ARGP_KEY_HELP_EXTRA: | |
208 | /* We print some extra information. */ | |
8b748aed | 209 | if (asprintf (&tp, gettext ("\ |
d40eb37a | 210 | For bug reporting instructions, please see:\n\ |
8b748aed JM |
211 | %s.\n"), REPORT_BUGS_TO) < 0) |
212 | return NULL; | |
213 | return tp; | |
cab30d75 UD |
214 | default: |
215 | break; | |
216 | } | |
217 | return (char *) text; | |
218 | } | |
cbbcaf23 UD |
219 | |
220 | /* Print the version information. */ | |
221 | static void | |
222 | print_version (FILE *stream, struct argp_state *state) | |
223 | { | |
8b748aed | 224 | fprintf (stream, "pcprofiledump %s%s\n", PKGVERSION, VERSION); |
cbbcaf23 UD |
225 | fprintf (stream, gettext ("\ |
226 | Copyright (C) %s Free Software Foundation, Inc.\n\ | |
227 | This is free software; see the source for copying conditions. There is NO\n\ | |
228 | warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\ | |
09533208 | 229 | "), "2018"); |
cbbcaf23 UD |
230 | fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper"); |
231 | } |