Update.
[platform/upstream/glibc.git] / debug / pcprofiledump.c
1 /* Dump information generated by PC profiling.
2    Copyright (C) 1999, 2002 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, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 /* This is mainly an 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   { "unbuffered", 'u', NULL, 0, N_("Don't buffer output") },
53   { NULL, 0, NULL, 0, NULL }
54 };
55
56 /* Short description of program.  */
57 static const char doc[] = N_("Dump information generated by PC profiling.");
58
59 /* Strings for arguments in help texts.  */
60 static const char args_doc[] = N_("[FILE]");
61
62 /* Function to print some extra text in the help message.  */
63 static char *more_help (int key, const char *text, void *input);
64
65 /* Prototype for option handler.  */
66 static error_t parse_opt (int key, char *arg, struct argp_state *state);
67
68 /* Data structure to communicate with argp functions.  */
69 static struct argp argp =
70 {
71   options, parse_opt, args_doc, doc, NULL, more_help
72 };
73
74
75 int
76 main (int argc, char *argv[])
77 {
78   int fd;
79   int remaining;
80   int must_swap;
81   uint32_t word;
82
83   /* Parse and process arguments.  */
84   argp_parse (&argp, argc, argv, 0, &remaining, NULL);
85
86   if (remaining == argc)
87     fd = STDIN_FILENO;
88   else if (remaining + 1 != argc)
89     {
90       argp_help (&argp, stdout, ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR,
91                  program_invocation_short_name);
92       exit (1);
93     }
94   else
95     {
96       /* Open the given file.  */
97       fd = open (argv[remaining], O_RDONLY);
98
99       if (fd == -1)
100         error (EXIT_FAILURE, errno, _("cannot open input file"));
101     }
102
103   /* Read the first 4-byte word.  It contains the information about
104      the word size and the endianess.  */
105   if (TEMP_FAILURE_RETRY (read (fd, &word, 4)) != 4)
106     error (EXIT_FAILURE, errno, _("cannot read header"));
107
108   /* Check whether we have to swap the byte order.  */
109   must_swap = (word & 0xfffffff0) == bswap_32 (0xdeb00000);
110   if (must_swap)
111     word = bswap_32 (word);
112
113   /* We have two loops, one for 32 bit pointers, one for 64 bit pointers.  */
114   if (word == 0xdeb00004)
115     {
116       union
117       {
118         uint32_t ptrs[2];
119         char bytes[8];
120       } pair;
121
122       while (1)
123         {
124           size_t len = sizeof (pair);
125           size_t n;
126
127           while (len > 0
128                  && (n = TEMP_FAILURE_RETRY (read (fd, &pair.bytes[8 - len],
129                                                    len))) != 0)
130             len -= n;
131
132           if (len != 0)
133             /* Nothing to read.  */
134             break;
135
136           printf ("this = %#010" PRIx32 ", caller = %#010" PRIx32 "\n",
137                   must_swap ? bswap_32 (pair.ptrs[0]) : pair.ptrs[0],
138                   must_swap ? bswap_32 (pair.ptrs[1]) : pair.ptrs[1]);
139         }
140     }
141   else if (word == 0xdeb00008)
142     {
143       union
144       {
145         uint64_t ptrs[2];
146         char bytes[16];
147       } pair;
148
149       while (1)
150         {
151           size_t len = sizeof (pair);
152           size_t n;
153
154           while (len > 0
155                  && (n = TEMP_FAILURE_RETRY (read (fd, &pair.bytes[8 - len],
156                                                    len))) != 0)
157             len -= n;
158
159           if (len != 0)
160             /* Nothing to read.  */
161             break;
162
163           printf ("this = %#018" PRIx64 ", caller = %#018" PRIx64 "\n",
164                   must_swap ? bswap_64 (pair.ptrs[0]) : pair.ptrs[0],
165                   must_swap ? bswap_64 (pair.ptrs[1]) : pair.ptrs[1]);
166         }
167     }
168   else
169     /* This should not happen.  */
170     error (EXIT_FAILURE, 0, _("invalid pointer size"));
171
172   /* Clean up.  */
173   close (fd);
174
175   return 0;
176 }
177
178 static error_t
179 parse_opt (int key, char *arg, struct argp_state *state)
180 {
181   switch (key)
182     {
183     case 'u':
184       setbuf (stdout, NULL);
185       break;
186     default:
187       return ARGP_ERR_UNKNOWN;
188     }
189   return 0;
190 }
191
192 static char *
193 more_help (int key, const char *text, void *input)
194 {
195   switch (key)
196     {
197     case ARGP_KEY_HELP_EXTRA:
198       /* We print some extra information.  */
199       return strdup (gettext ("\
200 Report bugs using the `glibcbug' script to <bugs@gnu.org>.\n"));
201     default:
202       break;
203     }
204   return (char *) text;
205 }