Skip to content

Commit 1ff511e

Browse files
mhiramathitachiacmel
authored andcommitted
tracing/kprobes: Add bitfield type
Add bitfield type for tracing arguments on kprobe-tracer. The syntax of a bitfield type is: b<bit-size>@<bit-offset>/<container-size> e.g. Accessing 2 bits-width field with 4 bits-offset in 32 bits-width data at 4 bytes offseted from the address pointed by AX register: +4(%ax):b2@4/32 Since the width of container data depends on the arch, so I just added the container-size at the end. Cc: 2nddept-manager@sdl.hitachi.co.jp Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Ingo Molnar <mingo@elte.hu> Cc: Srikar Dronamraju <srikar@linux.vnet.ibm.com> Cc: Steven Rostedt <rostedt@goodmis.org> LKML-Reference: <20110204125205.9507.11363.stgit@ltc236.sdl.hitachi.co.jp> Signed-off-by: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent e374536 commit 1ff511e

2 files changed

Lines changed: 118 additions & 2 deletions

File tree

‎Documentation/trace/kprobetrace.txt‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,25 @@ Synopsis of kprobe_events
4242
+|-offs(FETCHARG) : Fetch memory at FETCHARG +|- offs address.(**)
4343
NAME=FETCHARG : Set NAME as the argument name of FETCHARG.
4444
FETCHARG:TYPE : Set TYPE as the type of FETCHARG. Currently, basic types
45-
(u8/u16/u32/u64/s8/s16/s32/s64) and string are supported.
45+
(u8/u16/u32/u64/s8/s16/s32/s64), "string" and bitfield
46+
are supported.
4647

4748
(*) only for return probe.
4849
(**) this is useful for fetching a field of data structures.
4950

51+
Types
52+
-----
53+
Several types are supported for fetch-args. Kprobe tracer will access memory
54+
by given type. Prefix 's' and 'u' means those types are signed and unsigned
55+
respectively. Traced arguments are shown in decimal (signed) or hex (unsigned).
56+
String type is a special type, which fetches a "null-terminated" string from
57+
kernel space. This means it will fail and store NULL if the string container
58+
has been paged out.
59+
Bitfield is another special type, which takes 3 parameters, bit-width, bit-
60+
offset, and container-size (usually 32). The syntax is;
61+
62+
b<bit-width>@<bit-offset>/<container-size>
63+
5064

5165
Per-Probe Event Filtering
5266
-------------------------

‎kernel/trace/trace_kprobe.c‎

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,43 @@ static __kprobes void free_deref_fetch_param(struct deref_fetch_param *data)
353353
kfree(data);
354354
}
355355

356+
/* Bitfield fetch function */
357+
struct bitfield_fetch_param {
358+
struct fetch_param orig;
359+
unsigned char hi_shift;
360+
unsigned char low_shift;
361+
};
362+
363+
#define DEFINE_FETCH_bitfield(type) \
364+
static __kprobes void FETCH_FUNC_NAME(bitfield, type)(struct pt_regs *regs,\
365+
void *data, void *dest) \
366+
{ \
367+
struct bitfield_fetch_param *bprm = data; \
368+
type buf = 0; \
369+
call_fetch(&bprm->orig, regs, &buf); \
370+
if (buf) { \
371+
buf <<= bprm->hi_shift; \
372+
buf >>= bprm->low_shift; \
373+
} \
374+
*(type *)dest = buf; \
375+
}
376+
DEFINE_BASIC_FETCH_FUNCS(bitfield)
377+
#define fetch_bitfield_string NULL
378+
#define fetch_bitfield_string_size NULL
379+
380+
static __kprobes void
381+
free_bitfield_fetch_param(struct bitfield_fetch_param *data)
382+
{
383+
/*
384+
* Don't check the bitfield itself, because this must be the
385+
* last fetch function.
386+
*/
387+
if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
388+
free_deref_fetch_param(data->orig.data);
389+
else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
390+
free_symbol_cache(data->orig.data);
391+
kfree(data);
392+
}
356393
/* Default (unsigned long) fetch type */
357394
#define __DEFAULT_FETCH_TYPE(t) u##t
358395
#define _DEFAULT_FETCH_TYPE(t) __DEFAULT_FETCH_TYPE(t)
@@ -367,6 +404,7 @@ enum {
367404
FETCH_MTD_memory,
368405
FETCH_MTD_symbol,
369406
FETCH_MTD_deref,
407+
FETCH_MTD_bitfield,
370408
FETCH_MTD_END,
371409
};
372410

