Blame tests/addrscopes.c

Packit 032894
/* Test program for dwarf_getscopes.
Packit 032894
   Copyright (C) 2005, 2014 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
Packit 032894
Packit 032894
   This file is free software; you can redistribute it and/or modify
Packit 032894
   it under the terms of the GNU General Public License as published by
Packit 032894
   the Free Software Foundation; either version 3 of the License, or
Packit 032894
   (at your option) any later version.
Packit 032894
Packit 032894
   elfutils is distributed in the hope that it will be useful, but
Packit 032894
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 032894
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 032894
   GNU General Public License for more details.
Packit 032894
Packit 032894
   You should have received a copy of the GNU General Public License
Packit 032894
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 032894
Packit 032894
#include <config.h>
Packit 032894
#include <assert.h>
Packit 032894
#include <inttypes.h>
Packit 032894
#include ELFUTILS_HEADER(dwfl)
Packit 032894
#include <dwarf.h>
Packit 032894
#include <argp.h>
Packit 032894
#include <stdio.h>
Packit 032894
#include <stdio_ext.h>
Packit 032894
#include <locale.h>
Packit 032894
#include <stdlib.h>
Packit 032894
#include <string.h>
Packit 032894
#include "system.h"
Packit 032894
Packit 032894
Packit 032894
static void
Packit 032894
paddr (const char *prefix, Dwarf_Addr addr, Dwfl_Line *line)
Packit 032894
{
Packit 032894
  const char *src;
Packit 032894
  int lineno, linecol;
Packit 032894
  if (line != NULL
Packit 032894
      && (src = dwfl_lineinfo (line, &addr, &lineno, &linecol,
Packit 032894
			       NULL, NULL)) != NULL)
Packit 032894
    {
Packit 032894
      if (linecol != 0)
Packit 032894
	printf ("%s%#" PRIx64 " (%s:%d:%d)",
Packit 032894
		prefix, addr, src, lineno, linecol);
Packit 032894
      else
Packit 032894
	printf ("%s%#" PRIx64 " (%s:%d)",
Packit 032894
		prefix, addr, src, lineno);
Packit 032894
    }
Packit 032894
  else
Packit 032894
    printf ("%s%#" PRIx64, prefix, addr);
Packit 032894
}
Packit 032894
Packit 032894
static void
Packit 032894
print_vars (unsigned int indent, Dwarf_Die *die)
Packit 032894
{
Packit 032894
  Dwarf_Die child;
Packit 032894
  if (dwarf_child (die, &child) == 0)
Packit 032894
    do
Packit 032894
      switch (dwarf_tag (&child))
Packit 032894
	{
Packit 032894
	case DW_TAG_variable:
Packit 032894
	case DW_TAG_formal_parameter:
Packit 032894
	  printf ("%*s%-30s[%6" PRIx64 "]\n", indent, "",
Packit 032894
		  dwarf_diename (&child),
Packit 032894
		  (uint64_t) dwarf_dieoffset (&child));
Packit 032894
	  break;
Packit 032894
	default:
Packit 032894
	  break;
Packit 032894
	}
Packit 032894
    while (dwarf_siblingof (&child, &child) == 0);
Packit 032894
Packit 032894
  Dwarf_Attribute attr_mem;
Packit 032894
  Dwarf_Die origin;
Packit 032894
  if (dwarf_hasattr (die, DW_AT_abstract_origin)
Packit 032894
      && dwarf_formref_die (dwarf_attr (die, DW_AT_abstract_origin, &attr_mem),
Packit 032894
			    &origin) != NULL
Packit 032894
      && dwarf_child (&origin, &child) == 0)
Packit 032894
    do
Packit 032894
      switch (dwarf_tag (&child))
Packit 032894
	{
Packit 032894
	case DW_TAG_variable:
Packit 032894
	case DW_TAG_formal_parameter:
Packit 032894
	  printf ("%*s%s (abstract)\n", indent, "",
Packit 032894
		  dwarf_diename (&child));
Packit 032894
	  break;
Packit 032894
	default:
Packit 032894
	  break;
Packit 032894
	}
Packit 032894
    while (dwarf_siblingof (&child, &child) == 0);
Packit 032894
}
Packit 032894
Packit 032894
Packit 032894
#define INDENT 4
Packit 032894
Packit 032894
static void
Packit 032894
handle_address (GElf_Addr pc, Dwfl *dwfl)
Packit 032894
{
Packit 032894
  Dwarf_Addr cubias;
Packit 032894
  Dwarf_Die *cudie = dwfl_addrdie (dwfl, pc, &cubias);
Packit 032894
  if (cudie == NULL)
Packit 032894
    error (EXIT_FAILURE, 0, "dwfl_addrdie: %s", dwfl_errmsg (-1));
Packit 032894
Packit 032894
  Dwarf_Die *scopes;
Packit 032894
  int n = dwarf_getscopes (cudie, pc - cubias, &scopes);
Packit 032894
  if (n < 0)
Packit 032894
    error (EXIT_FAILURE, 0, "dwarf_getscopes: %s", dwarf_errmsg (-1));
Packit 032894
  else if (n == 0)
Packit 032894
    printf ("%#" PRIx64 ": not in any scope\n", pc);
Packit 032894
  else
Packit 032894
    {
Packit 032894
      printf ("%#" PRIx64 ":\n", pc);
Packit 032894
      unsigned int indent = 0;
Packit 032894
      while (n-- > 0)
Packit 032894
	{
Packit 032894
	  Dwarf_Die *const die = &scopes[n];
Packit 032894
Packit 032894
	  indent += INDENT;
Packit 032894
	  printf ("%*s%s (%#x)", indent, "",
Packit 032894
		  dwarf_diename (die) ?: "<unnamed>",
Packit 032894
		  dwarf_tag (die));
Packit 032894
Packit 032894
	  Dwarf_Addr lowpc, highpc;
Packit 032894
	  if (dwarf_lowpc (die, &lowpc) == 0
Packit 032894
	      && dwarf_highpc (die, &highpc) == 0)
Packit 032894
	    {
Packit 032894
	      lowpc += cubias;
Packit 032894
	      highpc += cubias;
Packit 032894
	      Dwfl_Line *loline = dwfl_getsrc (dwfl, lowpc);
Packit 032894
	      Dwfl_Line *hiline = dwfl_getsrc (dwfl, highpc - 1);
Packit 032894
	      paddr (": ", lowpc, loline);
Packit 032894
	      if (highpc != lowpc)
Packit 032894
		paddr (" .. ", highpc - 1, hiline == loline ? NULL : hiline);
Packit 032894
	    }
Packit 032894
	  puts ("");
Packit 032894
Packit 032894
	  print_vars (indent + INDENT, die);
Packit 032894
	}
Packit 032894
      free (scopes);
Packit 032894
    }
Packit 032894
}
Packit 032894
Packit 032894
int
Packit 032894
main (int argc, char *argv[])
Packit 032894
{
Packit 032894
  int remaining;
Packit 032894
Packit 032894
  /* Set locale.  */
