From 1c8d98f84da6351be7e20ab6474645e2da681b8d Mon Sep 17 00:00:00 2001 From: Kris Van Hees Date: Fri, 22 Sep 2023 10:46:06 -0400 Subject: [PATCH] Replace dt_variable_read() The dt_variable_read() function is only used in a few places, all of them involving reading data from a data record. Callers adjust the base address using the record offset, and then pass in the data size from the record as well. The new dt_read_scalar() function takes a base address and a pointer to a record descriptor which is more in line with the nature of data record descriptors. Signed-off-by: Kris Van Hees Reviewed-by: Eugene Loh --- libdtrace/dt_consume.c | 43 +++++++++++++++++++++++++++++------------- libdtrace/dt_impl.h | 2 -- libdtrace/dt_subr.c | 23 ---------------------- 3 files changed, 30 insertions(+), 38 deletions(-) diff --git a/libdtrace/dt_consume.c b/libdtrace/dt_consume.c index a67c0f7bf..1edca2925 100644 --- a/libdtrace/dt_consume.c +++ b/libdtrace/dt_consume.c @@ -506,6 +506,31 @@ dt_nullrec() return DTRACE_CONSUME_NEXT; } +static int +dt_read_scalar(caddr_t addr, const dtrace_recdesc_t *rec, uint64_t *valp) +{ + addr += rec->dtrd_offset; + + switch (rec->dtrd_size) { + case sizeof(uint64_t): + *valp = *((uint64_t *)addr); + break; + case sizeof(uint32_t): + *valp = *((uint32_t *)addr); + break; + case sizeof(uint16_t): + *valp = *((uint16_t *)addr); + break; + case sizeof(uint8_t): + *valp = *((uint8_t *)addr); + break; + default: + return -1; + } + + return 0; +} + int dt_print_quantline(dtrace_hdl_t *dtp, FILE *fp, int64_t val, uint64_t normal, long double total, char positives, char negatives) @@ -1017,7 +1042,6 @@ dt_print_tracemem(dtrace_hdl_t *dtp, FILE *fp, const dtrace_recdesc_t *rec, if (arg == DTRACE_TRACEMEM_DYNAMIC) { const dtrace_recdesc_t *drec; uint64_t darg; - caddr_t daddr; uint64_t dsize; int dpositive; @@ -1026,14 +1050,13 @@ dt_print_tracemem(dtrace_hdl_t *dtp, FILE *fp, const dtrace_recdesc_t *rec, drec = rec + 1; darg = drec->dtrd_arg; - daddr = buf + drec->dtrd_offset; if (drec->dtrd_action != DTRACEACT_TRACEMEM || (darg != DTRACE_TRACEMEM_SIZE && darg != DTRACE_TRACEMEM_SSIZE)) return dt_set_errno(dtp, EDT_TRACEMEM); - if (dt_variable_read(daddr, drec->dtrd_size, &dsize) < 0) + if (dt_read_scalar(buf, drec, &dsize) < 0) return dt_set_errno(dtp, EDT_TRACEMEM); dpositive = drec->dtrd_arg == DTRACE_TRACEMEM_SIZE || @@ -1407,16 +1430,14 @@ int dt_print_pcap(dtrace_hdl_t *dtp, FILE *fp, dtrace_recdesc_t *rec, const caddr_t buf) { - caddr_t paddr, addr; - const dtrace_recdesc_t *prec; + caddr_t addr; uint64_t time, proto, pktlen, maxlen; const char *filename; addr = (caddr_t)buf + rec->dtrd_offset; - if (dt_variable_read(addr, sizeof(uint64_t), &time) < 0 || - dt_variable_read(addr + sizeof(uint64_t), sizeof(uint64_t), - &pktlen) < 0) + if (dt_read_scalar(buf, rec, &time) < 0 || + dt_read_scalar(buf + sizeof(uint64_t), rec, &pktlen) < 0) return dt_set_errno(dtp, EDT_PCAP); if (pktlen == 0) { @@ -1427,11 +1448,7 @@ dt_print_pcap(dtrace_hdl_t *dtp, FILE *fp, dtrace_recdesc_t *rec, } maxlen = DT_PCAPSIZE(dtp->dt_options[DTRACEOPT_PCAPSIZE]); - prec = rec + 1; - - paddr = (caddr_t)buf + prec->dtrd_offset; - - if (dt_variable_read(paddr, prec->dtrd_size, &proto) < 0) + if (dt_read_scalar(buf, rec + 1, &proto) < 0) return dt_set_errno(dtp, EDT_PCAP); /* diff --git a/libdtrace/dt_impl.h b/libdtrace/dt_impl.h index 674905b9b..f60258173 100644 --- a/libdtrace/dt_impl.h +++ b/libdtrace/dt_impl.h @@ -863,8 +863,6 @@ extern int dt_handle_setopt(dtrace_hdl_t *, dtrace_setoptdata_t *); extern int dt_lib_depend_add(dtrace_hdl_t *, dt_list_t *, const char *); extern dt_lib_depend_t *dt_lib_depend_lookup(dt_list_t *, const char *); -extern int dt_variable_read(caddr_t, size_t, uint64_t *); - extern dt_pcb_t *yypcb; /* pointer to current parser control block */ extern char yyintprefix; /* int token prefix for macros (+/-) */ extern char yyintsuffix[4]; /* int token suffix ([uUlL]*) */ diff --git a/libdtrace/dt_subr.c b/libdtrace/dt_subr.c index 4b6b525f5..d6aad7637 100644 --- a/libdtrace/dt_subr.c +++ b/libdtrace/dt_subr.c @@ -934,29 +934,6 @@ dtrace_uaddr2str(dtrace_hdl_t *dtp, pid_t pid, uint64_t addr, char *str, return dt_string2str(c, str, nbytes); } -int -dt_variable_read(caddr_t addr, size_t size, uint64_t *valp) -{ - switch (size) { - case sizeof(uint8_t): - *valp = *((uint8_t *)(uintptr_t)addr); - break; - case sizeof(uint16_t): - *valp = *((uint16_t *)(uintptr_t)addr); - break; - case sizeof(uint32_t): - *valp = *((uint32_t *)(uintptr_t)addr); - break; - case sizeof(uint64_t): - *valp = *((uint64_t *)(uintptr_t)addr); - break; - default: - return -1; - } - - return 0; -} - /* * The function converts string representation of kernel version * into the dt_version_t type.