1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/seq_file.h>
14#include <linux/slab.h>
15#include <linux/smp.h>
16#include <linux/tracefs.h>
17#include <linux/types.h>
18#include <linux/string.h>
19#include <linux/ctype.h>
20#include <linux/ptrace.h>
21#include <linux/perf_event.h>
22#include <linux/kprobes.h>
23#include <linux/stringify.h>
24#include <linux/limits.h>
25#include <linux/uaccess.h>
26#include <asm/bitsperlong.h>
27
28#include "trace.h"
29#include "trace_output.h"
30
31#define MAX_TRACE_ARGS 128
32#define MAX_ARGSTR_LEN 63
33#define MAX_STRING_SIZE PATH_MAX
34
35
36#define FIELD_STRING_IP "__probe_ip"
37#define FIELD_STRING_RETIP "__probe_ret_ip"
38#define FIELD_STRING_FUNC "__probe_func"
39
40#undef DEFINE_FIELD
41#define DEFINE_FIELD(type, item, name, is_signed) \
42 do { \
43 ret = trace_define_field(event_call, #type, name, \
44 offsetof(typeof(field), item), \
45 sizeof(field.item), is_signed, \
46 FILTER_OTHER); \
47 if (ret) \
48 return ret; \
49 } while (0)
50
51
52
53#define TP_FLAG_TRACE 1
54#define TP_FLAG_PROFILE 2
55#define TP_FLAG_REGISTERED 4
56
57
58
59#define make_data_rloc(len, roffs) \
60 (((u32)(len) << 16) | ((u32)(roffs) & 0xffff))
61#define get_rloc_len(dl) ((u32)(dl) >> 16)
62#define get_rloc_offs(dl) ((u32)(dl) & 0xffff)
63
64
65
66
67
68
69#define convert_rloc_to_loc(dl, offs) ((u32)(dl) + (offs))
70
71static nokprobe_inline void *get_rloc_data(u32 *dl)
72{
73 return (u8 *)dl + get_rloc_offs(*dl);
74}
75
76
77static nokprobe_inline void *get_loc_data(u32 *dl, void *ent)
78{
79 return (u8 *)ent + get_rloc_offs(*dl);
80}
81
82
83typedef void (*fetch_func_t)(struct pt_regs *, void *, void *);
84
85typedef int (*print_type_func_t)(struct trace_seq *, const char *, void *, void *);
86
87
88enum {
89 FETCH_MTD_reg = 0,
90 FETCH_MTD_stack,
91 FETCH_MTD_retval,
92 FETCH_MTD_comm,
93 FETCH_MTD_memory,
94 FETCH_MTD_symbol,
95 FETCH_MTD_deref,
96 FETCH_MTD_bitfield,
97 FETCH_MTD_file_offset,
98 FETCH_MTD_END,
99};
100
101
102struct fetch_type {
103 const char *name;
104 size_t size;
105 int is_signed;
106 print_type_func_t print;
107 const char *fmt;
108 const char *fmttype;
109
110 fetch_func_t fetch[FETCH_MTD_END];
111};
112
113struct fetch_param {
114 fetch_func_t fn;
115 void *data;
116};
117
118
119typedef u32 string;
120typedef u32 string_size;
121
122#define PRINT_TYPE_FUNC_NAME(type) print_type_##type
123#define PRINT_TYPE_FMT_NAME(type) print_type_format_##type
124
125
126#define DECLARE_BASIC_PRINT_TYPE_FUNC(type) \
127int PRINT_TYPE_FUNC_NAME(type)(struct trace_seq *s, const char *name, \
128 void *data, void *ent); \
129extern const char PRINT_TYPE_FMT_NAME(type)[]
130
131DECLARE_BASIC_PRINT_TYPE_FUNC(u8);
132DECLARE_BASIC_PRINT_TYPE_FUNC(u16);
133DECLARE_BASIC_PRINT_TYPE_FUNC(u32);
134DECLARE_BASIC_PRINT_TYPE_FUNC(u64);
135DECLARE_BASIC_PRINT_TYPE_FUNC(s8);
136DECLARE_BASIC_PRINT_TYPE_FUNC(s16);
137DECLARE_BASIC_PRINT_TYPE_FUNC(s32);
138DECLARE_BASIC_PRINT_TYPE_FUNC(s64);
139DECLARE_BASIC_PRINT_TYPE_FUNC(x8);
140DECLARE_BASIC_PRINT_TYPE_FUNC(x16);
141DECLARE_BASIC_PRINT_TYPE_FUNC(x32);
142DECLARE_BASIC_PRINT_TYPE_FUNC(x64);
143
144DECLARE_BASIC_PRINT_TYPE_FUNC(string);
145
146#define FETCH_FUNC_NAME(method, type) fetch_##method##_##type
147
148
149#define DECLARE_FETCH_FUNC(method, type) \
150extern void FETCH_FUNC_NAME(method, type)(struct pt_regs *regs, \
151 void *data, void *dest)
152
153#define DECLARE_BASIC_FETCH_FUNCS(method) \
154DECLARE_FETCH_FUNC(method, u8); \
155DECLARE_FETCH_FUNC(method, u16); \
156DECLARE_FETCH_FUNC(method, u32); \
157DECLARE_FETCH_FUNC(method, u64)
158
159DECLARE_BASIC_FETCH_FUNCS(reg);
160#define fetch_reg_string NULL
161#define fetch_reg_string_size NULL
162
163DECLARE_BASIC_FETCH_FUNCS(retval);
164#define fetch_retval_string NULL
165#define fetch_retval_string_size NULL
166
167DECLARE_BASIC_FETCH_FUNCS(symbol);
168DECLARE_FETCH_FUNC(symbol, string);
169DECLARE_FETCH_FUNC(symbol, string_size);
170
171DECLARE_BASIC_FETCH_FUNCS(deref);
172DECLARE_FETCH_FUNC(deref, string);
173DECLARE_FETCH_FUNC(deref, string_size);
174
175DECLARE_BASIC_FETCH_FUNCS(bitfield);
176#define fetch_bitfield_string NULL
177#define fetch_bitfield_string_size NULL
178
179
180#define fetch_comm_u8 NULL
181#define fetch_comm_u16 NULL
182#define fetch_comm_u32 NULL
183#define fetch_comm_u64 NULL
184DECLARE_FETCH_FUNC(comm, string);
185DECLARE_FETCH_FUNC(comm, string_size);
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) x##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(comm, ftype), \
218ASSIGN_FETCH_FUNC(memory, ftype), \
219ASSIGN_FETCH_FUNC(symbol, ftype), \
220ASSIGN_FETCH_FUNC(deref, ftype), \
221ASSIGN_FETCH_FUNC(bitfield, ftype), \
222ASSIGN_FETCH_FUNC(file_offset, ftype), \
223 } \
224 }
225
226#define ASSIGN_FETCH_TYPE(ptype, ftype, sign) \
227 __ASSIGN_FETCH_TYPE(#ptype, ptype, ftype, sizeof(ftype), sign, #ptype)
228
229
230#define ASSIGN_FETCH_TYPE_ALIAS(ptype, atype, ftype, sign) \
231 __ASSIGN_FETCH_TYPE(#ptype, ptype, ftype, sizeof(ftype), sign, #atype)
232
233#define ASSIGN_FETCH_TYPE_END {}
234
235#define FETCH_TYPE_STRING 0
236#define FETCH_TYPE_STRSIZE 1
237
238#ifdef CONFIG_KPROBE_EVENTS
239struct symbol_cache;
240unsigned long update_symbol_cache(struct symbol_cache *sc);
241void free_symbol_cache(struct symbol_cache *sc);
242struct symbol_cache *alloc_symbol_cache(const char *sym, long offset);
243bool trace_kprobe_on_func_entry(struct trace_event_call *call);
244bool trace_kprobe_error_injectable(struct trace_event_call *call);
245#else
246
247#define fetch_symbol_u8 NULL
248#define fetch_symbol_u16 NULL
249#define fetch_symbol_u32 NULL
250#define fetch_symbol_u64 NULL
251#define fetch_symbol_string NULL
252#define fetch_symbol_string_size NULL
253
254struct symbol_cache {
255};
256static inline unsigned long __used update_symbol_cache(struct symbol_cache *sc)
257{
258 return 0;
259}
260
261static inline void __used free_symbol_cache(struct symbol_cache *sc)
262{
263}
264
265static inline struct symbol_cache * __used
266alloc_symbol_cache(const char *sym, long offset)
267{
268 return NULL;
269}
270
271static inline bool trace_kprobe_on_func_entry(struct trace_event_call *call)
272{
273 return false;
274}
275
276static inline bool trace_kprobe_error_injectable(struct trace_event_call *call)
277{
278 return false;
279}
280#endif
281
282struct probe_arg {
283 struct fetch_param fetch;
284 struct fetch_param fetch_size;
285 unsigned int offset;
286 const char *name;
287 const char *comm;
288 const struct fetch_type *type;
289};
290
291struct trace_probe {
292 unsigned int flags;
293 struct trace_event_class class;
294 struct trace_event_call call;
295 struct list_head files;
296 ssize_t size;
297 unsigned int nr_args;
298 struct probe_arg args[];
299};
300
301struct event_file_link {
302 struct trace_event_file *file;
303 struct list_head list;
304};
305
306static inline bool trace_probe_is_enabled(struct trace_probe *tp)
307{
308 return !!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE));
309}
310
311static inline bool trace_probe_is_registered(struct trace_probe *tp)
312{
313 return !!(tp->flags & TP_FLAG_REGISTERED);
314}
315
316static nokprobe_inline void call_fetch(struct fetch_param *fprm,
317 struct pt_regs *regs, void *dest)
318{
319 return fprm->fn(regs, fprm->data, dest);
320}
321
322
323static inline bool is_good_name(const char *name)
324{
325 if (!isalpha(*name) && *name != '_')
326 return false;
327 while (*++name != '\0') {
328 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
329 return false;
330 }
331 return true;
332}
333
334static inline struct event_file_link *
335find_event_file_link(struct trace_probe *tp, struct trace_event_file *file)
336{
337 struct event_file_link *link;
338
339 list_for_each_entry(link, &tp->files, list)
340 if (link->file == file)
341 return link;
342
343 return NULL;
344}
345
346extern int traceprobe_parse_probe_arg(char *arg, ssize_t *size,
347 struct probe_arg *parg, bool is_return, bool is_kprobe,
348 const struct fetch_type *ftbl);
349
350extern int traceprobe_conflict_field_name(const char *name,
351 struct probe_arg *args, int narg);
352
353extern void traceprobe_update_arg(struct probe_arg *arg);
354extern void traceprobe_free_probe_arg(struct probe_arg *arg);
355
356extern int traceprobe_split_symbol_offset(char *symbol, long *offset);
357
358
359static nokprobe_inline int
360__get_data_size(struct trace_probe *tp, struct pt_regs *regs)
361{
362 int i, ret = 0;
363 u32 len;
364
365 for (i = 0; i < tp->nr_args; i++)
366 if (unlikely(tp->args[i].fetch_size.fn)) {
367 call_fetch(&tp->args[i].fetch_size, regs, &len);
368 ret += len;
369 }
370
371 return ret;
372}
373
374
375static nokprobe_inline void
376store_trace_args(int ent_size, struct trace_probe *tp, struct pt_regs *regs,
377 u8 *data, int maxlen)
378{
379 int i;
380 u32 end = tp->size;
381 u32 *dl;
382
383 for (i = 0; i < tp->nr_args; i++) {
384 if (unlikely(tp->args[i].fetch_size.fn)) {
385
386
387
388
389 dl = (u32 *)(data + tp->args[i].offset);
390 *dl = make_data_rloc(maxlen, end - tp->args[i].offset);
391
392 call_fetch(&tp->args[i].fetch, regs, dl);
393
394 end += get_rloc_len(*dl);
395 maxlen -= get_rloc_len(*dl);
396
397 *dl = convert_rloc_to_loc(*dl,
398 ent_size + tp->args[i].offset);
399 } else
400
401 call_fetch(&tp->args[i].fetch, regs,
402 data + tp->args[i].offset);
403 }
404}
405
406extern int set_print_fmt(struct trace_probe *tp, bool is_return);
407
408#ifdef CONFIG_PERF_EVENTS
409extern struct trace_event_call *
410create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
411 bool is_return);
412extern void destroy_local_trace_kprobe(struct trace_event_call *event_call);
413
414extern struct trace_event_call *
415create_local_trace_uprobe(char *name, unsigned long offs, bool is_return);
416extern void destroy_local_trace_uprobe(struct trace_event_call *event_call);
417#endif
418