Avoid use of atoi in malloc
[glibc.git] / debug / backtracesyms.c
blob17e0626df88035a5dd42195e7d643341e8ca37e4
1 /* Return list with names for address in backtrace.
2 Copyright (C) 1998-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <assert.h>
20 #include <execinfo.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #include <ldsodefs.h>
27 #if __ELF_NATIVE_CLASS == 32
28 # define WORD_WIDTH 8
29 #else
30 /* We assume 64bits. */
31 # define WORD_WIDTH 16
32 #endif
35 char **
36 __backtrace_symbols (void *const *array, int size)
38 Dl_info info[size];
39 int status[size];
40 int cnt;
41 size_t total = 0;
42 char **result;
44 /* Fill in the information we can get from `dladdr'. */
45 for (cnt = 0; cnt < size; ++cnt)
47 struct link_map *map;
48 status[cnt] = _dl_addr (array[cnt], &info[cnt], &map, NULL);
49 if (status[cnt] && info[cnt].dli_fname && info[cnt].dli_fname[0] != '\0')
51 /* We have some info, compute the length of the string which will be
52 "<file-name>(<sym-name>+offset) [address]. */
53 total += (strlen (info[cnt].dli_fname ?: "")
54 + strlen (info[cnt].dli_sname ?: "")
55 + 3 + WORD_WIDTH + 3 + WORD_WIDTH + 5);
57 /* The load bias is more useful to the user than the load
58 address. The use of these addresses is to calculate an
59 address in the ELF file, so its prelinked bias is not
60 something we want to subtract out. */
61 info[cnt].dli_fbase = (void *) map->l_addr;
63 else
64 total += 5 + WORD_WIDTH;
67 /* Allocate memory for the result. */
68 result = (char **) malloc (size * sizeof (char *) + total);
69 if (result != NULL)
71 char *last = (char *) (result + size);
73 for (cnt = 0; cnt < size; ++cnt)
75 result[cnt] = last;
77 if (status[cnt]
78 && info[cnt].dli_fname != NULL && info[cnt].dli_fname[0] != '\0')
80 if (info[cnt].dli_sname == NULL)
81 /* We found no symbol name to use, so describe it as
82 relative to the file. */
83 info[cnt].dli_saddr = info[cnt].dli_fbase;
85 if (info[cnt].dli_sname == NULL && info[cnt].dli_saddr == 0)
86 last += 1 + sprintf (last, "%s(%s) [%p]",
87 info[cnt].dli_fname ?: "",
88 info[cnt].dli_sname ?: "",
89 array[cnt]);
90 else
92 char sign;
93 ptrdiff_t offset;
94 if (array[cnt] >= (void *) info[cnt].dli_saddr)
96 sign = '+';
97 offset = array[cnt] - info[cnt].dli_saddr;
99 else
101 sign = '-';
102 offset = info[cnt].dli_saddr - array[cnt];
105 last += 1 + sprintf (last, "%s(%s%c%#tx) [%p]",
106 info[cnt].dli_fname ?: "",
107 info[cnt].dli_sname ?: "",
108 sign, offset, array[cnt]);
111 else
112 last += 1 + sprintf (last, "[%p]", array[cnt]);
114 assert (last <= (char *) result + size * sizeof (char *) + total);
117 return result;
119 weak_alias (__backtrace_symbols, backtrace_symbols)