Blame debug/backtracesyms.c

Packit 6c4009
/* Return list with names for address in backtrace.
Packit 6c4009
   Copyright (C) 1998-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <assert.h>
Packit 6c4009
#include <execinfo.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
#include <ldsodefs.h>
Packit 6c4009
Packit 6c4009
#if __ELF_NATIVE_CLASS == 32
Packit 6c4009
# define WORD_WIDTH 8
Packit 6c4009
#else
Packit 6c4009
/* We assume 64bits.  */
Packit 6c4009
# define WORD_WIDTH 16
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
char **
Packit 6c4009
__backtrace_symbols (void *const *array, int size)
Packit 6c4009
{
Packit 6c4009
  Dl_info info[size];
Packit 6c4009
  int status[size];
Packit 6c4009
  int cnt;
Packit 6c4009
  size_t total = 0;
Packit 6c4009
  char **result;
Packit 6c4009
Packit 6c4009
  /* Fill in the information we can get from `dladdr'.  */
Packit 6c4009
  for (cnt = 0; cnt < size; ++cnt)
Packit 6c4009
    {
Packit 6c4009
      struct link_map *map;
Packit 6c4009
      status[cnt] = _dl_addr (array[cnt], &info[cnt], &map, NULL);
Packit 6c4009
      if (status[cnt] && info[cnt].dli_fname && info[cnt].dli_fname[0] != '\0')
Packit 6c4009
	{
Packit 6c4009
	  /* We have some info, compute the length of the string which will be
Packit 6c4009
	     "<file-name>(<sym-name>+offset) [address].  */
Packit 6c4009
	  total += (strlen (info[cnt].dli_fname ?: "")
Packit 6c4009
		    + strlen (info[cnt].dli_sname ?: "")
Packit 6c4009
		    + 3 + WORD_WIDTH + 3 + WORD_WIDTH + 5);
Packit 6c4009
Packit 6c4009
	  /* The load bias is more useful to the user than the load
Packit 6c4009
	     address.  The use of these addresses is to calculate an
Packit 6c4009
	     address in the ELF file, so its prelinked bias is not
Packit 6c4009
	     something we want to subtract out.  */
Packit 6c4009
	  info[cnt].dli_fbase = (void *) map->l_addr;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	total += 5 + WORD_WIDTH;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Allocate memory for the result.  */
Packit 6c4009
  result = (char **) malloc (size * sizeof (char *) + total);
Packit 6c4009
  if (result != NULL)
Packit 6c4009
    {
Packit 6c4009
      char *last = (char *) (result + size);
Packit 6c4009
Packit 6c4009
      for (cnt = 0; cnt < size; ++cnt)
Packit 6c4009
	{
Packit 6c4009
	  result[cnt] = last;
Packit 6c4009
Packit 6c4009
	  if (status[cnt]
Packit 6c4009
	      && info[cnt].dli_fname != NULL && info[cnt].dli_fname[0] != '\0')
Packit 6c4009
	    {
Packit 6c4009
	      if (info[cnt].dli_sname == NULL)
Packit 6c4009
		/* We found no symbol name to use, so describe it as
Packit 6c4009
		   relative to the file.  */
Packit 6c4009
		info[cnt].dli_saddr = info[cnt].dli_fbase;
Packit 6c4009
Packit 6c4009
	      if (info[cnt].dli_sname == NULL && info[cnt].dli_saddr == 0)
Packit 6c4009
		last += 1 + sprintf (last, "%s(%s) [%p]",
Packit 6c4009
				     info[cnt].dli_fname ?: "",
Packit 6c4009
				     info[cnt].dli_sname ?: "",
Packit 6c4009
				     array[cnt]);
Packit 6c4009
	      else
Packit 6c4009
		{
Packit 6c4009
		  char sign;
Packit 6c4009
		  ptrdiff_t offset;
Packit 6c4009
		  if (array[cnt] >= (void *) info[cnt].dli_saddr)
Packit 6c4009
		    {
Packit 6c4009
		      sign = '+';
Packit 6c4009
		      offset = array[cnt] - info[cnt].dli_saddr;
Packit 6c4009
		    }
Packit 6c4009
		  else
Packit 6c4009
		    {
Packit 6c4009
		      sign = '-';
Packit 6c4009
		      offset = info[cnt].dli_saddr - array[cnt];
Packit 6c4009
		    }
Packit 6c4009
Packit 6c4009
		  last += 1 + sprintf (last, "%s(%s%c%#tx) [%p]",
Packit 6c4009
				       info[cnt].dli_fname ?: "",
Packit 6c4009
				       info[cnt].dli_sname ?: "",
Packit 6c4009
				       sign, offset, array[cnt]);
Packit 6c4009
		}
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    last += 1 + sprintf (last, "[%p]", array[cnt]);
Packit 6c4009
	}
Packit 6c4009
      assert (last <= (char *) result + size * sizeof (char *) + total);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
weak_alias (__backtrace_symbols, backtrace_symbols)