Packit 032894
  (void) setlocale (LC_ALL, "");
Packit 032894
Packit 032894
  Dwfl *dwfl = NULL;
Packit 032894
  (void) argp_parse (dwfl_standard_argp (), argc, argv, 0, &remaining, &dwfl);
Packit 032894
  assert (dwfl != NULL);
Packit 032894
Packit 032894
  int result = 0;
Packit 032894
Packit 032894
  /* Now handle the addresses.  In case none are given on the command
Packit 032894
     line, read from stdin.  */
Packit 032894
  if (remaining == argc)
Packit 032894
    {
Packit 032894
      /* We use no threads here which can interfere with handling a stream.  */
Packit 032894
      (void) __fsetlocking (stdin, FSETLOCKING_BYCALLER);
Packit 032894
Packit 032894
      char *buf = NULL;
Packit 032894
      size_t len = 0;
Packit 032894
      while (!feof_unlocked (stdin))
Packit 032894
	{
Packit 032894
	  if (getline (&buf, &len, stdin) < 0)
Packit 032894
	    break;
Packit 032894
Packit 032894
	  char *endp;
Packit 032894
	  uintmax_t addr = strtoumax (buf, &endp, 0);
Packit 032894
	  if (endp != buf)
Packit 032894
	    handle_address (addr, dwfl);
Packit 032894
	  else
Packit 032894
	    result = 1;
Packit 032894
	}
Packit 032894
Packit 032894
      free (buf);
Packit 032894
    }
Packit 032894
  else
Packit 032894
    {
Packit 032894
      do
Packit 032894
	{
Packit 032894
	  char *endp;
Packit 032894
	  uintmax_t addr = strtoumax (argv[remaining], &endp, 0);
Packit 032894
	  if (endp != argv[remaining])
Packit 032894
	    handle_address (addr, dwfl);
Packit 032894
	  else
Packit 032894
	    result = 1;
Packit 032894
	}
Packit 032894
      while (++remaining < argc);
Packit 032894
    }
Packit 032894
Packit 032894
  dwfl_end (dwfl);
Packit 032894
Packit 032894
  return result;
Packit 032894
}