Blame debug/pcprofiledump.c

Packit 6c4009
/* Dump information generated by PC profiling.
Packit 6c4009
   Copyright (C) 1999-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1999.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
/* This is mainly an example.  It shows how programs which want to use
Packit 6c4009
   the information should read the file.  */
Packit 6c4009
#ifdef HAVE_CONFIG_H
Packit 6c4009
# include <config.h>
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#include <argp.h>
Packit 6c4009
#include <byteswap.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <error.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <inttypes.h>
Packit 6c4009
#include <libintl.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
Packit 6c4009
#include "../version.h"
Packit 6c4009
Packit 6c4009
#define PACKAGE _libc_intl_domainname
Packit 6c4009
Packit 6c4009
#ifndef _
Packit 6c4009
# define _(Str) gettext (Str)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#ifndef N_
Packit 6c4009
# define N_(Str) Str
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Definitions of arguments for argp functions.  */
Packit 6c4009
static const struct argp_option options[] =
Packit 6c4009
{
Packit 6c4009
  { "unbuffered", 'u', NULL, 0, N_("Don't buffer output") },
Packit 6c4009
  { NULL, 0, NULL, 0, NULL }
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
/* Short description of program.  */
Packit 6c4009
static const char doc[] = N_("Dump information generated by PC profiling.");
Packit 6c4009
Packit 6c4009
/* Strings for arguments in help texts.  */
Packit 6c4009
static const char args_doc[] = N_("[FILE]");
Packit 6c4009
Packit 6c4009
/* Function to print some extra text in the help message.  */
Packit 6c4009
static char *more_help (int key, const char *text, void *input);
Packit 6c4009
Packit 6c4009
/* Prototype for option handler.  */
Packit 6c4009
static error_t parse_opt (int key, char *arg, struct argp_state *state);
Packit 6c4009
Packit 6c4009
/* Name and version of program.  */
Packit 6c4009
static void print_version (FILE *stream, struct argp_state *state);
Packit 6c4009
void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
Packit 6c4009
Packit 6c4009
/* Data structure to communicate with argp functions.  */
Packit 6c4009
static struct argp argp =
Packit 6c4009
{
Packit 6c4009
  options, parse_opt, args_doc, doc, NULL, more_help
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (int argc, char *argv[])
Packit 6c4009
{
Packit 6c4009
  /* Set locale via LC_ALL.  */
Packit 6c4009
  setlocale (LC_ALL, "");
Packit 6c4009
Packit 6c4009
  /* Set the text message domain.  */
Packit 6c4009
  textdomain (PACKAGE);
Packit 6c4009
Packit 6c4009
  /* Parse and process arguments.  */
Packit 6c4009
  int remaining;
Packit 6c4009
  argp_parse (&argp, argc, argv, 0, &remaining, NULL);
Packit 6c4009
Packit 6c4009
  int fd;
Packit 6c4009
  if (remaining == argc)
Packit 6c4009
    fd = STDIN_FILENO;
Packit 6c4009
  else if (remaining + 1 != argc)
Packit 6c4009
    {
Packit 6c4009
      argp_help (&argp, stdout, ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR,
Packit 6c4009
		 program_invocation_short_name);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* Open the given file.  */
Packit 6c4009
      fd = open (argv[remaining], O_RDONLY);
Packit 6c4009
Packit 6c4009
      if (fd == -1)
Packit 6c4009
	error (EXIT_FAILURE, errno, _("cannot open input file"));
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Read the first 4-byte word.  It contains the information about
Packit 6c4009
     the word size and the endianess.  */
Packit 6c4009
  uint32_t word;
Packit 6c4009
  if (TEMP_FAILURE_RETRY (read (fd, &word, 4)) != 4)
Packit 6c4009
    error (EXIT_FAILURE, errno, _("cannot read header"));
Packit 6c4009
Packit 6c4009
  /* Check whether we have to swap the byte order.  */
Packit 6c4009
  int must_swap = (word & 0x0fffffff) == bswap_32 (0xdeb00000);
Packit 6c4009
  if (must_swap)
Packit 6c4009
    word = bswap_32 (word);
Packit 6c4009
Packit 6c4009
  /* We have two loops, one for 32 bit pointers, one for 64 bit pointers.  */
Packit 6c4009
  if (word == 0xdeb00004)
Packit 6c4009
    {
Packit 6c4009
      union
Packit 6c4009
      {
Packit 6c4009
	uint32_t ptrs[2];
Packit 6c4009
	char bytes[8];
Packit 6c4009
      } pair;
Packit 6c4009
Packit 6c4009
      while (1)
Packit 6c4009
	{
Packit 6c4009
	  size_t len = sizeof (pair);
Packit 6c4009
	  size_t n;
Packit 6c4009
Packit 6c4009
	  while (len > 0
Packit 6c4009
		 && (n = TEMP_FAILURE_RETRY (read (fd, &pair.bytes[8 - len],
Packit 6c4009
						   len))) != 0)
Packit 6c4009
	    len -= n;
Packit 6c4009
Packit 6c4009
	  if (len != 0)
Packit 6c4009
	    /* Nothing to read.  */
Packit 6c4009
	    break;
Packit 6c4009
Packit 6c4009
	  printf ("this = %#010" PRIx32 ", caller = %#010" PRIx32 "\n",
Packit 6c4009
		  must_swap ? bswap_32 (pair.ptrs[0]) : pair.ptrs[0],
Packit 6c4009
		  must_swap ? bswap_32 (pair.ptrs[1]) : pair.ptrs[1]);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else if (word == 0xdeb00008)
Packit 6c4009
    {
Packit 6c4009
      union
Packit 6c4009
      {
Packit 6c4009
	uint64_t ptrs[2];
Packit 6c4009
	char bytes[16];
Packit 6c4009
      } pair;
Packit 6c4009
Packit 6c4009
      while (1)
Packit 6c4009
	{
Packit 6c4009
	  size_t len = sizeof (pair);
Packit 6c4009
	  size_t n;
Packit 6c4009
Packit 6c4009
	  while (len > 0
Packit 6c4009
		 && (n = TEMP_FAILURE_RETRY (read (fd, &pair.bytes[8 - len],
Packit 6c4009
						   len))) != 0)
Packit 6c4009
	    len -= n;
Packit 6c4009
Packit 6c4009
	  if (len != 0)
Packit 6c4009
	    /* Nothing to read.  */
Packit 6c4009
	    break;
Packit 6c4009
Packit 6c4009
	  printf ("this = %#018" PRIx64 ", caller = %#018" PRIx64 "\n",
Packit 6c4009
		  must_swap ? bswap_64 (pair.ptrs[0]) : pair.ptrs[0],
Packit 6c4009
		  must_swap ? bswap_64 (pair.ptrs[1]) : pair.ptrs[1]);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    /* This should not happen.  */
Packit 6c4009
    error (EXIT_FAILURE, 0, _("invalid pointer size"));
Packit 6c4009
Packit 6c4009
  /* Clean up.  */
Packit 6c4009
  close (fd);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static error_t
Packit 6c4009
parse_opt (int key, char *arg, struct argp_state *state)
Packit 6c4009
{
Packit 6c4009
  switch (key)
Packit 6c4009
    {
Packit 6c4009
    case 'u':
Packit 6c4009
      setbuf (stdout, NULL);
Packit 6c4009
      break;
Packit 6c4009
    default:
Packit 6c4009
      return ARGP_ERR_UNKNOWN;
Packit 6c4009
    }
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static char *
Packit 6c4009
more_help (int key, const char *text, void *input)
Packit 6c4009
{
Packit 6c4009
  char *tp = NULL;
Packit 6c4009
  switch (key)
Packit 6c4009
    {
Packit 6c4009
    case ARGP_KEY_HELP_EXTRA:
Packit 6c4009
      /* We print some extra information.  */
Packit 6c4009
      if (asprintf (&tp, gettext ("\
Packit 6c4009
For bug reporting instructions, please see:\n\
Packit 6c4009
%s.\n"), REPORT_BUGS_TO) < 0)
Packit 6c4009
	return NULL;
Packit 6c4009
      return tp;
Packit 6c4009
    default:
Packit 6c4009
      break;
Packit 6c4009
    }
Packit 6c4009
  return (char *) text;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Print the version information.  */
Packit 6c4009
static void
Packit 6c4009
print_version (FILE *stream, struct argp_state *state)
Packit 6c4009
{
Packit 6c4009
  fprintf (stream, "pcprofiledump %s%s\n", PKGVERSION, VERSION);
Packit 6c4009
  fprintf (stream, gettext ("\
Packit 6c4009
Copyright (C) %s Free Software Foundation, Inc.\n\
Packit 6c4009
This is free software; see the source for copying conditions.  There is NO\n\
Packit 6c4009
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
Packit 6c4009
"), "2018");
Packit 6c4009
  fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
Packit 6c4009
}