@@ -387,6 +425,7 @@ ASSIGN_FETCH_FUNC(retval, ftype), \
387425
ASSIGN_FETCH_FUNC(memory, ftype), \
388426
ASSIGN_FETCH_FUNC(symbol, ftype), \
389427
ASSIGN_FETCH_FUNC(deref, ftype), \
428+
ASSIGN_FETCH_FUNC(bitfield, ftype), \
390429
} \
391430
}
392431

@@ -430,9 +469,33 @@ static const struct fetch_type *find_fetch_type(const char *type)
430469
if (!type)
431470
type = DEFAULT_FETCH_TYPE_STR;
432471

472+
/* Special case: bitfield */
473+
if (*type == 'b') {
474+
unsigned long bs;
475+
type = strchr(type, '/');
476+
if (!type)
477+
goto fail;
478+
type++;
479+
if (strict_strtoul(type, 0, &bs))
480+
goto fail;
481+
switch (bs) {
482+
case 8:
483+
return find_fetch_type("u8");
484+
case 16:
485+
return find_fetch_type("u16");
486+
case 32:
487+
return find_fetch_type("u32");
488+
case 64:
489+
return find_fetch_type("u64");
490+
default:
491+
goto fail;
492+
}
493+
}
494+
433495
for (i = 0; i < ARRAY_SIZE(fetch_type_table); i++)
434496
if (strcmp(type, fetch_type_table[i].name) == 0)
435497
return &fetch_type_table[i];
498+
fail:
436499
return NULL;
437500
}
438501

@@ -586,7 +649,9 @@ static struct trace_probe *alloc_trace_probe(const char *group,
586649

587650
static void free_probe_arg(struct probe_arg *arg)
588651
{
589-
if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn))
652+
if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn))
653+
free_bitfield_fetch_param(arg->fetch.data);
654+
else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn))
590655
free_deref_fetch_param(arg->fetch.data);
591656
else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn))
592657
free_symbol_cache(arg->fetch.data);
@@ -806,6 +871,41 @@ static int __parse_probe_arg(char *arg, const struct fetch_type *t,
806871
return ret;
807872
}
808873

874+
#define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
875+
876+
/* Bitfield type needs to be parsed into a fetch function */
877+
static int __parse_bitfield_probe_arg(const char *bf,
878+
const struct fetch_type *t,
879+
struct fetch_param *f)
880+
{
881+
struct bitfield_fetch_param *bprm;
882+
unsigned long bw, bo;
883+
char *tail;
884+
885+
if (*bf != 'b')
886+
return 0;
887+
888+
bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
889+
if (!bprm)
890+
return -ENOMEM;
891+
bprm->orig = *f;
892+
f->fn = t->fetch[FETCH_MTD_bitfield];
893+
f->data = (void *)bprm;
894+
895+
bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
896+
if (bw == 0 || *tail != '@')
897+
return -EINVAL;
898+
899+
bf = tail + 1;
900+
bo = simple_strtoul(bf, &tail, 0);
901+
if (tail == bf || *tail != '/')
902+
return -EINVAL;
903+
904+
bprm->hi_shift = BYTES_TO_BITS(t->size) - (bw + bo);
905+
bprm->low_shift = bprm->hi_shift + bo;
906+
return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
907+
}
908+
809909
/* String length checking wrapper */
810910
static int parse_probe_arg(char *arg, struct trace_probe *tp,
811911
struct probe_arg *parg, int is_return)
@@ -835,6 +935,8 @@ static int parse_probe_arg(char *arg, struct trace_probe *tp,
835935
parg->offset = tp->size;
836936
tp->size += parg->type->size;
837937
ret = __parse_probe_arg(arg, parg->type, &parg->fetch, is_return);
938+
if (ret >= 0)
939+
ret = __parse_bitfield_probe_arg(t, parg->type, &parg->fetch);
838940
if (ret >= 0) {
839941
parg->fetch_size.fn = get_fetch_size_function(parg->type,
840942
parg->fetch.fn);

0 commit comments

Comments
 (0)