trace_probe.c 18 KB

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