]>
Commit | Line | Data |
---|---|---|
cab30d75 UD |
1 | /* Dump information generating by PC profiling. |
2 | Copyright (C) 1999 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 Library General Public License as | |
8 | published by the Free Software Foundation; either version 2 of the | |
9 | 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 | Library General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Library General Public | |
17 | License along with the GNU C Library; see the file COPYING.LIB. If not, | |
18 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
19 | Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | /* This is mainly and example. It shows how programs which want to use | |
22 | the information should read the file. */ | |
23 | #ifdef HAVE_CONFIG_H | |
24 | # include <config.h> | |
25 | #endif | |
26 | ||
27 | #include <argp.h> | |
28 | #include <byteswap.h> | |
29 | #include <errno.h> | |
30 | #include <error.h> | |
31 | #include <fcntl.h> | |
32 | #include <inttypes.h> | |
33 | #include <libintl.h> | |
34 | #include <stdlib.h> | |
35 | #include <string.h> | |
36 | #include <unistd.h> | |
37 | ||
38 | #include "../version.h" | |
39 | ||
40 | ||
41 | #ifndef _ | |
42 | # define _(Str) gettext (Str) | |
43 | #endif | |
44 | ||
45 | #ifndef N_ | |
46 | # define N_(Str) Str | |
47 | #endif | |
48 | ||
49 | /* Definitions of arguments for argp functions. */ | |
50 | static const struct argp_option options[] = | |
51 | { | |
52 | { NULL, 0, NULL, 0, NULL } | |
53 | }; | |
54 | ||
55 | /* Short description of program. */ | |
56 | static const char doc[] = N_("Dump information generating by PC profiling."); | |
57 | ||
58 | /* Strings for arguments in help texts. */ | |
59 | static const char args_doc[] = N_("[FILE]"); | |
60 | ||
61 | /* Function to print some extra text in the help message. */ | |
62 | static char *more_help (int key, const char *text, void *input); | |
63 | ||
64 | /* Data structure to communicate with argp functions. */ | |
65 | static struct argp argp = | |
66 | { | |
67 | options, NULL, args_doc, doc, NULL, more_help | |
68 | }; | |
69 | ||
70 | ||
71 | int | |
72 | main (int argc, char *argv[]) | |
73 | { | |
74 | int fd; | |
75 | int remaining; | |
76 | int must_swap; | |
77 | uint32_t word; | |
78 | ||
79 | /* Parse and process arguments. */ | |
80 | argp_parse (&argp, argc, argv, 0, &remaining, NULL); | |
81 | ||
82 | if (remaining == argc) | |
83 | fd = STDIN_FILENO; | |
84 | else if (remaining + 1 != argc) | |
85 | { | |
86 | argp_help (&argp, stdout, ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR, | |
87 | program_invocation_short_name); | |
88 | exit (1); | |
89 | } | |
90 | else | |
91 | { | |
92 | /* Open the given file. */ | |
93 | fd = open (argv[remaining], O_RDONLY); | |
94 | ||
95 | if (fd == -1) | |
96 | error (EXIT_FAILURE, errno, _("cannot open input file")); | |
97 | } | |
98 | ||
99 | /* Read the first 4-byte word. It contains the information about | |
100 | the word size and the endianess. */ | |
101 | if (TEMP_FAILURE_RETRY (read (fd, &word, 4)) != 4) | |
102 | error (EXIT_FAILURE, errno, _("cannot read header")); | |
103 | ||
104 | /* Check whether we have to swap the byte order. */ | |
105 | must_swap = (word & 0xfffffff0) == bswap_32 (0xdeb00000); | |
106 | if (must_swap) | |
107 | word = bswap_32 (word); | |
108 | ||
109 | /* We have two loops, one for 32 bit pointers, one for 64 bit pointers. */ | |
110 | if (word == 0xdeb00004) | |
111 | { | |
112 | union | |
113 | { | |
114 | uint32_t ptrs[2]; | |
115 | char bytes[8]; | |
116 | } pair; | |
117 | ||
118 | while (1) | |
119 | { | |
120 | size_t len = sizeof (pair); | |
121 | size_t n; | |
122 | ||
123 | while (len > 0 | |
124 | && (n = TEMP_FAILURE_RETRY (read (fd, &pair.bytes[8 - len], | |
125 | len))) != 0) | |
126 | len -= n; | |
127 | ||
128 | if (len != 0) | |
129 | /* Nothing to read. */ | |
130 | break; | |
131 | ||
132 | printf ("this = %#010" PRIx32 ", caller = %#010" PRIx32 "\n", | |
133 | must_swap ? bswap_32 (pair.ptrs[0]) : pair.ptrs[0], | |
134 | must_swap ? bswap_32 (pair.ptrs[1]) : pair.ptrs[1]); | |
135 | } | |
136 | } | |
137 | else if (word == 0xdeb00008) | |
138 | { | |
139 | union | |
140 | { | |
141 | uint64_t ptrs[2]; | |
142 | char bytes[16]; | |
143 | } pair; | |
144 | ||
145 | while (1) | |
146 | { | |
147 | size_t len = sizeof (pair); | |
148 | size_t n; | |
149 | ||
150 | while (len > 0 | |
151 | && (n = TEMP_FAILURE_RETRY (read (fd, &pair.bytes[8 - len], | |
152 | len))) != 0) | |
153 | len -= n; | |
154 | ||
155 | if (len != 0) | |
156 | /* Nothing to read. */ | |
157 | break; | |
158 | ||
159 | printf ("this = %#018" PRIx64 ", caller = %#018" PRIx64 "\n", | |
160 | must_swap ? bswap_64 (pair.ptrs[0]) : pair.ptrs[0], | |
161 | must_swap ? bswap_64 (pair.ptrs[1]) : pair.ptrs[1]); | |
162 | } | |
163 | } | |
164 | else | |
165 | /* This should not happen. */ | |
166 | error (EXIT_FAILURE, 0, _("invalid pointer size")); | |
167 | ||
168 | /* Clean up. */ | |
169 | close (fd); | |
170 | ||
171 | return 0; | |
172 | } | |
173 | ||
174 | static char * | |
175 | more_help (int key, const char *text, void *input) | |
176 | { | |
177 | switch (key) | |
178 | { | |
179 | case ARGP_KEY_HELP_EXTRA: | |
180 | /* We print some extra information. */ | |
181 | return strdup (gettext ("\ | |
182 | Report bugs using the `glibcbug' script to <bugs@gnu.org>.\n")); | |
183 | default: | |
184 | break; | |
185 | } | |
186 | return (char *) text; | |
187 | } |