blob: 66d7687ca33666608d092d574ade295935530d7d [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* Write formatted list with names for addresses in backtrace to a file.
2 Copyright (C) 1998, 2000, 2003, 2005 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; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
20
21 Based on glibc/sysdeps/generic/elf/backtracesymsfd.c
22
23 Copyright (C) 2010 STMicroelectronics Ltd
24 Author(s): Carmelo Amoroso <carmelo.amoroso@st.com>
25 * Modified to work with uClibc
26 - updated headers inclusion
27 - updated formatting and style
28 - updated to use dladdr from libdl
29 - updated to use snprintf instead of _itoa_word */
30
31#include <execinfo.h>
32#include <string.h>
33#include <sys/uio.h>
34#include <dlfcn.h>
35#include <stdio.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#define BUF_SIZE (WORD_WIDTH + 1)
46
47void backtrace_symbols_fd (void *const *array, int size, int fd)
48{
49 struct iovec iov[9];
50 int cnt;
51
52 for (cnt = 0; cnt < size; ++cnt) {
53 char buf[BUF_SIZE];
54 Dl_info info;
55 size_t last = 0;
56 size_t len = 0;
57
58 memset(buf, 0, sizeof(buf));
59 if (dladdr (array[cnt], &info)
60 && info.dli_fname && info.dli_fname[0] != '\0') {
61 /* Name of the file. */
62 iov[0].iov_base = (void *) info.dli_fname;
63 iov[0].iov_len = strlen (info.dli_fname);
64 last = 1;
65
66 /* Symbol name. */
67 if (info.dli_sname != NULL) {
68 char buf2[BUF_SIZE];
69 memset(buf2, 0, sizeof(buf2));
70 size_t diff;
71
72 iov[1].iov_base = (void *) "(";
73 iov[1].iov_len = 1;
74 iov[2].iov_base = (void *) info.dli_sname;
75 iov[2].iov_len = strlen (info.dli_sname);
76
77 if (array[cnt] >= (void *) info.dli_saddr) {
78 iov[3].iov_base = (void *) "+0x";
79 diff = array[cnt] - info.dli_saddr;
80 } else {
81 iov[3].iov_base = (void *) "-0x";
82 diff = info.dli_saddr - array[cnt];
83 }
84
85 iov[3].iov_len = 3;
86
87 /* convert diff to a string in hex format */
88 len = snprintf(buf2, sizeof(buf2), "%lx", (unsigned long) diff);
89 iov[4].iov_base = buf2;
90 iov[4].iov_len = len;
91
92 iov[5].iov_base = (void *) ")";
93 iov[5].iov_len = 1;
94
95 last = 6;
96 }
97 }
98
99 iov[last].iov_base = (void *) "[0x";
100 iov[last].iov_len = 3;
101 ++last;
102
103 /* convert array[cnt] to a string in hex format */
104 len = snprintf(buf, sizeof(buf), "%lx", (unsigned long) array[cnt]);
105 iov[last].iov_base = buf;
106 iov[last].iov_len = len;
107
108 ++last;
109
110 iov[last].iov_base = (void *) "]\n";
111 iov[last].iov_len = 2;
112 ++last;
113
114 writev (fd, iov, last);
115 }
116}