Back to index

glibc  2.9
pcprofiledump.c
Go to the documentation of this file.
00001 /* Dump information generated by PC profiling.
00002    Copyright (C) 1999, 2002, 2007 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1999.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 /* This is mainly an example.  It shows how programs which want to use
00022    the information should read the file.  */
00023 #ifdef HAVE_CONFIG_H
00024 # include <config.h>
00025 #endif
00026 
00027 #include <argp.h>
00028 #include <byteswap.h>
00029 #include <errno.h>
00030 #include <error.h>
00031 #include <fcntl.h>
00032 #include <inttypes.h>
00033 #include <libintl.h>
00034 #include <stdlib.h>
00035 #include <string.h>
00036 #include <unistd.h>
00037 
00038 #include "../version.h"
00039 
00040 #define PACKAGE _libc_intl_domainname
00041 
00042 #ifndef _
00043 # define _(Str) gettext (Str)
00044 #endif
00045 
00046 #ifndef N_
00047 # define N_(Str) Str
00048 #endif
00049 
00050 /* Definitions of arguments for argp functions.  */
00051 static const struct argp_option options[] =
00052 {
00053   { "unbuffered", 'u', NULL, 0, N_("Don't buffer output") },
00054   { NULL, 0, NULL, 0, NULL }
00055 };
00056 
00057 /* Short description of program.  */
00058 static const char doc[] = N_("Dump information generated by PC profiling.");
00059 
00060 /* Strings for arguments in help texts.  */
00061 static const char args_doc[] = N_("[FILE]");
00062 
00063 /* Function to print some extra text in the help message.  */
00064 static char *more_help (int key, const char *text, void *input);
00065 
00066 /* Prototype for option handler.  */
00067 static error_t parse_opt (int key, char *arg, struct argp_state *state);
00068 
00069 /* Data structure to communicate with argp functions.  */
00070 static struct argp argp =
00071 {
00072   options, parse_opt, args_doc, doc, NULL, more_help
00073 };
00074 
00075 
00076 int
00077 main (int argc, char *argv[])
00078 {
00079   /* Set locale via LC_ALL.  */
00080   setlocale (LC_ALL, "");
00081 
00082   /* Set the text message domain.  */
00083   textdomain (PACKAGE);
00084 
00085   /* Parse and process arguments.  */
00086   int remaining;
00087   argp_parse (&argp, argc, argv, 0, &remaining, NULL);
00088 
00089   int fd;
00090   if (remaining == argc)
00091     fd = STDIN_FILENO;
00092   else if (remaining + 1 != argc)
00093     {
00094       argp_help (&argp, stdout, ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR,
00095                program_invocation_short_name);
00096       exit (1);
00097     }
00098   else
00099     {
00100       /* Open the given file.  */
00101       fd = open (argv[remaining], O_RDONLY);
00102 
00103       if (fd == -1)
00104        error (EXIT_FAILURE, errno, _("cannot open input file"));
00105     }
00106 
00107   /* Read the first 4-byte word.  It contains the information about
00108      the word size and the endianess.  */
00109   uint32_t word;
00110   if (TEMP_FAILURE_RETRY (read (fd, &word, 4)) != 4)
00111     error (EXIT_FAILURE, errno, _("cannot read header"));
00112 
00113   /* Check whether we have to swap the byte order.  */
00114   int must_swap = (word & 0xfffffff0) == bswap_32 (0xdeb00000);
00115   if (must_swap)
00116     word = bswap_32 (word);
00117 
00118   /* We have two loops, one for 32 bit pointers, one for 64 bit pointers.  */
00119   if (word == 0xdeb00004)
00120     {
00121       union
00122       {
00123        uint32_t ptrs[2];
00124        char bytes[8];
00125       } pair;
00126 
00127       while (1)
00128        {
00129          size_t len = sizeof (pair);
00130          size_t n;
00131 
00132          while (len > 0
00133                && (n = TEMP_FAILURE_RETRY (read (fd, &pair.bytes[8 - len],
00134                                              len))) != 0)
00135            len -= n;
00136 
00137          if (len != 0)
00138            /* Nothing to read.  */
00139            break;
00140 
00141          printf ("this = %#010" PRIx32 ", caller = %#010" PRIx32 "\n",
00142                 must_swap ? bswap_32 (pair.ptrs[0]) : pair.ptrs[0],
00143                 must_swap ? bswap_32 (pair.ptrs[1]) : pair.ptrs[1]);
00144        }
00145     }
00146   else if (word == 0xdeb00008)
00147     {
00148       union
00149       {
00150        uint64_t ptrs[2];
00151        char bytes[16];
00152       } pair;
00153 
00154       while (1)
00155        {
00156          size_t len = sizeof (pair);
00157          size_t n;
00158 
00159          while (len > 0
00160                && (n = TEMP_FAILURE_RETRY (read (fd, &pair.bytes[8 - len],
00161                                              len))) != 0)
00162            len -= n;
00163 
00164          if (len != 0)
00165            /* Nothing to read.  */
00166            break;
00167 
00168          printf ("this = %#018" PRIx64 ", caller = %#018" PRIx64 "\n",
00169                 must_swap ? bswap_64 (pair.ptrs[0]) : pair.ptrs[0],
00170                 must_swap ? bswap_64 (pair.ptrs[1]) : pair.ptrs[1]);
00171        }
00172     }
00173   else
00174     /* This should not happen.  */
00175     error (EXIT_FAILURE, 0, _("invalid pointer size"));
00176 
00177   /* Clean up.  */
00178   close (fd);
00179 
00180   return 0;
00181 }
00182 
00183 static error_t
00184 parse_opt (int key, char *arg, struct argp_state *state)
00185 {
00186   switch (key)
00187     {
00188     case 'u':
00189       setbuf (stdout, NULL);
00190       break;
00191     default:
00192       return ARGP_ERR_UNKNOWN;
00193     }
00194   return 0;
00195 }
00196 
00197 static char *
00198 more_help (int key, const char *text, void *input)
00199 {
00200   switch (key)
00201     {
00202     case ARGP_KEY_HELP_EXTRA:
00203       /* We print some extra information.  */
00204       return strdup (gettext ("\
00205 For bug reporting instructions, please see:\n\
00206 <http://www.gnu.org/software/libc/bugs.html>.\n"));
00207     default:
00208       break;
00209     }
00210   return (char *) text;
00211 }