trace_probe.c 19 KB

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