Skip to content

Commit 3fd996a

Browse files
Namhyung Kimrostedt
authored andcommitted
tracing/probes: Implement 'stack' fetch method for uprobes
Use separate method to fetch from stack. Move existing functions to trace_kprobe.c and make them static. Also add new stack fetch implementation for uprobes. Acked-by: Oleg Nesterov <oleg@redhat.com> Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> Cc: Srikar Dronamraju <srikar@linux.vnet.ibm.com> Cc: zhangwei(Jovi) <jovi.zhangwei@huawei.com> Cc: Arnaldo Carvalho de Melo <acme@ghostprotocols.net> Signed-off-by: Namhyung Kim <namhyung@kernel.org>
1 parent 34fee3a commit 3fd996a

File tree

4 files changed

+66
-26
lines changed

4 files changed

+66
-26
lines changed

kernel/trace/trace_kprobe.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,21 @@ static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
8888
static int kretprobe_dispatcher(struct kretprobe_instance *ri,
8989
struct pt_regs *regs);
9090

91+
/*
92+
* Kprobes-specific fetch functions
93+
*/
94+
#define DEFINE_FETCH_stack(type) \
95+
static __kprobes void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs,\
96+
void *offset, void *dest) \
97+
{ \
98+
*(type *)dest = (type)regs_get_kernel_stack_nth(regs, \
99+
(unsigned int)((unsigned long)offset)); \
100+
}
101+
DEFINE_BASIC_FETCH_FUNCS(stack)
102+
/* No string on the stack entry */
103+
#define fetch_stack_string NULL
104+
#define fetch_stack_string_size NULL
105+
91106
/* Fetch type information table */
92107
const struct fetch_type kprobes_fetch_type_table[] = {
93108
/* Special types */

kernel/trace/trace_probe.c

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,6 @@ __kprobes int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s,
7070

7171
const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
7272

73-
/*
74-
* Define macro for basic types - we don't need to define s* types, because
75-
* we have to care only about bitwidth at recording time.
76-
*/
77-
#define DEFINE_BASIC_FETCH_FUNCS(method) \
78-
DEFINE_FETCH_##method(u8) \
79-
DEFINE_FETCH_##method(u16) \
80-
DEFINE_FETCH_##method(u32) \
81-
DEFINE_FETCH_##method(u64)
82-
8373
#define CHECK_FETCH_FUNCS(method, fn) \
8474
(((FETCH_FUNC_NAME(method, u8) == fn) || \
8575
(FETCH_FUNC_NAME(method, u16) == fn) || \
@@ -102,18 +92,6 @@ DEFINE_BASIC_FETCH_FUNCS(reg)
10292
#define fetch_reg_string NULL
10393
#define fetch_reg_string_size NULL
10494

105-
#define DEFINE_FETCH_stack(type) \
106-
__kprobes void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs, \
107-
void *offset, void *dest) \
108-
{ \
109-
*(type *)dest = (type)regs_get_kernel_stack_nth(regs, \
110-
(unsigned int)((unsigned long)offset)); \
111-
}
112-
DEFINE_BASIC_FETCH_FUNCS(stack)
113-
/* No string on the stack entry */
114-
#define fetch_stack_string NULL
115-
#define fetch_stack_string_size NULL
116-
11795
#define DEFINE_FETCH_retval(type) \
11896
__kprobes void FETCH_FUNC_NAME(retval, type)(struct pt_regs *regs, \
11997
void *dummy, void *dest) \

kernel/trace/trace_probe.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,6 @@ DECLARE_BASIC_FETCH_FUNCS(reg);
167167
#define fetch_reg_string NULL
168168
#define fetch_reg_string_size NULL
169169

170-
DECLARE_BASIC_FETCH_FUNCS(stack);
171-
#define fetch_stack_string NULL
172-
#define fetch_stack_string_size NULL
173-
174170
DECLARE_BASIC_FETCH_FUNCS(retval);
175171
#define fetch_retval_string NULL
176172
#define fetch_retval_string_size NULL
@@ -191,6 +187,16 @@ DECLARE_BASIC_FETCH_FUNCS(bitfield);
191187
#define fetch_bitfield_string NULL
192188
#define fetch_bitfield_string_size NULL
193189

190+
/*
191+
* Define macro for basic types - we don't need to define s* types, because
192+
* we have to care only about bitwidth at recording time.
193+
*/
194+
#define DEFINE_BASIC_FETCH_FUNCS(method) \
195+
DEFINE_FETCH_##method(u8) \
196+
DEFINE_FETCH_##method(u16) \
197+
DEFINE_FETCH_##method(u32) \
198+
DEFINE_FETCH_##method(u64)
199+
194200
/* Default (unsigned long) fetch type */
195201
#define __DEFAULT_FETCH_TYPE(t) u##t
196202
#define _DEFAULT_FETCH_TYPE(t) __DEFAULT_FETCH_TYPE(t)

kernel/trace/trace_uprobe.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,47 @@ static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs);
7474
static int uretprobe_dispatcher(struct uprobe_consumer *con,
7575
unsigned long func, struct pt_regs *regs);
7676

77+
#ifdef CONFIG_STACK_GROWSUP
78+
static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
79+
{
80+
return addr - (n * sizeof(long));
81+
}
82+
#else
83+
static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
84+
{
85+
return addr + (n * sizeof(long));
86+
}
87+
#endif
88+
89+
static unsigned long get_user_stack_nth(struct pt_regs *regs, unsigned int n)
90+
{
91+
unsigned long ret;
92+
unsigned long addr = user_stack_pointer(regs);
93+
94+
addr = adjust_stack_addr(addr, n);
95+
96+
if (copy_from_user(&ret, (void __force __user *) addr, sizeof(ret)))
97+
return 0;
98+
99+
return ret;
100+
}
101+
102+
/*
103+
* Uprobes-specific fetch functions
104+
*/
105+
#define DEFINE_FETCH_stack(type) \
106+
static __kprobes void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs,\
107+
void *offset, void *dest) \
108+
{ \
109+
*(type *)dest = (type)get_user_stack_nth(regs, \
110+
((unsigned long)offset)); \
111+
}
112+
DEFINE_BASIC_FETCH_FUNCS(stack)
113+
/* No string on the stack entry */
114+
#define fetch_stack_string NULL
115+
#define fetch_stack_string_size NULL
116+
117+
77118
/* Fetch type information table */
78119
const struct fetch_type uprobes_fetch_type_table[] = {
79120
/* Special types */

0 commit comments

Comments
 (0)