trace_probe.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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. #include "trace_probe.h"
  25. const char *reserved_field_names[] = {
  26. "common_type",
  27. "common_flags",
  28. "common_preempt_count",
  29. "common_pid",
  30. "common_tgid",
  31. FIELD_STRING_IP,
  32. FIELD_STRING_RETIP,
  33. FIELD_STRING_FUNC,
  34. };
  35. /* Printing in basic type function template */
  36. #define DEFINE_BASIC_PRINT_TYPE_FUNC(type, fmt) \
  37. __kprobes int PRINT_TYPE_FUNC_NAME(type)(struct trace_seq *s, \
  38. const char *name, \
  39. void *data, void *ent) \
  40. { \
  41. return trace_seq_printf(s, " %s=" fmt, name, *(type *)data); \
  42. } \
  43. const char PRINT_TYPE_FMT_NAME(type)[] = fmt;
  44. DEFINE_BASIC_PRINT_TYPE_FUNC(u8 , "0x%x")
  45. DEFINE_BASIC_PRINT_TYPE_FUNC(u16, "0x%x")
  46. DEFINE_BASIC_PRINT_TYPE_FUNC(u32, "0x%x")
  47. DEFINE_BASIC_PRINT_TYPE_FUNC(u64, "0x%Lx")
  48. DEFINE_BASIC_PRINT_TYPE_FUNC(s8, "%d")
  49. DEFINE_BASIC_PRINT_TYPE_FUNC(s16, "%d")
  50. DEFINE_BASIC_PRINT_TYPE_FUNC(s32, "%d")
  51. DEFINE_BASIC_PRINT_TYPE_FUNC(s64, "%Ld")
  52. /* Print type function for string type */
  53. __kprobes int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s,
  54. const char *name,
  55. void *data, void *ent)
  56. {
  57. int len = *(u32 *)data >> 16;
  58. if (!len)
  59. return trace_seq_printf(s, " %s=(fault)", name);
  60. else
  61. return trace_seq_printf(s, " %s=\"%s\"", name,
  62. (const char *)get_loc_data(data, ent));
  63. }
  64. const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
  65. #define CHECK_FETCH_FUNCS(method, fn) \
  66. (((FETCH_FUNC_NAME(method, u8) == fn) || \
  67. (FETCH_FUNC_NAME(method, u16) == fn) || \
  68. (FETCH_FUNC_NAME(method, u32) == fn) || \
  69. (FETCH_FUNC_NAME(method, u64) == fn) || \
  70. (FETCH_FUNC_NAME(method, string) == fn) || \
  71. (FETCH_FUNC_NAME(method, string_size) == fn)) \
  72. && (fn != NULL))
  73. /* Data fetch function templates */
  74. #define DEFINE_FETCH_reg(type) \
  75. __kprobes void FETCH_FUNC_NAME(reg, type)(struct pt_regs *regs, \
  76. void *offset, void *dest) \
  77. { \
  78. *(type *)dest = (type)regs_get_register(regs, \
  79. (unsigned int)((unsigned long)offset)); \
  80. }
  81. DEFINE_BASIC_FETCH_FUNCS(reg)
  82. /* No string on the register */
  83. #define fetch_reg_string NULL
  84. #define fetch_reg_string_size NULL
  85. #define DEFINE_FETCH_retval(type) \
  86. __kprobes void FETCH_FUNC_NAME(retval, type)(struct pt_regs *regs, \
  87. void *dummy, void *dest) \
  88. { \
  89. *(type *)dest = (type)regs_return_value(regs); \
  90. }
  91. DEFINE_BASIC_FETCH_FUNCS(retval)
  92. /* No string on the retval */
  93. #define fetch_retval_string NULL
  94. #define fetch_retval_string_size NULL
  95. /* Dereference memory access function */
  96. struct deref_fetch_param {
  97. struct fetch_param orig;
  98. long offset;
  99. fetch_func_t fetch;
  100. fetch_func_t fetch_size;
  101. };
  102. #define DEFINE_FETCH_deref(type) \
  103. __kprobes void FETCH_FUNC_NAME(deref, type)(struct pt_regs *regs, \
  104. void *data, void *dest) \
  105. { \
  106. struct deref_fetch_param *dprm = data; \
  107. unsigned long addr; \
  108. call_fetch(&dprm->orig, regs, &addr); \
  109. if (addr) { \
  110. addr += dprm->offset; \
  111. dprm->fetch(regs, (void *)addr, dest); \
  112. } else \
  113. *(type *)dest = 0; \
  114. }
  115. DEFINE_BASIC_FETCH_FUNCS(deref)
  116. DEFINE_FETCH_deref(string)
  117. __kprobes void FETCH_FUNC_NAME(deref, string_size)(struct pt_regs *regs,
  118. void *data, void *dest)
  119. {
  120. struct deref_fetch_param *dprm = data;
  121. unsigned long addr;
  122. call_fetch(&dprm->orig, regs, &addr);
  123. if (addr && dprm->fetch_size) {
  124. addr += dprm->offset;
  125. dprm->fetch_size(regs, (void *)addr, dest);
  126. } else
  127. *(string_size *)dest = 0;
  128. }
  129. static __kprobes void update_deref_fetch_param(struct deref_fetch_param *data)
  130. {
  131. if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
  132. update_deref_fetch_param(data->orig.data);
  133. else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
  134. update_symbol_cache(data->orig.data);
  135. }
  136. static __kprobes void free_deref_fetch_param(struct deref_fetch_param *data)
  137. {
  138. if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
  139. free_deref_fetch_param(data->orig.data);
  140. else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
  141. free_symbol_cache(data->orig.data);
  142. kfree(data);
  143. }
  144. /* Bitfield fetch function */
  145. struct bitfield_fetch_param {
  146. struct fetch_param orig;
  147. unsigned char hi_shift;
  148. unsigned char low_shift;
  149. };
  150. #define DEFINE_FETCH_bitfield(type) \
  151. __kprobes void FETCH_FUNC_NAME(bitfield, type)(struct pt_regs *regs, \
  152. void *data, void *dest) \
  153. { \
  154. struct bitfield_fetch_param *bprm = data; \
  155. type buf = 0; \
  156. call_fetch(&bprm->orig, regs, &buf); \
  157. if (buf) { \
  158. buf <<= bprm->hi_shift; \
  159. buf >>= bprm->low_shift; \
  160. } \
  161. *(type *)dest = buf; \
  162. }
  163. DEFINE_BASIC_FETCH_FUNCS(bitfield)
  164. #define fetch_bitfield_string NULL
  165. #define fetch_bitfield_string_size NULL
  166. static __kprobes void
  167. update_bitfield_fetch_param(struct bitfield_fetch_param *data)
  168. {
  169. /*
  170. * Don't check the bitfield itself, because this must be the
  171. * last fetch function.
  172. */
  173. if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
  174. update_deref_fetch_param(data->orig.data);
  175. else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
  176. update_symbol_cache(data->orig.data);
  177. }
  178. static __kprobes void
  179. free_bitfield_fetch_param(struct bitfield_fetch_param *data)
  180. {
  181. /*
  182. * Don't check the bitfield itself, because this must be the
  183. * last fetch function.
  184. */
  185. if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
  186. free_deref_fetch_param(data->orig.data);
  187. else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
  188. free_symbol_cache(data->orig.data);
  189. kfree(data);
  190. }
  191. static const struct fetch_type *find_fetch_type(const char *type,
  192. const struct fetch_type *ftbl)
  193. {
  194. int i;
  195. if (!type)
  196. type = DEFAULT_FETCH_TYPE_STR;
  197. /* Special case: bitfield */
  198. if (*type == 'b') {
  199. unsigned long bs;
  200. type = strchr(type, '/');
  201. if (!type)
  202. goto fail;
  203. type++;
  204. if (kstrtoul(type, 0, &bs))
  205. goto fail;
  206. switch (bs) {
  207. case 8:
  208. return find_fetch_type("u8", ftbl);
  209. case 16:
  210. return find_fetch_type("u16", ftbl);
  211. case 32:
  212. return find_fetch_type("u32", ftbl);
  213. case 64:
  214. return find_fetch_type("u64", ftbl);
  215. default:
  216. goto fail;
  217. }
  218. }
  219. for (i = 0; ftbl[i].name; i++) {
  220. if (strcmp(type, ftbl[i].name) == 0)
  221. return &ftbl[i];
  222. }
  223. fail:
  224. return NULL;
  225. }
  226. /* Special function : only accept unsigned long */
  227. static __kprobes void fetch_kernel_stack_address(struct pt_regs *regs,
  228. void *dummy, void *dest)
  229. {
  230. *(unsigned long *)dest = kernel_stack_pointer(regs);
  231. }
  232. static __kprobes void fetch_user_stack_address(struct pt_regs *regs,
  233. void *dummy, void *dest)
  234. {
  235. *(unsigned long *)dest = user_stack_pointer(regs);
  236. }
  237. static fetch_func_t get_fetch_size_function(const struct fetch_type *type,
  238. fetch_func_t orig_fn,
  239. const struct fetch_type *ftbl)
  240. {
  241. int i;
  242. if (type != &ftbl[FETCH_TYPE_STRING])
  243. return NULL; /* Only string type needs size function */
  244. for (i = 0; i < FETCH_MTD_END; i++)
  245. if (type->fetch[i] == orig_fn)
  246. return ftbl[FETCH_TYPE_STRSIZE].fetch[i];
  247. WARN_ON(1); /* This should not happen */
  248. return NULL;
  249. }
  250. /* Split symbol and offset. */
  251. int traceprobe_split_symbol_offset(char *symbol, unsigned long *offset)
  252. {
  253. char *tmp;
  254. int ret;
  255. if (!offset)
  256. return -EINVAL;
  257. tmp = strchr(symbol, '+');
  258. if (tmp) {
  259. /* skip sign because kstrtoul doesn't accept '+' */
  260. ret = kstrtoul(tmp + 1, 0, offset);
  261. if (ret)
  262. return ret;
  263. *tmp = '\0';
  264. } else
  265. *offset = 0;
  266. return 0;
  267. }
  268. #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
  269. static int parse_probe_vars(char *arg, const struct fetch_type *t,
  270. struct fetch_param *f, bool is_return,
  271. bool is_kprobe)
  272. {
  273. int ret = 0;
  274. unsigned long param;
  275. if (strcmp(arg, "retval") == 0) {
  276. if (is_return)
  277. f->fn = t->fetch[FETCH_MTD_retval];
  278. else
  279. ret = -EINVAL;
  280. } else if (strncmp(arg, "stack", 5) == 0) {
  281. if (arg[5] == '\0') {
  282. if (strcmp(t->name, DEFAULT_FETCH_TYPE_STR))
  283. return -EINVAL;
  284. if (is_kprobe)
  285. f->fn = fetch_kernel_stack_address;
  286. else
  287. f->fn = fetch_user_stack_address;
  288. } else if (isdigit(arg[5])) {
  289. ret = kstrtoul(arg + 5, 10, &param);
  290. if (ret || (is_kprobe && param > PARAM_MAX_STACK))
  291. ret = -EINVAL;
  292. else {
  293. f->fn = t->fetch[FETCH_MTD_stack];
  294. f->data = (void *)param;
  295. }
  296. } else
  297. ret = -EINVAL;
  298. } else
  299. ret = -EINVAL;
  300. return ret;
  301. }
  302. /* Recursive argument parser */
  303. static int parse_probe_arg(char *arg, const struct fetch_type *t,
  304. struct fetch_param *f, bool is_return, bool is_kprobe)
  305. {
  306. const struct fetch_type *ftbl;
  307. unsigned long param;
  308. long offset;
  309. char *tmp;
  310. int ret = 0;
  311. ftbl = is_kprobe ? kprobes_fetch_type_table : uprobes_fetch_type_table;
  312. BUG_ON(ftbl == NULL);
  313. switch (arg[0]) {
  314. case '$':
  315. ret = parse_probe_vars(arg + 1, t, f, is_return, is_kprobe);
  316. break;
  317. case '%': /* named register */
  318. ret = regs_query_register_offset(arg + 1);
  319. if (ret >= 0) {
  320. f->fn = t->fetch[FETCH_MTD_reg];
  321. f->data = (void *)(unsigned long)ret;
  322. ret = 0;
  323. }
  324. break;
  325. case '@': /* memory, file-offset or symbol */
  326. if (isdigit(arg[1])) {
  327. ret = kstrtoul(arg + 1, 0, &param);
  328. if (ret)
  329. break;
  330. f->fn = t->fetch[FETCH_MTD_memory];
  331. f->data = (void *)param;
  332. } else if (arg[1] == '+') {
  333. /* kprobes don't support file offsets */
  334. if (is_kprobe)
  335. return -EINVAL;
  336. ret = kstrtol(arg + 2, 0, &offset);
  337. if (ret)
  338. break;
  339. f->fn = t->fetch[FETCH_MTD_file_offset];
  340. f->data = (void *)offset;
  341. } else {
  342. /* uprobes don't support symbols */
  343. if (!is_kprobe)
  344. return -EINVAL;
  345. ret = traceprobe_split_symbol_offset(arg + 1, &offset);
  346. if (ret)
  347. break;
  348. f->data = alloc_symbol_cache(arg + 1, offset);
  349. if (f->data)
  350. f->fn = t->fetch[FETCH_MTD_symbol];
  351. }
  352. break;
  353. case '+': /* deref memory */
  354. arg++; /* Skip '+', because kstrtol() rejects it. */
  355. case '-':
  356. tmp = strchr(arg, '(');
  357. if (!tmp)
  358. break;
  359. *tmp = '\0';
  360. ret = kstrtol(arg, 0, &offset);
  361. if (ret)
  362. break;
  363. arg = tmp + 1;
  364. tmp = strrchr(arg, ')');
  365. if (tmp) {
  366. struct deref_fetch_param *dprm;
  367. const struct fetch_type *t2;
  368. t2 = find_fetch_type(NULL, ftbl);
  369. *tmp = '\0';
  370. dprm = kzalloc(sizeof(struct deref_fetch_param), GFP_KERNEL);
  371. if (!dprm)
  372. return -ENOMEM;
  373. dprm->offset = offset;
  374. dprm->fetch = t->fetch[FETCH_MTD_memory];
  375. dprm->fetch_size = get_fetch_size_function(t,
  376. dprm->fetch, ftbl);
  377. ret = parse_probe_arg(arg, t2, &dprm->orig, is_return,
  378. is_kprobe);
  379. if (ret)
  380. kfree(dprm);
  381. else {
  382. f->fn = t->fetch[FETCH_MTD_deref];
  383. f->data = (void *)dprm;
  384. }
  385. }
  386. break;
  387. }
  388. if (!ret && !f->fn) { /* Parsed, but do not find fetch method */
  389. pr_info("%s type has no corresponding fetch method.\n", t->name);
  390. ret = -EINVAL;
  391. }
  392. return ret;
  393. }
  394. #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
  395. /* Bitfield type needs to be parsed into a fetch function */
  396. static int __parse_bitfield_probe_arg(const char *bf,
  397. const struct fetch_type *t,
  398. struct fetch_param *f)
  399. {
  400. struct bitfield_fetch_param *bprm;
  401. unsigned long bw, bo;
  402. char *tail;
  403. if (*bf != 'b')
  404. return 0;
  405. bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
  406. if (!bprm)
  407. return -ENOMEM;
  408. bprm->orig = *f;
  409. f->fn = t->fetch[FETCH_MTD_bitfield];
  410. f->data = (void *)bprm;
  411. bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
  412. if (bw == 0 || *tail != '@')
  413. return -EINVAL;
  414. bf = tail + 1;
  415. bo = simple_strtoul(bf, &tail, 0);
  416. if (tail == bf || *tail != '/')
  417. return -EINVAL;
  418. bprm->hi_shift = BYTES_TO_BITS(t->size) - (bw + bo);
  419. bprm->low_shift = bprm->hi_shift + bo;
  420. return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
  421. }
  422. /* String length checking wrapper */
  423. int traceprobe_parse_probe_arg(char *arg, ssize_t *size,
  424. struct probe_arg *parg, bool is_return, bool is_kprobe)
  425. {
  426. const struct fetch_type *ftbl;
  427. const char *t;
  428. int ret;
  429. ftbl = is_kprobe ? kprobes_fetch_type_table : uprobes_fetch_type_table;
  430. BUG_ON(ftbl == NULL);
  431. if (strlen(arg) > MAX_ARGSTR_LEN) {
  432. pr_info("Argument is too long.: %s\n", arg);
  433. return -ENOSPC;
  434. }
  435. parg->comm = kstrdup(arg, GFP_KERNEL);
  436. if (!parg->comm) {
  437. pr_info("Failed to allocate memory for command '%s'.\n", arg);
  438. return -ENOMEM;
  439. }
  440. t = strchr(parg->comm, ':');
  441. if (t) {
  442. arg[t - parg->comm] = '\0';
  443. t++;
  444. }
  445. parg->type = find_fetch_type(t, ftbl);
  446. if (!parg->type) {
  447. pr_info("Unsupported type: %s\n", t);
  448. return -EINVAL;
  449. }
  450. parg->offset = *size;
  451. *size += parg->type->size;
  452. ret = parse_probe_arg(arg, parg->type, &parg->fetch, is_return, is_kprobe);
  453. if (ret >= 0 && t != NULL)
  454. ret = __parse_bitfield_probe_arg(t, parg->type, &parg->fetch);
  455. if (ret >= 0) {
  456. parg->fetch_size.fn = get_fetch_size_function(parg->type,
  457. parg->fetch.fn,
  458. ftbl);
  459. parg->fetch_size.data = parg->fetch.data;
  460. }
  461. return ret;
  462. }
  463. /* Return 1 if name is reserved or already used by another argument */
  464. int traceprobe_conflict_field_name(const char *name,
  465. struct probe_arg *args, int narg)
  466. {
  467. int i;
  468. for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
  469. if (strcmp(reserved_field_names[i], name) == 0)
  470. return 1;
  471. for (i = 0; i < narg; i++)
  472. if (strcmp(args[i].name, name) == 0)
  473. return 1;
  474. return 0;
  475. }
  476. void traceprobe_update_arg(struct probe_arg *arg)
  477. {
  478. if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn))
  479. update_bitfield_fetch_param(arg->fetch.data);
  480. else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn))
  481. update_deref_fetch_param(arg->fetch.data);
  482. else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn))
  483. update_symbol_cache(arg->fetch.data);
  484. }
  485. void traceprobe_free_probe_arg(struct probe_arg *arg)
  486. {
  487. if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn))
  488. free_bitfield_fetch_param(arg->fetch.data);
  489. else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn))
  490. free_deref_fetch_param(arg->fetch.data);
  491. else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn))
  492. free_symbol_cache(arg->fetch.data);
  493. kfree(arg->name);
  494. kfree(arg->comm);
  495. }
  496. int traceprobe_command(const char *buf, int (*createfn)(int, char **))
  497. {
  498. char **argv;
  499. int argc, ret;
  500. argc = 0;
  501. ret = 0;
  502. argv = argv_split(GFP_KERNEL, buf, &argc);
  503. if (!argv)
  504. return -ENOMEM;
  505. if (argc)
  506. ret = createfn(argc, argv);
  507. argv_free(argv);
  508. return ret;
  509. }
  510. #define WRITE_BUFSIZE 4096
  511. ssize_t traceprobe_probes_write(struct file *file, const char __user *buffer,
  512. size_t count, loff_t *ppos,
  513. int (*createfn)(int, char **))
  514. {
  515. char *kbuf, *tmp;
  516. int ret = 0;
  517. size_t done = 0;
  518. size_t size;
  519. kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
  520. if (!kbuf)
  521. return -ENOMEM;
  522. while (done < count) {
  523. size = count - done;
  524. if (size >= WRITE_BUFSIZE)
  525. size = WRITE_BUFSIZE - 1;
  526. if (copy_from_user(kbuf, buffer + done, size)) {
  527. ret = -EFAULT;
  528. goto out;
  529. }
  530. kbuf[size] = '\0';
  531. tmp = strchr(kbuf, '\n');
  532. if (tmp) {
  533. *tmp = '\0';
  534. size = tmp - kbuf + 1;
  535. } else if (done + size < count) {
  536. pr_warning("Line length is too long: "
  537. "Should be less than %d.", WRITE_BUFSIZE);
  538. ret = -EINVAL;
  539. goto out;
  540. }
  541. done += size;
  542. /* Remove comments */
  543. tmp = strchr(kbuf, '#');
  544. if (tmp)
  545. *tmp = '\0';
  546. ret = traceprobe_command(kbuf, createfn);
  547. if (ret)
  548. goto out;
  549. }
  550. ret = done;
  551. out:
  552. kfree(kbuf);
  553. return ret;
  554. }
  555. static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
  556. bool is_return)
  557. {
  558. int i;
  559. int pos = 0;
  560. const char *fmt, *arg;
  561. if (!is_return) {
  562. fmt = "(%lx)";
  563. arg = "REC->" FIELD_STRING_IP;
  564. } else {
  565. fmt = "(%lx <- %lx)";
  566. arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
  567. }
  568. /* When len=0, we just calculate the needed length */
  569. #define LEN_OR_ZERO (len ? len - pos : 0)
  570. pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
  571. for (i = 0; i < tp->nr_args; i++) {
  572. pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
  573. tp->args[i].name, tp->args[i].type->fmt);
  574. }
  575. pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
  576. for (i = 0; i < tp->nr_args; i++) {
  577. if (strcmp(tp->args[i].type->name, "string") == 0)
  578. pos += snprintf(buf + pos, LEN_OR_ZERO,
  579. ", __get_str(%s)",
  580. tp->args[i].name);
  581. else
  582. pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
  583. tp->args[i].name);
  584. }
  585. #undef LEN_OR_ZERO
  586. /* return the length of print_fmt */
  587. return pos;
  588. }
  589. int set_print_fmt(struct trace_probe *tp, bool is_return)
  590. {
  591. int len;
  592. char *print_fmt;
  593. /* First: called with 0 length to calculate the needed length */
  594. len = __set_print_fmt(tp, NULL, 0, is_return);
  595. print_fmt = kmalloc(len + 1, GFP_KERNEL);
  596. if (!print_fmt)
  597. return -ENOMEM;
  598. /* Second: actually write the @print_fmt */
  599. __set_print_fmt(tp, print_fmt, len + 1, is_return);
  600. tp->call.print_fmt = print_fmt;
  601. return 0;
  602. }