Skip to content

Commit 4d32fdb

Browse files
committed
DAOS-18625 utils: interpret key based to object's type for ddb output - b26
Current ddb logic may print some integer keys' output as not-readable, for example dkey 10, 60 will be printed as following: DKEY: (/[4]/[2]/[0]) /b2b37b1a-2894-4970-b51c-1a964526bb02/948289433254841843.256.535.2/ {8} DKEY: (/[4]/[2]/[2]) /b2b37b1a-2894-4970-b51c-1a964526bb02/948289433254841843.256.535.2/<{8} That is very confused. The patch defines new ddb_key_to_printable_buf() interface that will interpret key based to object's type and generate more readable ddb output. Test-tag: recovery Allow-unstable-test: true Signed-off-by: Xuezhao Liu <xuezhao.liu@hpe.com> Signed-off-by: Fan Yong <fan.yong@hpe.com>
1 parent 89823c6 commit 4d32fdb

10 files changed

Lines changed: 239 additions & 169 deletions

File tree

src/utils/ddb/ddb_commands.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* (C) Copyright 2022-2024 Intel Corporation.
3-
* (C) Copyright 2025 Hewlett Packard Enterprise Development LP.
3+
* (C) Copyright 2025-2026 Hewlett Packard Enterprise Development LP.
44
*
55
* SPDX-License-Identifier: BSD-2-Clause-Patent
66
*/
@@ -285,7 +285,7 @@ print_value_cb(void *cb_args, d_iov_t *value)
285285
return 0;
286286
}
287287

288-
ddb_iov_to_printable_buf(value, buf, ARRAY_SIZE(buf));
288+
ddb_iov_to_printable_buf(value, buf, ARRAY_SIZE(buf), NULL);
289289
ddb_printf(ctx, "Value (size: %lu):\n", value->iov_len);
290290
ddb_printf(ctx, "%s\n", buf);
291291
return 0;

src/utils/ddb/ddb_parse.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* (C) Copyright 2019-2024 Intel Corporation.
3+
* (C) Copyright 2026 Hewlett Packard Enterprise Development LP
34
*
45
* SPDX-License-Identifier: BSD-2-Clause-Patent
56
*/
@@ -42,7 +43,7 @@ void ddb_str2argv_free(struct argv_parsed *parse_args);
4243
int ddb_parse_program_args(struct ddb_ctx *ctx, uint32_t argc, char **argv,
4344
struct program_args *pa);
4445

45-
/* See ddb_iov_to_printable_buf for how the keys will be printed */
46+
/* See ddb_key_to_printable_buf for how the keys will be printed */
4647
int ddb_parse_key(const char *input, daos_key_t *key);
4748

4849
/* Parse a string into the parts of a dtx_id. See DF_DTIF for how the format of the dtx_id is

src/utils/ddb/ddb_printer.c

Lines changed: 78 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* (C) Copyright 2022-2024 Intel Corporation.
3+
* (C) Copyright 2026 Hewlett Packard Enterprise Development LP
34
*
45
* SPDX-License-Identifier: BSD-2-Clause-Patent
56
*/
@@ -22,69 +23,91 @@ ddb_can_print(d_iov_t *iov)
2223
uint32_t len = iov->iov_len;
2324
int i;
2425

25-
for (i = 0 ; i < len ; i++) {
26+
for (i = 0; i < len; i++) {
2627
if (str[i] == '\0')
2728
return true;
28-
if (!isprint(str[i]) && str[i] != '\n' && str[i] != '\r')
29+
if (!isprint(str[i]))
2930
return false;
3031
}
3132
return true;
3233
}
3334

