trace_probe.c 16 KB

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