Skip to content

Commit a1303af

Browse files
mhiramatrostedt
authored andcommitted
tracing: probeevent: Add $argN for accessing function args
Add $argN special fetch variable for accessing function arguments. This allows user to trace the Nth argument easily at the function entry. Note that this returns most probably assignment of registers and stacks. In some case, it may not work well. If you need to access correct registers or stacks you should use perf-probe. Link: http://lkml.kernel.org/r/152465888632.26224.3412465701570253696.stgit@devbox Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org> Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
1 parent 3c88ee1 commit a1303af

6 files changed

Lines changed: 55 additions & 24 deletions

File tree

‎Documentation/trace/kprobetrace.rst‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,18 @@ Synopsis of kprobe_events
4545
@SYM[+|-offs] : Fetch memory at SYM +|- offs (SYM should be a data symbol)
4646
$stackN : Fetch Nth entry of stack (N >= 0)
4747
$stack : Fetch stack address.
48-
$retval : Fetch return value.(*)
48+
$argN : Fetch the Nth function argument. (N >= 1) (\*1)
49+
$retval : Fetch return value.(\*2)
4950
$comm : Fetch current task comm.
50-
+|-offs(FETCHARG) : Fetch memory at FETCHARG +|- offs address.(**)
51+
+|-offs(FETCHARG) : Fetch memory at FETCHARG +|- offs address.(\*3)
5152
NAME=FETCHARG : Set NAME as the argument name of FETCHARG.
5253
FETCHARG:TYPE : Set TYPE as the type of FETCHARG. Currently, basic types
5354
(u8/u16/u32/u64/s8/s16/s32/s64), hexadecimal types
5455
(x8/x16/x32/x64), "string" and bitfield are supported.
5556

56-
(*) only for return probe.
57-
(**) this is useful for fetching a field of data structures.
57+
(\*1) only for the probe on function entry (offs == 0).
58+
(\*2) only for return probe.
59+
(\*3) this is useful for fetching a field of data structures.
5860

5961
Types
6062
-----

‎kernel/trace/trace.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4625,7 +4625,11 @@ static const char readme_msg[] =
46254625
#endif
46264626
"\t args: <name>=fetcharg[:type]\n"
46274627
"\t fetcharg: %<register>, @<address>, @<symbol>[+|-<offset>],\n"
4628+
#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
4629+
"\t $stack<index>, $stack, $retval, $comm, $arg<N>\n"
4630+
#else
46284631
"\t $stack<index>, $stack, $retval, $comm\n"
4632+
#endif
46294633
"\t type: s8/16/32/64, u8/16/32/64, x8/16/32/64, string, symbol,\n"
46304634
"\t b<bit-width>@<bit-offset>/<container-size>,\n"
46314635
"\t <type>\\[<array-size>\\]\n"

‎kernel/trace/trace_kprobe.c‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -533,13 +533,15 @@ static int create_trace_kprobe(int argc, char **argv)
533533
long offset = 0;
534534
void *addr = NULL;
535535
char buf[MAX_EVENT_NAME_LEN];
536+
unsigned int flags = TPARG_FL_KERNEL;
536537

