Blame tests/aggregate_size.c

Packit 032894
/* Test program for dwarf_aggregate_size. Prints size of top-level vars.
Packit 032894
   Copyright (C) 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
#ifdef HAVE_CONFIG_H
Packit 032894
# include <config.h>
Packit 032894
#endif
Packit 032894
Packit 032894
#include <assert.h>
Packit 032894
#include <argp.h>
Packit 032894
#include <inttypes.h>
Packit 032894
#include <fcntl.h>
Packit 032894
#include ELFUTILS_HEADER(dw)
Packit 032894
#include ELFUTILS_HEADER(dwfl)
Packit 032894
#include <stdio.h>
Packit 032894
#include <unistd.h>
Packit 032894
#include <dwarf.h>
Packit 032894
Packit 032894
void
Packit 032894
print_var_type_size (Dwarf_Die *var)
Packit 032894
{
Packit 032894
  Dwarf_Attribute attr_mem;
Packit 032894
  Dwarf_Die type_mem;
Packit 032894
  Dwarf_Die *type;
Packit 032894
  const char *name = dwarf_diename (var);
Packit 032894
Packit 032894
  type = dwarf_formref_die (dwarf_attr (var, DW_AT_type, &attr_mem),
Packit 032894
			    &type_mem);
Packit 032894
  if (type != NULL)
Packit 032894
    {
Packit 032894
      Dwarf_Word size;
Packit 032894
      if (dwarf_aggregate_size (type, &size) < 0)
Packit 032894
        printf ("%s no size: %s\n", name, dwarf_errmsg (-1));
Packit 032894
      else
Packit 032894
	printf ("%s size %" PRIu64 "\n", name, size);
Packit 032894
    }
Packit 032894
  else
Packit 032894
    printf ("%s has no type.\n", name);
Packit 032894
}
Packit 032894
Packit 032894
int
Packit 032894
main (int argc, char *argv[])
Packit 032894
{
Packit 032894
Packit 032894
  int remaining;
Packit 032894
  Dwfl *dwfl;
Packit 032894
  (void) argp_parse (dwfl_standard_argp (), argc, argv, 0, &remaining,
Packit 032894
                     &dwfl);
Packit 032894
  assert (dwfl != NULL);
Packit 032894
Packit 032894
  Dwarf_Die *cu = NULL;
Packit 032894
  Dwarf_Addr dwbias;
Packit 032894
  while ((cu = dwfl_nextcu (dwfl, cu, &dwbias)) != NULL)
Packit 032894
    {
Packit 032894
      Dwarf_Die die_mem;
Packit 032894
      Dwarf_Die *die = &die_mem;
Packit 032894
      dwarf_child (cu, &die_mem);
Packit 032894
Packit 032894
      while (1)
Packit 032894
	{
Packit 032894
	  if (dwarf_tag (die) == DW_TAG_variable)
Packit 032894
	    print_var_type_size (die);
Packit 032894
Packit 032894
	  if (dwarf_siblingof (die, &die_mem) != 0)
Packit 032894
	    break;
Packit 032894
	}
Packit 032894
    }
Packit 032894
Packit 032894
  dwfl_end (dwfl);
Packit 032894
}