Blame libdw/dwarf_getfuncs.c

Packit 032894
/* Get function information.
Packit 032894
   Copyright (C) 2005, 2013, 2015 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
Packit 032894
   Written by Ulrich Drepper <drepper@redhat.com>, 2005.
Packit 032894
Packit 032894
   This file is free software; you can redistribute it and/or modify
Packit 032894
   it under the terms of either
Packit 032894
Packit 032894
     * the GNU Lesser General Public License as published by the Free
Packit 032894
       Software Foundation; either version 3 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or
Packit 032894
Packit 032894
     * the GNU General Public License as published by the Free
Packit 032894
       Software Foundation; either version 2 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or both in parallel, as here.
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 GNU
Packit 032894
   General Public License for more details.
Packit 032894
Packit 032894
   You should have received copies of the GNU General Public License and
Packit 032894
   the GNU Lesser General Public License along with this program.  If
Packit 032894
   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 <dwarf.h>
Packit 032894
#include "libdwP.h"
Packit 032894
Packit 032894
Packit 032894
struct visitor_info
Packit 032894
{
Packit 032894
  /* The user callback of dwarf_getfuncs.  */
Packit 032894
  int (*callback) (Dwarf_Die *, void *);
Packit 032894
Packit 032894
  /* The user arg value to dwarf_getfuncs.  */
Packit 032894
  void *arg;
Packit 032894
Packit 032894
  /* Addr of the DIE offset where to (re)start the search.  Zero for all.  */
Packit 032894
  void *start_addr;
Packit 032894
Packit 032894
  /* Last subprogram DIE addr seen.  */
Packit 032894
  void *last_addr;
Packit 032894
Packit 032894
  /* The CU only contains C functions.  Allows pruning of most subtrees.  */
Packit 032894
  bool c_cu;
Packit 032894
};
Packit 032894
Packit 032894
static int
Packit 032894
tree_visitor (unsigned int depth __attribute__ ((unused)),
Packit 032894
	      struct Dwarf_Die_Chain *chain, void *arg)
Packit 032894
{
Packit 032894
  struct visitor_info *const v = arg;
Packit 032894
  Dwarf_Die *die = &chain->die;
Packit 032894
  void *start_addr = v->start_addr;
Packit 032894
  void *die_addr = die->addr;
Packit 032894
Packit 032894
  /* Pure C CUs can only contain defining subprogram DIEs as direct
Packit 032894
     children of the CU DIE or as nested function inside a normal C
Packit 032894
     code constructs.  */
Packit 032894
  int tag = INTUSE(dwarf_tag) (die);
Packit 032894
  if (v->c_cu
Packit 032894
      && tag != DW_TAG_subprogram
Packit 032894
      && tag != DW_TAG_lexical_block
Packit 032894
      && tag != DW_TAG_inlined_subroutine)
Packit 032894
    {
Packit 032894
      chain->prune = true;
Packit 032894
      return DWARF_CB_OK;
Packit 032894
    }
Packit 032894
Packit 032894
  /* Skip all DIEs till we found the (re)start addr.  */
Packit 032894
  if (start_addr != NULL)
Packit 032894
    {
Packit 032894
      if (die_addr == start_addr)
Packit 032894
	v->start_addr = NULL;
Packit 032894
      return DWARF_CB_OK;
Packit 032894
    }
Packit 032894
Packit 032894
  /* If this isn't a (defining) subprogram entity, skip DIE.  */
Packit 032894
  if (tag != DW_TAG_subprogram
Packit 032894
      || INTUSE(dwarf_hasattr) (die, DW_AT_declaration))
Packit 032894
    return DWARF_CB_OK;
Packit 032894
Packit 032894
  v->last_addr = die_addr;
Packit 032894
  return (*v->callback) (die, v->arg);
Packit 032894
}
Packit 032894
Packit 032894
ptrdiff_t
Packit 032894
dwarf_getfuncs (Dwarf_Die *cudie, int (*callback) (Dwarf_Die *, void *),
Packit 032894
		void *arg, ptrdiff_t offset)
Packit 032894
{
Packit 032894
  if (unlikely (cudie == NULL
Packit 032894
		|| INTUSE(dwarf_tag) (cudie) != DW_TAG_compile_unit))
Packit 032894
    return -1;
Packit 032894
Packit 032894
  int lang = INTUSE(dwarf_srclang) (cudie);
Packit 032894
  bool c_cu = (lang == DW_LANG_C89
Packit 032894
	       || lang == DW_LANG_C
Packit 032894
	       || lang == DW_LANG_C99
Packit 032894
	       || lang == DW_LANG_C11);
Packit 032894
Packit 032894
  struct visitor_info v = { callback, arg, (void *) offset, NULL, c_cu };
Packit 032894
  struct Dwarf_Die_Chain chain = { .die = CUDIE (cudie->cu),
Packit 032894
				   .parent = NULL };
Packit 032894
  int res = __libdw_visit_scopes (0, &chain, NULL, &tree_visitor, NULL, &v);
Packit 032894
Packit 032894
  if (res == DWARF_CB_ABORT)
Packit 032894
    return (ptrdiff_t) v.last_addr;
Packit 032894
  else
Packit 032894
    return res;
Packit 032894
}