Add missing magic to GLIBC_PROVIDES.
[glibc.git] / debug / backtracesyms.c
blob3fabf26a8be2289c3bb6653ea4754eab2a30f6b2
1 /* Return list with names for address in backtrace.
2 Copyright (C) 1998-2012 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <assert.h>
21 #include <execinfo.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include <ldsodefs.h>
28 #if __ELF_NATIVE_CLASS == 32
29 # define WORD_WIDTH 8
30 #else
31 /* We assyme 64bits. */
32 # define WORD_WIDTH 16
33 #endif
36 char **
37 __backtrace_symbols (array, size)
38 void *const *array;
39 int size;
41 Dl_info info[size];
42 int status[size];
43 int cnt;
44 size_t total = 0;
45 char **result;
47 /* Fill in the information we can get from `dladdr'. */
48 for (cnt = 0; cnt < size; ++cnt)
50 struct link_map *map;
51 status[cnt] = _dl_addr (array[cnt], &info[cnt], &map, NULL);
52 if (status[cnt] && info[cnt].dli_fname && info[cnt].dli_fname[0] != '\0')
54 /* We have some info, compute the length of the string which will be
55 "<file-name>(<sym-name>+offset) [address]. */
56 total += (strlen (info[cnt].dli_fname ?: "")
57 + strlen (info[cnt].dli_sname ?: "")
58 + 3 + WORD_WIDTH + 3 + WORD_WIDTH + 5);
60 /* The load bias is more useful to the user than the load
61 address. The use of these addresses is to calculate an
62 address in the ELF file, so its prelinked bias is not
63 something we want to subtract out. */
64 info[cnt].dli_fbase = (void *) map->l_addr;
66 else
67 total += 5 + WORD_WIDTH;
70 /* Allocate memory for the result. */
71 result = (char **) malloc (size * sizeof (char *) + total);
72 if (result != NULL)
74 char *last = (char *) (result + size);
76 for (cnt = 0; cnt < size; ++cnt)
78 result[cnt] = last;
80 if (status[cnt]
81 && info[cnt].dli_fname != NULL && info[cnt].dli_fname[0] != '\0')
83 if (info[cnt].dli_sname == NULL)
84 /* We found no symbol name to use, so describe it as
85 relative to the file. */
86 info[cnt].dli_saddr = info[cnt].dli_fbase;
88 if (info[cnt].dli_sname == NULL && info[cnt].dli_saddr == 0)
89 last += 1 + sprintf (last, "%s(%s) [%p]",
90 info[cnt].dli_fname ?: "",
91 info[cnt].dli_sname ?: "",
92 array[cnt]);
93 else
95 char sign;
96 ptrdiff_t offset;
97 if (array[cnt] >= (void *) info[cnt].dli_saddr)
99 sign = '+';
100 offset = array[cnt] - info[cnt].dli_saddr;
102 else
104 sign = '-';
105 offset = info[cnt].dli_saddr - array[cnt];
108 last += 1 + sprintf (last, "%s(%s%c%#tx) [%p]",
109 info[cnt].dli_fname ?: "",
110 info[cnt].dli_sname ?: "",
111 sign, offset, array[cnt]);
114 else
115 last += 1 + sprintf (last, "[%p]", array[cnt]);
117 assert (last <= (char *) result + size * sizeof (char *) + total);
120 return result;
122 weak_alias (__backtrace_symbols, backtrace_symbols)