1 /* Return list with names for address in backtrace.
2    Copyright (C) 1998,1999,2000,2001,2003 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5 
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10 
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15 
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; see the file COPYING.LIB.  If
18    not, see <http://www.gnu.org/licenses/>.
19 
20    Based on glibc/sysdeps/generic/elf/backtracesyms.c
21 
22    Copyright (C) 2010 STMicroelectronics Ltd
23    Author(s): Carmelo Amoroso <carmelo.amoroso@st.com>
24    * Modified to work with uClibc
25      - updated headers inclusion
26      - updated formatting and style
27      - updated to use dladdr from libdl */
28 
29 #include <execinfo.h>
30 #include <assert.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 
35 #include <dlfcn.h>
36 #include <link.h>	/* required for __ELF_NATIVE_CLASS */
37 
38 #if __ELF_NATIVE_CLASS == 32
39 # define WORD_WIDTH 8
40 #else
41 /* We assyme 64bits.  */
42 # define WORD_WIDTH 16
43 #endif
44 
45 
backtrace_symbols(void * const * array,int size)46 char ** backtrace_symbols (void *const *array,  int size)
47 {
48 	Dl_info info[size];
49 	int status[size];
50 	int cnt;
51 	size_t total = 0;
52 	char **result;
53 
54 	/* Fill in the information we can get from `dladdr'.  */
55 	for (cnt = 0; cnt < size; ++cnt) {
56 		status[cnt] = dladdr (array[cnt], &info[cnt]);
57 		if (status[cnt] && info[cnt].dli_fname &&
58 			info[cnt].dli_fname[0] != '\0')
59 		/*
60 		 * We have some info, compute the length of the string which will be
61 		 * "<file-name>(<sym-name>) [+offset].
62 		 */
63 		total += (strlen (info[cnt].dli_fname ?: "") +
64 				  (info[cnt].dli_sname ?
65 				  strlen (info[cnt].dli_sname) + 3 + WORD_WIDTH + 3 : 1)
66 				  + WORD_WIDTH + 5);
67 		else
68 			total += 5 + WORD_WIDTH;
69 	}
70 
71 	/* Allocate memory for the result.  */
72 	result = (char **) malloc (size * sizeof (char *) + total);
73 	if (result != NULL) {
74 		char *last = (char *) (result + size);
75 		for (cnt = 0; cnt < size; ++cnt) {
76 			result[cnt] = last;
77 
78 			if (status[cnt] && info[cnt].dli_fname
79 				&& info[cnt].dli_fname[0] != '\0') {
80 
81 				char buf[20];
82 
83 				if (array[cnt] >= (void *) info[cnt].dli_saddr)
84 					sprintf (buf, "+%#lx",
85 							(unsigned long)(array[cnt] - info[cnt].dli_saddr));
86 				else
87 					sprintf (buf, "-%#lx",
88 					(unsigned long)(info[cnt].dli_saddr - array[cnt]));
89 
90 				last += 1 + sprintf (last, "%s%s%s%s%s[%p]",
91 				info[cnt].dli_fname ?: "",
92 				info[cnt].dli_sname ? "(" : "",
93 				info[cnt].dli_sname ?: "",
94 				info[cnt].dli_sname ? buf : "",
95 				info[cnt].dli_sname ? ") " : " ",
96 				array[cnt]);
97 			} else
98 				last += 1 + sprintf (last, "[%p]", array[cnt]);
99 		}
100 		assert (last <= (char *) result + size * sizeof (char *) + total);
101 	}
102 
103 	return result;
104 }
105