125b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* Find a named variable or parameter within given scopes.
225b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Copyright (C) 2005-2009 Red Hat, Inc.
325b3c049e70834cf33790a28643ab058b507b35cBen Cheng   This file is part of Red Hat elfutils.
425b3c049e70834cf33790a28643ab058b507b35cBen Cheng
525b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Red Hat elfutils is free software; you can redistribute it and/or modify
625b3c049e70834cf33790a28643ab058b507b35cBen Cheng   it under the terms of the GNU General Public License as published by the
725b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Free Software Foundation; version 2 of the License.
825b3c049e70834cf33790a28643ab058b507b35cBen Cheng
925b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Red Hat elfutils is distributed in the hope that it will be useful, but
1025b3c049e70834cf33790a28643ab058b507b35cBen Cheng   WITHOUT ANY WARRANTY; without even the implied warranty of
1125b3c049e70834cf33790a28643ab058b507b35cBen Cheng   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1225b3c049e70834cf33790a28643ab058b507b35cBen Cheng   General Public License for more details.
1325b3c049e70834cf33790a28643ab058b507b35cBen Cheng
1425b3c049e70834cf33790a28643ab058b507b35cBen Cheng   You should have received a copy of the GNU General Public License along
1525b3c049e70834cf33790a28643ab058b507b35cBen Cheng   with Red Hat elfutils; if not, write to the Free Software Foundation,
1625b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
1725b3c049e70834cf33790a28643ab058b507b35cBen Cheng
1825b3c049e70834cf33790a28643ab058b507b35cBen Cheng   In addition, as a special exception, Red Hat, Inc. gives You the
1925b3c049e70834cf33790a28643ab058b507b35cBen Cheng   additional right to link the code of Red Hat elfutils with code licensed
2025b3c049e70834cf33790a28643ab058b507b35cBen Cheng   under any Open Source Initiative certified open source license
2125b3c049e70834cf33790a28643ab058b507b35cBen Cheng   (http://www.opensource.org/licenses/index.php) which requires the
2225b3c049e70834cf33790a28643ab058b507b35cBen Cheng   distribution of source code with any binary distribution and to
2325b3c049e70834cf33790a28643ab058b507b35cBen Cheng   distribute linked combinations of the two.  Non-GPL Code permitted under
2425b3c049e70834cf33790a28643ab058b507b35cBen Cheng   this exception must only link to the code of Red Hat elfutils through
2525b3c049e70834cf33790a28643ab058b507b35cBen Cheng   those well defined interfaces identified in the file named EXCEPTION
2625b3c049e70834cf33790a28643ab058b507b35cBen Cheng   found in the source code files (the "Approved Interfaces").  The files
2725b3c049e70834cf33790a28643ab058b507b35cBen Cheng   of Non-GPL Code may instantiate templates or use macros or inline
2825b3c049e70834cf33790a28643ab058b507b35cBen Cheng   functions from the Approved Interfaces without causing the resulting
2925b3c049e70834cf33790a28643ab058b507b35cBen Cheng   work to be covered by the GNU General Public License.  Only Red Hat,
3025b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Inc. may make changes or additions to the list of Approved Interfaces.
3125b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Red Hat's grant of this exception is conditioned upon your not adding
3225b3c049e70834cf33790a28643ab058b507b35cBen Cheng   any new exceptions.  If you wish to add a new Approved Interface or
3325b3c049e70834cf33790a28643ab058b507b35cBen Cheng   exception, please contact Red Hat.  You must obey the GNU General Public
3425b3c049e70834cf33790a28643ab058b507b35cBen Cheng   License in all respects for all of the Red Hat elfutils code and other
3525b3c049e70834cf33790a28643ab058b507b35cBen Cheng   code used in conjunction with Red Hat elfutils except the Non-GPL Code
3625b3c049e70834cf33790a28643ab058b507b35cBen Cheng   covered by this exception.  If you modify this file, you may extend this
3725b3c049e70834cf33790a28643ab058b507b35cBen Cheng   exception to your version of the file, but you are not obligated to do
3825b3c049e70834cf33790a28643ab058b507b35cBen Cheng   so.  If you do not wish to provide this exception without modification,
3925b3c049e70834cf33790a28643ab058b507b35cBen Cheng   you must delete this exception statement from your version and license
4025b3c049e70834cf33790a28643ab058b507b35cBen Cheng   this file solely under the GPL without exception.
4125b3c049e70834cf33790a28643ab058b507b35cBen Cheng
4225b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Red Hat elfutils is an included package of the Open Invention Network.
4325b3c049e70834cf33790a28643ab058b507b35cBen Cheng   An included package of the Open Invention Network is a package for which
4425b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Open Invention Network licensees cross-license their patents.  No patent
4525b3c049e70834cf33790a28643ab058b507b35cBen Cheng   license is granted, either expressly or impliedly, by designation as an
4625b3c049e70834cf33790a28643ab058b507b35cBen Cheng   included package.  Should you wish to participate in the Open Invention
4725b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Network licensing program, please visit www.openinventionnetwork.com
4825b3c049e70834cf33790a28643ab058b507b35cBen Cheng   <http://www.openinventionnetwork.com>.  */
4925b3c049e70834cf33790a28643ab058b507b35cBen Cheng
5025b3c049e70834cf33790a28643ab058b507b35cBen Cheng#ifdef HAVE_CONFIG_H
5125b3c049e70834cf33790a28643ab058b507b35cBen Cheng# include <config.h>
5225b3c049e70834cf33790a28643ab058b507b35cBen Cheng#endif
5325b3c049e70834cf33790a28643ab058b507b35cBen Cheng
5425b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <stdbool.h>
5525b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <string.h>
5625b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include "libdwP.h"
5725b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <dwarf.h>
5825b3c049e70834cf33790a28643ab058b507b35cBen Cheng
5925b3c049e70834cf33790a28643ab058b507b35cBen Cheng
6025b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* Find the containing CU's files.  */
6125b3c049e70834cf33790a28643ab058b507b35cBen Chengstatic int
6225b3c049e70834cf33790a28643ab058b507b35cBen Chenggetfiles (Dwarf_Die *die, Dwarf_Files **files)
6325b3c049e70834cf33790a28643ab058b507b35cBen Cheng{
6425b3c049e70834cf33790a28643ab058b507b35cBen Cheng  return INTUSE(dwarf_getsrcfiles) (&CUDIE (die->cu), files, NULL);
6525b3c049e70834cf33790a28643ab058b507b35cBen Cheng}
6625b3c049e70834cf33790a28643ab058b507b35cBen Cheng
6725b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* Fetch an attribute that should have a constant integer form.  */
6825b3c049e70834cf33790a28643ab058b507b35cBen Chengstatic int
6925b3c049e70834cf33790a28643ab058b507b35cBen Chenggetattr (Dwarf_Die *die, int search_name, Dwarf_Word *value)
7025b3c049e70834cf33790a28643ab058b507b35cBen Cheng{
7125b3c049e70834cf33790a28643ab058b507b35cBen Cheng  Dwarf_Attribute attr_mem;
7225b3c049e70834cf33790a28643ab058b507b35cBen Cheng  return INTUSE(dwarf_formudata) (INTUSE(dwarf_attr) (die, search_name,
7325b3c049e70834cf33790a28643ab058b507b35cBen Cheng						      &attr_mem), value);
7425b3c049e70834cf33790a28643ab058b507b35cBen Cheng}
7525b3c049e70834cf33790a28643ab058b507b35cBen Cheng
7625b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* Search SCOPES[0..NSCOPES-1] for a variable called NAME.
7725b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Ignore the first SKIP_SHADOWS scopes that match the name.
7825b3c049e70834cf33790a28643ab058b507b35cBen Cheng   If MATCH_FILE is not null, accept only declaration in that source file;
7925b3c049e70834cf33790a28643ab058b507b35cBen Cheng   if MATCH_LINENO or MATCH_LINECOL are also nonzero, accept only declaration
8025b3c049e70834cf33790a28643ab058b507b35cBen Cheng   at that line and column.
8125b3c049e70834cf33790a28643ab058b507b35cBen Cheng
8225b3c049e70834cf33790a28643ab058b507b35cBen Cheng   If successful, fill in *RESULT with the DIE of the variable found,
8325b3c049e70834cf33790a28643ab058b507b35cBen Cheng   and return N where SCOPES[N] is the scope defining the variable.
8425b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Return -1 for errors or -2 for no matching variable found.  */
8525b3c049e70834cf33790a28643ab058b507b35cBen Cheng
8625b3c049e70834cf33790a28643ab058b507b35cBen Chengint
8725b3c049e70834cf33790a28643ab058b507b35cBen Chengdwarf_getscopevar (Dwarf_Die *scopes, int nscopes,
8825b3c049e70834cf33790a28643ab058b507b35cBen Cheng		   const char *name, int skip_shadows,
8925b3c049e70834cf33790a28643ab058b507b35cBen Cheng		   const char *match_file, int match_lineno, int match_linecol,
9025b3c049e70834cf33790a28643ab058b507b35cBen Cheng		   Dwarf_Die *result)
9125b3c049e70834cf33790a28643ab058b507b35cBen Cheng{
9225b3c049e70834cf33790a28643ab058b507b35cBen Cheng  /* Match against the given file name.  */
9325b3c049e70834cf33790a28643ab058b507b35cBen Cheng  size_t match_file_len = match_file == NULL ? 0 : strlen (match_file);
9425b3c049e70834cf33790a28643ab058b507b35cBen Cheng  bool lastfile_matches = false;
9525b3c049e70834cf33790a28643ab058b507b35cBen Cheng  const char *lastfile = NULL;
9625b3c049e70834cf33790a28643ab058b507b35cBen Cheng  inline bool file_matches (Dwarf_Files *files, size_t idx)
9725b3c049e70834cf33790a28643ab058b507b35cBen Cheng    {
9825b3c049e70834cf33790a28643ab058b507b35cBen Cheng      if (idx >= files->nfiles)
9925b3c049e70834cf33790a28643ab058b507b35cBen Cheng	return false;
10025b3c049e70834cf33790a28643ab058b507b35cBen Cheng
10125b3c049e70834cf33790a28643ab058b507b35cBen Cheng      const char *file = files->info[idx].name;
10225b3c049e70834cf33790a28643ab058b507b35cBen Cheng      if (file != lastfile)
10325b3c049e70834cf33790a28643ab058b507b35cBen Cheng	{
10425b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  size_t len = strlen (file);
10525b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  lastfile_matches = (len >= match_file_len
10625b3c049e70834cf33790a28643ab058b507b35cBen Cheng			      && !memcmp (match_file, file, match_file_len)
10725b3c049e70834cf33790a28643ab058b507b35cBen Cheng			      && (len == match_file_len
10825b3c049e70834cf33790a28643ab058b507b35cBen Cheng				  || file[len - match_file_len - 1] == '/'));
10925b3c049e70834cf33790a28643ab058b507b35cBen Cheng	}
11025b3c049e70834cf33790a28643ab058b507b35cBen Cheng      return lastfile_matches;
11125b3c049e70834cf33790a28643ab058b507b35cBen Cheng    }
11225b3c049e70834cf33790a28643ab058b507b35cBen Cheng
11325b3c049e70834cf33790a28643ab058b507b35cBen Cheng  /* Start with the innermost scope and move out.  */
11425b3c049e70834cf33790a28643ab058b507b35cBen Cheng  for (int out = 0; out < nscopes; ++out)
11525b3c049e70834cf33790a28643ab058b507b35cBen Cheng    if (INTUSE(dwarf_haschildren) (&scopes[out]))
11625b3c049e70834cf33790a28643ab058b507b35cBen Cheng      {
11725b3c049e70834cf33790a28643ab058b507b35cBen Cheng	if (INTUSE(dwarf_child) (&scopes[out], result) != 0)
11825b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  return -1;
11925b3c049e70834cf33790a28643ab058b507b35cBen Cheng	do
12025b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  {
12125b3c049e70834cf33790a28643ab058b507b35cBen Cheng	    switch (INTUSE(dwarf_tag) (result))
12225b3c049e70834cf33790a28643ab058b507b35cBen Cheng	      {
12325b3c049e70834cf33790a28643ab058b507b35cBen Cheng	      case DW_TAG_variable:
12425b3c049e70834cf33790a28643ab058b507b35cBen Cheng	      case DW_TAG_formal_parameter:
12525b3c049e70834cf33790a28643ab058b507b35cBen Cheng		break;
12625b3c049e70834cf33790a28643ab058b507b35cBen Cheng
12725b3c049e70834cf33790a28643ab058b507b35cBen Cheng	      default:
12825b3c049e70834cf33790a28643ab058b507b35cBen Cheng		continue;
12925b3c049e70834cf33790a28643ab058b507b35cBen Cheng	      }
13025b3c049e70834cf33790a28643ab058b507b35cBen Cheng
13125b3c049e70834cf33790a28643ab058b507b35cBen Cheng	    /* Only get here for a variable or parameter.  Check the name.  */
13225b3c049e70834cf33790a28643ab058b507b35cBen Cheng	    const char *diename = INTUSE(dwarf_diename) (result);
13325b3c049e70834cf33790a28643ab058b507b35cBen Cheng	    if (diename != NULL && !strcmp (name, diename))
13425b3c049e70834cf33790a28643ab058b507b35cBen Cheng	      {
13525b3c049e70834cf33790a28643ab058b507b35cBen Cheng		/* We have a matching name.  */
13625b3c049e70834cf33790a28643ab058b507b35cBen Cheng
13725b3c049e70834cf33790a28643ab058b507b35cBen Cheng		if (skip_shadows > 0)
13825b3c049e70834cf33790a28643ab058b507b35cBen Cheng		  {
13925b3c049e70834cf33790a28643ab058b507b35cBen Cheng		    /* Punt this scope for the one it shadows.  */
14025b3c049e70834cf33790a28643ab058b507b35cBen Cheng		    --skip_shadows;
14125b3c049e70834cf33790a28643ab058b507b35cBen Cheng		    break;
14225b3c049e70834cf33790a28643ab058b507b35cBen Cheng		  }
14325b3c049e70834cf33790a28643ab058b507b35cBen Cheng
14425b3c049e70834cf33790a28643ab058b507b35cBen Cheng		if (match_file != NULL)
14525b3c049e70834cf33790a28643ab058b507b35cBen Cheng		  {
14625b3c049e70834cf33790a28643ab058b507b35cBen Cheng		    /* Check its decl_file.  */
14725b3c049e70834cf33790a28643ab058b507b35cBen Cheng
14825b3c049e70834cf33790a28643ab058b507b35cBen Cheng		    Dwarf_Word i;
14925b3c049e70834cf33790a28643ab058b507b35cBen Cheng		    Dwarf_Files *files;
15025b3c049e70834cf33790a28643ab058b507b35cBen Cheng		    if (getattr (result, DW_AT_decl_file, &i) != 0
15125b3c049e70834cf33790a28643ab058b507b35cBen Cheng			|| getfiles (&scopes[out], &files) != 0)
15225b3c049e70834cf33790a28643ab058b507b35cBen Cheng		      break;
15325b3c049e70834cf33790a28643ab058b507b35cBen Cheng
15425b3c049e70834cf33790a28643ab058b507b35cBen Cheng		    if (!file_matches (files, i))
15525b3c049e70834cf33790a28643ab058b507b35cBen Cheng		      break;
15625b3c049e70834cf33790a28643ab058b507b35cBen Cheng
15725b3c049e70834cf33790a28643ab058b507b35cBen Cheng		    if (match_lineno > 0
15825b3c049e70834cf33790a28643ab058b507b35cBen Cheng			&& (getattr (result, DW_AT_decl_line, &i) != 0
15925b3c049e70834cf33790a28643ab058b507b35cBen Cheng			    || (int) i != match_lineno))
16025b3c049e70834cf33790a28643ab058b507b35cBen Cheng		      break;
16125b3c049e70834cf33790a28643ab058b507b35cBen Cheng		    if (match_linecol > 0
16225b3c049e70834cf33790a28643ab058b507b35cBen Cheng			&& (getattr (result, DW_AT_decl_column, &i) != 0
16325b3c049e70834cf33790a28643ab058b507b35cBen Cheng			    || (int) i != match_linecol))
16425b3c049e70834cf33790a28643ab058b507b35cBen Cheng		      break;
16525b3c049e70834cf33790a28643ab058b507b35cBen Cheng		  }
16625b3c049e70834cf33790a28643ab058b507b35cBen Cheng
16725b3c049e70834cf33790a28643ab058b507b35cBen Cheng		/* We have a winner!  */
16825b3c049e70834cf33790a28643ab058b507b35cBen Cheng		return out;
16925b3c049e70834cf33790a28643ab058b507b35cBen Cheng	      }
17025b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  }
17125b3c049e70834cf33790a28643ab058b507b35cBen Cheng	while (INTUSE(dwarf_siblingof) (result, result) == 0);
17225b3c049e70834cf33790a28643ab058b507b35cBen Cheng      }
17325b3c049e70834cf33790a28643ab058b507b35cBen Cheng
17425b3c049e70834cf33790a28643ab058b507b35cBen Cheng  return -2;
17525b3c049e70834cf33790a28643ab058b507b35cBen Cheng}
176