Skip to content

Commit

Permalink
Replace dt_variable_read()
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Eugene Loh <[email protected]>
  • Loading branch information
kvanhees committed Sep 26, 2023
1 parent 450e5b5 commit 1c8d98f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 38 deletions.
43 changes: 30 additions & 13 deletions libdtrace/dt_consume.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;

Expand All @@ -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 ||
Expand Down Expand Up @@ -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) {
Expand All @@ -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);

/*
Expand Down
2 changes: 0 additions & 2 deletions libdtrace/dt_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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]*) */
Expand Down
23 changes: 0 additions & 23 deletions libdtrace/dt_subr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 1c8d98f

Please sign in to comment.