Skip to content

Commit 35abb67

Browse files
osandovrostedt
authored andcommitted
tracing: expose current->comm to [ku]probe events
ftrace is very quick to give up on saving the task command line (see `trace_save_cmdline()`). The workaround for events which really care about the command line is to explicitly assign it as part of the entry. However, this doesn't work for kprobe events, as there's no straightforward way to get access to current->comm. Add a kprobe/uprobe event variable $comm which provides exactly that. Link: http://lkml.kernel.org/r/f59b472033b943a370f5f48d0af37698f409108f.1465435894.git.osandov@fb.com Acked-by: Masami Hiramatsu <mhiramat@kernel.org> Signed-off-by: Omar Sandoval <osandov@fb.com> Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
1 parent 345ddcc commit 35abb67

5 files changed

Lines changed: 50 additions & 0 deletions

File tree

‎Documentation/trace/kprobetrace.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Synopsis of kprobe_events
4040
$stackN : Fetch Nth entry of stack (N >= 0)
4141
$stack : Fetch stack address.
4242
$retval : Fetch return value.(*)
43+
$comm : Fetch current task comm.
4344
+|-offs(FETCHARG) : Fetch memory at FETCHARG +|- offs address.(**)
4445
NAME=FETCHARG : Set NAME as the argument name of FETCHARG.
4546
FETCHARG:TYPE : Set TYPE as the type of FETCHARG. Currently, basic types
@@ -62,6 +63,8 @@ offset, and container-size (usually 32). The syntax is;
6263

6364
b<bit-width>@<bit-offset>/<container-size>
6465

66+
For $comm, the default type is "string"; any other type is invalid.
67+
6568

6669
Per-Probe Event Filtering
6770
-------------------------

‎Documentation/trace/uprobetracer.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Synopsis of uprobe_tracer
3636
$stackN : Fetch Nth entry of stack (N >= 0)
3737
$stack : Fetch stack address.
3838
$retval : Fetch return value.(*)
39+
$comm : Fetch current task comm.
3940
+|-offs(FETCHARG) : Fetch memory at FETCHARG +|- offs address.(**)
4041
NAME=FETCHARG : Set NAME as the argument name of FETCHARG.
4142
FETCHARG:TYPE : Set TYPE as the type of FETCHARG. Currently, basic types
@@ -57,6 +58,8 @@ offset, and container-size (usually 32). The syntax is;
5758

5859
b<bit-width>@<bit-offset>/<container-size>
5960

61+
For $comm, the default type is "string"; any other type is invalid.
62+
6063

6164
Event Profiling
6265
---------------

‎kernel/trace/trace_kprobe.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@ static int create_trace_kprobe(int argc, char **argv)
587587
* $retval : fetch return value
588588
* $stack : fetch stack address
589589
* $stackN : fetch Nth of stack (N:0-)
590+
* $comm : fetch current task comm
590591
* @ADDR : fetch memory at ADDR (ADDR should be in kernel)
591592
* @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
592593
* %REG : fetch register REG

‎kernel/trace/trace_probe.c‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,28 @@ free_bitfield_fetch_param(struct bitfield_fetch_param *data)
218218
kfree(data);
219219
}
220220

221+
void FETCH_FUNC_NAME(comm, string)(struct pt_regs *regs,
222+
void *data, void *dest)
223+
{
224+
int maxlen = get_rloc_len(*(u32 *)dest);
225+
u8 *dst = get_rloc_data(dest);
226+
long ret;
227+
228+
if (!maxlen)
229+
return;
230+
231+
ret = strlcpy(dst, current->comm, maxlen);
232+
*(u32 *)dest = make_data_rloc(ret, get_rloc_offs(*(u32 *)dest));
233+
}
234+
NOKPROBE_SYMBOL(FETCH_FUNC_NAME(comm, string));
235+
236+
void FETCH_FUNC_NAME(comm, string_size)(struct pt_regs *regs,
237+
void *data, void *dest)
238+
{
239+
*(u32 *)dest = strlen(current->comm) + 1;
240+
}
241+
NOKPROBE_SYMBOL(FETCH_FUNC_NAME(comm, string_size));
242+
221243
static const struct fetch_type *find_fetch_type(const char *type,
222244
const struct fetch_type *ftbl)
223245
{
@@ -348,6 +370,11 @@ static int parse_probe_vars(char *arg, const struct fetch_type *t,
348370
}
349371
} else
350372
ret = -EINVAL;
373+
} else if (strcmp(arg, "comm") == 0) {
374+
if (strcmp(t->name, "string") != 0 &&
375+
strcmp(t->name, "string_size") != 0)
376+
return -EINVAL;
377+
f->fn = t->fetch[FETCH_MTD_comm];
351378
} else
352379
ret = -EINVAL;
353380

@@ -522,6 +549,12 @@ int traceprobe_parse_probe_arg(char *arg, ssize_t *size,
522549
arg[t - parg->comm] = '\0';
523550
t++;
524551
}
552+
/*
553+
* The default type of $comm should be "string", and it can't be
554+
* dereferenced.
555+
*/
556+
if (!t && strcmp(arg, "$comm") == 0)
557+
t = "string";
525558
parg->type = find_fetch_type(t, ftbl);
526559
if (!parg->type) {
527560
pr_info("Unsupported type: %s\n", t);

‎kernel/trace/trace_probe.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ enum {
102102
FETCH_MTD_reg = 0,
103103
FETCH_MTD_stack,
104104
FETCH_MTD_retval,
105+
FETCH_MTD_comm,
105106
FETCH_MTD_memory,
106107
FETCH_MTD_symbol,
107108
FETCH_MTD_deref,
@@ -183,6 +184,14 @@ DECLARE_BASIC_FETCH_FUNCS(bitfield);
183184
#define fetch_bitfield_string NULL
184185
#define fetch_bitfield_string_size NULL
185186

187+
/* comm only makes sense as a string */
188+
#define fetch_comm_u8 NULL
189+
#define fetch_comm_u16 NULL
190+
#define fetch_comm_u32 NULL
191+
#define fetch_comm_u64 NULL
192+
DECLARE_FETCH_FUNC(comm, string);
193+
DECLARE_FETCH_FUNC(comm, string_size);
194+
186195
/*
187196
* Define macro for basic types - we don't need to define s* types, because
188197
* we have to care only about bitwidth at recording time.
@@ -213,6 +222,7 @@ DEFINE_FETCH_##method(u64)
213222
ASSIGN_FETCH_FUNC(reg, ftype), \
214223
ASSIGN_FETCH_FUNC(stack, ftype), \
215224
ASSIGN_FETCH_FUNC(retval, ftype), \
225+
ASSIGN_FETCH_FUNC(comm, ftype), \
216226
ASSIGN_FETCH_FUNC(memory, ftype), \
217227
ASSIGN_FETCH_FUNC(symbol, ftype), \
218228
ASSIGN_FETCH_FUNC(deref, ftype), \

0 commit comments

Comments
 (0)