Skip to content

Commit

Permalink
db_pprint: Properly handle complex pointer types
Browse files Browse the repository at this point in the history
The existing pretty-printing code fails to properly print complex
pointer types. This commit fixes this behaviour by traversing the
chain of CTF types until a base type is encountered.

Approved by: markj (mentor)
Fixes: c21bc6f
  • Loading branch information
bnovkov committed Jul 21, 2024
1 parent 82f5dfc commit 1cbd613
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions sys/ddb/db_pprint.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ static void db_pprint_type(db_addr_t addr, struct ctf_type_v3 *type,

static u_int max_depth = DB_PPRINT_DEFAULT_DEPTH;
static struct db_ctf_sym_data sym_data;
static const char *asteriskstr = "*****";

/*
* Pretty-prints a CTF_INT type.
Expand Down Expand Up @@ -248,35 +249,56 @@ db_pprint_ptr(db_addr_t addr, struct ctf_type_v3 *type, u_int depth)
const char *qual = "";
const char *name;
db_addr_t val;
uint32_t tid;
u_int kind;
int ptrcnt;

ref_type = db_ctf_typeid_to_type(&sym_data, type->ctt_type);
ptrcnt = 1;
tid = type->ctt_type;
again:
ref_type = db_ctf_typeid_to_type(&sym_data, tid);
kind = CTF_V3_INFO_KIND(ref_type->ctt_info);
switch (kind) {
case CTF_K_STRUCT:
qual = "struct ";
break;
case CTF_K_VOLATILE:
qual = "volatile ";
break;
tid = ref_type->ctt_type;
goto again;
case CTF_K_CONST:
qual = "const ";
break;
tid = ref_type->ctt_type;
goto again;
case CTF_K_RESTRICT:
qual = "restrict ";
tid = ref_type->ctt_type;
goto again;
case CTF_K_POINTER:
ptrcnt++;
tid = ref_type->ctt_type;
goto again;
case CTF_K_TYPEDEF:
tid = ref_type->ctt_type;
goto again;
default:
break;
}

val = db_get_value(addr, sizeof(db_addr_t), false);
if (depth < max_depth) {
ptrcnt = min(ptrcnt, strlen(asteriskstr));
val = (addr != 0) ? db_get_value(addr, sizeof(db_addr_t), false) : 0;
if (depth < max_depth && (val != 0)) {
/* Print contents of memory pointed to by this pointer. */
db_pprint_type(addr, ref_type, depth + 1);
db_pprint_type(val, ref_type, depth + 1);
} else {
name = db_ctf_stroff_to_str(&sym_data, ref_type->ctt_name);
db_indent = depth;
if (name != NULL)
db_printf("(%s%s *) 0x%lx", qual, name, (long)val);
db_printf("(%s%s %.*s) 0x%lx", qual, name, ptrcnt,
asteriskstr, (long)val);
else
db_printf("(%s *) 0x%lx", qual, (long)val);
db_printf("(%s %.*s) 0x%lx", qual, ptrcnt, asteriskstr,
(long)val);
}
}

Expand Down

0 comments on commit 1cbd613

Please sign in to comment.