]> Pileus Git - ~andy/linux/blob - kernel/trace/trace_probe.c
c26bc9eaa2ac843f3b8f71eb76f9a4f345a60360
[~andy/linux] / kernel / trace / trace_probe.c
1 /*
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
27 const 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
38 /* Printing  in basic type function template */
39 #define DEFINE_BASIC_PRINT_TYPE_FUNC(type, fmt)                         \
40 __kprobes int PRINT_TYPE_FUNC_NAME(type)(struct trace_seq *s,   \
41                                                 const char *name,       \
42                                                 void *data, void *ent)  \
43 {                                                                       \
44         return trace_seq_printf(s, " %s=" fmt, name, *(type *)data);    \
45 }                                                                       \
46 const char PRINT_TYPE_FMT_NAME(type)[] = fmt;
47
48 DEFINE_BASIC_PRINT_TYPE_FUNC(u8 , "0x%x")
49 DEFINE_BASIC_PRINT_TYPE_FUNC(u16, "0x%x")
50 DEFINE_BASIC_PRINT_TYPE_FUNC(u32, "0x%x")
51 DEFINE_BASIC_PRINT_TYPE_FUNC(u64, "0x%Lx")
52 DEFINE_BASIC_PRINT_TYPE_FUNC(s8,  "%d")
53 DEFINE_BASIC_PRINT_TYPE_FUNC(s16, "%d")
54 DEFINE_BASIC_PRINT_TYPE_FUNC(s32, "%d")
55 DEFINE_BASIC_PRINT_TYPE_FUNC(s64, "%Ld")
56
57 /* For defining macros, define string/string_size types */
58 typedef u32 string;
59 typedef u32 string_size;
60
61 /* Print type function for string type */
62 __kprobes int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s,
63                                                   const char *name,
64                                                   void *data, void *ent)
65 {
66         int len = *(u32 *)data >> 16;
67
68         if (!len)
69                 return trace_seq_printf(s, " %s=(fault)", name);
70         else
71                 return trace_seq_printf(s, " %s=\"%s\"", name,
72                                         (const char *)get_loc_data(data, ent));
73 }
74
75 const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
76
77 #define FETCH_FUNC_NAME(method, type)   fetch_##method##_##type
78 /*
79  * Define macro for basic types - we don't need to define s* types, because
80  * we have to care only about bitwidth at recording time.
81  */
82 #define DEFINE_BASIC_FETCH_FUNCS(method) \
83 DEFINE_FETCH_##method(u8)               \
84 DEFINE_FETCH_##method(u16)              \
85 DEFINE_FETCH_##method(u32)              \
86 DEFINE_FETCH_##method(u64)
87
88 #define CHECK_FETCH_FUNCS(method, fn)                   \
89         (((FETCH_FUNC_NAME(method, u8) == fn) ||        \
90           (FETCH_FUNC_NAME(method, u16) == fn) ||       \
91           (FETCH_FUNC_NAME(method, u32) == fn) ||       \
92           (FETCH_FUNC_NAME(method, u64) == fn) ||       \
93           (FETCH_FUNC_NAME(method, string) == fn) ||    \
94           (FETCH_FUNC_NAME(method, string_size) == fn)) \
95          && (fn != NULL))
96
97 /* Data fetch function templates */
98 #define DEFINE_FETCH_reg(type)                                          \
99 __kprobes void FETCH_FUNC_NAME(reg, type)(struct pt_regs *regs,         \
100                                         void *offset, void *dest)       \
101 {                                                                       \
102         *(type *)dest = (type)regs_get_register(regs,                   \
103                                 (unsigned int)((unsigned long)offset)); \
104 }
105 DEFINE_BASIC_FETCH_FUNCS(reg)
106 /* No string on the register */
107 #define fetch_reg_string        NULL
108 #define fetch_reg_string_size   NULL
109
110 #define DEFINE_FETCH_stack(type)                                        \
111 __kprobes void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs,       \
112                                           void *offset, void *dest)     \
113 {                                                                       \
114         *(type *)dest = (type)regs_get_kernel_stack_nth(regs,           \
115                                 (unsigned int)((unsigned long)offset)); \
116 }
117 DEFINE_BASIC_FETCH_FUNCS(stack)
118 /* No string on the stack entry */
119 #define fetch_stack_string      NULL
120 #define fetch_stack_string_size NULL
121
122 #define DEFINE_FETCH_retval(type)                                       \
123 __kprobes void FETCH_FUNC_NAME(retval, type)(struct pt_regs *regs,      \
124                                           void *dummy, void *dest)      \
125 {                                                                       \
126         *(type *)dest = (type)regs_return_value(regs);                  \
127 }
128 DEFINE_BASIC_FETCH_FUNCS(retval)
129 /* No string on the retval */
130 #define fetch_retval_string             NULL
131 #define fetch_retval_string_size        NULL
132
133 #define DEFINE_FETCH_memory(type)                                       \
134 __kprobes void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs,      \
135                                           void *addr, void *dest)       \
136 {                                                                       \
137         type retval;                                                    \
138         if (probe_kernel_address(addr, retval))                         \
139                 *(type *)dest = 0;                                      \
140         else                                                            \
141                 *(type *)dest = retval;                                 \
142 }
143 DEFINE_BASIC_FETCH_FUNCS(memory)
144 /*
145  * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
146  * length and relative data location.
147  */
148 __kprobes void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs,
149                                                       void *addr, void *dest)
150 {
151         long ret;
152         int maxlen = get_rloc_len(*(u32 *)dest);
153         u8 *dst = get_rloc_data(dest);
154         u8 *src = addr;
155         mm_segment_t old_fs = get_fs();
156
157         if (!maxlen)
158                 return;
159
160         /*
161          * Try to get string again, since the string can be changed while
162          * probing.
163          */
164         set_fs(KERNEL_DS);
165         pagefault_disable();
166
167         do
168                 ret = __copy_from_user_inatomic(dst++, src++, 1);
169         while (dst[-1] && ret == 0 && src - (u8 *)addr < maxlen);
170
171         dst[-1] = '\0';
172         pagefault_enable();
173         set_fs(old_fs);
174
175         if (ret < 0) {  /* Failed to fetch string */
176                 ((u8 *)get_rloc_data(dest))[0] = '\0';
177                 *(u32 *)dest = make_data_rloc(0, get_rloc_offs(*(u32 *)dest));
178         } else {
179                 *(u32 *)dest = make_data_rloc(src - (u8 *)addr,
180                                               get_rloc_offs(*(u32 *)dest));
181         }
182 }
183
184 /* Return the length of string -- including null terminal byte */
185 __kprobes void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs,
186                                                         void *addr, void *dest)
187 {
188         mm_segment_t old_fs;
189         int ret, len = 0;
190         u8 c;
191
192         old_fs = get_fs();
193         set_fs(KERNEL_DS);
194         pagefault_disable();
195
196         do {
197                 ret = __copy_from_user_inatomic(&c, (u8 *)addr + len, 1);
198                 len++;
199         } while (c && ret == 0 && len < MAX_STRING_SIZE);
200
201         pagefault_enable();
202         set_fs(old_fs);
203
204         if (ret < 0)    /* Failed to check the length */
205                 *(u32 *)dest = 0;
206         else
207                 *(u32 *)dest = len;
208 }
209
210 /* Memory fetching by symbol */
211 struct symbol_cache {
212         char            *symbol;
213         long            offset;
214         unsigned long   addr;
215 };
216
217 static unsigned long update_symbol_cache(struct symbol_cache *sc)
218 {
219         sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
220
221         if (sc->addr)
222                 sc->addr += sc->offset;
223
224         return sc->addr;
225 }
226
227 static void free_symbol_cache(struct symbol_cache *sc)
228 {
229         kfree(sc->symbol);
230         kfree(sc);
231 }
232
233 static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
234 {
235         struct symbol_cache *sc;
236
237         if (!sym || strlen(sym) == 0)
238                 return NULL;
239
240         sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
241         if (!sc)
242                 return NULL;
243
244         sc->symbol = kstrdup(sym, GFP_KERNEL);
245         if (!sc->symbol) {
246                 kfree(sc);
247                 return NULL;
248         }
249         sc->offset = offset;
250         update_symbol_cache(sc);
251
252         return sc;
253 }
254
255 #define DEFINE_FETCH_symbol(type)                                       \
256 __kprobes void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs,      \
257                                           void *data, void *dest)       \
258 {                                                                       \
259         struct symbol_cache *sc = data;                                 \
260         if (sc->addr)                                                   \
261                 fetch_memory_##type(regs, (void *)sc->addr, dest);      \
262         else                                                            \
263                 *(type *)dest = 0;                                      \
264 }
265 DEFINE_BASIC_FETCH_FUNCS(symbol)
266 DEFINE_FETCH_symbol(string)
267 DEFINE_FETCH_symbol(string_size)
268
269 /* Dereference memory access function */
270 struct deref_fetch_param {
271         struct fetch_param      orig;
272         long                    offset;
273 };
274
275 #define DEFINE_FETCH_deref(type)                                        \
276 __kprobes void FETCH_FUNC_NAME(deref, type)(struct pt_regs *regs,       \
277                                             void *data, void *dest)     \
278 {                                                                       \
279         struct deref_fetch_param *dprm = data;                          \
280         unsigned long addr;                                             \
281         call_fetch(&dprm->orig, regs, &addr);                           \
282         if (addr) {                                                     \
283                 addr += dprm->offset;                                   \
284                 fetch_memory_##type(regs, (void *)addr, dest);          \
285         } else                                                          \
286                 *(type *)dest = 0;                                      \
287 }
288 DEFINE_BASIC_FETCH_FUNCS(deref)
289 DEFINE_FETCH_deref(string)
290 DEFINE_FETCH_deref(string_size)
291
292 static __kprobes void update_deref_fetch_param(struct deref_fetch_param *data)
293 {
294         if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
295                 update_deref_fetch_param(data->orig.data);
296         else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
297                 update_symbol_cache(data->orig.data);
298 }
299
300 static __kprobes void free_deref_fetch_param(struct deref_fetch_param *data)
301 {
302         if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
303                 free_deref_fetch_param(data->orig.data);
304         else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
305                 free_symbol_cache(data->orig.data);
306         kfree(data);
307 }
308
309 /* Bitfield fetch function */
310 struct bitfield_fetch_param {
311         struct fetch_param      orig;
312         unsigned char           hi_shift;
313         unsigned char           low_shift;
314 };
315
316 #define DEFINE_FETCH_bitfield(type)                                     \
317 __kprobes void FETCH_FUNC_NAME(bitfield, type)(struct pt_regs *regs,    \
318                                             void *data, void *dest)     \
319 {                                                                       \
320         struct bitfield_fetch_param *bprm = data;                       \
321         type buf = 0;                                                   \
322         call_fetch(&bprm->orig, regs, &buf);                            \
323         if (buf) {                                                      \
324                 buf <<= bprm->hi_shift;                                 \
325                 buf >>= bprm->low_shift;                                \
326         }                                                               \
327         *(type *)dest = buf;                                            \
328 }
329
330 DEFINE_BASIC_FETCH_FUNCS(bitfield)
331 #define fetch_bitfield_string           NULL
332 #define fetch_bitfield_string_size      NULL
333
334 static __kprobes void
335 update_bitfield_fetch_param(struct bitfield_fetch_param *data)
336 {
337         /*
338          * Don't check the bitfield itself, because this must be the
339          * last fetch function.
340          */
341         if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
342                 update_deref_fetch_param(data->orig.data);
343         else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
344                 update_symbol_cache(data->orig.data);
345 }
346
347 static __kprobes void
348 free_bitfield_fetch_param(struct bitfield_fetch_param *data)
349 {
350         /*
351          * Don't check the bitfield itself, because this must be the
352          * last fetch function.
353          */
354         if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
355                 free_deref_fetch_param(data->orig.data);
356         else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
357                 free_symbol_cache(data->orig.data);
358
359         kfree(data);
360 }
361
362 /* Fetch type information table */
363 static const struct fetch_type fetch_type_table[] = {
364         /* Special types */
365         [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string,
366                                         sizeof(u32), 1, "__data_loc char[]"),
367         [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32,
368                                         string_size, sizeof(u32), 0, "u32"),
369         /* Basic types */
370         ASSIGN_FETCH_TYPE(u8,  u8,  0),
371         ASSIGN_FETCH_TYPE(u16, u16, 0),
372         ASSIGN_FETCH_TYPE(u32, u32, 0),
373         ASSIGN_FETCH_TYPE(u64, u64, 0),
374         ASSIGN_FETCH_TYPE(s8,  u8,  1),
375         ASSIGN_FETCH_TYPE(s16, u16, 1),
376         ASSIGN_FETCH_TYPE(s32, u32, 1),
377         ASSIGN_FETCH_TYPE(s64, u64, 1),
378 };
379
380 static const struct fetch_type *find_fetch_type(const char *type)
381 {
382         int i;
383
384         if (!type)
385                 type = DEFAULT_FETCH_TYPE_STR;
386
387         /* Special case: bitfield */
388         if (*type == 'b') {
389                 unsigned long bs;
390
391                 type = strchr(type, '/');
392                 if (!type)
393                         goto fail;
394
395                 type++;
396                 if (kstrtoul(type, 0, &bs))
397                         goto fail;
398
399                 switch (bs) {
400                 case 8:
401                         return find_fetch_type("u8");
402                 case 16:
403                         return find_fetch_type("u16");
404                 case 32:
405                         return find_fetch_type("u32");
406                 case 64:
407                         return find_fetch_type("u64");
408                 default:
409                         goto fail;
410                 }
411         }
412
413         for (i = 0; i < ARRAY_SIZE(fetch_type_table); i++)
414                 if (strcmp(type, fetch_type_table[i].name) == 0)
415                         return &fetch_type_table[i];
416
417 fail:
418         return NULL;
419 }
420
421 /* Special function : only accept unsigned long */
422 static __kprobes void fetch_stack_address(struct pt_regs *regs,
423                                         void *dummy, void *dest)
424 {
425         *(unsigned long *)dest = kernel_stack_pointer(regs);
426 }
427
428 static fetch_func_t get_fetch_size_function(const struct fetch_type *type,
429                                         fetch_func_t orig_fn)
430 {
431         int i;
432
433         if (type != &fetch_type_table[FETCH_TYPE_STRING])
434                 return NULL;    /* Only string type needs size function */
435
436         for (i = 0; i < FETCH_MTD_END; i++)
437                 if (type->fetch[i] == orig_fn)
438                         return fetch_type_table[FETCH_TYPE_STRSIZE].fetch[i];
439
440         WARN_ON(1);     /* This should not happen */
441
442         return NULL;
443 }
444
445 /* Split symbol and offset. */
446 int traceprobe_split_symbol_offset(char *symbol, unsigned long *offset)
447 {
448         char *tmp;
449         int ret;
450
451         if (!offset)
452                 return -EINVAL;
453
454         tmp = strchr(symbol, '+');
455         if (tmp) {
456                 /* skip sign because kstrtoul doesn't accept '+' */
457                 ret = kstrtoul(tmp + 1, 0, offset);
458                 if (ret)
459                         return ret;
460
461                 *tmp = '\0';
462         } else
463                 *offset = 0;
464
465         return 0;
466 }
467
468 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
469
470 static int parse_probe_vars(char *arg, const struct fetch_type *t,
471                             struct fetch_param *f, bool is_return)
472 {
473         int ret = 0;
474         unsigned long param;
475
476         if (strcmp(arg, "retval") == 0) {
477                 if (is_return)
478                         f->fn = t->fetch[FETCH_MTD_retval];
479                 else
480                         ret = -EINVAL;
481         } else if (strncmp(arg, "stack", 5) == 0) {
482                 if (arg[5] == '\0') {
483                         if (strcmp(t->name, DEFAULT_FETCH_TYPE_STR) == 0)
484                                 f->fn = fetch_stack_address;
485                         else
486                                 ret = -EINVAL;
487                 } else if (isdigit(arg[5])) {
488                         ret = kstrtoul(arg + 5, 10, &param);
489                         if (ret || param > PARAM_MAX_STACK)
490                                 ret = -EINVAL;
491                         else {
492                                 f->fn = t->fetch[FETCH_MTD_stack];
493                                 f->data = (void *)param;
494                         }
495                 } else
496                         ret = -EINVAL;
497         } else
498                 ret = -EINVAL;
499
500         return ret;
501 }
502
503 /* Recursive argument parser */
504 static int parse_probe_arg(char *arg, const struct fetch_type *t,
505                      struct fetch_param *f, bool is_return, bool is_kprobe)
506 {
507         unsigned long param;
508         long offset;
509         char *tmp;
510         int ret;
511
512         ret = 0;
513
514         /* Until uprobe_events supports only reg arguments */
515         if (!is_kprobe && arg[0] != '%')
516                 return -EINVAL;
517
518         switch (arg[0]) {
519         case '$':
520                 ret = parse_probe_vars(arg + 1, t, f, is_return);
521                 break;
522
523         case '%':       /* named register */
524                 ret = regs_query_register_offset(arg + 1);
525                 if (ret >= 0) {
526                         f->fn = t->fetch[FETCH_MTD_reg];
527                         f->data = (void *)(unsigned long)ret;
528                         ret = 0;
529                 }
530                 break;
531
532         case '@':       /* memory or symbol */
533                 if (isdigit(arg[1])) {
534                         ret = kstrtoul(arg + 1, 0, &param);
535                         if (ret)
536                                 break;
537
538                         f->fn = t->fetch[FETCH_MTD_memory];
539                         f->data = (void *)param;
540                 } else {
541                         ret = traceprobe_split_symbol_offset(arg + 1, &offset);
542                         if (ret)
543                                 break;
544
545                         f->data = alloc_symbol_cache(arg + 1, offset);
546                         if (f->data)
547                                 f->fn = t->fetch[FETCH_MTD_symbol];
548                 }
549                 break;
550
551         case '+':       /* deref memory */
552                 arg++;  /* Skip '+', because kstrtol() rejects it. */
553         case '-':
554                 tmp = strchr(arg, '(');
555                 if (!tmp)
556                         break;
557
558                 *tmp = '\0';
559                 ret = kstrtol(arg, 0, &offset);
560
561                 if (ret)
562                         break;
563
564                 arg = tmp + 1;
565                 tmp = strrchr(arg, ')');
566
567                 if (tmp) {
568                         struct deref_fetch_param        *dprm;
569                         const struct fetch_type         *t2;
570
571                         t2 = find_fetch_type(NULL);
572                         *tmp = '\0';
573                         dprm = kzalloc(sizeof(struct deref_fetch_param), GFP_KERNEL);
574
575                         if (!dprm)
576                                 return -ENOMEM;
577
578                         dprm->offset = offset;
579                         ret = parse_probe_arg(arg, t2, &dprm->orig, is_return,
580                                                         is_kprobe);
581                         if (ret)
582                                 kfree(dprm);
583                         else {
584                                 f->fn = t->fetch[FETCH_MTD_deref];
585                                 f->data = (void *)dprm;
586                         }
587                 }
588                 break;
589         }
590         if (!ret && !f->fn) {   /* Parsed, but do not find fetch method */
591                 pr_info("%s type has no corresponding fetch method.\n", t->name);
592                 ret = -EINVAL;
593         }
594
595         return ret;
596 }
597
598 #define BYTES_TO_BITS(nb)       ((BITS_PER_LONG * (nb)) / sizeof(long))
599
600 /* Bitfield type needs to be parsed into a fetch function */
601 static int __parse_bitfield_probe_arg(const char *bf,
602                                       const struct fetch_type *t,
603                                       struct fetch_param *f)
604 {
605         struct bitfield_fetch_param *bprm;
606         unsigned long bw, bo;
607         char *tail;
608
609         if (*bf != 'b')
610                 return 0;
611
612         bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
613         if (!bprm)
614                 return -ENOMEM;
615
616         bprm->orig = *f;
617         f->fn = t->fetch[FETCH_MTD_bitfield];
618         f->data = (void *)bprm;
619         bw = simple_strtoul(bf + 1, &tail, 0);  /* Use simple one */
620
621         if (bw == 0 || *tail != '@')
622                 return -EINVAL;
623
624         bf = tail + 1;
625         bo = simple_strtoul(bf, &tail, 0);
626
627         if (tail == bf || *tail != '/')
628                 return -EINVAL;
629
630         bprm->hi_shift = BYTES_TO_BITS(t->size) - (bw + bo);
631         bprm->low_shift = bprm->hi_shift + bo;
632
633         return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
634 }
635
636 /* String length checking wrapper */
637 int traceprobe_parse_probe_arg(char *arg, ssize_t *size,
638                 struct probe_arg *parg, bool is_return, bool is_kprobe)
639 {
640         const char *t;
641         int ret;
642
643         if (strlen(arg) > MAX_ARGSTR_LEN) {
644                 pr_info("Argument is too long.: %s\n",  arg);
645                 return -ENOSPC;
646         }
647         parg->comm = kstrdup(arg, GFP_KERNEL);
648         if (!parg->comm) {
649                 pr_info("Failed to allocate memory for command '%s'.\n", arg);
650                 return -ENOMEM;
651         }
652         t = strchr(parg->comm, ':');
653         if (t) {
654                 arg[t - parg->comm] = '\0';
655                 t++;
656         }
657         parg->type = find_fetch_type(t);
658         if (!parg->type) {
659                 pr_info("Unsupported type: %s\n", t);
660                 return -EINVAL;
661         }
662         parg->offset = *size;
663         *size += parg->type->size;
664         ret = parse_probe_arg(arg, parg->type, &parg->fetch, is_return, is_kprobe);
665
666         if (ret >= 0 && t != NULL)
667                 ret = __parse_bitfield_probe_arg(t, parg->type, &parg->fetch);
668
669         if (ret >= 0) {
670                 parg->fetch_size.fn = get_fetch_size_function(parg->type,
671                                                               parg->fetch.fn);
672                 parg->fetch_size.data = parg->fetch.data;
673         }
674
675         return ret;
676 }
677
678 /* Return 1 if name is reserved or already used by another argument */
679 int traceprobe_conflict_field_name(const char *name,
680                                struct probe_arg *args, int narg)
681 {
682         int i;
683
684         for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
685                 if (strcmp(reserved_field_names[i], name) == 0)
686                         return 1;
687
688         for (i = 0; i < narg; i++)
689                 if (strcmp(args[i].name, name) == 0)
690                         return 1;
691
692         return 0;
693 }
694
695 void traceprobe_update_arg(struct probe_arg *arg)
696 {
697         if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn))
698                 update_bitfield_fetch_param(arg->fetch.data);
699         else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn))
700                 update_deref_fetch_param(arg->fetch.data);
701         else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn))
702                 update_symbol_cache(arg->fetch.data);
703 }
704
705 void traceprobe_free_probe_arg(struct probe_arg *arg)
706 {
707         if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn))
708                 free_bitfield_fetch_param(arg->fetch.data);
709         else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn))
710                 free_deref_fetch_param(arg->fetch.data);
711         else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn))
712                 free_symbol_cache(arg->fetch.data);
713
714         kfree(arg->name);
715         kfree(arg->comm);
716 }
717
718 int traceprobe_command(const char *buf, int (*createfn)(int, char **))
719 {
720         char **argv;
721         int argc, ret;
722
723         argc = 0;
724         ret = 0;
725         argv = argv_split(GFP_KERNEL, buf, &argc);
726         if (!argv)
727                 return -ENOMEM;
728
729         if (argc)
730                 ret = createfn(argc, argv);
731
732         argv_free(argv);
733
734         return ret;
735 }
736
737 #define WRITE_BUFSIZE  4096
738
739 ssize_t traceprobe_probes_write(struct file *file, const char __user *buffer,
740                                 size_t count, loff_t *ppos,
741                                 int (*createfn)(int, char **))
742 {
743         char *kbuf, *tmp;
744         int ret = 0;
745         size_t done = 0;
746         size_t size;
747
748         kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
749         if (!kbuf)
750                 return -ENOMEM;
751
752         while (done < count) {
753                 size = count - done;
754
755                 if (size >= WRITE_BUFSIZE)
756                         size = WRITE_BUFSIZE - 1;
757
758                 if (copy_from_user(kbuf, buffer + done, size)) {
759                         ret = -EFAULT;
760                         goto out;
761                 }
762                 kbuf[size] = '\0';
763                 tmp = strchr(kbuf, '\n');
764
765                 if (tmp) {
766                         *tmp = '\0';
767                         size = tmp - kbuf + 1;
768                 } else if (done + size < count) {
769                         pr_warning("Line length is too long: "
770                                    "Should be less than %d.", WRITE_BUFSIZE);
771                         ret = -EINVAL;
772                         goto out;
773                 }
774                 done += size;
775                 /* Remove comments */
776                 tmp = strchr(kbuf, '#');
777
778                 if (tmp)
779                         *tmp = '\0';
780
781                 ret = traceprobe_command(kbuf, createfn);
782                 if (ret)
783                         goto out;
784         }
785         ret = done;
786
787 out:
788         kfree(kbuf);
789
790         return ret;
791 }
792
793 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
794                            bool is_return)
795 {
796         int i;
797         int pos = 0;
798
799         const char *fmt, *arg;
800
801         if (!is_return) {
802                 fmt = "(%lx)";
803                 arg = "REC->" FIELD_STRING_IP;
804         } else {
805                 fmt = "(%lx <- %lx)";
806                 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
807         }
808
809         /* When len=0, we just calculate the needed length */
810 #define LEN_OR_ZERO (len ? len - pos : 0)
811
812         pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
813
814         for (i = 0; i < tp->nr_args; i++) {
815                 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
816                                 tp->args[i].name, tp->args[i].type->fmt);
817         }
818
819         pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
820
821         for (i = 0; i < tp->nr_args; i++) {
822                 if (strcmp(tp->args[i].type->name, "string") == 0)
823                         pos += snprintf(buf + pos, LEN_OR_ZERO,
824                                         ", __get_str(%s)",
825                                         tp->args[i].name);
826                 else
827                         pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
828                                         tp->args[i].name);
829         }
830
831 #undef LEN_OR_ZERO
832
833         /* return the length of print_fmt */
834         return pos;
835 }
836
837 int set_print_fmt(struct trace_probe *tp, bool is_return)
838 {
839         int len;
840         char *print_fmt;
841
842         /* First: called with 0 length to calculate the needed length */
843         len = __set_print_fmt(tp, NULL, 0, is_return);
844         print_fmt = kmalloc(len + 1, GFP_KERNEL);
845         if (!print_fmt)
846                 return -ENOMEM;
847
848         /* Second: actually write the @print_fmt */
849         __set_print_fmt(tp, print_fmt, len + 1, is_return);
850         tp->call.print_fmt = print_fmt;
851
852         return 0;
853 }