537538
/* argc must be >= 1 */
538539
if (argv[0][0] == 'p')
539540
is_return = false;
540-
else if (argv[0][0] == 'r')
541+
else if (argv[0][0] == 'r') {
541542
is_return = true;
542-
else if (argv[0][0] == '-')
543+
flags |= TPARG_FL_RETURN;
544+
} else if (argv[0][0] == '-')
543545
is_delete = true;
544546
else {
545547
pr_info("Probe definition must be started with 'p', 'r' or"
@@ -625,8 +627,9 @@ static int create_trace_kprobe(int argc, char **argv)
625627
pr_info("Failed to parse either an address or a symbol.\n");
626628
return ret;
627629
}
628-
if (offset && is_return &&
629-
!kprobe_on_func_entry(NULL, symbol, offset)) {
630+
if (kprobe_on_func_entry(NULL, symbol, offset))
631+
flags |= TPARG_FL_FENTRY;
632+
if (offset && is_return && !(flags & TPARG_FL_FENTRY)) {
630633
pr_info("Given offset is not valid for return probe.\n");
631634
return -EINVAL;
632635
}
@@ -696,7 +699,7 @@ static int create_trace_kprobe(int argc, char **argv)
696699

697700
/* Parse fetch argument */
698701
ret = traceprobe_parse_probe_arg(arg, &tk->tp.size, parg,
699-
is_return, true);
702+
flags);
700703
if (ret) {
701704
pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
702705
goto error;
@@ -932,6 +935,11 @@ process_fetch_insn(struct fetch_insn *code, struct pt_regs *regs, void *dest,
932935
case FETCH_OP_COMM:
933936
val = (unsigned long)current->comm;
934937
break;
938+
#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
939+
case FETCH_OP_ARG:
940+
val = regs_get_kernel_argument(regs, code->param);
941+
break;
942+
#endif
935943
default:
936944
return -EILSEQ;
937945
}

‎kernel/trace/trace_probe.c‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,13 @@ int traceprobe_split_symbol_offset(char *symbol, long *offset)
157157
#define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
158158

159159
static int parse_probe_vars(char *arg, const struct fetch_type *t,
160-
struct fetch_insn *code, bool is_return,
161-
bool is_kprobe)
160+
struct fetch_insn *code, unsigned int flags)
162161
{
163162
int ret = 0;
164163
unsigned long param;
165164

166165
if (strcmp(arg, "retval") == 0) {
167-
if (is_return)
166+
if (flags & TPARG_FL_RETURN)
168167
code->op = FETCH_OP_RETVAL;
169168
else
170169
ret = -EINVAL;
@@ -173,7 +172,8 @@ static int parse_probe_vars(char *arg, const struct fetch_type *t,
173172
code->op = FETCH_OP_STACKP;
174173
} else if (isdigit(arg[5])) {
175174
ret = kstrtoul(arg + 5, 10, &param);
176-
if (ret || (is_kprobe && param > PARAM_MAX_STACK))
175+
if (ret || ((flags & TPARG_FL_KERNEL) &&
176+
param > PARAM_MAX_STACK))
177177
ret = -EINVAL;
178178
else {
179179
code->op = FETCH_OP_STACK;
@@ -183,6 +183,18 @@ static int parse_probe_vars(char *arg, const struct fetch_type *t,
183183
ret = -EINVAL;
184184
} else if (strcmp(arg, "comm") == 0) {
185185
code->op = FETCH_OP_COMM;
186+
#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
187+
} else if (((flags & TPARG_FL_MASK) ==
188+
(TPARG_FL_KERNEL | TPARG_FL_FENTRY)) &&
189+
strncmp(arg, "arg", 3) == 0) {
190+
if (!isdigit(arg[3]))
191+
return -EINVAL;
192+
ret = kstrtoul(arg + 3, 10, &param);
193+
if (ret || !param || param > PARAM_MAX_STACK)
194+
return -EINVAL;
195+
code->op = FETCH_OP_ARG;
196+
code->param = (unsigned int)param - 1;
197+
#endif
186198
} else
187199
ret = -EINVAL;
188200

@@ -193,7 +205,7 @@ static int parse_probe_vars(char *arg, const struct fetch_type *t,
193205
static int
194206
parse_probe_arg(char *arg, const struct fetch_type *type,
195207
struct fetch_insn **pcode, struct fetch_insn *end,
196-
bool is_return, bool is_kprobe)
208+
unsigned int flags)
197209
{
198210
struct fetch_insn *code = *pcode;
199211
unsigned long param;
@@ -203,8 +215,7 @@ parse_probe_arg(char *arg, const struct fetch_type *type,
203215

204216
switch (arg[0]) {
205217
case '$':
206-
ret = parse_probe_vars(arg + 1, type, code,
207-
is_return, is_kprobe);
218+
ret = parse_probe_vars(arg + 1, type, code, flags);
208219
break;
209220

210221
case '%': /* named register */
@@ -226,7 +237,7 @@ parse_probe_arg(char *arg, const struct fetch_type *type,
226237
code->immediate = param;
227238
} else if (arg[1] == '+') {
228239
/* kprobes don't support file offsets */
229-
if (is_kprobe)
240+
if (flags & TPARG_FL_KERNEL)
230241
return -EINVAL;
231242

232243
ret = kstrtol(arg + 2, 0, &offset);
@@ -237,7 +248,7 @@ parse_probe_arg(char *arg, const struct fetch_type *type,
237248
code->immediate = (unsigned long)offset; // imm64?
238249
} else {
239250
/* uprobes don't support symbols */
240-
if (!is_kprobe)
251+
if (!(flags & TPARG_FL_KERNEL))
241252
return -EINVAL;
242253

243254
ret = traceprobe_split_symbol_offset(arg + 1, &offset);
@@ -278,8 +289,7 @@ parse_probe_arg(char *arg, const struct fetch_type *type,
278289
const struct fetch_type *t2 = find_fetch_type(NULL);
279290

280291
*tmp = '\0';
281-
ret = parse_probe_arg(arg, t2, &code, end, is_return,
282-
is_kprobe);
292+
ret = parse_probe_arg(arg, t2, &code, end, flags);
283293
if (ret)
284294
break;
285295
if (code->op == FETCH_OP_COMM)
@@ -339,7 +349,7 @@ static int __parse_bitfield_probe_arg(const char *bf,
339349

340350
/* String length checking wrapper */
341351
int traceprobe_parse_probe_arg(char *arg, ssize_t *size,
342-
struct probe_arg *parg, bool is_return, bool is_kprobe)
352+
struct probe_arg *parg, unsigned int flags)
343353
{
344354
struct fetch_insn *code, *scode, *tmp = NULL;
345355
char *t, *t2;
@@ -397,7 +407,7 @@ int traceprobe_parse_probe_arg(char *arg, ssize_t *size,
397407
code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
398408

399409
ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
400-
is_return, is_kprobe);
410+
flags);
401411
if (ret)
402412
goto fail;
403413

‎kernel/trace/trace_probe.h‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <linux/stringify.h>
2424
#include <linux/limits.h>
2525
#include <linux/uaccess.h>
26+
#include <linux/bitops.h>
2627
#include <asm/bitsperlong.h>
2728

2829
#include "trace.h"
@@ -86,6 +87,7 @@ enum fetch_op {
8687
FETCH_OP_RETVAL, /* Return value */
8788
FETCH_OP_IMM, /* Immediate : .immediate */
8889
FETCH_OP_COMM, /* Current comm */
90+
FETCH_OP_ARG, /* Function argument : .param */
8991
FETCH_OP_FOFFS, /* File offset: .immediate */
9092
// Stage 2 (dereference) op
9193
FETCH_OP_DEREF, /* Dereference: .offset */
@@ -263,8 +265,13 @@ find_event_file_link(struct trace_probe *tp, struct trace_event_file *file)
263265
return NULL;
264266
}
265267

268+
#define TPARG_FL_RETURN BIT(0)
269+
#define TPARG_FL_KERNEL BIT(1)
270+
#define TPARG_FL_FENTRY BIT(2)
271+
#define TPARG_FL_MASK GENMASK(2, 0)
272+
266273
extern int traceprobe_parse_probe_arg(char *arg, ssize_t *size,
267-
struct probe_arg *parg, bool is_return, bool is_kprobe);
274+
struct probe_arg *parg, unsigned int flags);
268275

269276
extern int traceprobe_conflict_field_name(const char *name,
270277
struct probe_arg *args, int narg);

‎kernel/trace/trace_uprobe.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ static int create_trace_uprobe(int argc, char **argv)
557557

558558
/* Parse fetch argument */
559559
ret = traceprobe_parse_probe_arg(arg, &tu->tp.size, parg,
560-
is_return, false);
560+
is_return ? TPARG_FL_RETURN : 0);
561561
if (ret) {
562562
pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
563563
goto error;

0 commit comments

Comments
 (0)