1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <linux/seq_file.h>
26#include <linux/slab.h>
27#include <linux/smp.h>
28#include <linux/debugfs.h>
29#include <linux/types.h>
30#include <linux/string.h>
31#include <linux/ctype.h>
32#include <linux/ptrace.h>
33#include <linux/perf_event.h>
34#include <linux/kprobes.h>
35#include <linux/stringify.h>
36#include <linux/limits.h>
37#include <linux/uaccess.h>
38#include <asm/bitsperlong.h>
39
40#include "trace.h"
41#include "trace_output.h"
42
43#define MAX_TRACE_ARGS 128
44#define MAX_ARGSTR_LEN 63
45#define MAX_EVENT_NAME_LEN 64
46#define MAX_STRING_SIZE PATH_MAX
47
48
49#define FIELD_STRING_IP "__probe_ip"
50#define FIELD_STRING_RETIP "__probe_ret_ip"
51#define FIELD_STRING_FUNC "__probe_func"
52
53#undef DEFINE_FIELD
54#define DEFINE_FIELD(type, item, name, is_signed) \
55 do { \
56 ret = trace_define_field(event_call, #type, name, \
57 offsetof(typeof(field), item), \
58 sizeof(field.item), is_signed, \
59 FILTER_OTHER); \
60 if (ret) \
61 return ret; \
62 } while (0)
63
64
65
66#define TP_FLAG_TRACE 1
67#define TP_FLAG_PROFILE 2
68#define TP_FLAG_REGISTERED 4
69
70
71
72#define make_data_rloc(len, roffs) \
73 (((u32)(len) << 16) | ((u32)(roffs) & 0xffff))
74#define get_rloc_len(dl) ((u32)(dl) >> 16)
75#define get_rloc_offs(dl) ((u32)(dl) & 0xffff)
76
77
78
79
80
81
82#define convert_rloc_to_loc(dl, offs) ((u32)(dl) + (offs))
83
84static inline void *get_rloc_data(u32 *dl)
85{
86 return (u8 *)dl + get_rloc_offs(*dl);
87}
88
89
90static inline void *get_loc_data(u32 *dl, void *ent)
91{
92 return (u8 *)ent + get_rloc_offs(*dl);
93}
94
95
96typedef void (*fetch_func_t)(struct pt_regs *, void *, void *);
97
98typedef int (*print_type_func_t)(struct trace_seq *, const char *, void *, void *);
99
100
101enum {
102 FETCH_MTD_reg = 0,
103 FETCH_MTD_stack,
104 FETCH_MTD_retval,
105 FETCH_MTD_memory,
106 FETCH_MTD_symbol,
107 FETCH_MTD_deref,
108 FETCH_MTD_bitfield,
109 FETCH_MTD_file_offset,
110 FETCH_MTD_END,
111};
112
113
114struct fetch_type {
115 const char *name;
116 size_t size;
117 int is_signed;
118 print_type_func_t print;
119 const char *fmt;
120 const char *fmttype;
121
122 fetch_func_t fetch[FETCH_MTD_END];
123};
124
125struct fetch_param {
126 fetch_func_t fn;
127 void *data;
128};
129
130
131typedef u32 string;
132typedef u32 string_size;
133
134#define PRINT_TYPE_FUNC_NAME(type) print_type_##type
135#define PRINT_TYPE_FMT_NAME(type) print_type_format_##type
136
137
138#define DECLARE_BASIC_PRINT_TYPE_FUNC(type) \
139__kprobes int PRINT_TYPE_FUNC_NAME(type)(struct trace_seq *s, \
140 const char *name, \
141 void *data, void *ent); \
142extern const char PRINT_TYPE_FMT_NAME(type)[]
143
144DECLARE_BASIC_PRINT_TYPE_FUNC(u8);
145DECLARE_BASIC_PRINT_TYPE_FUNC(u16);
146DECLARE_BASIC_PRINT_TYPE_FUNC(u32);
147DECLARE_BASIC_PRINT_TYPE_FUNC(u64);
148DECLARE_BASIC_PRINT_TYPE_FUNC(s8);
149DECLARE_BASIC_PRINT_TYPE_FUNC(s16);
150DECLARE_BASIC_PRINT_TYPE_FUNC(s32);
151DECLARE_BASIC_PRINT_TYPE_FUNC(s64);
152DECLARE_BASIC_PRINT_TYPE_FUNC(string);
153
154#define FETCH_FUNC_NAME(method, type) fetch_##method##_##type
155
156
157#define DECLARE_FETCH_FUNC(method, type) \
158extern void FETCH_FUNC_NAME(method, type)(struct pt_regs *regs, \
159 void *data, void *dest)
160
161#define DECLARE_BASIC_FETCH_FUNCS(method) \
162DECLARE_FETCH_FUNC(method, u8); \
163DECLARE_FETCH_FUNC(method, u16); \
164DECLARE_FETCH_FUNC(method, u32); \
165DECLARE_FETCH_FUNC(method, u64)
166
167DECLARE_BASIC_FETCH_FUNCS(reg);
168#define fetch_reg_string NULL
169#define fetch_reg_string_size NULL
170
171DECLARE_BASIC_FETCH_FUNCS(retval);
172#define fetch_retval_string NULL
173#define fetch_retval_string_size NULL
174
175DECLARE_BASIC_FETCH_FUNCS(symbol);
176DECLARE_FETCH_FUNC(symbol, string);
177DECLARE_FETCH_FUNC(symbol, string_size);
178
179DECLARE_BASIC_FETCH_FUNCS(deref);
180DECLARE_FETCH_FUNC(deref, string);
181DECLARE_FETCH_FUNC(deref, string_size);
182
183DECLARE_BASIC_FETCH_FUNCS(bitfield);
184#define fetch_bitfield_string NULL
185#define fetch_bitfield_string_size NULL
186
187
188
189
190
191#define DEFINE_BASIC_FETCH_FUNCS(method) \
192DEFINE_FETCH_##method(u8) \
193DEFINE_FETCH_##method(u16) \
194DEFINE_FETCH_##method(u32) \
195DEFINE_FETCH_##method(u64)
196
197
198#define __DEFAULT_FETCH_TYPE(t) u##t
199#define _DEFAULT_FETCH_TYPE(t) __DEFAULT_FETCH_TYPE(t)
200#define DEFAULT_FETCH_TYPE _DEFAULT_FETCH_TYPE(BITS_PER_LONG)
201#define DEFAULT_FETCH_TYPE_STR __stringify(DEFAULT_FETCH_TYPE)
202
203#define ASSIGN_FETCH_FUNC(method, type) \
204 [FETCH_MTD_##method] = FETCH_FUNC_NAME(method, type)
205
206#define __ASSIGN_FETCH_TYPE(_name, ptype, ftype, _size, sign, _fmttype) \
207 {.name = _name, \
208 .size = _size, \
209 .is_signed = sign, \
210 .print = PRINT_TYPE_FUNC_NAME(ptype), \
211 .fmt = PRINT_TYPE_FMT_NAME(ptype), \
212 .fmttype = _fmttype, \
213 .fetch = { \
214ASSIGN_FETCH_FUNC(reg, ftype), \
215ASSIGN_FETCH_FUNC(stack, ftype), \
216ASSIGN_FETCH_FUNC(retval, ftype), \
217ASSIGN_FETCH_FUNC(memory, ftype), \
218ASSIGN_FETCH_FUNC(symbol, ftype), \
219ASSIGN_FETCH_FUNC(deref, ftype), \
220ASSIGN_FETCH_FUNC(bitfield, ftype), \
221ASSIGN_FETCH_FUNC(file_offset, ftype), \
222 } \
223 }
224
225#define ASSIGN_FETCH_TYPE(ptype, ftype, sign) \
226 __ASSIGN_FETCH_TYPE(#ptype, ptype, ftype, sizeof(ftype), sign, #ptype)
227
228#define ASSIGN_FETCH_TYPE_END {}
229
230#define FETCH_TYPE_STRING 0
231#define FETCH_TYPE_STRSIZE 1
232
233
234
235
236
237extern __weak const struct fetch_type kprobes_fetch_type_table[];
238extern __weak const struct fetch_type uprobes_fetch_type_table[];
239
240#ifdef CONFIG_KPROBE_EVENT
241struct symbol_cache;
242unsigned long update_symbol_cache(struct symbol_cache *sc);
243void free_symbol_cache(struct symbol_cache *sc);
244struct symbol_cache *alloc_symbol_cache(const char *sym, long offset);
245#else
246struct symbol_cache {
247};
248static inline unsigned long __used update_symbol_cache(struct symbol_cache *sc)
249{
250 return 0;
251}
252
253static inline void __used free_symbol_cache(struct symbol_cache *sc)
254{
255}
256
257static inline struct symbol_cache * __used
258alloc_symbol_cache(const char *sym, long offset)
259{
260 return NULL;
261}
262#endif
263
264struct probe_arg {
265 struct fetch_param fetch;
266 struct fetch_param fetch_size;
267 unsigned int offset;
268 const char *name;
269 const char *comm;
270 const struct fetch_type *type;
271};
272
273static inline __kprobes void call_fetch(struct fetch_param *fprm,
274 struct pt_regs *regs, void *dest)
275{
276 return fprm->fn(regs, fprm->data, dest);
277}
278
279
280static inline int is_good_name(const char *name)
281{
282 if (!isalpha(*name) && *name != '_')
283 return 0;
284 while (*++name != '\0') {
285 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
286 return 0;
287 }
288 return 1;
289}
290
291extern int traceprobe_parse_probe_arg(char *arg, ssize_t *size,
292 struct probe_arg *parg, bool is_return, bool is_kprobe);
293
294extern int traceprobe_conflict_field_name(const char *name,
295 struct probe_arg *args, int narg);
296
297extern void traceprobe_update_arg(struct probe_arg *arg);
298extern void traceprobe_free_probe_arg(struct probe_arg *arg);
299
300extern int traceprobe_split_symbol_offset(char *symbol, unsigned long *offset);
301
302extern ssize_t traceprobe_probes_write(struct file *file,
303 const char __user *buffer, size_t count, loff_t *ppos,
304 int (*createfn)(int, char**));
305
306extern int traceprobe_command(const char *buf, int (*createfn)(int, char**));
307