Blame tests/funcscopes.c

Packit Service 97d2fb
/* Test program for dwarf_getscopes.
Packit Service 97d2fb
   Copyright (C) 2005, 2014 Red Hat, Inc.
Packit Service 97d2fb
   This file is part of elfutils.
Packit Service 97d2fb
Packit Service 97d2fb
   This file is free software; you can redistribute it and/or modify
Packit Service 97d2fb
   it under the terms of the GNU General Public License as published by
Packit Service 97d2fb
   the Free Software Foundation; either version 3 of the License, or
Packit Service 97d2fb
   (at your option) any later version.
Packit Service 97d2fb
Packit Service 97d2fb
   elfutils is distributed in the hope that it will be useful, but
Packit Service 97d2fb
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 97d2fb
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 97d2fb
   GNU General Public License for more details.
Packit Service 97d2fb
Packit Service 97d2fb
   You should have received a copy of the GNU General Public License
Packit Service 97d2fb
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit Service 97d2fb
Packit Service 97d2fb
#include <config.h>
Packit Service 97d2fb
#include <assert.h>
Packit Service 97d2fb
#include <inttypes.h>
Packit Service 97d2fb
#include ELFUTILS_HEADER(dwfl)
Packit Service 97d2fb
#include <dwarf.h>
Packit Service 97d2fb
#include <argp.h>
Packit Service 97d2fb
#include <stdio.h>
Packit Service 97d2fb
#include <stdio_ext.h>
Packit Service 97d2fb
#include <locale.h>
Packit Service 97d2fb
#include <stdlib.h>
Packit Service 97d2fb
#include <string.h>
Packit Service 97d2fb
#include <fnmatch.h>
Packit Service 97d2fb
#include "system.h"
Packit Service 97d2fb
Packit Service 97d2fb
Packit Service 97d2fb
static void
Packit Service 97d2fb
paddr (const char *prefix, Dwarf_Addr addr, Dwfl_Line *line)
Packit Service 97d2fb
{
Packit Service 97d2fb
  const char *src;
Packit Service 97d2fb
  int lineno, linecol;
Packit Service 97d2fb
  if (line != NULL
Packit Service 97d2fb
      && (src = dwfl_lineinfo (line, &addr, &lineno, &linecol,
Packit Service 97d2fb
			       NULL, NULL)) != NULL)
Packit Service 97d2fb
    {
Packit Service 97d2fb
      if (linecol != 0)
Packit Service 97d2fb
	printf ("%s%#" PRIx64 " (%s:%d:%d)",
Packit Service 97d2fb
		prefix, addr, src, lineno, linecol);
Packit Service 97d2fb
      else
Packit Service 97d2fb
	printf ("%s%#" PRIx64 " (%s:%d)",
Packit Service 97d2fb
		prefix, addr, src, lineno);
Packit Service 97d2fb
    }
Packit Service 97d2fb
  else
Packit Service 97d2fb
    printf ("%s%#" PRIx64, prefix, addr);
Packit Service 97d2fb
}
Packit Service 97d2fb
Packit Service 97d2fb
Packit Service 97d2fb
static void
Packit Service 97d2fb
print_vars (unsigned int indent, Dwarf_Die *die)
Packit Service 97d2fb
{
Packit Service 97d2fb
  Dwarf_Die child;
Packit Service 97d2fb
  if (dwarf_child (die, &child) == 0)
Packit Service 97d2fb
    do
Packit Service 97d2fb
      switch (dwarf_tag (&child))
Packit Service 97d2fb
	{
Packit Service 97d2fb
	case DW_TAG_variable:
Packit Service 97d2fb
	case DW_TAG_formal_parameter:
Packit Service 97d2fb
	  printf ("%*s%-30s[%6" PRIx64 "]\n", indent, "",
Packit Service 97d2fb
		  dwarf_diename (&child),
Packit Service 97d2fb
		  (uint64_t) dwarf_dieoffset (&child));
Packit Service 97d2fb
	  break;
Packit Service 97d2fb
	default:
Packit Service 97d2fb
	  break;
Packit Service 97d2fb
	}
Packit Service 97d2fb
    while (dwarf_siblingof (&child, &child) == 0);
Packit Service 97d2fb
Packit Service 97d2fb
  Dwarf_Attribute attr_mem;
Packit Service 97d2fb
  Dwarf_Die origin;
Packit Service 97d2fb
  if (dwarf_hasattr (die, DW_AT_abstract_origin)
Packit Service 97d2fb
      && dwarf_formref_die (dwarf_attr (die, DW_AT_abstract_origin, &attr_mem),
Packit Service 97d2fb
			    &origin) != NULL
Packit Service 97d2fb
      && dwarf_child (&origin, &child) == 0)
Packit Service 97d2fb
    do
Packit Service 97d2fb
      switch (dwarf_tag (&child))
Packit Service 97d2fb
	{
Packit Service 97d2fb
	case DW_TAG_variable:
Packit Service 97d2fb
	case DW_TAG_formal_parameter:
Packit Service 97d2fb
	  printf ("%*s%s (abstract)\n", indent, "",
Packit Service 97d2fb
		  dwarf_diename (&child));
Packit Service 97d2fb
	  break;
Packit Service 97d2fb
	default:
Packit Service 97d2fb
	  break;
Packit Service 97d2fb
	}
Packit Service 97d2fb
    while (dwarf_siblingof (&child, &child) == 0);
Packit Service 97d2fb
}
Packit Service 97d2fb
Packit Service 97d2fb
Packit Service 97d2fb
#define INDENT 4
Packit Service 97d2fb
Packit Service 97d2fb
struct args
Packit Service 97d2fb
{
Packit Service 97d2fb
  Dwfl *dwfl;
Packit Service 97d2fb
  Dwarf_Die *cu;
Packit Service 97d2fb
  Dwarf_Addr dwbias;
Packit Service 97d2fb
  char **argv;
Packit Service 97d2fb
};
Packit Service 97d2fb
Packit Service 97d2fb
static int
Packit Service 97d2fb
handle_function (Dwarf_Die *funcdie, void *arg)
Packit Service 97d2fb
{
Packit Service 97d2fb
  struct args *a = arg;
Packit Service 97d2fb
Packit Service 97d2fb
  const char *name = dwarf_diename (funcdie);
Packit Service 97d2fb
  char **argv = a->argv;
Packit Service 97d2fb
  if (argv[0] != NULL)
Packit Service 97d2fb
    {
Packit Service 97d2fb
      bool match;
Packit Service 97d2fb
      do
Packit Service 97d2fb
	match = fnmatch (*argv, name, 0) == 0;
Packit Service 97d2fb
      while (!match && *++argv);
Packit Service 97d2fb
      if (!match)
Packit Service 97d2fb
	return 0;
Packit Service 97d2fb
    }
Packit Service 97d2fb
Packit Service 97d2fb
  Dwarf_Die *scopes;
Packit Service 97d2fb
  int n = dwarf_getscopes_die (funcdie, &scopes);
Packit Service 97d2fb
  if (n <= 0)
Packit Service 97d2fb
    error (EXIT_FAILURE, 0, "dwarf_getscopes_die: %s", dwarf_errmsg (-1));
Packit Service 97d2fb
  else
Packit Service 97d2fb
    {
Packit Service 97d2fb
      Dwarf_Addr start, end;
Packit Service 97d2fb
      const char *fname;
Packit Service 97d2fb
      const char *modname = dwfl_module_info (dwfl_cumodule (a->cu), NULL,
Packit Service 97d2fb
					      &start, &end,
Packit Service 97d2fb
					      NULL, NULL,
Packit Service 97d2fb
					      &fname, NULL);
Packit Service 97d2fb
      if (modname == NULL)
Packit Service 97d2fb
	error (EXIT_FAILURE, 0, "dwfl_module_info: %s", dwarf_errmsg (-1));
Packit Service 97d2fb
      if (modname[0] == '\0')
Packit Service 97d2fb
	modname = fname;
Packit Service 97d2fb
      printf ("%s: %#" PRIx64 " .. %#" PRIx64 "\n", modname, start, end);
Packit Service 97d2fb
Packit Service 97d2fb
      unsigned int indent = 0;
Packit Service 97d2fb
      while (n-- > 0)
Packit Service 97d2fb
	{
Packit Service 97d2fb
	  Dwarf_Die *const die = &scopes[n];
Packit Service 97d2fb
Packit Service 97d2fb
	  indent += INDENT;
Packit Service 97d2fb
	  printf ("%*s%s (%#x)", indent, "",
Packit Service 97d2fb
		  dwarf_diename (die) ?: "<unnamed>",
Packit Service 97d2fb
		  dwarf_tag (die));
Packit Service 97d2fb
Packit Service 97d2fb
	  Dwarf_Addr lowpc, highpc;
Packit Service 97d2fb
	  if (dwarf_lowpc (die, &lowpc) == 0
Packit Service 97d2fb
	      && dwarf_highpc (die, &highpc) == 0)
Packit Service 97d2fb
	    {
Packit Service 97d2fb
	      lowpc += a->dwbias;
Packit Service 97d2fb
	      highpc += a->dwbias;
Packit Service 97d2fb
	      Dwfl_Line *loline = dwfl_getsrc (a->dwfl, lowpc);
Packit Service 97d2fb
	      Dwfl_Line *hiline = dwfl_getsrc (a->dwfl, highpc - 1);
Packit Service 97d2fb
	      paddr (": ", lowpc, loline);
Packit Service 97d2fb
	      if (highpc != lowpc)
Packit Service 97d2fb
		paddr (" .. ", highpc - 1, hiline == loline ? NULL : hiline);
Packit Service 97d2fb
	    }
Packit Service 97d2fb
	  puts ("");
Packit Service 97d2fb
Packit Service 97d2fb
	  print_vars (indent + INDENT, die);
Packit Service 97d2fb
	}
Packit Service 97d2fb
      free (scopes);
Packit Service 97d2fb
    }
Packit Service 97d2fb
Packit Service 97d2fb
  return 0;
Packit Service 97d2fb
}
Packit Service 97d2fb
Packit Service 97d2fb
Packit Service 97d2fb
int
Packit Service 97d2fb
main (int argc, char *argv[])
Packit Service 97d2fb
{
Packit Service 97d2fb
  int remaining;
Packit Service 97d2fb
Packit Service 97d2fb
  /* Set locale.  */
Packit Service 97d2fb
  (void) setlocale (LC_ALL, "");
Packit Service 97d2fb
Packit Service 97d2fb
  struct args a = { .dwfl = NULL, .cu = NULL };
Packit Service 97d2fb
Packit Service 97d2fb
  (void) argp_parse (dwfl_standard_argp (), argc, argv, 0, &remaining,
Packit Service 97d2fb
		     &a.dwfl);
Packit Service 97d2fb
  assert (a.dwfl != NULL);
Packit Service 97d2fb
  a.argv = &argv[remaining];
Packit Service 97d2fb
Packit Service 97d2fb
  int result = 0;
Packit Service 97d2fb
Packit Service 97d2fb
  while ((a.cu = dwfl_nextcu (a.dwfl, a.cu, &a.dwbias)) != NULL)
Packit Service 97d2fb
    dwarf_getfuncs (a.cu, &handle_function, &a, 0);
Packit Service 97d2fb
Packit Service 97d2fb
  dwfl_end (a.dwfl);
Packit Service 97d2fb
Packit Service 97d2fb
  return result;
Packit Service 97d2fb
}