3435
/*
35-
* Converts contents of an iov to something that is more printable.
36+
* Converts contents of an @iov to something that is more printable.
3637
*
37-
* Returns number of characters that would have been written if buf_len was long
38-
* enough, not including null terminator
38+
* Returns number of characters that would have been written if @buf is large enough,
39+
* not including the null terminator.
3940
*/
40-
int
41-
ddb_iov_to_printable_buf(d_iov_t *iov, char buf[], uint32_t buf_len)
41+
uint32_t
42+
ddb_iov_to_printable_buf(d_iov_t *iov, char buf[], uint32_t buf_len, const char *prefix)
4243
{
44+
char tmp[32];
45+
uint32_t new_len;
46+
uint32_t result = 0;
47+
int i;
48+
4349
if (iov->iov_len == 0 || iov->iov_buf == NULL)
4450
return 0;
4551

4652
if (ddb_can_print(iov))
4753
return snprintf(buf, buf_len, "%.*s", (int)iov->iov_len, (char *)iov->iov_buf);
4854

49-
switch (iov->iov_len) {
50-
case sizeof(uint8_t):
51-
return snprintf(buf, buf_len, "uint8:0x%x", ((uint8_t *)iov->iov_buf)[0]);
52-
case sizeof(uint16_t):
53-
return snprintf(buf, buf_len, "uint16:0x%04hx", ((uint16_t *)iov->iov_buf)[0]);
54-
case sizeof(uint32_t):
55-
return snprintf(buf, buf_len, "uint32:0x%x", ((uint32_t *)iov->iov_buf)[0]);
56-
case sizeof(uint64_t):
57-
return snprintf(buf, buf_len, "uint64:0x%lx", ((uint64_t *)iov->iov_buf)[0]);
58-
default:
59-
{
60-
char tmp_buf[32];
61-
uint32_t new_len;
62-
uint32_t result = 0;
63-
int i;
64-
65-
result += snprintf(buf, buf_len, "bin(%lu):0x", iov->iov_len);
66-
67-
for (i = 0; i < iov->iov_len; i++) {
68-
new_len = snprintf(tmp_buf, ARRAY_SIZE(tmp_buf), "%02x",
69-
((uint8_t *)iov->iov_buf)[i]);
70-
if (new_len + result > buf_len) {
71-
/* Buffer not big enough */
72-
result += new_len;
73-
} else {
74-
result += sprintf(buf + result, "%s", tmp_buf);
75-
}
76-
}
55+
if (prefix != NULL)
56+
result = snprintf(buf, buf_len, "%s", prefix);
7757

78-
if (result > buf_len) {
79-
buf[buf_len - 1] = '\0';
80-
buf[buf_len - 2] = '.';
81-
buf[buf_len - 3] = '.';
82-
buf[buf_len - 4] = '.';
58+
for (i = 0; i < iov->iov_len; i++) {
59+
new_len = snprintf(tmp, ARRAY_SIZE(tmp), "%02x", ((uint8_t *)iov->iov_buf)[i]);
60+
if (new_len + result > buf_len)
61+
result += new_len; /* Buffer is not big enough. */
62+
else
63+
result += sprintf(buf + result, "%s", tmp);
64+
}
8365

84-
}
85-
return result;
66+
if (result > buf_len) {
67+
buf[buf_len - 1] = '\0';
68+
for (i = 2; buf_len >= i && i <= 4; i++)
69+
buf[buf_len - i] = '.';
8670
}
71+
72+
return result;
73+
}
74+
75+
/*
76+
* Converts contents of an @key to something that is more printable.
77+
*
78+
* Returns number of characters that would have been written if @buf is long enough,
79+
* not including the null terminator.
80+
*/
81+
uint32_t
82+
ddb_key_to_printable_buf(daos_key_t *key, enum daos_otype_t otype, char buf[], uint32_t buf_len)
83+
{
84+
char tmp[32];
85+
86+
if (key->iov_len == 0 || key->iov_buf == NULL)
87+
return 0;
88+
89+
if (ddb_key_is_lexical(otype))
90+
return snprintf(buf, buf_len, "%.*s", (int)key->iov_len, (char *)key->iov_buf);
91+
92+
if (ddb_key_is_int(otype)) {
93+
switch (key->iov_len) {
94+
case sizeof(uint8_t):
95+
return snprintf(buf, buf_len, "uint8:0x%x", ((uint8_t *)key->iov_buf)[0]);
96+
case sizeof(uint16_t):
97+
return snprintf(buf, buf_len, "uint16:0x%04hx",
98+
((uint16_t *)key->iov_buf)[0]);
99+
case sizeof(uint32_t):
100+
return snprintf(buf, buf_len, "uint32:0x%x", ((uint32_t *)key->iov_buf)[0]);
101+
case sizeof(uint64_t):
102+
return snprintf(buf, buf_len, "uint64:0x%lx",
103+
((uint64_t *)key->iov_buf)[0]);
104+
/* Fall through. */
105+
}
87106
}
107+
108+
snprintf(tmp, ARRAY_SIZE(tmp), "bin(%lu):0x", key->iov_len);
109+
110+
return ddb_iov_to_printable_buf(key, buf, buf_len, tmp);
88111
}
89112

90113
void
@@ -113,22 +136,22 @@ ddb_print_key(struct ddb_ctx *ctx, struct ddb_key *key, uint32_t indent)
113136

114137
memset(buf, 0, buf_len);
115138

116-
ddb_iov_to_printable_buf(&key->ddbk_key, buf, buf_len);
139+
ddb_key_to_printable_buf(&key->ddbk_key, key->ddbk_otype, buf, buf_len);
117140

118141
print_indent(ctx, indent);
119-
if (ddb_can_print(&key->ddbk_key)) {
120-
ddb_printf(ctx, DF_IDX" '%s' (%lu)%s\n",
121-
DP_IDX(key->ddbk_idx),
122-
buf,
123-
key->ddbk_key.iov_len,
124-
key->ddbk_child_type == VOS_ITER_SINGLE ? " (SV)" :
125-
key->ddbk_child_type == VOS_ITER_RECX ? " (ARRAY)" : "");
126-
return;
127-
}
128142

129-
ddb_printf(ctx, DF_IDX" {%s}%s\n", DP_IDX(key->ddbk_idx), buf,
130-
key->ddbk_child_type == VOS_ITER_SINGLE ? " (SV)" :
131-
key->ddbk_child_type == VOS_ITER_RECX ? " (ARRAY)" : "");
143+
if (ddb_key_is_lexical(key->ddbk_otype) ||
144+
(!ddb_key_is_int(key->ddbk_otype) && ddb_can_print(&key->ddbk_key)))
145+
ddb_printf(ctx, DF_IDX " '%s' (%lu)%s\n", DP_IDX(key->ddbk_idx), buf,
146+
key->ddbk_key.iov_len,
147+
key->ddbk_child_type == VOS_ITER_SINGLE ? " (SV)"
148+
: key->ddbk_child_type == VOS_ITER_RECX ? " (ARRAY)"
149+
: "");
150+
else
151+
ddb_printf(ctx, DF_IDX " {%s}%s\n", DP_IDX(key->ddbk_idx), buf,
152+
key->ddbk_child_type == VOS_ITER_SINGLE ? " (SV)"
153+
: key->ddbk_child_type == VOS_ITER_RECX ? " (ARRAY)"
154+
: "");
132155
}
133156

134157
void

src/utils/ddb/ddb_printer.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* (C) Copyright 2022 Intel Corporation.
3+
* (C) Copyright 2025-2026 Hewlett Packard Enterprise Development LP
34
*
45
* SPDX-License-Identifier: BSD-2-Clause-Patent
56
*/
@@ -12,7 +13,10 @@
1213
#define DF_IDX "[%d]"
1314
#define DP_IDX(idx) idx
1415

15-
int ddb_iov_to_printable_buf(d_iov_t *iov, char buf[], uint32_t buf_len);
16+
uint32_t
17+
ddb_iov_to_printable_buf(d_iov_t *iov, char buf[], uint32_t buf_len, const char *prefix);
18+
uint32_t
19+
ddb_key_to_printable_buf(daos_key_t *key, enum daos_otype_t otype, char buf[], uint32_t buf_len);
1620
void ddb_print_cont(struct ddb_ctx *ctx, struct ddb_cont *cont);
1721
void ddb_print_obj(struct ddb_ctx *ctx, struct ddb_obj *obj, uint32_t indent);
1822
void ddb_print_key(struct ddb_ctx *ctx, struct ddb_key *key, uint32_t indent);
@@ -29,5 +33,16 @@ bool ddb_can_print(d_iov_t *iov);
2933
/* some utility functions helpful for printing */
3034
void ddb_bytes_hr(uint64_t bytes, char *buf, uint32_t buf_len);
3135

36+
static inline bool
37+
ddb_key_is_lexical(enum daos_otype_t otype)
38+
{
39+
return daos_is_dkey_lexical_type(otype) || daos_is_akey_lexical_type(otype);
40+
}
41+
42+
static inline bool
43+
ddb_key_is_int(enum daos_otype_t otype)
44+
{
45+
return daos_is_dkey_uint64_type(otype) || daos_is_akey_uint64_type(otype);
46+
}
3247

3348
#endif /* DAOS_DDB_PRINTER_H */

0 commit comments

Comments
 (0)