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