Blame tests/get-aranges.c

Packit 032894
/* Copyright (C) 2002, 2004 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
Packit 032894
   Written by Ulrich Drepper <drepper@redhat.com>, 2002.
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
#ifdef HAVE_CONFIG_H
Packit 032894
# include <config.h>
Packit 032894
#endif
Packit 032894
Packit 032894
#include <fcntl.h>
Packit 032894
#include <libelf.h>
Packit 032894
#include ELFUTILS_HEADER(dw)
Packit 032894
#include <stdio.h>
Packit 032894
#include <unistd.h>
Packit 032894
Packit 032894
Packit 032894
static const Dwarf_Addr testaddr[] =
Packit 032894
{
Packit 032894
  0x804842b, 0x804842c, 0x804843c, 0x8048459, 0x804845a,
Packit 032894
  0x804845b, 0x804845c, 0x8048460, 0x8048465, 0x8048466,
Packit 032894
  0x8048467, 0x8048468, 0x8048470, 0x8048471, 0x8048472
Packit 032894
};
Packit 032894
#define ntestaddr (sizeof (testaddr) / sizeof (testaddr[0]))
Packit 032894
Packit 032894
Packit 032894
int
Packit 032894
main (int argc, char *argv[])
Packit 032894
{
Packit 032894
  int result = 0;
Packit 032894
  int cnt;
Packit 032894
Packit 032894
  for (cnt = 1; cnt < argc; ++cnt)
Packit 032894
    {
Packit 032894
      int fd = open (argv[cnt], O_RDONLY);
Packit 032894
Packit 032894
      Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
Packit 032894
      if (dbg == NULL)
Packit 032894
	{
Packit 032894
	  printf ("%s not usable\n", argv[cnt]);
Packit 032894
	  result = 1;
Packit 032894
	  close (fd);
Packit 032894
	  continue;
Packit 032894
	}
Packit 032894
Packit 032894
      Dwarf_Aranges *aranges;
Packit 032894
      size_t naranges;
Packit 032894
      if (dwarf_getaranges (dbg, &aranges, &naranges) != 0)
Packit 032894
	printf ("%s: cannot get aranges\n", argv[cnt]);
Packit 032894
      else
Packit 032894
	{
Packit 032894
	  for (size_t i = 0; i < ntestaddr; ++i)
Packit 032894
	    {
Packit 032894
	      Dwarf_Arange *found;
Packit 032894
Packit 032894
	      found = dwarf_getarange_addr (aranges, testaddr[i]);
Packit 032894
	      if (found != NULL)
Packit 032894
		{
Packit 032894
		  Dwarf_Off cu_offset;
Packit 032894
Packit 032894
		  if (dwarf_getarangeinfo (found, NULL, NULL, &cu_offset) != 0)
Packit 032894
		    {
Packit 032894
		      puts ("failed to get CU die offset");
Packit 032894
		      result = 1;
Packit 032894
		    }
Packit 032894
		  else
Packit 032894
		    {
Packit 032894
		      const char *cuname;
Packit 032894
		      Dwarf_Die cu_die;
Packit 032894
Packit 032894
		      if (dwarf_offdie (dbg, cu_offset, &cu_die) == NULL
Packit 032894
			  || (cuname = dwarf_diename (&cu_die)) == NULL)
Packit 032894
			{
Packit 032894
			  puts ("failed to get CU die");
Packit 032894
			  result = 1;
Packit 032894
			}
Packit 032894
		      else
Packit 032894
			printf ("CU name: \"%s\"\n", cuname);
Packit 032894
		    }
Packit 032894
		}
Packit 032894
	      else
Packit 032894
		printf ("%#llx: not in range\n",
Packit 032894
			(unsigned long long int) testaddr[i]);
Packit 032894
	    }
Packit 032894
Packit 032894
	  for (size_t i = 0; i < naranges; ++i)
Packit 032894
	    {
Packit 032894
	      Dwarf_Arange *arange = dwarf_onearange (aranges, i);
Packit 032894
	      if (arange == NULL)
Packit 032894
		{
Packit 032894
		  printf ("cannot get arange %zu: %s\n", i, dwarf_errmsg (-1));
Packit 032894
		  break;
Packit 032894
		}
Packit 032894
Packit 032894
	      Dwarf_Addr start;
Packit 032894
	      Dwarf_Word length;
Packit 032894
	      Dwarf_Off cu_offset;
Packit 032894
Packit 032894
	      if (dwarf_getarangeinfo (arange, &start, &length, &cu_offset)
Packit 032894
		  != 0)
Packit 032894
		{
Packit 032894
		  printf ("cannot get info from aranges[%zu]\n", i);
Packit 032894
		  result = 1;
Packit 032894
		}
Packit 032894
	      else
Packit 032894
		{
Packit 032894
		  printf (" [%2zu] start: %#llx, length: %llu, cu: %llu\n",
Packit 032894
			  i, (unsigned long long int) start,
Packit 032894
			  (unsigned long long int) length,
Packit 032894
			  (unsigned long long int) cu_offset);
Packit 032894
Packit 032894
		  const char *cuname;
Packit 032894
		  Dwarf_Die cu_die;
Packit 032894
		  if (dwarf_offdie (dbg, cu_offset, &cu_die) == NULL
Packit 032894
		      || (cuname = dwarf_diename (&cu_die)) == NULL)
Packit 032894
		    {
Packit 032894
		      puts ("failed to get CU die");
Packit 032894
		      result = 1;
Packit 032894
		    }
Packit 032894
		  else
Packit 032894
		    printf ("CU name: \"%s\"\n", cuname);
Packit 032894
		}
Packit 032894
	    }
Packit 032894
	}
Packit 032894
Packit 032894
      dwarf_end (dbg);
Packit 032894
      close (fd);
Packit 032894
    }
Packit 032894
Packit 032894
  return result;
Packit 032894
}