Blame tests/dwarf-getmacros.c

Packit 032894
/* Test program for dwarf_getmacros and related
Packit 032894
   Copyright (C) 2009, 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 ELFUTILS_HEADER(dw)
Packit 032894
#include <dwarf.h>
Packit 032894
#include <sys/types.h>
Packit 032894
#include <sys/stat.h>
Packit 032894
#include <fcntl.h>
Packit 032894
#include <stdio.h>
Packit 032894
#include <stdint.h>
Packit 032894
#include <stdlib.h>
Packit 032894
#include <assert.h>
Packit 032894
#include <inttypes.h>
Packit 032894
Packit 032894
static void include (Dwarf *dbg, Dwarf_Off macoff, ptrdiff_t token);
Packit 032894
Packit 032894
static int
Packit 032894
mac (Dwarf_Macro *macro, void *dbg)
Packit 032894
{
Packit 032894
  static int level = 0;
Packit 032894
Packit 032894
  unsigned int opcode;
Packit 032894
  dwarf_macro_opcode (macro, &opcode);
Packit 032894
  switch (opcode)
Packit 032894
    {
Packit 032894
    case DW_MACRO_import:
Packit 032894
      {
Packit 032894
	Dwarf_Attribute at;
Packit 032894
	int r = dwarf_macro_param (macro, 0, &at);
Packit 032894
	assert (r == 0);
Packit 032894
Packit 032894
	Dwarf_Word w;
Packit 032894
	r = dwarf_formudata (&at, &w);
Packit 032894
	assert (r == 0);
Packit 032894
Packit 032894
	printf ("%*sinclude %#" PRIx64 "\n", level, "", w);
Packit 032894
	++level;
Packit 032894
	include (dbg, w, DWARF_GETMACROS_START);
Packit 032894
	--level;
Packit 032894
	printf ("%*s/include\n", level, "");
Packit 032894
	break;
Packit 032894
      }
Packit 032894
Packit 032894
    case DW_MACRO_start_file:
Packit 032894
      {
Packit 032894
	Dwarf_Files *files;
Packit 032894
	size_t nfiles;
Packit 032894
	if (dwarf_macro_getsrcfiles (dbg, macro, &files, &nfiles) < 0)
Packit 032894
	  printf ("dwarf_macro_getsrcfiles: %s\n",
Packit 032894
		  dwarf_errmsg (dwarf_errno ()));
Packit 032894
Packit 032894
	Dwarf_Word w = 0;
Packit 032894
	dwarf_macro_param2 (macro, &w, NULL);
Packit 032894
Packit 032894
	const char *name = dwarf_filesrc (files, (size_t) w, NULL, NULL);
Packit 032894
	printf ("%*sfile %s\n", level, "", name);
Packit 032894
	++level;
Packit 032894
	break;
Packit 032894
      }
Packit 032894
Packit 032894
    case DW_MACRO_end_file:
Packit 032894
      {
Packit 032894
	--level;
Packit 032894
	printf ("%*s/file\n", level, "");
Packit 032894
	break;
Packit 032894
      }
Packit 032894
Packit 032894
    case DW_MACINFO_define:
Packit 032894
    case DW_MACRO_define_strp:
Packit 032894
      {
Packit 032894
	const char *value;
Packit 032894
	dwarf_macro_param2 (macro, NULL, &value);
Packit 032894
	printf ("%*s%s\n", level, "", value);
Packit 032894
	break;
Packit 032894
      }
Packit 032894
Packit 032894
    case DW_MACINFO_undef:
Packit 032894
    case DW_MACRO_undef_strp:
Packit 032894
      break;
Packit 032894
Packit 032894
    default:
Packit 032894
      {
Packit 032894
	size_t paramcnt;
Packit 032894
	dwarf_macro_getparamcnt (macro, &paramcnt);
Packit 032894
	printf ("%*sopcode %u with %zd arguments\n",
Packit 032894
		level, "", opcode, paramcnt);
Packit 032894
	break;
Packit 032894
      }
Packit 032894
    }
Packit 032894
Packit 032894
  return DWARF_CB_ABORT;
Packit 032894
}
Packit 032894
Packit 032894
static void
Packit 032894
include (Dwarf *dbg, Dwarf_Off macoff, ptrdiff_t token)
Packit 032894
{
Packit 032894
  while ((token = dwarf_getmacros_off (dbg, macoff, mac, dbg, token)) != 0)
Packit 032894
    if (token == -1)
Packit 032894
      {
Packit 032894
	puts (dwarf_errmsg (dwarf_errno ()));
Packit 032894
	break;
Packit 032894
      }
Packit 032894
}
Packit 032894
Packit 032894
int
Packit 032894
main (int argc, char *argv[])
Packit 032894
{
Packit 032894
  assert (argc >= 3);
Packit 032894
  const char *name = argv[1];
Packit 032894
  ptrdiff_t cuoff = strtol (argv[2], NULL, 0);
Packit 032894
  bool new_style = argc > 3;
Packit 032894
Packit 032894
  int fd = open (name, O_RDONLY);
Packit 032894
  Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
Packit 032894
Packit 032894
  Dwarf_Die cudie_mem, *cudie = dwarf_offdie (dbg, cuoff, &cudie_mem);
Packit 032894
Packit 032894
  for (ptrdiff_t off = new_style ? DWARF_GETMACROS_START : 0;
Packit 032894
       (off = dwarf_getmacros (cudie, mac, dbg, off)); )
Packit 032894
    if (off == -1)
Packit 032894
      {
Packit 032894
	puts (dwarf_errmsg (dwarf_errno ()));
Packit 032894
	break;
Packit 032894
      }
Packit 032894
Packit 032894
  dwarf_end (dbg);
Packit 032894
Packit 032894
  return 0;
Packit 032894
}