blob: 5ea191b917e90997d52619be56ff8bde096076f9 [file] [log] [blame]
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +05301/*
2 * Common code for probe-based Dynamic events.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * This code was copied from kernel/trace/trace_kprobe.c written by
18 * Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
19 *
20 * Updates to make this generic:
21 * Copyright (C) IBM Corporation, 2010-2011
22 * Author: Srikar Dronamraju
23 */
24
25#include "trace_probe.h"
26
27const char *reserved_field_names[] = {
28 "common_type",
29 "common_flags",
30 "common_preempt_count",
31 "common_pid",
32 "common_tgid",
33 FIELD_STRING_IP,
34 FIELD_STRING_RETIP,
35 FIELD_STRING_FUNC,
36};
37
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053038/* Printing in basic type function template */
Masami Hiramatsu17ce3dc2016-08-18 17:57:50 +090039#define DEFINE_BASIC_PRINT_TYPE_FUNC(tname, type, fmt) \
40int PRINT_TYPE_FUNC_NAME(tname)(struct trace_seq *s, const char *name, \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +090041 void *data, void *ent) \
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053042{ \
Steven Rostedt (Red Hat)d2b01912014-11-12 17:19:51 -050043 trace_seq_printf(s, " %s=" fmt, name, *(type *)data); \
44 return !trace_seq_has_overflowed(s); \
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053045} \
Masami Hiramatsu17ce3dc2016-08-18 17:57:50 +090046const char PRINT_TYPE_FMT_NAME(tname)[] = fmt; \
47NOKPROBE_SYMBOL(PRINT_TYPE_FUNC_NAME(tname));
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053048
Masami Hiramatsubdca79c22016-08-18 17:59:21 +090049DEFINE_BASIC_PRINT_TYPE_FUNC(u8, u8, "%u")
50DEFINE_BASIC_PRINT_TYPE_FUNC(u16, u16, "%u")
51DEFINE_BASIC_PRINT_TYPE_FUNC(u32, u32, "%u")
52DEFINE_BASIC_PRINT_TYPE_FUNC(u64, u64, "%Lu")
Masami Hiramatsu17ce3dc2016-08-18 17:57:50 +090053DEFINE_BASIC_PRINT_TYPE_FUNC(s8, s8, "%d")
54DEFINE_BASIC_PRINT_TYPE_FUNC(s16, s16, "%d")
55DEFINE_BASIC_PRINT_TYPE_FUNC(s32, s32, "%d")
56DEFINE_BASIC_PRINT_TYPE_FUNC(s64, s64, "%Ld")
57DEFINE_BASIC_PRINT_TYPE_FUNC(x8, u8, "0x%x")
58DEFINE_BASIC_PRINT_TYPE_FUNC(x16, u16, "0x%x")
59DEFINE_BASIC_PRINT_TYPE_FUNC(x32, u32, "0x%x")
60DEFINE_BASIC_PRINT_TYPE_FUNC(x64, u64, "0x%Lx")
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053061
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053062/* Print type function for string type */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +090063int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, const char *name,
64 void *data, void *ent)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053065{
66 int len = *(u32 *)data >> 16;
67
68 if (!len)
Steven Rostedt (Red Hat)d2b01912014-11-12 17:19:51 -050069 trace_seq_printf(s, " %s=(fault)", name);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053070 else
Steven Rostedt (Red Hat)d2b01912014-11-12 17:19:51 -050071 trace_seq_printf(s, " %s=\"%s\"", name,
72 (const char *)get_loc_data(data, ent));
73 return !trace_seq_has_overflowed(s);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053074}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +090075NOKPROBE_SYMBOL(PRINT_TYPE_FUNC_NAME(string));
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053076
Namhyung Kimb26c74e2013-11-26 14:19:59 +090077const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053078
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053079#define CHECK_FETCH_FUNCS(method, fn) \
80 (((FETCH_FUNC_NAME(method, u8) == fn) || \
81 (FETCH_FUNC_NAME(method, u16) == fn) || \
82 (FETCH_FUNC_NAME(method, u32) == fn) || \
83 (FETCH_FUNC_NAME(method, u64) == fn) || \
84 (FETCH_FUNC_NAME(method, string) == fn) || \
85 (FETCH_FUNC_NAME(method, string_size) == fn)) \
86 && (fn != NULL))
87
88/* Data fetch function templates */
89#define DEFINE_FETCH_reg(type) \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +090090void FETCH_FUNC_NAME(reg, type)(struct pt_regs *regs, void *offset, void *dest) \
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053091{ \
92 *(type *)dest = (type)regs_get_register(regs, \
93 (unsigned int)((unsigned long)offset)); \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +090094} \
95NOKPROBE_SYMBOL(FETCH_FUNC_NAME(reg, type));
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053096DEFINE_BASIC_FETCH_FUNCS(reg)
97/* No string on the register */
98#define fetch_reg_string NULL
99#define fetch_reg_string_size NULL
100
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530101#define DEFINE_FETCH_retval(type) \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900102void FETCH_FUNC_NAME(retval, type)(struct pt_regs *regs, \
103 void *dummy, void *dest) \
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530104{ \
105 *(type *)dest = (type)regs_return_value(regs); \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900106} \
107NOKPROBE_SYMBOL(FETCH_FUNC_NAME(retval, type));
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530108DEFINE_BASIC_FETCH_FUNCS(retval)
109/* No string on the retval */
110#define fetch_retval_string NULL
111#define fetch_retval_string_size NULL
112
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530113/* Dereference memory access function */
114struct deref_fetch_param {
115 struct fetch_param orig;
116 long offset;
Hyeoncheol Lee3925f4a2013-07-01 13:44:32 +0900117 fetch_func_t fetch;
118 fetch_func_t fetch_size;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530119};
120
121#define DEFINE_FETCH_deref(type) \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900122void FETCH_FUNC_NAME(deref, type)(struct pt_regs *regs, \
123 void *data, void *dest) \
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530124{ \
125 struct deref_fetch_param *dprm = data; \
126 unsigned long addr; \
127 call_fetch(&dprm->orig, regs, &addr); \
128 if (addr) { \
129 addr += dprm->offset; \
Hyeoncheol Lee3925f4a2013-07-01 13:44:32 +0900130 dprm->fetch(regs, (void *)addr, dest); \
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530131 } else \
132 *(type *)dest = 0; \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900133} \
134NOKPROBE_SYMBOL(FETCH_FUNC_NAME(deref, type));
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530135DEFINE_BASIC_FETCH_FUNCS(deref)
136DEFINE_FETCH_deref(string)
Hyeoncheol Lee3925f4a2013-07-01 13:44:32 +0900137
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900138void FETCH_FUNC_NAME(deref, string_size)(struct pt_regs *regs,
139 void *data, void *dest)
Hyeoncheol Lee3925f4a2013-07-01 13:44:32 +0900140{
141 struct deref_fetch_param *dprm = data;
142 unsigned long addr;
143
144 call_fetch(&dprm->orig, regs, &addr);
145 if (addr && dprm->fetch_size) {
146 addr += dprm->offset;
147 dprm->fetch_size(regs, (void *)addr, dest);
148 } else
149 *(string_size *)dest = 0;
150}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900151NOKPROBE_SYMBOL(FETCH_FUNC_NAME(deref, string_size));
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530152
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900153static void update_deref_fetch_param(struct deref_fetch_param *data)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530154{
155 if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
156 update_deref_fetch_param(data->orig.data);
157 else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
158 update_symbol_cache(data->orig.data);
159}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900160NOKPROBE_SYMBOL(update_deref_fetch_param);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530161
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900162static void free_deref_fetch_param(struct deref_fetch_param *data)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530163{
164 if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
165 free_deref_fetch_param(data->orig.data);
166 else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
167 free_symbol_cache(data->orig.data);
168 kfree(data);
169}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900170NOKPROBE_SYMBOL(free_deref_fetch_param);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530171
172/* Bitfield fetch function */
173struct bitfield_fetch_param {
174 struct fetch_param orig;
175 unsigned char hi_shift;
176 unsigned char low_shift;
177};
178
179#define DEFINE_FETCH_bitfield(type) \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900180void FETCH_FUNC_NAME(bitfield, type)(struct pt_regs *regs, \
181 void *data, void *dest) \
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530182{ \
183 struct bitfield_fetch_param *bprm = data; \
184 type buf = 0; \
185 call_fetch(&bprm->orig, regs, &buf); \
186 if (buf) { \
187 buf <<= bprm->hi_shift; \
188 buf >>= bprm->low_shift; \
189 } \
190 *(type *)dest = buf; \
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900191} \
192NOKPROBE_SYMBOL(FETCH_FUNC_NAME(bitfield, type));
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530193DEFINE_BASIC_FETCH_FUNCS(bitfield)
194#define fetch_bitfield_string NULL
195#define fetch_bitfield_string_size NULL
196
Masami Hiramatsufbc19632014-04-17 17:18:00 +0900197static void
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530198update_bitfield_fetch_param(struct bitfield_fetch_param *data)
199{
200 /*
201 * Don't check the bitfield itself, because this must be the
202 * last fetch function.
203 */
204 if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
205 update_deref_fetch_param(data->orig.data);
206 else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
207 update_symbol_cache(data->orig.data);
208}
209
Masami Hiramatsufbc19632014-04-17 17:18:00 +0900210static void
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530211free_bitfield_fetch_param(struct bitfield_fetch_param *data)
212{
213 /*
214 * Don't check the bitfield itself, because this must be the
215 * last fetch function.
216 */
217 if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
218 free_deref_fetch_param(data->orig.data);
219 else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
220 free_symbol_cache(data->orig.data);
221
222 kfree(data);
223}
224
Omar Sandoval35abb672016-06-08 18:38:02 -0700225void FETCH_FUNC_NAME(comm, string)(struct pt_regs *regs,
226 void *data, void *dest)
227{
228 int maxlen = get_rloc_len(*(u32 *)dest);
229 u8 *dst = get_rloc_data(dest);
230 long ret;
231
232 if (!maxlen)
233 return;
234
235 ret = strlcpy(dst, current->comm, maxlen);
236 *(u32 *)dest = make_data_rloc(ret, get_rloc_offs(*(u32 *)dest));
237}
238NOKPROBE_SYMBOL(FETCH_FUNC_NAME(comm, string));
239
240void FETCH_FUNC_NAME(comm, string_size)(struct pt_regs *regs,
241 void *data, void *dest)
242{
243 *(u32 *)dest = strlen(current->comm) + 1;
244}
245NOKPROBE_SYMBOL(FETCH_FUNC_NAME(comm, string_size));
246
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900247static const struct fetch_type *find_fetch_type(const char *type,
248 const struct fetch_type *ftbl)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530249{
250 int i;
251
252 if (!type)
253 type = DEFAULT_FETCH_TYPE_STR;
254
255 /* Special case: bitfield */
256 if (*type == 'b') {
257 unsigned long bs;
258
259 type = strchr(type, '/');
260 if (!type)
261 goto fail;
262
263 type++;
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200264 if (kstrtoul(type, 0, &bs))
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530265 goto fail;
266
267 switch (bs) {
268 case 8:
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900269 return find_fetch_type("u8", ftbl);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530270 case 16:
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900271 return find_fetch_type("u16", ftbl);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530272 case 32:
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900273 return find_fetch_type("u32", ftbl);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530274 case 64:
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900275 return find_fetch_type("u64", ftbl);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530276 default:
277 goto fail;
278 }
279 }
280
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900281 for (i = 0; ftbl[i].name; i++) {
282 if (strcmp(type, ftbl[i].name) == 0)
283 return &ftbl[i];
284 }
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530285
286fail:
287 return NULL;
288}
289
290/* Special function : only accept unsigned long */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900291static void fetch_kernel_stack_address(struct pt_regs *regs, void *dummy, void *dest)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530292{
293 *(unsigned long *)dest = kernel_stack_pointer(regs);
294}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900295NOKPROBE_SYMBOL(fetch_kernel_stack_address);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530296
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900297static void fetch_user_stack_address(struct pt_regs *regs, void *dummy, void *dest)
Namhyung Kimb079d372013-07-03 18:34:23 +0900298{
299 *(unsigned long *)dest = user_stack_pointer(regs);
300}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900301NOKPROBE_SYMBOL(fetch_user_stack_address);
Namhyung Kimb079d372013-07-03 18:34:23 +0900302
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530303static fetch_func_t get_fetch_size_function(const struct fetch_type *type,
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900304 fetch_func_t orig_fn,
305 const struct fetch_type *ftbl)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530306{
307 int i;
308
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900309 if (type != &ftbl[FETCH_TYPE_STRING])
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530310 return NULL; /* Only string type needs size function */
311
312 for (i = 0; i < FETCH_MTD_END; i++)
313 if (type->fetch[i] == orig_fn)
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900314 return ftbl[FETCH_TYPE_STRSIZE].fetch[i];
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530315
316 WARN_ON(1); /* This should not happen */
317
318 return NULL;
319}
320
321/* Split symbol and offset. */
Masami Hiramatsud434dae2018-03-17 21:38:10 +0900322int traceprobe_split_symbol_offset(char *symbol, long *offset)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530323{
324 char *tmp;
325 int ret;
326
327 if (!offset)
328 return -EINVAL;
329
Masami Hiramatsud434dae2018-03-17 21:38:10 +0900330 tmp = strpbrk(symbol, "+-");
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530331 if (tmp) {
Masami Hiramatsud434dae2018-03-17 21:38:10 +0900332 ret = kstrtol(tmp, 0, offset);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530333 if (ret)
334 return ret;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530335 *tmp = '\0';
336 } else
337 *offset = 0;
338
339 return 0;
340}
341
342#define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
343
344static int parse_probe_vars(char *arg, const struct fetch_type *t,
Namhyung Kimb079d372013-07-03 18:34:23 +0900345 struct fetch_param *f, bool is_return,
346 bool is_kprobe)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530347{
348 int ret = 0;
349 unsigned long param;
350
351 if (strcmp(arg, "retval") == 0) {
352 if (is_return)
353 f->fn = t->fetch[FETCH_MTD_retval];
354 else
355 ret = -EINVAL;
356 } else if (strncmp(arg, "stack", 5) == 0) {
357 if (arg[5] == '\0') {
Namhyung Kimb079d372013-07-03 18:34:23 +0900358 if (strcmp(t->name, DEFAULT_FETCH_TYPE_STR))
359 return -EINVAL;
360
361 if (is_kprobe)
362 f->fn = fetch_kernel_stack_address;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530363 else
Namhyung Kimb079d372013-07-03 18:34:23 +0900364 f->fn = fetch_user_stack_address;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530365 } else if (isdigit(arg[5])) {
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200366 ret = kstrtoul(arg + 5, 10, &param);
Namhyung Kimb079d372013-07-03 18:34:23 +0900367 if (ret || (is_kprobe && param > PARAM_MAX_STACK))
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530368 ret = -EINVAL;
369 else {
370 f->fn = t->fetch[FETCH_MTD_stack];
371 f->data = (void *)param;
372 }
373 } else
374 ret = -EINVAL;
Omar Sandoval35abb672016-06-08 18:38:02 -0700375 } else if (strcmp(arg, "comm") == 0) {
376 if (strcmp(t->name, "string") != 0 &&
377 strcmp(t->name, "string_size") != 0)
378 return -EINVAL;
379 f->fn = t->fetch[FETCH_MTD_comm];
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530380 } else
381 ret = -EINVAL;
382
383 return ret;
384}
385
386/* Recursive argument parser */
387static int parse_probe_arg(char *arg, const struct fetch_type *t,
Stephen Rothwelld9a16d32015-03-12 16:58:34 +1100388 struct fetch_param *f, bool is_return, bool is_kprobe,
389 const struct fetch_type *ftbl)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530390{
391 unsigned long param;
392 long offset;
393 char *tmp;
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900394 int ret = 0;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530395
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530396 switch (arg[0]) {
397 case '$':
Namhyung Kimb079d372013-07-03 18:34:23 +0900398 ret = parse_probe_vars(arg + 1, t, f, is_return, is_kprobe);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530399 break;
400
401 case '%': /* named register */
402 ret = regs_query_register_offset(arg + 1);
403 if (ret >= 0) {
404 f->fn = t->fetch[FETCH_MTD_reg];
405 f->data = (void *)(unsigned long)ret;
406 ret = 0;
407 }
408 break;
409
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900410 case '@': /* memory, file-offset or symbol */
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530411 if (isdigit(arg[1])) {
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200412 ret = kstrtoul(arg + 1, 0, &param);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530413 if (ret)
414 break;
415
416 f->fn = t->fetch[FETCH_MTD_memory];
417 f->data = (void *)param;
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900418 } else if (arg[1] == '+') {
419 /* kprobes don't support file offsets */
420 if (is_kprobe)
421 return -EINVAL;
422
423 ret = kstrtol(arg + 2, 0, &offset);
424 if (ret)
425 break;
426
427 f->fn = t->fetch[FETCH_MTD_file_offset];
428 f->data = (void *)offset;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530429 } else {
Namhyung Kimb079d372013-07-03 18:34:23 +0900430 /* uprobes don't support symbols */
431 if (!is_kprobe)
432 return -EINVAL;
433
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530434 ret = traceprobe_split_symbol_offset(arg + 1, &offset);
435 if (ret)
436 break;
437
438 f->data = alloc_symbol_cache(arg + 1, offset);
439 if (f->data)
440 f->fn = t->fetch[FETCH_MTD_symbol];
441 }
442 break;
443
444 case '+': /* deref memory */
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200445 arg++; /* Skip '+', because kstrtol() rejects it. */
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530446 case '-':
447 tmp = strchr(arg, '(');
448 if (!tmp)
449 break;
450
451 *tmp = '\0';
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200452 ret = kstrtol(arg, 0, &offset);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530453
454 if (ret)
455 break;
456
457 arg = tmp + 1;
458 tmp = strrchr(arg, ')');
459
460 if (tmp) {
461 struct deref_fetch_param *dprm;
462 const struct fetch_type *t2;
463
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900464 t2 = find_fetch_type(NULL, ftbl);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530465 *tmp = '\0';
466 dprm = kzalloc(sizeof(struct deref_fetch_param), GFP_KERNEL);
467
468 if (!dprm)
469 return -ENOMEM;
470
471 dprm->offset = offset;
Hyeoncheol Lee3925f4a2013-07-01 13:44:32 +0900472 dprm->fetch = t->fetch[FETCH_MTD_memory];
473 dprm->fetch_size = get_fetch_size_function(t,
474 dprm->fetch, ftbl);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530475 ret = parse_probe_arg(arg, t2, &dprm->orig, is_return,
Stephen Rothwelld9a16d32015-03-12 16:58:34 +1100476 is_kprobe, ftbl);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530477 if (ret)
478 kfree(dprm);
479 else {
480 f->fn = t->fetch[FETCH_MTD_deref];
481 f->data = (void *)dprm;
482 }
483 }
484 break;
485 }
486 if (!ret && !f->fn) { /* Parsed, but do not find fetch method */
487 pr_info("%s type has no corresponding fetch method.\n", t->name);
488 ret = -EINVAL;
489 }
490
491 return ret;
492}
493
494#define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
495
496/* Bitfield type needs to be parsed into a fetch function */
497static int __parse_bitfield_probe_arg(const char *bf,
498 const struct fetch_type *t,
499 struct fetch_param *f)
500{
501 struct bitfield_fetch_param *bprm;
502 unsigned long bw, bo;
503 char *tail;
504
505 if (*bf != 'b')
506 return 0;
507
508 bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
509 if (!bprm)
510 return -ENOMEM;
511
512 bprm->orig = *f;
513 f->fn = t->fetch[FETCH_MTD_bitfield];
514 f->data = (void *)bprm;
515 bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
516
517 if (bw == 0 || *tail != '@')
518 return -EINVAL;
519
520 bf = tail + 1;
521 bo = simple_strtoul(bf, &tail, 0);
522
523 if (tail == bf || *tail != '/')
524 return -EINVAL;
525
526 bprm->hi_shift = BYTES_TO_BITS(t->size) - (bw + bo);
527 bprm->low_shift = bprm->hi_shift + bo;
528
529 return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
530}
531
532/* String length checking wrapper */
533int traceprobe_parse_probe_arg(char *arg, ssize_t *size,
Stephen Rothwelld9a16d32015-03-12 16:58:34 +1100534 struct probe_arg *parg, bool is_return, bool is_kprobe,
535 const struct fetch_type *ftbl)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530536{
537 const char *t;
538 int ret;
539
540 if (strlen(arg) > MAX_ARGSTR_LEN) {
541 pr_info("Argument is too long.: %s\n", arg);
542 return -ENOSPC;
543 }
544 parg->comm = kstrdup(arg, GFP_KERNEL);
545 if (!parg->comm) {
546 pr_info("Failed to allocate memory for command '%s'.\n", arg);
547 return -ENOMEM;
548 }
549 t = strchr(parg->comm, ':');
550 if (t) {
551 arg[t - parg->comm] = '\0';
552 t++;
553 }
Omar Sandoval35abb672016-06-08 18:38:02 -0700554 /*
555 * The default type of $comm should be "string", and it can't be
556 * dereferenced.
557 */
558 if (!t && strcmp(arg, "$comm") == 0)
559 t = "string";
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900560 parg->type = find_fetch_type(t, ftbl);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530561 if (!parg->type) {
562 pr_info("Unsupported type: %s\n", t);
563 return -EINVAL;
564 }
565 parg->offset = *size;
566 *size += parg->type->size;
Stephen Rothwelld9a16d32015-03-12 16:58:34 +1100567 ret = parse_probe_arg(arg, parg->type, &parg->fetch, is_return,
568 is_kprobe, ftbl);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530569
570 if (ret >= 0 && t != NULL)
571 ret = __parse_bitfield_probe_arg(t, parg->type, &parg->fetch);
572
573 if (ret >= 0) {
574 parg->fetch_size.fn = get_fetch_size_function(parg->type,
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900575 parg->fetch.fn,
576 ftbl);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530577 parg->fetch_size.data = parg->fetch.data;
578 }
579
580 return ret;
581}
582
583/* Return 1 if name is reserved or already used by another argument */
584int traceprobe_conflict_field_name(const char *name,
585 struct probe_arg *args, int narg)
586{
587 int i;
588
589 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
590 if (strcmp(reserved_field_names[i], name) == 0)
591 return 1;
592
593 for (i = 0; i < narg; i++)
594 if (strcmp(args[i].name, name) == 0)
595 return 1;
596
597 return 0;
598}
599
600void traceprobe_update_arg(struct probe_arg *arg)
601{
602 if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn))
603 update_bitfield_fetch_param(arg->fetch.data);
604 else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn))
605 update_deref_fetch_param(arg->fetch.data);
606 else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn))
607 update_symbol_cache(arg->fetch.data);
608}
609
610void traceprobe_free_probe_arg(struct probe_arg *arg)
611{
612 if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn))
613 free_bitfield_fetch_param(arg->fetch.data);
614 else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn))
615 free_deref_fetch_param(arg->fetch.data);
616 else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn))
617 free_symbol_cache(arg->fetch.data);
618
619 kfree(arg->name);
620 kfree(arg->comm);
621}
622
623int traceprobe_command(const char *buf, int (*createfn)(int, char **))
624{
625 char **argv;
626 int argc, ret;
627
628 argc = 0;
629 ret = 0;
630 argv = argv_split(GFP_KERNEL, buf, &argc);
631 if (!argv)
632 return -ENOMEM;
633
634 if (argc)
635 ret = createfn(argc, argv);
636
637 argv_free(argv);
638
639 return ret;
640}
641
642#define WRITE_BUFSIZE 4096
643
644ssize_t traceprobe_probes_write(struct file *file, const char __user *buffer,
645 size_t count, loff_t *ppos,
646 int (*createfn)(int, char **))
647{
648 char *kbuf, *tmp;
649 int ret = 0;
650 size_t done = 0;
651 size_t size;
652
653 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
654 if (!kbuf)
655 return -ENOMEM;
656
657 while (done < count) {
658 size = count - done;
659
660 if (size >= WRITE_BUFSIZE)
661 size = WRITE_BUFSIZE - 1;
662
663 if (copy_from_user(kbuf, buffer + done, size)) {
664 ret = -EFAULT;
665 goto out;
666 }
667 kbuf[size] = '\0';
668 tmp = strchr(kbuf, '\n');
669
670 if (tmp) {
671 *tmp = '\0';
672 size = tmp - kbuf + 1;
673 } else if (done + size < count) {
Joe Perchesa395d6a2016-03-22 14:28:09 -0700674 pr_warn("Line length is too long: Should be less than %d\n",
675 WRITE_BUFSIZE);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530676 ret = -EINVAL;
677 goto out;
678 }
679 done += size;
680 /* Remove comments */
681 tmp = strchr(kbuf, '#');
682
683 if (tmp)
684 *tmp = '\0';
685
686 ret = traceprobe_command(kbuf, createfn);
687 if (ret)
688 goto out;
689 }
690 ret = done;
691
692out:
693 kfree(kbuf);
694
695 return ret;
696}
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900697
698static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
699 bool is_return)
700{
701 int i;
702 int pos = 0;
703
704 const char *fmt, *arg;
705
706 if (!is_return) {
707 fmt = "(%lx)";
708 arg = "REC->" FIELD_STRING_IP;
709 } else {
710 fmt = "(%lx <- %lx)";
711 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
712 }
713
714 /* When len=0, we just calculate the needed length */
715#define LEN_OR_ZERO (len ? len - pos : 0)
716
717 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
718
719 for (i = 0; i < tp->nr_args; i++) {
720 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
721 tp->args[i].name, tp->args[i].type->fmt);
722 }
723
724 pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
725
726 for (i = 0; i < tp->nr_args; i++) {
727 if (strcmp(tp->args[i].type->name, "string") == 0)
728 pos += snprintf(buf + pos, LEN_OR_ZERO,
729 ", __get_str(%s)",
730 tp->args[i].name);
731 else
732 pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
733 tp->args[i].name);
734 }
735
736#undef LEN_OR_ZERO
737
738 /* return the length of print_fmt */
739 return pos;
740}
741
742int set_print_fmt(struct trace_probe *tp, bool is_return)
743{
744 int len;
745 char *print_fmt;
746
747 /* First: called with 0 length to calculate the needed length */
748 len = __set_print_fmt(tp, NULL, 0, is_return);
749 print_fmt = kmalloc(len + 1, GFP_KERNEL);
750 if (!print_fmt)
751 return -ENOMEM;
752
753 /* Second: actually write the @print_fmt */
754 __set_print_fmt(tp, print_fmt, len + 1, is_return);
755 tp->call.print_fmt = print_fmt;
756
757 return 0;
758}