103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes/* Test program for dwarf_aggregate_size. Prints size of top-level vars.
203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   Copyright (C) 2014 Red Hat, Inc.
303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   This file is part of elfutils.
403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   This file is free software; you can redistribute it and/or modify
603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   it under the terms of the GNU General Public License as published by
703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   the Free Software Foundation; either version 3 of the License, or
803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   (at your option) any later version.
903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
1003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   elfutils is distributed in the hope that it will be useful, but
1103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   WITHOUT ANY WARRANTY; without even the implied warranty of
1203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   GNU General Public License for more details.
1403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
1503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   You should have received a copy of the GNU General Public License
1603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
1703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
1803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#ifdef HAVE_CONFIG_H
1903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# include <config.h>
2003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#endif
2103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
2203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include <assert.h>
2303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include <argp.h>
2403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include <inttypes.h>
2503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include <fcntl.h>
2603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include ELFUTILS_HEADER(dw)
2703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include ELFUTILS_HEADER(dwfl)
2803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include <stdio.h>
2903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include <unistd.h>
3003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include <dwarf.h>
3103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
3203333823c75a1c1887e923828113a1b0fd12020cElliott Hughesvoid
3303333823c75a1c1887e923828113a1b0fd12020cElliott Hughesprint_var_type_size (Dwarf_Die *var)
3403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes{
3503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  Dwarf_Attribute attr_mem;
3603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  Dwarf_Die type_mem;
3703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  Dwarf_Die *type;
3803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  const char *name = dwarf_diename (var);
3903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
4003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  type = dwarf_formref_die (dwarf_attr (var, DW_AT_type, &attr_mem),
4103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes			    &type_mem);
4203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  if (type != NULL)
4303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes    {
4403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes      Dwarf_Word size;
4503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes      if (dwarf_aggregate_size (type, &size) < 0)
4603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes        printf ("%s no size: %s\n", name, dwarf_errmsg (-1));
4703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes      else
4803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	printf ("%s size %" PRIu64 "\n", name, size);
4903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes    }
5003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  else
5103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes    printf ("%s has no type.\n", name);
5203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes}
5303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
5403333823c75a1c1887e923828113a1b0fd12020cElliott Hughesint
5503333823c75a1c1887e923828113a1b0fd12020cElliott Hughesmain (int argc, char *argv[])
5603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes{
5703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
5803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  int remaining;
5903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  Dwfl *dwfl;
6003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  (void) argp_parse (dwfl_standard_argp (), argc, argv, 0, &remaining,
6103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes                     &dwfl);
6203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  assert (dwfl != NULL);
6303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
6403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  Dwarf_Die *cu = NULL;
6503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  Dwarf_Addr dwbias;
6603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  while ((cu = dwfl_nextcu (dwfl, cu, &dwbias)) != NULL)
6703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes    {
6803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes      Dwarf_Die die_mem;
6903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes      Dwarf_Die *die = &die_mem;
7003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes      dwarf_child (cu, &die_mem);
7103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
7203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes      while (1)
7303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	{
7403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	  if (dwarf_tag (die) == DW_TAG_variable)
7503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	    print_var_type_size (die);
7603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
7703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	  if (dwarf_siblingof (die, &die_mem) != 0)
7803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	    break;
7903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	}
8003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes    }
8103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
8203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  dwfl_end (dwfl);
8303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes}
84