trace_probe.c 19 KB

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