trace_probe.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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. return trace_seq_printf(s, " %s=" fmt, name, *(type *)data); \
  41. } \
  42. const char PRINT_TYPE_FMT_NAME(type)[] = fmt; \
  43. NOKPROBE_SYMBOL(PRINT_TYPE_FUNC_NAME(type));
  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. int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, const char *name,
  54. void *data, void *ent)
  55. {
  56. int len = *(u32 *)data >> 16;
  57. if (!len)
  58. return trace_seq_printf(s, " %s=(fault)", name);
  59. else
  60. return trace_seq_printf(s, " %s=\"%s\"", name,
  61. (const char *)get_loc_data(data, ent));
  62. }
  63. NOKPROBE_SYMBOL(PRINT_TYPE_FUNC_NAME(string));
  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. void FETCH_FUNC_NAME(reg, type)(struct pt_regs *regs, void *offset, void *dest) \
  76. { \
  77. *(type *)dest = (type)regs_get_register(regs, \
  78. (unsigned int)((unsigned long)offset)); \
  79. } \
  80. NOKPROBE_SYMBOL(FETCH_FUNC_NAME(reg, type));
  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. 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. NOKPROBE_SYMBOL(FETCH_FUNC_NAME(retval, type));
  92. DEFINE_BASIC_FETCH_FUNCS(retval)
  93. /* No string on the retval */
  94. #define fetch_retval_string NULL
  95. #define fetch_retval_string_size NULL
  96. /* Dereference memory access function */
  97. struct deref_fetch_param {
  98. struct fetch_param orig;
  99. long offset;
  100. fetch_func_t fetch;
  101. fetch_func_t fetch_size;
  102. };
  103. #define DEFINE_FETCH_deref(type) \
  104. void FETCH_FUNC_NAME(deref, type)(struct pt_regs *regs, \
  105. void *data, void *dest) \
  106. { \
  107. struct deref_fetch_param *dprm = data; \
  108. unsigned long addr; \
  109. call_fetch(&dprm->orig, regs, &addr); \
  110. if (addr) { \
  111. addr += dprm->offset; \
  112. dprm->fetch(regs, (void *)addr, dest); \
  113. } else \
  114. *(type *)dest = 0; \
  115. } \
  116. NOKPROBE_SYMBOL(FETCH_FUNC_NAME(deref, type));
  117. DEFINE_BASIC_FETCH_FUNCS(deref)
  118. DEFINE_FETCH_deref(string)
  119. void FETCH_FUNC_NAME(deref, string_size)(struct pt_regs *regs,
  120. void *data, void *dest)
  121. {
  122. struct deref_fetch_param *dprm = data;
  123. unsigned long addr;
  124. call_fetch(&dprm->orig, regs, &addr);
  125. if (addr && dprm->fetch_size) {
  126. addr += dprm->offset;
  127. dprm->fetch_size(regs, (void *)addr, dest);
  128. } else
  129. *(string_size *)dest = 0;
  130. }
  131. NOKPROBE_SYMBOL(FETCH_FUNC_NAME(deref, string_size));
  132. static void update_deref_fetch_param(struct deref_fetch_param *data)
  133. {
  134. if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
  135. update_deref_fetch_param(data->orig.data);
  136. else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
  137. update_symbol_cache(data->orig.data);
  138. }
  139. NOKPROBE_SYMBOL(update_deref_fetch_param);
  140. static void free_deref_fetch_param(struct deref_fetch_param *data)
  141. {
  142. if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
  143. free_deref_fetch_param(data->orig.data);
  144. else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
  145. free_symbol_cache(data->orig.data);
  146. kfree(data);
  147. }
  148. NOKPROBE_SYMBOL(free_deref_fetch_param);
  149. /* Bitfield fetch function */
  150. struct bitfield_fetch_param {
  151. struct fetch_param orig;
  152. unsigned char hi_shift;
  153. unsigned char low_shift;
  154. };
  155. #define DEFINE_FETCH_bitfield(type) \
  156. void FETCH_FUNC_NAME(bitfield, type)(struct pt_regs *regs, \
  157. void *data, void *dest) \
  158. { \
  159. struct bitfield_fetch_param *bprm = data; \
  160. type buf = 0; \
  161. call_fetch(&bprm->orig, regs, &buf); \
  162. if (buf) { \
  163. buf <<= bprm->hi_shift; \
  164. buf >>= bprm->low_shift; \
  165. } \
  166. *(type *)dest = buf; \
  167. } \
  168. NOKPROBE_SYMBOL(FETCH_FUNC_NAME(bitfield, type));
  169. DEFINE_BASIC_FETCH_FUNCS(bitfield)
  170. #define fetch_bitfield_string NULL
  171. #define fetch_bitfield_string_size NULL
  172. static void
  173. update_bitfield_fetch_param(struct bitfield_fetch_param *data)
  174. {
  175. /*
  176. * Don't check the bitfield itself, because this must be the
  177. * last fetch function.
  178. */
  179. if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
  180. update_deref_fetch_param(data->orig.data);
  181. else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
  182. update_symbol_cache(data->orig.data);
  183. }
  184. static void
  185. free_bitfield_fetch_param(struct bitfield_fetch_param *data)
  186. {
  187. /*
  188. * Don't check the bitfield itself, because this must be the
  189. * last fetch function.
  190. */
  191. if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
  192. free_deref_fetch_param(data->orig.data);
  193. else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
  194. free_symbol_cache(data->orig.data);
  195. kfree(data);
  196. }
  197. static const struct fetch_type *find_fetch_type(const char *type,
  198. const struct fetch_type *ftbl)
  199. {
  200. int i;
  201. if (!type)
  202. type = DEFAULT_FETCH_TYPE_STR;
  203. /* Special case: bitfield */
  204. if (*type == 'b') {
  205. unsigned long bs;
  206. type = strchr(type, '/');
  207. if (!type)
  208. goto fail;
  209. type++;
  210. if (kstrtoul(type, 0, &bs))
  211. goto fail;
  212. switch (bs) {
  213. case 8:
  214. return find_fetch_type("u8", ftbl);
  215. case 16:
  216. return find_fetch_type("u16", ftbl);
  217. case 32:
  218. return find_fetch_type("u32", ftbl);
  219. case 64:
  220. return find_fetch_type("u64", ftbl);
  221. default:
  222. goto fail;
  223. }
  224. }
  225. for (i = 0; ftbl[i].name; i++) {
  226. if (strcmp(type, ftbl[i].name) == 0)
  227. return &ftbl[i];
  228. }
  229. fail:
  230. return NULL;
  231. }
  232. /* Special function : only accept unsigned long */
  233. static void fetch_kernel_stack_address(struct pt_regs *regs, void *dummy, void *dest)
  234. {
  235. *(unsigned long *)dest = kernel_stack_pointer(regs);
  236. }
  237. NOKPROBE_SYMBOL(fetch_kernel_stack_address);
  238. static void fetch_user_stack_address(struct pt_regs *regs, void *dummy, void *dest)
  239. {
  240. *(unsigned long *)dest = user_stack_pointer(regs);
  241. }
  242. NOKPROBE_SYMBOL(fetch_user_stack_address);
  243. static fetch_func_t get_fetch_size_function(const struct fetch_type *type,
  244. fetch_func_t orig_fn,
  245. const struct fetch_type *ftbl)
  246. {
  247. int i;
  248. if (type != &ftbl[FETCH_TYPE_STRING])
  249. return NULL; /* Only string type needs size function */
  250. for (i = 0; i < FETCH_MTD_END; i++)
  251. if (type->fetch[i] == orig_fn)
  252. return ftbl[FETCH_TYPE_STRSIZE].fetch[i];
  253. WARN_ON(1); /* This should not happen */
  254. return NULL;
  255. }
  256. /* Split symbol and offset. */
  257. int traceprobe_split_symbol_offset(char *symbol, unsigned long *offset)
  258. {
  259. char *tmp;
  260. int ret;
  261. if (!offset)
  262. return -EINVAL;
  263. tmp = strchr(symbol, '+');
  264. if (tmp) {
  265. /* skip sign because kstrtoul doesn't accept '+' */
  266. ret = kstrtoul(tmp + 1, 0, offset);
  267. if (ret)
  268. return ret;
  269. *tmp = '\0';
  270. } else
  271. *offset = 0;
  272. return 0;
  273. }
  274. #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
  275. static int parse_probe_vars(char *arg, const struct fetch_type *t,
  276. struct fetch_param *f, bool is_return,
  277. bool is_kprobe)
  278. {
  279. int ret = 0;
  280. unsigned long param;
  281. if (strcmp(arg, "retval") == 0) {
  282. if (is_return)
  283. f->fn = t->fetch[FETCH_MTD_retval];
  284. else
  285. ret = -EINVAL;
  286. } else if (strncmp(arg, "stack", 5) == 0) {
  287. if (arg[5] == '\0') {
  288. if (strcmp(t->name, DEFAULT_FETCH_TYPE_STR))
  289. return -EINVAL;
  290. if (is_kprobe)
  291. f->fn = fetch_kernel_stack_address;
  292. else
  293. f->fn = fetch_user_stack_address;
  294. } else if (isdigit(arg[5])) {
  295. ret = kstrtoul(arg + 5, 10, &param);
  296. if (ret || (is_kprobe && param > PARAM_MAX_STACK))
  297. ret = -EINVAL;
  298. else {
  299. f->fn = t->fetch[FETCH_MTD_stack];
  300. f->data = (void *)param;
  301. }
  302. } else
  303. ret = -EINVAL;
  304. } else
  305. ret = -EINVAL;
  306. return ret;
  307. }
  308. /* Recursive argument parser */
  309. static int parse_probe_arg(char *arg, const struct fetch_type *t,
  310. struct fetch_param *f, bool is_return, bool is_kprobe)
  311. {
  312. const struct fetch_type *ftbl;
  313. unsigned long param;
  314. long offset;
  315. char *tmp;
  316. int ret = 0;
  317. ftbl = is_kprobe ? kprobes_fetch_type_table : uprobes_fetch_type_table;
  318. BUG_ON(ftbl == NULL);
  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);
  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. {
  432. const struct fetch_type *ftbl;
  433. const char *t;
  434. int ret;
  435. ftbl = is_kprobe ? kprobes_fetch_type_table : uprobes_fetch_type_table;
  436. BUG_ON(ftbl == NULL);
  437. if (strlen(arg) > MAX_ARGSTR_LEN) {
  438. pr_info("Argument is too long.: %s\n", arg);
  439. return -ENOSPC;
  440. }
  441. parg->comm = kstrdup(arg, GFP_KERNEL);
  442. if (!parg->comm) {
  443. pr_info("Failed to allocate memory for command '%s'.\n", arg);
  444. return -ENOMEM;
  445. }
  446. t = strchr(parg->comm, ':');
  447. if (t) {
  448. arg[t - parg->comm] = '\0';
  449. t++;
  450. }
  451. parg->type = find_fetch_type(t, ftbl);
  452. if (!parg->type) {
  453. pr_info("Unsupported type: %s\n", t);
  454. return -EINVAL;
  455. }
  456. parg->offset = *size;
  457. *size += parg->type->size;
  458. ret = parse_probe_arg(arg, parg->type, &parg->fetch, is_return, is_kprobe);
  459. if (ret >= 0 && t != NULL)
  460. ret = __parse_bitfield_probe_arg(t, parg->type, &parg->fetch);
  461. if (ret >= 0) {
  462. parg->fetch_size.fn = get_fetch_size_function(parg->type,
  463. parg->fetch.fn,
  464. ftbl);
  465. parg->fetch_size.data = parg->fetch.data;
  466. }
  467. return ret;
  468. }
  469. /* Return 1 if name is reserved or already used by another argument */
  470. int traceprobe_conflict_field_name(const char *name,
  471. struct probe_arg *args, int narg)
  472. {
  473. int i;
  474. for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
  475. if (strcmp(reserved_field_names[i], name) == 0)
  476. return 1;
  477. for (i = 0; i < narg; i++)
  478. if (strcmp(args[i].name, name) == 0)
  479. return 1;
  480. return 0;
  481. }
  482. void traceprobe_update_arg(struct probe_arg *arg)
  483. {
  484. if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn))
  485. update_bitfield_fetch_param(arg->fetch.data);
  486. else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn))
  487. update_deref_fetch_param(arg->fetch.data);
  488. else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn))
  489. update_symbol_cache(arg->fetch.data);
  490. }
  491. void traceprobe_free_probe_arg(struct probe_arg *arg)
  492. {
  493. if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn))
  494. free_bitfield_fetch_param(arg->fetch.data);
  495. else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn))
  496. free_deref_fetch_param(arg->fetch.data);
  497. else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn))
  498. free_symbol_cache(arg->fetch.data);
  499. kfree(arg->name);
  500. kfree(arg->comm);
  501. }
  502. int traceprobe_command(const char *buf, int (*createfn)(int, char **))
  503. {
  504. char **argv;
  505. int argc, ret;
  506. argc = 0;
  507. ret = 0;
  508. argv = argv_split(GFP_KERNEL, buf, &argc);
  509. if (!argv)
  510. return -ENOMEM;
  511. if (argc)
  512. ret = createfn(argc, argv);
  513. argv_free(argv);
  514. return ret;
  515. }
  516. #define WRITE_BUFSIZE 4096
  517. ssize_t traceprobe_probes_write(struct file *file, const char __user *buffer,
  518. size_t count, loff_t *ppos,
  519. int (*createfn)(int, char **))
  520. {
  521. char *kbuf, *tmp;
  522. int ret = 0;
  523. size_t done = 0;
  524. size_t size;
  525. kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
  526. if (!kbuf)
  527. return -ENOMEM;
  528. while (done < count) {
  529. size = count - done;
  530. if (size >= WRITE_BUFSIZE)
  531. size = WRITE_BUFSIZE - 1;
  532. if (copy_from_user(kbuf, buffer + done, size)) {
  533. ret = -EFAULT;
  534. goto out;
  535. }
  536. kbuf[size] = '\0';
  537. tmp = strchr(kbuf, '\n');
  538. if (tmp) {
  539. *tmp = '\0';
  540. size = tmp - kbuf + 1;
  541. } else if (done + size < count) {
  542. pr_warning("Line length is too long: "
  543. "Should be less than %d.", WRITE_BUFSIZE);
  544. ret = -EINVAL;
  545. goto out;
  546. }
  547. done += size;
  548. /* Remove comments */
  549. tmp = strchr(kbuf, '#');
  550. if (tmp)
  551. *tmp = '\0';
  552. ret = traceprobe_command(kbuf, createfn);
  553. if (ret)
  554. goto out;
  555. }
  556. ret = done;
  557. out:
  558. kfree(kbuf);
  559. return ret;
  560. }
  561. static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
  562. bool is_return)
  563. {
  564. int i;
  565. int pos = 0;
  566. const char *fmt, *arg;
  567. if (!is_return) {
  568. fmt = "(%lx)";
  569. arg = "REC->" FIELD_STRING_IP;
  570. } else {
  571. fmt = "(%lx <- %lx)";
  572. arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
  573. }
  574. /* When len=0, we just calculate the needed length */
  575. #define LEN_OR_ZERO (len ? len - pos : 0)
  576. pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
  577. for (i = 0; i < tp->nr_args; i++) {
  578. pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
  579. tp->args[i].name, tp->args[i].type->fmt);
  580. }
  581. pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
  582. for (i = 0; i < tp->nr_args; i++) {
  583. if (strcmp(tp->args[i].type->name, "string") == 0)
  584. pos += snprintf(buf + pos, LEN_OR_ZERO,
  585. ", __get_str(%s)",
  586. tp->args[i].name);
  587. else
  588. pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
  589. tp->args[i].name);
  590. }
  591. #undef LEN_OR_ZERO
  592. /* return the length of print_fmt */
  593. return pos;
  594. }
  595. int set_print_fmt(struct trace_probe *tp, bool is_return)
  596. {
  597. int len;
  598. char *print_fmt;
  599. /* First: called with 0 length to calculate the needed length */
  600. len = __set_print_fmt(tp, NULL, 0, is_return);
  601. print_fmt = kmalloc(len + 1, GFP_KERNEL);
  602. if (!print_fmt)
  603. return -ENOMEM;
  604. /* Second: actually write the @print_fmt */
  605. __set_print_fmt(tp, print_fmt, len + 1, is_return);
  606. tp->call.print_fmt = print_fmt;
  607. return 0;
  608. }