1 /* Dump information generated by PC profiling.
2 Copyright (C) 1999, 2002, 2007, 2009 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 /* This is mainly an example. It shows how programs which want to use
21 the information should read the file. */
37 #include "../version.h"
39 #define PACKAGE _libc_intl_domainname
42 # define _(Str) gettext (Str)
49 /* Definitions of arguments for argp functions. */
50 static const struct argp_option options[] =
52 { "unbuffered", 'u', NULL, 0, N_("Don't buffer output") },
53 { NULL, 0, NULL, 0, NULL }
56 /* Short description of program. */
57 static const char doc[] = N_("Dump information generated by PC profiling.");
59 /* Strings for arguments in help texts. */
60 static const char args_doc[] = N_("[FILE]");
62 /* Function to print some extra text in the help message. */
63 static char *more_help (int key, const char *text, void *input);
65 /* Prototype for option handler. */
66 static error_t parse_opt (int key, char *arg, struct argp_state *state);
68 /* Name and version of program. */
69 static void print_version (FILE *stream, struct argp_state *state);
70 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
72 /* Data structure to communicate with argp functions. */
73 static struct argp argp =
75 options, parse_opt, args_doc, doc, NULL, more_help
80 main (int argc, char *argv[])
82 /* Set locale via LC_ALL. */
83 setlocale (LC_ALL, "");
85 /* Set the text message domain. */
88 /* Parse and process arguments. */
90 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
93 if (remaining == argc)
95 else if (remaining + 1 != argc)
97 argp_help (&argp, stdout, ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR,
98 program_invocation_short_name);
103 /* Open the given file. */
104 fd = open (argv[remaining], O_RDONLY);
107 error (EXIT_FAILURE, errno, _("cannot open input file"));
110 /* Read the first 4-byte word. It contains the information about
111 the word size and the endianess. */
113 if (TEMP_FAILURE_RETRY (read (fd, &word, 4)) != 4)
114 error (EXIT_FAILURE, errno, _("cannot read header"));
116 /* Check whether we have to swap the byte order. */
117 int must_swap = (word & 0xfffffff0) == bswap_32 (0xdeb00000);
119 word = bswap_32 (word);
121 /* We have two loops, one for 32 bit pointers, one for 64 bit pointers. */
122 if (word == 0xdeb00004)
132 size_t len = sizeof (pair);
136 && (n = TEMP_FAILURE_RETRY (read (fd, &pair.bytes[8 - len],
141 /* Nothing to read. */
144 printf ("this = %#010" PRIx32 ", caller = %#010" PRIx32 "\n",
145 must_swap ? bswap_32 (pair.ptrs[0]) : pair.ptrs[0],
146 must_swap ? bswap_32 (pair.ptrs[1]) : pair.ptrs[1]);
149 else if (word == 0xdeb00008)
159 size_t len = sizeof (pair);
163 && (n = TEMP_FAILURE_RETRY (read (fd, &pair.bytes[8 - len],
168 /* Nothing to read. */
171 printf ("this = %#018" PRIx64 ", caller = %#018" PRIx64 "\n",
172 must_swap ? bswap_64 (pair.ptrs[0]) : pair.ptrs[0],
173 must_swap ? bswap_64 (pair.ptrs[1]) : pair.ptrs[1]);
177 /* This should not happen. */
178 error (EXIT_FAILURE, 0, _("invalid pointer size"));
187 parse_opt (int key, char *arg, struct argp_state *state)
192 setbuf (stdout, NULL);
195 return ARGP_ERR_UNKNOWN;
201 more_help (int key, const char *text, void *input)
205 case ARGP_KEY_HELP_EXTRA:
206 /* We print some extra information. */
207 return strdup (gettext ("\
208 For bug reporting instructions, please see:\n\
209 <http://www.gnu.org/software/libc/bugs.html>.\n"));
213 return (char *) text;
216 /* Print the version information. */
218 print_version (FILE *stream, struct argp_state *state)
220 fprintf (stream, "pcprofiledump (GNU %s) %s\n", PACKAGE, VERSION);
221 fprintf (stream, gettext ("\
222 Copyright (C) %s Free Software Foundation, Inc.\n\
223 This is free software; see the source for copying conditions. There is NO\n\
224 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
226 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");