Skip to content

Commit a0206dc

Browse files
robnlundman
authored andcommitted
libspl/backtrace: rename and document hex conversion function
Sponsored-by: https://despairlabs.com/sponsor/ Reviewed-by: Brian Behlendorf <[email protected]> Reviewed-by: Tino Reichardt <[email protected]> Signed-off-by: Rob Norris <[email protected]> Closes openzfs#16653
1 parent d41090d commit a0206dc

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

lib/libspl/backtrace.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,24 @@
4444
#define UNW_LOCAL_ONLY
4545
#include <libunwind.h>
4646

47+
/*
48+
* Convert `v` to ASCII hex characters. The bottom `n` nybbles (4-bits ie one
49+
* hex digit) will be written, up to `buflen`. The buffer will not be
50+
* null-terminated. Returns the number of digits written.
51+
*/
4752
static size_t
48-
libspl_u64_to_hex_str(uint64_t v, size_t digits, char *buf, size_t buflen)
53+
spl_bt_u64_to_hex_str(uint64_t v, size_t n, char *buf, size_t buflen)
4954
{
5055
static const char hexdigits[] = {
5156
'0', '1', '2', '3', '4', '5', '6', '7',
5257
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
5358
};
5459

5560
size_t pos = 0;
56-
boolean_t want = (digits == 0);
61+
boolean_t want = (n == 0);
5762
for (int i = 15; i >= 0; i--) {
5863
const uint64_t d = v >> (i * 4) & 0xf;
59-
if (!want && (d != 0 || digits > i))
64+
if (!want && (d != 0 || n > i))
6065
want = B_TRUE;
6166
if (want) {
6267
buf[pos++] = hexdigits[d];
@@ -88,14 +93,14 @@ libspl_backtrace(int fd)
8893
for (n = 0; name[n] != '\0' && name[n] != '?'; n++) {}
8994
if (n == 0) {
9095
buf[0] = '?';
91-
n = libspl_u64_to_hex_str(regnum, 2,
96+
n = spl_bt_u64_to_hex_str(regnum, 2,
9297
&buf[1], sizeof (buf)-1) + 1;
9398
name = buf;
9499
}
95100
spl_bt_write_n(fd, " ", 5-MIN(n, 3));
96101
spl_bt_write_n(fd, name, n);
97102
spl_bt_write(fd, ": 0x");
98-
n = libspl_u64_to_hex_str(v, 18, buf, sizeof (buf));
103+
n = spl_bt_u64_to_hex_str(v, 18, buf, sizeof (buf));
99104
spl_bt_write_n(fd, buf, n);
100105
if (!(++c % 3))
101106
spl_bt_write(fd, "\n");
@@ -108,22 +113,22 @@ libspl_backtrace(int fd)
108113
while (unw_step(&cp) > 0) {
109114
unw_get_reg(&cp, UNW_REG_IP, &v);
110115
spl_bt_write(fd, " [0x");
111-
n = libspl_u64_to_hex_str(v, 18, buf, sizeof (buf));
116+
n = spl_bt_u64_to_hex_str(v, 18, buf, sizeof (buf));
112117
spl_bt_write_n(fd, buf, n);
113118
spl_bt_write(fd, "] ");
114119
unw_get_proc_name(&cp, buf, sizeof (buf), &v);
115120
for (n = 0; n < sizeof (buf) && buf[n] != '\0'; n++) {}
116121
spl_bt_write_n(fd, buf, n);
117122
spl_bt_write(fd, "+0x");
118-
n = libspl_u64_to_hex_str(v, 2, buf, sizeof (buf));
123+
n = spl_bt_u64_to_hex_str(v, 2, buf, sizeof (buf));
119124
spl_bt_write_n(fd, buf, n);
120125
#ifdef HAVE_LIBUNWIND_ELF
121126
spl_bt_write(fd, " (in ");
122127
unw_get_elf_filename(&cp, buf, sizeof (buf), &v);
123128
for (n = 0; n < sizeof (buf) && buf[n] != '\0'; n++) {}
124129
spl_bt_write_n(fd, buf, n);
125130
spl_bt_write(fd, " +0x");
126-
n = libspl_u64_to_hex_str(v, 2, buf, sizeof (buf));
131+
n = spl_bt_u64_to_hex_str(v, 2, buf, sizeof (buf));
127132
spl_bt_write_n(fd, buf, n);
128133
spl_bt_write(fd, ")");
129134
#endif

0 commit comments

Comments
 (0)