trace_probe.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  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 function type */
  36. #define PRINT_TYPE_FUNC_NAME(type) print_type_##type
  37. #define PRINT_TYPE_FMT_NAME(type) print_type_format_##type
  38. /* Printing in basic type function template */
  39. #define DEFINE_BASIC_PRINT_TYPE_FUNC(type, fmt, cast) \
  40. static __kprobes int PRINT_TYPE_FUNC_NAME(type)(struct trace_seq *s, \
  41. const char *name, \
  42. void *data, void *ent)\
  43. { \
  44. return trace_seq_printf(s, " %s=" fmt, name, (cast)*(type *)data);\
  45. } \
  46. static const char PRINT_TYPE_FMT_NAME(type)[] = fmt;
  47. DEFINE_BASIC_PRINT_TYPE_FUNC(u8, "%x", unsigned int)
  48. DEFINE_BASIC_PRINT_TYPE_FUNC(u16, "%x", unsigned int)
  49. DEFINE_BASIC_PRINT_TYPE_FUNC(u32, "%lx", unsigned long)
  50. DEFINE_BASIC_PRINT_TYPE_FUNC(u64, "%llx", unsigned long long)
  51. DEFINE_BASIC_PRINT_TYPE_FUNC(s8, "%d", int)
  52. DEFINE_BASIC_PRINT_TYPE_FUNC(s16, "%d", int)
  53. DEFINE_BASIC_PRINT_TYPE_FUNC(s32, "%ld", long)
  54. DEFINE_BASIC_PRINT_TYPE_FUNC(s64, "%lld", long long)
  55. static inline void *get_rloc_data(u32 *dl)
  56. {
  57. return (u8 *)dl + get_rloc_offs(*dl);
  58. }
  59. /* For data_loc conversion */
  60. static inline void *get_loc_data(u32 *dl, void *ent)
  61. {
  62. return (u8 *)ent + get_rloc_offs(*dl);
  63. }
  64. /* For defining macros, define string/string_size types */
  65. typedef u32 string;
  66. typedef u32 string_size;
  67. /* Print type function for string type */
  68. static __kprobes int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s,
  69. const char *name,
  70. void *data, void *ent)
  71. {
  72. int len = *(u32 *)data >> 16;
  73. if (!len)
  74. return trace_seq_printf(s, " %s=(fault)", name);
  75. else
  76. return trace_seq_printf(s, " %s=\"%s\"", name,
  77. (const char *)get_loc_data(data, ent));
  78. }
  79. static const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
  80. #define FETCH_FUNC_NAME(method, type) fetch_##method##_##type
  81. /*
  82. * Define macro for basic types - we don't need to define s* types, because
  83. * we have to care only about bitwidth at recording time.
  84. */
  85. #define DEFINE_BASIC_FETCH_FUNCS(method) \
  86. DEFINE_FETCH_##method(u8) \
  87. DEFINE_FETCH_##method(u16) \
  88. DEFINE_FETCH_##method(u32) \
  89. DEFINE_FETCH_##method(u64)
  90. #define CHECK_FETCH_FUNCS(method, fn) \
  91. (((FETCH_FUNC_NAME(method, u8) == fn) || \
  92. (FETCH_FUNC_NAME(method, u16) == fn) || \
  93. (FETCH_FUNC_NAME(method, u32) == fn) || \
  94. (FETCH_FUNC_NAME(method, u64) == fn) || \
  95. (FETCH_FUNC_NAME(method, string) == fn) || \
  96. (FETCH_FUNC_NAME(method, string_size) == fn)) \
  97. && (fn != NULL))
  98. /* Data fetch function templates */
  99. #define DEFINE_FETCH_reg(type) \
  100. static __kprobes void FETCH_FUNC_NAME(reg, type)(struct pt_regs *regs, \
  101. void *offset, void *dest) \
  102. { \
  103. *(type *)dest = (type)regs_get_register(regs, \
  104. (unsigned int)((unsigned long)offset)); \
  105. }
  106. DEFINE_BASIC_FETCH_FUNCS(reg)
  107. /* No string on the register */
  108. #define fetch_reg_string NULL
  109. #define fetch_reg_string_size NULL
  110. #define DEFINE_FETCH_stack(type) \
  111. static __kprobes void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs,\
  112. void *offset, void *dest) \
  113. { \
  114. *(type *)dest = (type)regs_get_kernel_stack_nth(regs, \
  115. (unsigned int)((unsigned long)offset)); \
  116. }
  117. DEFINE_BASIC_FETCH_FUNCS(stack)
  118. /* No string on the stack entry */
  119. #define fetch_stack_string NULL
  120. #define fetch_stack_string_size NULL
  121. #define DEFINE_FETCH_retval(type) \
  122. static __kprobes void FETCH_FUNC_NAME(retval, type)(struct pt_regs *regs,\
  123. void *dummy, void *dest) \
  124. { \
  125. *(type *)dest = (type)regs_return_value(regs); \
  126. }
  127. DEFINE_BASIC_FETCH_FUNCS(retval)
  128. /* No string on the retval */
  129. #define fetch_retval_string NULL
  130. #define fetch_retval_string_size NULL
  131. #define DEFINE_FETCH_memory(type) \
  132. static __kprobes void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs,\
  133. void *addr, void *dest) \
  134. { \
  135. type retval; \
  136. if (probe_kernel_address(addr, retval)) \
  137. *(type *)dest = 0; \
  138. else \
  139. *(type *)dest = retval; \
  140. }
  141. DEFINE_BASIC_FETCH_FUNCS(memory)
  142. /*
  143. * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
  144. * length and relative data location.
  145. */
  146. static __kprobes void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs,
  147. void *addr, void *dest)
  148. {
  149. long ret;
  150. int maxlen = get_rloc_len(*(u32 *)dest);
  151. u8 *dst = get_rloc_data(dest);
  152. u8 *src = addr;
  153. mm_segment_t old_fs = get_fs();
  154. if (!maxlen)
  155. return;
  156. /*
  157. * Try to get string again, since the string can be changed while
  158. * probing.
  159. */
  160. set_fs(KERNEL_DS);
  161. pagefault_disable();
  162. do
  163. ret = __copy_from_user_inatomic(dst++, src++, 1);
  164. while (dst[-1] && ret == 0 && src - (u8 *)addr < maxlen);
  165. dst[-1] = '\0';
  166. pagefault_enable();
  167. set_fs(old_fs);
  168. if (ret < 0) { /* Failed to fetch string */
  169. ((u8 *)get_rloc_data(dest))[0] = '\0';
  170. *(u32 *)dest = make_data_rloc(0, get_rloc_offs(*(u32 *)dest));
  171. } else {
  172. *(u32 *)dest = make_data_rloc(src - (u8 *)addr,
  173. get_rloc_offs(*(u32 *)dest));
  174. }
  175. }
  176. /* Return the length of string -- including null terminal byte */
  177. static __kprobes void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs,
  178. void *addr, void *dest)
  179. {
  180. mm_segment_t old_fs;
  181. int ret, len = 0;
  182. u8 c;
  183. old_fs = get_fs();
  184. set_fs(KERNEL_DS);
  185. pagefault_disable();
  186. do {
  187. ret = __copy_from_user_inatomic(&c, (u8 *)addr + len, 1);
  188. len++;
  189. } while (c && ret == 0 && len < MAX_STRING_SIZE);
  190. pagefault_enable();
  191. set_fs(old_fs);
  192. if (ret < 0) /* Failed to check the length */
  193. *(u32 *)dest = 0;
  194. else
  195. *(u32 *)dest = len;
  196. }
  197. /* Memory fetching by symbol */
  198. struct symbol_cache {
  199. char *symbol;
  200. long offset;
  201. unsigned long addr;
  202. };
  203. static unsigned long update_symbol_cache(struct symbol_cache *sc)
  204. {
  205. sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
  206. if (sc->addr)
  207. sc->addr += sc->offset;
  208. return sc->addr;
  209. }
  210. static void free_symbol_cache(struct symbol_cache *sc)
  211. {
  212. kfree(sc->symbol);
  213. kfree(sc);
  214. }
  215. static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
  216. {
  217. struct symbol_cache *sc;
  218. if (!sym || strlen(sym) == 0)
  219. return NULL;
  220. sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
  221. if (!sc)
  222. return NULL;
  223. sc->symbol = kstrdup(sym, GFP_KERNEL);
  224. if (!sc->symbol) {
  225. kfree(sc);
  226. return NULL;
  227. }
  228. sc->offset = offset;
  229. update_symbol_cache(sc);
  230. return sc;
  231. }
  232. #define DEFINE_FETCH_symbol(type) \
  233. static __kprobes void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs,\
  234. void *data, void *dest) \
  235. { \
  236. struct symbol_cache *sc = data; \
  237. if (sc->addr) \
  238. fetch_memory_##type(regs, (void *)sc->addr, dest); \
  239. else \
  240. *(type *)dest = 0; \
  241. }
  242. DEFINE_BASIC_FETCH_FUNCS(symbol)
  243. DEFINE_FETCH_symbol(string)
  244. DEFINE_FETCH_symbol(string_size)
  245. /* Dereference memory access function */
  246. struct deref_fetch_param {
  247. struct fetch_param orig;
  248. long offset;
  249. };
  250. #define DEFINE_FETCH_deref(type) \
  251. static __kprobes void FETCH_FUNC_NAME(deref, type)(struct pt_regs *regs,\
  252. void *data, void *dest) \
  253. { \
  254. struct deref_fetch_param *dprm = data; \
  255. unsigned long addr; \
  256. call_fetch(&dprm->orig, regs, &addr); \
  257. if (addr) { \
  258. addr += dprm->offset; \
  259. fetch_memory_##type(regs, (void *)addr, dest); \
  260. } else \
  261. *(type *)dest = 0; \
  262. }
  263. DEFINE_BASIC_FETCH_FUNCS(deref)
  264. DEFINE_FETCH_deref(string)
  265. DEFINE_FETCH_deref(string_size)
  266. static __kprobes void update_deref_fetch_param(struct deref_fetch_param *data)
  267. {
  268. if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
  269. update_deref_fetch_param(data->orig.data);
  270. else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
  271. update_symbol_cache(data->orig.data);
  272. }
  273. static __kprobes void free_deref_fetch_param(struct deref_fetch_param *data)
  274. {
  275. if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
  276. free_deref_fetch_param(data->orig.data);
  277. else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
  278. free_symbol_cache(data->orig.data);
  279. kfree(data);
  280. }
  281. /* Bitfield fetch function */
  282. struct bitfield_fetch_param {
  283. struct fetch_param orig;
  284. unsigned char hi_shift;
  285. unsigned char low_shift;
  286. };
  287. #define DEFINE_FETCH_bitfield(type) \
  288. static __kprobes void FETCH_FUNC_NAME(bitfield, type)(struct pt_regs *regs,\
  289. void *data, void *dest) \
  290. { \
  291. struct bitfield_fetch_param *bprm = data; \
  292. type buf = 0; \
  293. call_fetch(&bprm->orig, regs, &buf); \
  294. if (buf) { \
  295. buf <<= bprm->hi_shift; \
  296. buf >>= bprm->low_shift; \
  297. } \
  298. *(type *)dest = buf; \
  299. }
  300. DEFINE_BASIC_FETCH_FUNCS(bitfield)
  301. #define fetch_bitfield_string NULL
  302. #define fetch_bitfield_string_size NULL
  303. static __kprobes void
  304. update_bitfield_fetch_param(struct bitfield_fetch_param *data)
  305. {
  306. /*
  307. * Don't check the bitfield itself, because this must be the
  308. * last fetch function.
  309. */
  310. if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
  311. update_deref_fetch_param(data->orig.data);
  312. else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
  313. update_symbol_cache(data->orig.data);
  314. }
  315. static __kprobes void
  316. free_bitfield_fetch_param(struct bitfield_fetch_param *data)
  317. {
  318. /*
  319. * Don't check the bitfield itself, because this must be the
  320. * last fetch function.
  321. */
  322. if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
  323. free_deref_fetch_param(data->orig.data);
  324. else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
  325. free_symbol_cache(data->orig.data);
  326. kfree(data);
  327. }
  328. /* Default (unsigned long) fetch type */
  329. #define __DEFAULT_FETCH_TYPE(t) u##t
  330. #define _DEFAULT_FETCH_TYPE(t) __DEFAULT_FETCH_TYPE(t)
  331. #define DEFAULT_FETCH_TYPE _DEFAULT_FETCH_TYPE(BITS_PER_LONG)
  332. #define DEFAULT_FETCH_TYPE_STR __stringify(DEFAULT_FETCH_TYPE)
  333. #define ASSIGN_FETCH_FUNC(method, type) \
  334. [FETCH_MTD_##method] = FETCH_FUNC_NAME(method, type)
  335. #define __ASSIGN_FETCH_TYPE(_name, ptype, ftype, _size, sign, _fmttype) \
  336. {.name = _name, \
  337. .size = _size, \
  338. .is_signed = sign, \
  339. .print = PRINT_TYPE_FUNC_NAME(ptype), \
  340. .fmt = PRINT_TYPE_FMT_NAME(ptype), \
  341. .fmttype = _fmttype, \
  342. .fetch = { \
  343. ASSIGN_FETCH_FUNC(reg, ftype), \
  344. ASSIGN_FETCH_FUNC(stack, ftype), \
  345. ASSIGN_FETCH_FUNC(retval, ftype), \
  346. ASSIGN_FETCH_FUNC(memory, ftype), \
  347. ASSIGN_FETCH_FUNC(symbol, ftype), \
  348. ASSIGN_FETCH_FUNC(deref, ftype), \
  349. ASSIGN_FETCH_FUNC(bitfield, ftype), \
  350. } \
  351. }
  352. #define ASSIGN_FETCH_TYPE(ptype, ftype, sign) \
  353. __ASSIGN_FETCH_TYPE(#ptype, ptype, ftype, sizeof(ftype), sign, #ptype)
  354. #define FETCH_TYPE_STRING 0
  355. #define FETCH_TYPE_STRSIZE 1
  356. /* Fetch type information table */
  357. static const struct fetch_type fetch_type_table[] = {
  358. /* Special types */
  359. [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string,
  360. sizeof(u32), 1, "__data_loc char[]"),
  361. [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32,
  362. string_size, sizeof(u32), 0, "u32"),
  363. /* Basic types */
  364. ASSIGN_FETCH_TYPE(u8, u8, 0),
  365. ASSIGN_FETCH_TYPE(u16, u16, 0),
  366. ASSIGN_FETCH_TYPE(u32, u32, 0),
  367. ASSIGN_FETCH_TYPE(u64, u64, 0),
  368. ASSIGN_FETCH_TYPE(s8, u8, 1),
  369. ASSIGN_FETCH_TYPE(s16, u16, 1),
  370. ASSIGN_FETCH_TYPE(s32, u32, 1),
  371. ASSIGN_FETCH_TYPE(s64, u64, 1),
  372. };
  373. static const struct fetch_type *find_fetch_type(const char *type)
  374. {
  375. int i;
  376. if (!type)
  377. type = DEFAULT_FETCH_TYPE_STR;
  378. /* Special case: bitfield */
  379. if (*type == 'b') {
  380. unsigned long bs;
  381. type = strchr(type, '/');
  382. if (!type)
  383. goto fail;
  384. type++;
  385. if (strict_strtoul(type, 0, &bs))
  386. goto fail;
  387. switch (bs) {
  388. case 8:
  389. return find_fetch_type("u8");
  390. case 16:
  391. return find_fetch_type("u16");
  392. case 32:
  393. return find_fetch_type("u32");
  394. case 64:
  395. return find_fetch_type("u64");
  396. default:
  397. goto fail;
  398. }
  399. }
  400. for (i = 0; i < ARRAY_SIZE(fetch_type_table); i++)
  401. if (strcmp(type, fetch_type_table[i].name) == 0)
  402. return &fetch_type_table[i];
  403. fail:
  404. return NULL;
  405. }
  406. /* Special function : only accept unsigned long */
  407. static __kprobes void fetch_stack_address(struct pt_regs *regs,
  408. void *dummy, void *dest)
  409. {
  410. *(unsigned long *)dest = kernel_stack_pointer(regs);
  411. }
  412. static fetch_func_t get_fetch_size_function(const struct fetch_type *type,
  413. fetch_func_t orig_fn)
  414. {
  415. int i;
  416. if (type != &fetch_type_table[FETCH_TYPE_STRING])
  417. return NULL; /* Only string type needs size function */
  418. for (i = 0; i < FETCH_MTD_END; i++)
  419. if (type->fetch[i] == orig_fn)
  420. return fetch_type_table[FETCH_TYPE_STRSIZE].fetch[i];
  421. WARN_ON(1); /* This should not happen */
  422. return NULL;
  423. }
  424. /* Split symbol and offset. */
  425. int traceprobe_split_symbol_offset(char *symbol, unsigned long *offset)
  426. {
  427. char *tmp;
  428. int ret;
  429. if (!offset)
  430. return -EINVAL;
  431. tmp = strchr(symbol, '+');
  432. if (tmp) {
  433. /* skip sign because strict_strtol doesn't accept '+' */
  434. ret = strict_strtoul(tmp + 1, 0, offset);
  435. if (ret)
  436. return ret;
  437. *tmp = '\0';
  438. } else
  439. *offset = 0;
  440. return 0;
  441. }
  442. #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
  443. static int parse_probe_vars(char *arg, const struct fetch_type *t,
  444. struct fetch_param *f, bool is_return)
  445. {
  446. int ret = 0;
  447. unsigned long param;
  448. if (strcmp(arg, "retval") == 0) {
  449. if (is_return)
  450. f->fn = t->fetch[FETCH_MTD_retval];
  451. else
  452. ret = -EINVAL;
  453. } else if (strncmp(arg, "stack", 5) == 0) {
  454. if (arg[5] == '\0') {
  455. if (strcmp(t->name, DEFAULT_FETCH_TYPE_STR) == 0)
  456. f->fn = fetch_stack_address;
  457. else
  458. ret = -EINVAL;
  459. } else if (isdigit(arg[5])) {
  460. ret = strict_strtoul(arg + 5, 10, &param);
  461. if (ret || param > PARAM_MAX_STACK)
  462. ret = -EINVAL;
  463. else {
  464. f->fn = t->fetch[FETCH_MTD_stack];
  465. f->data = (void *)param;
  466. }
  467. } else
  468. ret = -EINVAL;
  469. } else
  470. ret = -EINVAL;
  471. return ret;
  472. }
  473. /* Recursive argument parser */
  474. static int parse_probe_arg(char *arg, const struct fetch_type *t,
  475. struct fetch_param *f, bool is_return, bool is_kprobe)
  476. {
  477. unsigned long param;
  478. long offset;
  479. char *tmp;
  480. int ret;
  481. ret = 0;
  482. /* Until uprobe_events supports only reg arguments */
  483. if (!is_kprobe && arg[0] != '%')
  484. return -EINVAL;
  485. switch (arg[0]) {
  486. case '$':
  487. ret = parse_probe_vars(arg + 1, t, f, is_return);
  488. break;
  489. case '%': /* named register */
  490. ret = regs_query_register_offset(arg + 1);
  491. if (ret >= 0) {
  492. f->fn = t->fetch[FETCH_MTD_reg];
  493. f->data = (void *)(unsigned long)ret;
  494. ret = 0;
  495. }
  496. break;
  497. case '@': /* memory or symbol */
  498. if (isdigit(arg[1])) {
  499. ret = strict_strtoul(arg + 1, 0, &param);
  500. if (ret)
  501. break;
  502. f->fn = t->fetch[FETCH_MTD_memory];
  503. f->data = (void *)param;
  504. } else {
  505. ret = traceprobe_split_symbol_offset(arg + 1, &offset);
  506. if (ret)
  507. break;
  508. f->data = alloc_symbol_cache(arg + 1, offset);
  509. if (f->data)
  510. f->fn = t->fetch[FETCH_MTD_symbol];
  511. }
  512. break;
  513. case '+': /* deref memory */
  514. arg++; /* Skip '+', because strict_strtol() rejects it. */
  515. case '-':
  516. tmp = strchr(arg, '(');
  517. if (!tmp)
  518. break;
  519. *tmp = '\0';
  520. ret = strict_strtol(arg, 0, &offset);
  521. if (ret)
  522. break;
  523. arg = tmp + 1;
  524. tmp = strrchr(arg, ')');
  525. if (tmp) {
  526. struct deref_fetch_param *dprm;
  527. const struct fetch_type *t2;
  528. t2 = find_fetch_type(NULL);
  529. *tmp = '\0';
  530. dprm = kzalloc(sizeof(struct deref_fetch_param), GFP_KERNEL);
  531. if (!dprm)
  532. return -ENOMEM;
  533. dprm->offset = offset;
  534. ret = parse_probe_arg(arg, t2, &dprm->orig, is_return,
  535. is_kprobe);
  536. if (ret)
  537. kfree(dprm);
  538. else {
  539. f->fn = t->fetch[FETCH_MTD_deref];
  540. f->data = (void *)dprm;
  541. }
  542. }
  543. break;
  544. }
  545. if (!ret && !f->fn) { /* Parsed, but do not find fetch method */
  546. pr_info("%s type has no corresponding fetch method.\n", t->name);
  547. ret = -EINVAL;
  548. }
  549. return ret;
  550. }
  551. #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
  552. /* Bitfield type needs to be parsed into a fetch function */
  553. static int __parse_bitfield_probe_arg(const char *bf,
  554. const struct fetch_type *t,
  555. struct fetch_param *f)
  556. {
  557. struct bitfield_fetch_param *bprm;
  558. unsigned long bw, bo;
  559. char *tail;
  560. if (*bf != 'b')
  561. return 0;
  562. bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
  563. if (!bprm)
  564. return -ENOMEM;
  565. bprm->orig = *f;
  566. f->fn = t->fetch[FETCH_MTD_bitfield];
  567. f->data = (void *)bprm;
  568. bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
  569. if (bw == 0 || *tail != '@')
  570. return -EINVAL;
  571. bf = tail + 1;
  572. bo = simple_strtoul(bf, &tail, 0);
  573. if (tail == bf || *tail != '/')
  574. return -EINVAL;
  575. bprm->hi_shift = BYTES_TO_BITS(t->size) - (bw + bo);
  576. bprm->low_shift = bprm->hi_shift + bo;
  577. return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
  578. }
  579. /* String length checking wrapper */
  580. int traceprobe_parse_probe_arg(char *arg, ssize_t *size,
  581. struct probe_arg *parg, bool is_return, bool is_kprobe)
  582. {
  583. const char *t;
  584. int ret;
  585. if (strlen(arg) > MAX_ARGSTR_LEN) {
  586. pr_info("Argument is too long.: %s\n", arg);
  587. return -ENOSPC;
  588. }
  589. parg->comm = kstrdup(arg, GFP_KERNEL);
  590. if (!parg->comm) {
  591. pr_info("Failed to allocate memory for command '%s'.\n", arg);
  592. return -ENOMEM;
  593. }
  594. t = strchr(parg->comm, ':');
  595. if (t) {
  596. arg[t - parg->comm] = '\0';
  597. t++;
  598. }
  599. parg->type = find_fetch_type(t);
  600. if (!parg->type) {
  601. pr_info("Unsupported type: %s\n", t);
  602. return -EINVAL;
  603. }
  604. parg->offset = *size;
  605. *size += parg->type->size;
  606. ret = parse_probe_arg(arg, parg->type, &parg->fetch, is_return, is_kprobe);
  607. if (ret >= 0 && t != NULL)
  608. ret = __parse_bitfield_probe_arg(t, parg->type, &parg->fetch);
  609. if (ret >= 0) {
  610. parg->fetch_size.fn = get_fetch_size_function(parg->type,
  611. parg->fetch.fn);
  612. parg->fetch_size.data = parg->fetch.data;
  613. }
  614. return ret;
  615. }
  616. /* Return 1 if name is reserved or already used by another argument */
  617. int traceprobe_conflict_field_name(const char *name,
  618. struct probe_arg *args, int narg)
  619. {
  620. int i;
  621. for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
  622. if (strcmp(reserved_field_names[i], name) == 0)
  623. return 1;
  624. for (i = 0; i < narg; i++)
  625. if (strcmp(args[i].name, name) == 0)
  626. return 1;
  627. return 0;
  628. }
  629. void traceprobe_update_arg(struct probe_arg *arg)
  630. {
  631. if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn))
  632. update_bitfield_fetch_param(arg->fetch.data);
  633. else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn))
  634. update_deref_fetch_param(arg->fetch.data);
  635. else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn))
  636. update_symbol_cache(arg->fetch.data);
  637. }
  638. void traceprobe_free_probe_arg(struct probe_arg *arg)
  639. {
  640. if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn))
  641. free_bitfield_fetch_param(arg->fetch.data);
  642. else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn))
  643. free_deref_fetch_param(arg->fetch.data);
  644. else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn))
  645. free_symbol_cache(arg->fetch.data);
  646. kfree(arg->name);
  647. kfree(arg->comm);
  648. }
  649. int traceprobe_command(const char *buf, int (*createfn)(int, char **))
  650. {
  651. char **argv;
  652. int argc, ret;
  653. argc = 0;
  654. ret = 0;
  655. argv = argv_split(GFP_KERNEL, buf, &argc);
  656. if (!argv)
  657. return -ENOMEM;
  658. if (argc)
  659. ret = createfn(argc, argv);
  660. argv_free(argv);
  661. return ret;
  662. }
  663. #define WRITE_BUFSIZE 4096
  664. ssize_t traceprobe_probes_write(struct file *file, const char __user *buffer,
  665. size_t count, loff_t *ppos,
  666. int (*createfn)(int, char **))
  667. {
  668. char *kbuf, *tmp;
  669. int ret = 0;
  670. size_t done = 0;
  671. size_t size;
  672. kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
  673. if (!kbuf)
  674. return -ENOMEM;
  675. while (done < count) {
  676. size = count - done;
  677. if (size >= WRITE_BUFSIZE)
  678. size = WRITE_BUFSIZE - 1;
  679. if (copy_from_user(kbuf, buffer + done, size)) {
  680. ret = -EFAULT;
  681. goto out;
  682. }
  683. kbuf[size] = '\0';
  684. tmp = strchr(kbuf, '\n');
  685. if (tmp) {
  686. *tmp = '\0';
  687. size = tmp - kbuf + 1;
  688. } else if (done + size < count) {
  689. pr_warning("Line length is too long: "
  690. "Should be less than %d.", WRITE_BUFSIZE);
  691. ret = -EINVAL;
  692. goto out;
  693. }
  694. done += size;
  695. /* Remove comments */
  696. tmp = strchr(kbuf, '#');
  697. if (tmp)
  698. *tmp = '\0';
  699. ret = traceprobe_command(kbuf, createfn);
  700. if (ret)
  701. goto out;
  702. }
  703. ret = done;
  704. out:
  705. kfree(kbuf);
  706. return ret;
  707. }