Blame tests/line2addr.c

Packit 032894
/* Copyright (C) 2005 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
#ifdef HAVE_CONFIG_H
Packit 032894
# include <config.h>
Packit 032894
#endif
Packit 032894
Packit 032894
#include <inttypes.h>
Packit 032894
#include <assert.h>
Packit 032894
#include ELFUTILS_HEADER(dwfl)
Packit 032894
#include <argp.h>
Packit 032894
#include <stdio.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
print_address (Dwfl_Module *mod, Dwarf_Addr address)
Packit 032894
{
Packit 032894
  int n = dwfl_module_relocations (mod);
Packit 032894
  if (n < 0)
Packit 032894
    error (0, 0, "dwfl_module_relocations: %s", dwfl_errmsg (-1));
Packit 032894
  else if (n > 0)
Packit 032894
    {
Packit 032894
      int i = dwfl_module_relocate_address (mod, &address);
Packit 032894
      if (i < 0)
Packit 032894
	error (0, 0, "dwfl_module_relocate_address: %s", dwfl_errmsg (-1));
Packit 032894
      else
Packit 032894
	{
Packit 032894
	  const char *modname = dwfl_module_info (mod, NULL, NULL, NULL,
Packit 032894
						  NULL, NULL, NULL, NULL);
Packit 032894
	  const char *secname = dwfl_module_relocation_info (mod, i, NULL);
Packit 032894
	  if (n > 1 || secname[0] != '\0')
Packit 032894
	    printf ("%s(%s)+%#" PRIx64, modname, secname, address);
Packit 032894
	  else
Packit 032894
	    printf ("%s+%#" PRIx64, modname, address);
Packit 032894
	  return;
Packit 032894
	}
Packit 032894
    }
Packit 032894
Packit 032894
  printf ("%#" PRIx64, address);
Packit 032894
}
Packit 032894
Packit 032894
Packit 032894
struct args
Packit 032894
{
Packit 032894
  const char *arg;
Packit 032894
  char *file;
Packit 032894
  int line;
Packit 032894
};
Packit 032894
Packit 032894
static int
Packit 032894
handle_module (Dwfl_Module *mod __attribute__ ((unused)),
Packit 032894
	       void **udata __attribute__ ((unused)),
Packit 032894
	       const char *modname, Dwarf_Addr base __attribute__ ((unused)),
Packit 032894
	       Dwarf *dbg __attribute__ ((unused)),
Packit 032894
	       Dwarf_Addr bias __attribute__ ((unused)), void *arg)
Packit 032894
{
Packit 032894
  const struct args *const a = arg;
Packit 032894
Packit 032894
  Dwfl_Line **lines = NULL;
Packit 032894
  size_t nlines = 0;
Packit 032894
Packit 032894
  if (dwfl_module_getsrc_file (mod, a->file, a->line, 0, &lines, &nlines) == 0)
Packit 032894
    {
Packit 032894
      for (size_t inner = 0; inner < nlines; ++inner)
Packit 032894
	{
Packit 032894
	  Dwarf_Addr addr;
Packit 032894
	  int line = a->line, col = 0;
Packit 032894
	  const char *file = dwfl_lineinfo (lines[inner], &addr, &line, &col,
Packit 032894
					    NULL, NULL);
Packit 032894
	  if (file != NULL)
Packit 032894
	    {
Packit 032894
	      printf ("%s -> ", a->arg);
Packit 032894
	      print_address (mod, addr);
Packit 032894
	      if (modname[0] != '\0')
Packit 032894
		printf (" (%s:", modname);
Packit 032894
	      if (strcmp (file, a->file) || line != a->line || col != 0)
Packit 032894
		printf (" %s%s:%d", modname[0] != '\0' ? "" : "(",
Packit 032894
			file, line);
Packit 032894
	      if (col != 0)
Packit 032894
		printf (":%d", col);
Packit 032894
	      if (modname[0] != '\0'
Packit 032894
		  || strcmp (file, a->file) || line != a->line || col != 0)
Packit 032894
		puts (")");
Packit 032894
	      else
Packit 032894
		puts ("");
Packit 032894
	    }
Packit 032894
	}
Packit 032894
      free (lines);
Packit 032894
    }
Packit 032894
Packit 032894
  return DWARF_CB_OK;
Packit 032894
}
Packit 032894
Packit 032894
int
Packit 032894
main (int argc, char *argv[])
Packit 032894
{
Packit 032894
  int cnt;
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, &cnt, &dwfl);
Packit 032894
  assert (dwfl != NULL);
Packit 032894
Packit 032894
  for (; cnt < argc; ++cnt)
Packit 032894
    {
Packit 032894
      struct args a = { .arg = argv[cnt] };
Packit 032894
Packit 032894
      switch (sscanf (a.arg, "%m[^:]:%d", &a.file, &a.line))
Packit 032894
	{
Packit 032894
	default:
Packit 032894
	case 0:
Packit 032894
	  printf ("ignored %s\n", argv[cnt]);
Packit 032894
	  continue;
Packit 032894
	case 1:
Packit 032894
	  a.line = 0;
Packit 032894
	  break;
Packit 032894
	case 2:
Packit 032894
	  break;
Packit 032894
	}
Packit 032894
Packit 032894
      (void) dwfl_getdwarf (dwfl, &handle_module, &a, 0);
Packit 032894
Packit 032894
      free (a.file);
Packit 032894
    }
Packit 032894
Packit 032894
  dwfl_end (dwfl);
Packit 032894
Packit 032894
  return 0;
Packit 032894
}