Skip to content

Commit

Permalink
Add support for built-in variable execname
Browse files Browse the repository at this point in the history
Signed-off-by: Kris Van Hees <kris.van.hees@oracle.com>
Reviewed-by: Nick Alcock <nick.alcock@oracle.com>
Reviewed-by: Eugene Loh <eugene.loh@oracle.com>
  • Loading branch information
kvanhees committed Feb 8, 2022
1 parent 29e3f42 commit c165d73
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
21 changes: 21 additions & 0 deletions bpf/get_bvar.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,27 @@ noinline uint64_t dt_get_bvar(const dt_dctx_t *dctx, uint32_t id)

return val & 0x00000000ffffffffUL;
}
case DIF_VAR_EXECNAME: {
uint64_t ptr;
uint32_t key;
uint32_t *comm_off;

/*
* In the "state" map, look up the "struct task_struct" offset
* of "comm".
*/
key = DT_STATE_TASK_COMM_OFF;
comm_off = bpf_map_lookup_elem(&state, &key);
if (comm_off == NULL)
return error(dctx, DTRACEFLT_ILLOP, 0);

/* &(current->comm) */
ptr = bpf_get_current_task();
if (ptr == 0)
return error(dctx, DTRACEFLT_BADADDR, ptr);

return (uint64_t)ptr + *comm_off;
}
case DIF_VAR_WALLTIMESTAMP:
return bpf_ktime_get_ns() + ((uint64_t)&BOOTTM);
case DIF_VAR_PPID: {
Expand Down
4 changes: 4 additions & 0 deletions libdtrace/dt_bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ set_task_offsets(dtrace_hdl_t *dtp)
return -1;
dt_state_set_offtgid(dtp, ctm.ctm_offset / NBBY);

if (ctf_member_info(cfp, type, "comm", &ctm) == CTF_ERR)
return -1;
dt_state_set_offcomm(dtp, ctm.ctm_offset / NBBY);

return 0;
}

Expand Down
4 changes: 3 additions & 1 deletion libdtrace/dt_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
*/

#ifndef _DT_STATE_H
Expand All @@ -23,6 +23,7 @@ typedef enum dt_state_elem {
DT_STATE_ENDEDON, /* cpu END probe executed on */
DT_STATE_TASK_PARENT_OFF, /* offsetof(struct task_struct, real_parent) */
DT_STATE_TASK_TGID_OFF, /* offsetof(struct task_struct, tgid) */
DT_STATE_TASK_COMM_OFF, /* offsetof(struct task_struct, comm) */
DT_STATE_NUM_ELEMS
} dt_state_elem_t;

Expand Down Expand Up @@ -66,6 +67,7 @@ dt_state_set(dtrace_hdl_t *dtp, uint32_t key, uint32_t val)

# define dt_state_set_offparent(dtp, x) dt_state_set(dtp, DT_STATE_TASK_PARENT_OFF, (x))
# define dt_state_set_offtgid(dtp, x) dt_state_set(dtp, DT_STATE_TASK_TGID_OFF, (x))
# define dt_state_set_offcomm(dtp, x) dt_state_set(dtp, DT_STATE_TASK_COMM_OFF, (x))
#endif

#endif /* _DT_STATE_H */
1 change: 0 additions & 1 deletion test/demo/builtin/execname.d
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* @@xfail: dtv2 */
BEGIN {
trace(execname);
exit(0);
Expand Down
7 changes: 3 additions & 4 deletions test/unittest/variables/bvar/tst.execname.d
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/*
* Oracle Linux DTrace.
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/
/* @@xfail: dtv2 */

/*
* ASSERTION: The 'execname' variable can be accessed and is not -1.
* ASSERTION: The 'execname' variable value can be retrieved.
*
* SECTION: Variables/Built-in Variables/execname
*/
Expand All @@ -16,7 +15,7 @@

BEGIN {
trace(execname);
exit(execname != -1 ? 0 : 1);
exit(0);
}

ERROR {
Expand Down
1 change: 1 addition & 0 deletions test/unittest/variables/bvar/tst.execname.r
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dtrace

0 comments on commit c165d73

Please sign in to comment.