trace_probe.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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, unsigned long *offset)
  260. {
  261. char *tmp;
  262. int ret;
  263. if (!offset)
  264. return -EINVAL;
  265. tmp = strchr(symbol, '+');
  266. if (tmp) {
  267. /* skip sign because kstrtoul doesn't accept '+' */
  268. ret = kstrtoul(tmp + 1, 0, offset);
  269. if (ret)
  270. return ret;
  271. *tmp = '\0';
  272. } else
  273. *offset = 0;
  274. return 0;
  275. }
  276. #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
  277. static int parse_probe_vars(char *arg, const struct fetch_type *t,
  278. struct fetch_param *f, bool is_return,
  279. bool is_kprobe)
  280. {
  281. int ret = 0;
  282. unsigned long param;
  283. if (strcmp(arg, "retval") == 0) {
  284. if (is_return)
  285. f->fn = t->fetch[FETCH_MTD_retval];
  286. else
  287. ret = -EINVAL;
  288. } else if (strncmp(arg, "stack", 5) == 0) {
  289. if (arg[5] == '\0') {
  290. if (strcmp(t->name, DEFAULT_FETCH_TYPE_STR))
  291. return -EINVAL;
  292. if (is_kprobe)
  293. f->fn = fetch_kernel_stack_address;
  294. else
  295. f->fn = fetch_user_stack_address;
  296. } else if (isdigit(arg[5])) {
  297. ret = kstrtoul(arg + 5, 10, &param);
  298. if (ret || (is_kprobe && param > PARAM_MAX_STACK))
  299. ret = -EINVAL;
  300. else {
  301. f->fn = t->fetch[FETCH_MTD_stack];
  302. f->data = (void *)param;
  303. }
  304. } else
  305. ret = -EINVAL;
  306. } else
  307. ret = -EINVAL;
  308. return ret;
  309. }
  310. /* Recursive argument parser */
  311. static int parse_probe_arg(char *arg, const struct fetch_type *t,
  312. struct fetch_param *f, bool is_return, bool is_kprobe,
  313. const struct fetch_type *ftbl)
  314. {
  315. unsigned long param;
  316. long offset;
  317. char *tmp;
  318. int ret = 0;
  319. switch (arg[0]) {
  320. case '$':
  321. ret = parse_probe_vars(arg + 1, t, f, is_return, is_kprobe);
  322. break;
  323. case '%': /* named register */
  324. ret = regs_query_register_offset(arg + 1);
  325. if (ret >= 0) {
  326. f->fn = t->fetch[FETCH_MTD_reg];
  327. f->data = (void *)(unsigned long)ret;
  328. ret = 0;
  329. }
  330. break;
  331. case '@': /* memory, file-offset or symbol */
  332. if (isdigit(arg[1])) {
  333. ret = kstrtoul(arg + 1, 0, &param);
  334. if (ret)
  335. break;
  336. f->fn = t->fetch[FETCH_MTD_memory];
  337. f->data = (void *)param;
  338. } else if (arg[1] == '+') {
  339. /* kprobes don't support file offsets */
  340. if (is_kprobe)
  341. return -EINVAL;
  342. ret = kstrtol(arg + 2, 0, &offset);
  343. if (ret)
  344. break;
  345. f->fn = t->fetch[FETCH_MTD_file_offset];
  346. f->data = (void *)offset;
  347. } else {
  348. /* uprobes don't support symbols */
  349. if (!is_kprobe)
  350. return -EINVAL;
  351. ret = traceprobe_split_symbol_offset(arg + 1, &offset);
  352. if (ret)
  353. break;
  354. f->data = alloc_symbol_cache(arg + 1, offset);
  355. if (f->data)
  356. f->fn = t->fetch[FETCH_MTD_symbol];
  357. }
  358. break;
  359. case '+': /* deref memory */
  360. arg++; /* Skip '+', because kstrtol() rejects it. */
  361. case '-':
  362. tmp = strchr(arg, '(');
  363. if (!tmp)
  364. break;
  365. *tmp = '\0';
  366. ret = kstrtol(arg, 0, &offset);
  367. if (ret)
  368. break;
  369. arg = tmp + 1;
  370. tmp = strrchr(arg, ')');
  371. if (tmp) {
  372. struct deref_fetch_param *dprm;
  373. const struct fetch_type *t2;
  374. t2 = find_fetch_type(NULL, ftbl);
  375. *tmp = '\0';
  376. dprm = kzalloc(sizeof(struct deref_fetch_param), GFP_KERNEL);
  377. if (!dprm)
  378. return -ENOMEM;
  379. dprm->offset = offset;
  380. dprm->fetch = t->fetch[FETCH_MTD_memory];
  381. dprm->fetch_size = get_fetch_size_function(t,
  382. dprm->fetch, ftbl);
  383. ret = parse_probe_arg(arg, t2, &dprm->orig, is_return,
  384. is_kprobe, ftbl);
  385. if (ret)
  386. kfree(dprm);
  387. else {
  388. f->fn = t->fetch[FETCH_MTD_deref];
  389. f->data = (void *)dprm;
  390. }
  391. }
  392. break;
  393. }
  394. if (!ret && !f->fn) { /* Parsed, but do not find fetch method */
  395. pr_info("%s type has no corresponding fetch method.\n", t->name);
  396. ret = -EINVAL;
  397. }
  398. return ret;
  399. }
  400. #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
  401. /* Bitfield type needs to be parsed into a fetch function */
  402. static int __parse_bitfield_probe_arg(const char *bf,
  403. const struct fetch_type *t,
  404. struct fetch_param *f)
  405. {
  406. struct bitfield_fetch_param *bprm;
  407. unsigned long bw, bo;
  408. char *tail;
  409. if (*bf != 'b')
  410. return 0;
  411. bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
  412. if (!bprm)
  413. return -ENOMEM;
  414. bprm->orig = *f;
  415. f->fn = t->fetch[FETCH_MTD_bitfield];
  416. f->data = (void *)bprm;
  417. bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
  418. if (bw == 0 || *tail != '@')
  419. return -EINVAL;
  420. bf = tail + 1;
  421. bo = simple_strtoul(bf, &tail, 0);
  422. if (tail == bf || *tail != '/')
  423. return -EINVAL;
  424. bprm->hi_shift = BYTES_TO_BITS(t->size) - (bw + bo);
  425. bprm->low_shift = bprm->hi_shift + bo;
  426. return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
  427. }
  428. /* String length checking wrapper */
  429. int traceprobe_parse_probe_arg(char *arg, ssize_t *size,
  430. struct probe_arg *parg, bool is_return, bool is_kprobe,
  431. const struct fetch_type *ftbl)
  432. {
  433. const char *t;
  434. int ret;
  435. if (strlen(arg) > MAX_ARGSTR_LEN) {
  436. pr_info("Argument is too long.: %s\n", arg);
  437. return -ENOSPC;
  438. }
  439. parg->comm = kstrdup(arg, GFP_KERNEL);
  440. if (!parg->comm) {
  441. pr_info("Failed to allocate memory for command '%s'.\n", arg);
  442. return -ENOMEM;
  443. }
  444. t = strchr(parg->comm, ':');
  445. if (t) {
  446. arg[t - parg->comm] = '\0';
  447. t++;
  448. }
  449. parg->type = find_fetch_type(t, ftbl);
  450. if (!parg->type) {
  451. pr_info("Unsupported type: %s\n", t);
  452. return -EINVAL;
  453. }
  454. parg->offset = *size;
  455. *size += parg->type->size;
  456. ret = parse_probe_arg(arg, parg->type, &parg->fetch, is_return,
  457. is_kprobe, ftbl);
  458. if (ret >= 0 && t != NULL)
  459. ret = __parse_bitfield_probe_arg(t, parg->type, &parg->fetch);
  460. if (ret >= 0) {
  461. parg->fetch_size.fn = get_fetch_size_function(parg->type,
  462. parg->fetch.fn,
  463. ftbl);
  464. parg->fetch_size.data = parg->fetch.data;
  465. }
  466. return ret;
  467. }
  468. /* Return 1 if name is reserved or already used by another argument */
  469. int traceprobe_conflict_field_name(const char *name,
  470. struct probe_arg *args, int narg)
  471. {
  472. int i;
  473. for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
  474. if (strcmp(reserved_field_names[i], name) == 0)
  475. return 1;
  476. for (i = 0; i < narg; i++)
  477. if (strcmp(args[i].name, name) == 0)
  478. return 1;
  479. return 0;
  480. }
  481. void traceprobe_update_arg(struct probe_arg *arg)
  482. {
  483. if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn))
  484. update_bitfield_fetch_param(arg->fetch.data);
  485. else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn))
  486. update_deref_fetch_param(arg->fetch.data);
  487. else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn))
  488. update_symbol_cache(arg->fetch.data);
  489. }
  490. void traceprobe_free_probe_arg(struct probe_arg *arg)
  491. {
  492. if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn))
  493. free_bitfield_fetch_param(arg->fetch.data);
  494. else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn))
  495. free_deref_fetch_param(arg->fetch.data);
  496. else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn))
  497. free_symbol_cache(arg->fetch.data);
  498. kfree(arg->name);
  499. kfree(arg->comm);
  500. }
  501. int traceprobe_command(const char *buf, int (*createfn)(int, char **))
  502. {
  503. char **argv;
  504. int argc, ret;
  505. argc = 0;
  506. ret = 0;
  507. argv = argv_split(GFP_KERNEL, buf, &argc);
  508. if (!argv)
  509. return -ENOMEM;
  510. if (argc)
  511. ret = createfn(argc, argv);
  512. argv_free(argv);
  513. return ret;
  514. }
  515. #define WRITE_BUFSIZE 4096
  516. ssize_t traceprobe_probes_write(struct file *file, const char __user *buffer,
  517. size_t count, loff_t *ppos,
  518. int (*createfn)(int, char **))
  519. {
  520. char *kbuf, *tmp;
  521. int ret = 0;
  522. size_t done = 0;
  523. size_t size;
  524. kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
  525. if (!kbuf)
  526. return -ENOMEM;
  527. while (done < count) {
  528. size = count - done;
  529. if (size >= WRITE_BUFSIZE)
  530. size = WRITE_BUFSIZE - 1;
  531. if (copy_from_user(kbuf, buffer + done, size)) {
  532. ret = -EFAULT;
  533. goto out;
  534. }
  535. kbuf[size] = '\0';
  536. tmp = strchr(kbuf, '\n');
  537. if (tmp) {
  538. *tmp = '\0';
  539. size = tmp - kbuf + 1;
  540. } else if (done + size < count) {
  541. pr_warning("Line length is too long: "
  542. "Should be less than %d.", WRITE_BUFSIZE);
  543. ret = -EINVAL;
  544. goto out;
  545. }
  546. done += size;
  547. /* Remove comments */
  548. tmp = strchr(kbuf, '#');
  549. if (tmp)
  550. *tmp = '\0';
  551. ret = traceprobe_command(kbuf, createfn);
  552. if (ret)
  553. goto out;
  554. }
  555. ret = done;
  556. out:
  557. kfree(kbuf);
  558. return ret;
  559. }
  560. static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
  561. bool is_return)
  562. {
  563. int i;
  564. int pos = 0;
  565. const char *fmt, *arg;
  566. if (!is_return) {
  567. fmt = "(%lx)";
  568. arg = "REC->" FIELD_STRING_IP;
  569. } else {
  570. fmt = "(%lx <- %lx)";
  571. arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
  572. }
  573. /* When len=0, we just calculate the needed length */
  574. #define LEN_OR_ZERO (len ? len - pos : 0)
  575. pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
  576. for (i = 0; i < tp->nr_args; i++) {
  577. pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
  578. tp->args[i].name, tp->args[i].type->fmt);
  579. }
  580. pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
  581. for (i = 0; i < tp->nr_args; i++) {
  582. if (strcmp(tp->args[i].type->name, "string") == 0)
  583. pos += snprintf(buf + pos, LEN_OR_ZERO,
  584. ", __get_str(%s)",
  585. tp->args[i].name);
  586. else
  587. pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
  588. tp->args[i].name);
  589. }
  590. #undef LEN_OR_ZERO
  591. /* return the length of print_fmt */
  592. return pos;
  593. }
  594. int set_print_fmt(struct trace_probe *tp, bool is_return)
  595. {
  596. int len;
  597. char *print_fmt;
  598. /* First: called with 0 length to calculate the needed length */
  599. len = __set_print_fmt(tp, NULL, 0, is_return);
  600. print_fmt = kmalloc(len + 1, GFP_KERNEL);
  601. if (!print_fmt)
  602. return -ENOMEM;
  603. /* Second: actually write the @print_fmt */
  604. __set_print_fmt(tp, print_fmt, len + 1, is_return);
  605. tp->call.print_fmt = print_fmt;
  606. return 0;
  607. }