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
90113void
@@ -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
134157void
0 commit comments