trace_probe.c 18 KB

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