trace_probe.c 17 KB

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