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/>. */
27 #if __ELF_NATIVE_CLASS == 32
30 /* We assume 64bits. */
31 # define WORD_WIDTH 16
36 __backtrace_symbols (void *const *array
, int size
)
44 /* Fill in the information we can get from `dladdr'. */
45 for (cnt
= 0; cnt
< size
; ++cnt
)
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
;
64 total
+= 5 + WORD_WIDTH
;
67 /* Allocate memory for the result. */
68 result
= (char **) malloc (size
* sizeof (char *) + total
);
71 char *last
= (char *) (result
+ size
);
73 for (cnt
= 0; cnt
< size
; ++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
?: "",
94 if (array
[cnt
] >= (void *) info
[cnt
].dli_saddr
)
97 offset
= array
[cnt
] - info
[cnt
].dli_saddr
;
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
]);
112 last
+= 1 + sprintf (last
, "[%p]", array
[cnt
]);
114 assert (last
<= (char *) result
+ size
* sizeof (char *) + total
);
119 weak_alias (__backtrace_symbols
, backtrace_symbols
)