trace_probe.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Common code for probe-based Dynamic events.
  4. *
  5. * This code was copied from kernel/trace/trace_kprobe.c written by
  6. * Masami Hiramatsu <[email protected]>
  7. *
  8. * Updates to make this generic:
  9. * Copyright (C) IBM Corporation, 2010-2011
  10. * Author: Srikar Dronamraju
  11. */
  12. #define pr_fmt(fmt) "trace_probe: " fmt
  13. #include "trace_probe.h"
  14. #undef C
  15. #define C(a, b) b
  16. static const char *trace_probe_err_text[] = { ERRORS };
  17. static const char *reserved_field_names[] = {
  18. "common_type",
  19. "common_flags",
  20. "common_preempt_count",
  21. "common_pid",
  22. "common_tgid",
  23. FIELD_STRING_IP,
  24. FIELD_STRING_RETIP,
  25. FIELD_STRING_FUNC,
  26. };
  27. /* Printing in basic type function template */
  28. #define DEFINE_BASIC_PRINT_TYPE_FUNC(tname, type, fmt) \
  29. int PRINT_TYPE_FUNC_NAME(tname)(struct trace_seq *s, void *data, void *ent)\
  30. { \
  31. trace_seq_printf(s, fmt, *(type *)data); \
  32. return !trace_seq_has_overflowed(s); \
  33. } \
  34. const char PRINT_TYPE_FMT_NAME(tname)[] = fmt;
  35. DEFINE_BASIC_PRINT_TYPE_FUNC(u8, u8, "%u")
  36. DEFINE_BASIC_PRINT_TYPE_FUNC(u16, u16, "%u")
  37. DEFINE_BASIC_PRINT_TYPE_FUNC(u32, u32, "%u")
  38. DEFINE_BASIC_PRINT_TYPE_FUNC(u64, u64, "%Lu")
  39. DEFINE_BASIC_PRINT_TYPE_FUNC(s8, s8, "%d")
  40. DEFINE_BASIC_PRINT_TYPE_FUNC(s16, s16, "%d")
  41. DEFINE_BASIC_PRINT_TYPE_FUNC(s32, s32, "%d")
  42. DEFINE_BASIC_PRINT_TYPE_FUNC(s64, s64, "%Ld")
  43. DEFINE_BASIC_PRINT_TYPE_FUNC(x8, u8, "0x%x")
  44. DEFINE_BASIC_PRINT_TYPE_FUNC(x16, u16, "0x%x")
  45. DEFINE_BASIC_PRINT_TYPE_FUNC(x32, u32, "0x%x")
  46. DEFINE_BASIC_PRINT_TYPE_FUNC(x64, u64, "0x%Lx")
  47. int PRINT_TYPE_FUNC_NAME(symbol)(struct trace_seq *s, void *data, void *ent)
  48. {
  49. trace_seq_printf(s, "%pS", (void *)*(unsigned long *)data);
  50. return !trace_seq_has_overflowed(s);
  51. }
  52. const char PRINT_TYPE_FMT_NAME(symbol)[] = "%pS";
  53. /* Print type function for string type */
  54. int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, void *data, void *ent)
  55. {
  56. int len = *(u32 *)data >> 16;
  57. if (!len)
  58. trace_seq_puts(s, FAULT_STRING);
  59. else
  60. trace_seq_printf(s, "\"%s\"",
  61. (const char *)get_loc_data(data, ent));
  62. return !trace_seq_has_overflowed(s);
  63. }
  64. const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
  65. /* Fetch type information table */
  66. static const struct fetch_type probe_fetch_types[] = {
  67. /* Special types */
  68. __ASSIGN_FETCH_TYPE("string", string, string, sizeof(u32), 1, 1,
  69. "__data_loc char[]"),
  70. __ASSIGN_FETCH_TYPE("ustring", string, string, sizeof(u32), 1, 1,
  71. "__data_loc char[]"),
  72. __ASSIGN_FETCH_TYPE("symstr", string, string, sizeof(u32), 1, 1,
  73. "__data_loc char[]"),
  74. /* Basic types */
  75. ASSIGN_FETCH_TYPE(u8, u8, 0),
  76. ASSIGN_FETCH_TYPE(u16, u16, 0),
  77. ASSIGN_FETCH_TYPE(u32, u32, 0),
  78. ASSIGN_FETCH_TYPE(u64, u64, 0),
  79. ASSIGN_FETCH_TYPE(s8, u8, 1),
  80. ASSIGN_FETCH_TYPE(s16, u16, 1),
  81. ASSIGN_FETCH_TYPE(s32, u32, 1),
  82. ASSIGN_FETCH_TYPE(s64, u64, 1),
  83. ASSIGN_FETCH_TYPE_ALIAS(x8, u8, u8, 0),
  84. ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
  85. ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
  86. ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
  87. ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0),
  88. ASSIGN_FETCH_TYPE_END
  89. };
  90. static const struct fetch_type *find_fetch_type(const char *type)
  91. {
  92. int i;
  93. if (!type)
  94. type = DEFAULT_FETCH_TYPE_STR;
  95. /* Special case: bitfield */
  96. if (*type == 'b') {
  97. unsigned long bs;
  98. type = strchr(type, '/');
  99. if (!type)
  100. goto fail;
  101. type++;
  102. if (kstrtoul(type, 0, &bs))
  103. goto fail;
  104. switch (bs) {
  105. case 8:
  106. return find_fetch_type("u8");
  107. case 16:
  108. return find_fetch_type("u16");
  109. case 32:
  110. return find_fetch_type("u32");
  111. case 64:
  112. return find_fetch_type("u64");
  113. default:
  114. goto fail;
  115. }
  116. }
  117. for (i = 0; probe_fetch_types[i].name; i++) {
  118. if (strcmp(type, probe_fetch_types[i].name) == 0)
  119. return &probe_fetch_types[i];
  120. }
  121. fail:
  122. return NULL;
  123. }
  124. static struct trace_probe_log trace_probe_log;
  125. void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
  126. {
  127. trace_probe_log.subsystem = subsystem;
  128. trace_probe_log.argc = argc;
  129. trace_probe_log.argv = argv;
  130. trace_probe_log.index = 0;
  131. }
  132. void trace_probe_log_clear(void)
  133. {
  134. memset(&trace_probe_log, 0, sizeof(trace_probe_log));
  135. }
  136. void trace_probe_log_set_index(int index)
  137. {
  138. trace_probe_log.index = index;
  139. }
  140. void __trace_probe_log_err(int offset, int err_type)
  141. {
  142. char *command, *p;
  143. int i, len = 0, pos = 0;
  144. if (!trace_probe_log.argv)
  145. return;
  146. /* Recalculate the length and allocate buffer */
  147. for (i = 0; i < trace_probe_log.argc; i++) {
  148. if (i == trace_probe_log.index)
  149. pos = len;
  150. len += strlen(trace_probe_log.argv[i]) + 1;
  151. }
  152. command = kzalloc(len, GFP_KERNEL);
  153. if (!command)
  154. return;
  155. if (trace_probe_log.index >= trace_probe_log.argc) {
  156. /**
  157. * Set the error position is next to the last arg + space.
  158. * Note that len includes the terminal null and the cursor
  159. * appears at pos + 1.
  160. */
  161. pos = len;
  162. offset = 0;
  163. }
  164. /* And make a command string from argv array */
  165. p = command;
  166. for (i = 0; i < trace_probe_log.argc; i++) {
  167. len = strlen(trace_probe_log.argv[i]);
  168. strcpy(p, trace_probe_log.argv[i]);
  169. p[len] = ' ';
  170. p += len + 1;
  171. }
  172. *(p - 1) = '\0';
  173. tracing_log_err(NULL, trace_probe_log.subsystem, command,
  174. trace_probe_err_text, err_type, pos + offset);
  175. kfree(command);
  176. }
  177. /* Split symbol and offset. */
  178. int traceprobe_split_symbol_offset(char *symbol, long *offset)
  179. {
  180. char *tmp;
  181. int ret;
  182. if (!offset)
  183. return -EINVAL;
  184. tmp = strpbrk(symbol, "+-");
  185. if (tmp) {
  186. ret = kstrtol(tmp, 0, offset);
  187. if (ret)
  188. return ret;
  189. *tmp = '\0';
  190. } else
  191. *offset = 0;
  192. return 0;
  193. }
  194. /* @buf must has MAX_EVENT_NAME_LEN size */
  195. int traceprobe_parse_event_name(const char **pevent, const char **pgroup,
  196. char *buf, int offset)
  197. {
  198. const char *slash, *event = *pevent;
  199. int len;
  200. slash = strchr(event, '/');
  201. if (!slash)
  202. slash = strchr(event, '.');
  203. if (slash) {
  204. if (slash == event) {
  205. trace_probe_log_err(offset, NO_GROUP_NAME);
  206. return -EINVAL;
  207. }
  208. if (slash - event + 1 > MAX_EVENT_NAME_LEN) {
  209. trace_probe_log_err(offset, GROUP_TOO_LONG);
  210. return -EINVAL;
  211. }
  212. strlcpy(buf, event, slash - event + 1);
  213. if (!is_good_system_name(buf)) {
  214. trace_probe_log_err(offset, BAD_GROUP_NAME);
  215. return -EINVAL;
  216. }
  217. *pgroup = buf;
  218. *pevent = slash + 1;
  219. offset += slash - event + 1;
  220. event = *pevent;
  221. }
  222. len = strlen(event);
  223. if (len == 0) {
  224. if (slash) {
  225. *pevent = NULL;
  226. return 0;
  227. }
  228. trace_probe_log_err(offset, NO_EVENT_NAME);
  229. return -EINVAL;
  230. } else if (len > MAX_EVENT_NAME_LEN) {
  231. trace_probe_log_err(offset, EVENT_TOO_LONG);
  232. return -EINVAL;
  233. }
  234. if (!is_good_name(event)) {
  235. trace_probe_log_err(offset, BAD_EVENT_NAME);
  236. return -EINVAL;
  237. }
  238. return 0;
  239. }
  240. #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
  241. static int parse_probe_vars(char *arg, const struct fetch_type *t,
  242. struct fetch_insn *code, unsigned int flags, int offs)
  243. {
  244. unsigned long param;
  245. int ret = 0;
  246. int len;
  247. if (flags & TPARG_FL_TPOINT) {
  248. if (code->data)
  249. return -EFAULT;
  250. code->data = kstrdup(arg, GFP_KERNEL);
  251. if (!code->data)
  252. return -ENOMEM;
  253. code->op = FETCH_OP_TP_ARG;
  254. } else if (strcmp(arg, "retval") == 0) {
  255. if (flags & TPARG_FL_RETURN) {
  256. code->op = FETCH_OP_RETVAL;
  257. } else {
  258. trace_probe_log_err(offs, RETVAL_ON_PROBE);
  259. ret = -EINVAL;
  260. }
  261. } else if ((len = str_has_prefix(arg, "stack"))) {
  262. if (arg[len] == '\0') {
  263. code->op = FETCH_OP_STACKP;
  264. } else if (isdigit(arg[len])) {
  265. ret = kstrtoul(arg + len, 10, &param);
  266. if (ret) {
  267. goto inval_var;
  268. } else if ((flags & TPARG_FL_KERNEL) &&
  269. param > PARAM_MAX_STACK) {
  270. trace_probe_log_err(offs, BAD_STACK_NUM);
  271. ret = -EINVAL;
  272. } else {
  273. code->op = FETCH_OP_STACK;
  274. code->param = (unsigned int)param;
  275. }
  276. } else
  277. goto inval_var;
  278. } else if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) {
  279. code->op = FETCH_OP_COMM;
  280. #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
  281. } else if (((flags & TPARG_FL_MASK) ==
  282. (TPARG_FL_KERNEL | TPARG_FL_FENTRY)) &&
  283. (len = str_has_prefix(arg, "arg"))) {
  284. ret = kstrtoul(arg + len, 10, &param);
  285. if (ret) {
  286. goto inval_var;
  287. } else if (!param || param > PARAM_MAX_STACK) {
  288. trace_probe_log_err(offs, BAD_ARG_NUM);
  289. return -EINVAL;
  290. }
  291. code->op = FETCH_OP_ARG;
  292. code->param = (unsigned int)param - 1;
  293. #endif
  294. } else
  295. goto inval_var;
  296. return ret;
  297. inval_var:
  298. trace_probe_log_err(offs, BAD_VAR);
  299. return -EINVAL;
  300. }
  301. static int str_to_immediate(char *str, unsigned long *imm)
  302. {
  303. if (isdigit(str[0]))
  304. return kstrtoul(str, 0, imm);
  305. else if (str[0] == '-')
  306. return kstrtol(str, 0, (long *)imm);
  307. else if (str[0] == '+')
  308. return kstrtol(str + 1, 0, (long *)imm);
  309. return -EINVAL;
  310. }
  311. static int __parse_imm_string(char *str, char **pbuf, int offs)
  312. {
  313. size_t len = strlen(str);
  314. if (str[len - 1] != '"') {
  315. trace_probe_log_err(offs + len, IMMSTR_NO_CLOSE);
  316. return -EINVAL;
  317. }
  318. *pbuf = kstrndup(str, len - 1, GFP_KERNEL);
  319. if (!*pbuf)
  320. return -ENOMEM;
  321. return 0;
  322. }
  323. /* Recursive argument parser */
  324. static int
  325. parse_probe_arg(char *arg, const struct fetch_type *type,
  326. struct fetch_insn **pcode, struct fetch_insn *end,
  327. unsigned int flags, int offs)
  328. {
  329. struct fetch_insn *code = *pcode;
  330. unsigned long param;
  331. int deref = FETCH_OP_DEREF;
  332. long offset = 0;
  333. char *tmp;
  334. int ret = 0;
  335. switch (arg[0]) {
  336. case '$':
  337. ret = parse_probe_vars(arg + 1, type, code, flags, offs);
  338. break;
  339. case '%': /* named register */
  340. if (flags & TPARG_FL_TPOINT) {
  341. /* eprobes do not handle registers */
  342. trace_probe_log_err(offs, BAD_VAR);
  343. break;
  344. }
  345. ret = regs_query_register_offset(arg + 1);
  346. if (ret >= 0) {
  347. code->op = FETCH_OP_REG;
  348. code->param = (unsigned int)ret;
  349. ret = 0;
  350. } else
  351. trace_probe_log_err(offs, BAD_REG_NAME);
  352. break;
  353. case '@': /* memory, file-offset or symbol */
  354. if (isdigit(arg[1])) {
  355. ret = kstrtoul(arg + 1, 0, &param);
  356. if (ret) {
  357. trace_probe_log_err(offs, BAD_MEM_ADDR);
  358. break;
  359. }
  360. /* load address */
  361. code->op = FETCH_OP_IMM;
  362. code->immediate = param;
  363. } else if (arg[1] == '+') {
  364. /* kprobes don't support file offsets */
  365. if (flags & TPARG_FL_KERNEL) {
  366. trace_probe_log_err(offs, FILE_ON_KPROBE);
  367. return -EINVAL;
  368. }
  369. ret = kstrtol(arg + 2, 0, &offset);
  370. if (ret) {
  371. trace_probe_log_err(offs, BAD_FILE_OFFS);
  372. break;
  373. }
  374. code->op = FETCH_OP_FOFFS;
  375. code->immediate = (unsigned long)offset; // imm64?
  376. } else {
  377. /* uprobes don't support symbols */
  378. if (!(flags & TPARG_FL_KERNEL)) {
  379. trace_probe_log_err(offs, SYM_ON_UPROBE);
  380. return -EINVAL;
  381. }
  382. /* Preserve symbol for updating */
  383. code->op = FETCH_NOP_SYMBOL;
  384. code->data = kstrdup(arg + 1, GFP_KERNEL);
  385. if (!code->data)
  386. return -ENOMEM;
  387. if (++code == end) {
  388. trace_probe_log_err(offs, TOO_MANY_OPS);
  389. return -EINVAL;
  390. }
  391. code->op = FETCH_OP_IMM;
  392. code->immediate = 0;
  393. }
  394. /* These are fetching from memory */
  395. if (++code == end) {
  396. trace_probe_log_err(offs, TOO_MANY_OPS);
  397. return -EINVAL;
  398. }
  399. *pcode = code;
  400. code->op = FETCH_OP_DEREF;
  401. code->offset = offset;
  402. break;
  403. case '+': /* deref memory */
  404. case '-':
  405. if (arg[1] == 'u') {
  406. deref = FETCH_OP_UDEREF;
  407. arg[1] = arg[0];
  408. arg++;
  409. }
  410. if (arg[0] == '+')
  411. arg++; /* Skip '+', because kstrtol() rejects it. */
  412. tmp = strchr(arg, '(');
  413. if (!tmp) {
  414. trace_probe_log_err(offs, DEREF_NEED_BRACE);
  415. return -EINVAL;
  416. }
  417. *tmp = '\0';
  418. ret = kstrtol(arg, 0, &offset);
  419. if (ret) {
  420. trace_probe_log_err(offs, BAD_DEREF_OFFS);
  421. break;
  422. }
  423. offs += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0);
  424. arg = tmp + 1;
  425. tmp = strrchr(arg, ')');
  426. if (!tmp) {
  427. trace_probe_log_err(offs + strlen(arg),
  428. DEREF_OPEN_BRACE);
  429. return -EINVAL;
  430. } else {
  431. const struct fetch_type *t2 = find_fetch_type(NULL);
  432. *tmp = '\0';
  433. ret = parse_probe_arg(arg, t2, &code, end, flags, offs);
  434. if (ret)
  435. break;
  436. if (code->op == FETCH_OP_COMM ||
  437. code->op == FETCH_OP_DATA) {
  438. trace_probe_log_err(offs, COMM_CANT_DEREF);
  439. return -EINVAL;
  440. }
  441. if (++code == end) {
  442. trace_probe_log_err(offs, TOO_MANY_OPS);
  443. return -EINVAL;
  444. }
  445. *pcode = code;
  446. code->op = deref;
  447. code->offset = offset;
  448. }
  449. break;
  450. case '\\': /* Immediate value */
  451. if (arg[1] == '"') { /* Immediate string */
  452. ret = __parse_imm_string(arg + 2, &tmp, offs + 2);
  453. if (ret)
  454. break;
  455. code->op = FETCH_OP_DATA;
  456. code->data = tmp;
  457. } else {
  458. ret = str_to_immediate(arg + 1, &code->immediate);
  459. if (ret)
  460. trace_probe_log_err(offs + 1, BAD_IMM);
  461. else
  462. code->op = FETCH_OP_IMM;
  463. }
  464. break;
  465. }
  466. if (!ret && code->op == FETCH_OP_NOP) {
  467. /* Parsed, but do not find fetch method */
  468. trace_probe_log_err(offs, BAD_FETCH_ARG);
  469. ret = -EINVAL;
  470. }
  471. return ret;
  472. }
  473. #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
  474. /* Bitfield type needs to be parsed into a fetch function */
  475. static int __parse_bitfield_probe_arg(const char *bf,
  476. const struct fetch_type *t,
  477. struct fetch_insn **pcode)
  478. {
  479. struct fetch_insn *code = *pcode;
  480. unsigned long bw, bo;
  481. char *tail;
  482. if (*bf != 'b')
  483. return 0;
  484. bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
  485. if (bw == 0 || *tail != '@')
  486. return -EINVAL;
  487. bf = tail + 1;
  488. bo = simple_strtoul(bf, &tail, 0);
  489. if (tail == bf || *tail != '/')
  490. return -EINVAL;
  491. code++;
  492. if (code->op != FETCH_OP_NOP)
  493. return -EINVAL;
  494. *pcode = code;
  495. code->op = FETCH_OP_MOD_BF;
  496. code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
  497. code->rshift = BYTES_TO_BITS(t->size) - bw;
  498. code->basesize = t->size;
  499. return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
  500. }
  501. /* String length checking wrapper */
  502. static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size,
  503. struct probe_arg *parg, unsigned int flags, int offset)
  504. {
  505. struct fetch_insn *code, *scode, *tmp = NULL;
  506. char *t, *t2, *t3;
  507. char *arg;
  508. int ret, len;
  509. arg = kstrdup(argv, GFP_KERNEL);
  510. if (!arg)
  511. return -ENOMEM;
  512. ret = -EINVAL;
  513. len = strlen(arg);
  514. if (len > MAX_ARGSTR_LEN) {
  515. trace_probe_log_err(offset, ARG_TOO_LONG);
  516. goto out;
  517. } else if (len == 0) {
  518. trace_probe_log_err(offset, NO_ARG_BODY);
  519. goto out;
  520. }
  521. ret = -ENOMEM;
  522. parg->comm = kstrdup(arg, GFP_KERNEL);
  523. if (!parg->comm)
  524. goto out;
  525. ret = -EINVAL;
  526. t = strchr(arg, ':');
  527. if (t) {
  528. *t = '\0';
  529. t2 = strchr(++t, '[');
  530. if (t2) {
  531. *t2++ = '\0';
  532. t3 = strchr(t2, ']');
  533. if (!t3) {
  534. offset += t2 + strlen(t2) - arg;
  535. trace_probe_log_err(offset,
  536. ARRAY_NO_CLOSE);
  537. goto out;
  538. } else if (t3[1] != '\0') {
  539. trace_probe_log_err(offset + t3 + 1 - arg,
  540. BAD_ARRAY_SUFFIX);
  541. goto out;
  542. }
  543. *t3 = '\0';
  544. if (kstrtouint(t2, 0, &parg->count) || !parg->count) {
  545. trace_probe_log_err(offset + t2 - arg,
  546. BAD_ARRAY_NUM);
  547. goto out;
  548. }
  549. if (parg->count > MAX_ARRAY_LEN) {
  550. trace_probe_log_err(offset + t2 - arg,
  551. ARRAY_TOO_BIG);
  552. goto out;
  553. }
  554. }
  555. }
  556. /*
  557. * Since $comm and immediate string can not be dereferenced,
  558. * we can find those by strcmp. But ignore for eprobes.
  559. */
  560. if (!(flags & TPARG_FL_TPOINT) &&
  561. (strcmp(arg, "$comm") == 0 || strcmp(arg, "$COMM") == 0 ||
  562. strncmp(arg, "\\\"", 2) == 0)) {
  563. /* The type of $comm must be "string", and not an array. */
  564. if (parg->count || (t && strcmp(t, "string")))
  565. goto out;
  566. parg->type = find_fetch_type("string");
  567. } else
  568. parg->type = find_fetch_type(t);
  569. if (!parg->type) {
  570. trace_probe_log_err(offset + (t ? (t - arg) : 0), BAD_TYPE);
  571. goto out;
  572. }
  573. parg->offset = *size;
  574. *size += parg->type->size * (parg->count ?: 1);
  575. ret = -ENOMEM;
  576. if (parg->count) {
  577. len = strlen(parg->type->fmttype) + 6;
  578. parg->fmt = kmalloc(len, GFP_KERNEL);
  579. if (!parg->fmt)
  580. goto out;
  581. snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype,
  582. parg->count);
  583. }
  584. code = tmp = kcalloc(FETCH_INSN_MAX, sizeof(*code), GFP_KERNEL);
  585. if (!code)
  586. goto out;
  587. code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
  588. ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
  589. flags, offset);
  590. if (ret)
  591. goto fail;
  592. ret = -EINVAL;
  593. /* Store operation */
  594. if (parg->type->is_string) {
  595. if (!strcmp(parg->type->name, "symstr")) {
  596. if (code->op != FETCH_OP_REG && code->op != FETCH_OP_STACK &&
  597. code->op != FETCH_OP_RETVAL && code->op != FETCH_OP_ARG &&
  598. code->op != FETCH_OP_DEREF && code->op != FETCH_OP_TP_ARG) {
  599. trace_probe_log_err(offset + (t ? (t - arg) : 0),
  600. BAD_SYMSTRING);
  601. goto fail;
  602. }
  603. } else {
  604. if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF &&
  605. code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM &&
  606. code->op != FETCH_OP_DATA && code->op != FETCH_OP_TP_ARG) {
  607. trace_probe_log_err(offset + (t ? (t - arg) : 0),
  608. BAD_STRING);
  609. goto fail;
  610. }
  611. }
  612. if (!strcmp(parg->type->name, "symstr") ||
  613. (code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM ||
  614. code->op == FETCH_OP_DATA) || code->op == FETCH_OP_TP_ARG ||
  615. parg->count) {
  616. /*
  617. * IMM, DATA and COMM is pointing actual address, those
  618. * must be kept, and if parg->count != 0, this is an
  619. * array of string pointers instead of string address
  620. * itself.
  621. * For the symstr, it doesn't need to dereference, thus
  622. * it just get the value.
  623. */
  624. code++;
  625. if (code->op != FETCH_OP_NOP) {
  626. trace_probe_log_err(offset, TOO_MANY_OPS);
  627. goto fail;
  628. }
  629. }
  630. /* If op == DEREF, replace it with STRING */
  631. if (!strcmp(parg->type->name, "ustring") ||
  632. code->op == FETCH_OP_UDEREF)
  633. code->op = FETCH_OP_ST_USTRING;
  634. else if (!strcmp(parg->type->name, "symstr"))
  635. code->op = FETCH_OP_ST_SYMSTR;
  636. else
  637. code->op = FETCH_OP_ST_STRING;
  638. code->size = parg->type->size;
  639. parg->dynamic = true;
  640. } else if (code->op == FETCH_OP_DEREF) {
  641. code->op = FETCH_OP_ST_MEM;
  642. code->size = parg->type->size;
  643. } else if (code->op == FETCH_OP_UDEREF) {
  644. code->op = FETCH_OP_ST_UMEM;
  645. code->size = parg->type->size;
  646. } else {
  647. code++;
  648. if (code->op != FETCH_OP_NOP) {
  649. trace_probe_log_err(offset, TOO_MANY_OPS);
  650. goto fail;
  651. }
  652. code->op = FETCH_OP_ST_RAW;
  653. code->size = parg->type->size;
  654. }
  655. scode = code;
  656. /* Modify operation */
  657. if (t != NULL) {
  658. ret = __parse_bitfield_probe_arg(t, parg->type, &code);
  659. if (ret) {
  660. trace_probe_log_err(offset + t - arg, BAD_BITFIELD);
  661. goto fail;
  662. }
  663. }
  664. ret = -EINVAL;
  665. /* Loop(Array) operation */
  666. if (parg->count) {
  667. if (scode->op != FETCH_OP_ST_MEM &&
  668. scode->op != FETCH_OP_ST_STRING &&
  669. scode->op != FETCH_OP_ST_USTRING) {
  670. trace_probe_log_err(offset + (t ? (t - arg) : 0),
  671. BAD_STRING);
  672. goto fail;
  673. }
  674. code++;
  675. if (code->op != FETCH_OP_NOP) {
  676. trace_probe_log_err(offset, TOO_MANY_OPS);
  677. goto fail;
  678. }
  679. code->op = FETCH_OP_LP_ARRAY;
  680. code->param = parg->count;
  681. }
  682. code++;
  683. code->op = FETCH_OP_END;
  684. ret = 0;
  685. /* Shrink down the code buffer */
  686. parg->code = kcalloc(code - tmp + 1, sizeof(*code), GFP_KERNEL);
  687. if (!parg->code)
  688. ret = -ENOMEM;
  689. else
  690. memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
  691. fail:
  692. if (ret) {
  693. for (code = tmp; code < tmp + FETCH_INSN_MAX; code++)
  694. if (code->op == FETCH_NOP_SYMBOL ||
  695. code->op == FETCH_OP_DATA)
  696. kfree(code->data);
  697. }
  698. kfree(tmp);
  699. out:
  700. kfree(arg);
  701. return ret;
  702. }
  703. /* Return 1 if name is reserved or already used by another argument */
  704. static int traceprobe_conflict_field_name(const char *name,
  705. struct probe_arg *args, int narg)
  706. {
  707. int i;
  708. for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
  709. if (strcmp(reserved_field_names[i], name) == 0)
  710. return 1;
  711. for (i = 0; i < narg; i++)
  712. if (strcmp(args[i].name, name) == 0)
  713. return 1;
  714. return 0;
  715. }
  716. int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, const char *arg,
  717. unsigned int flags)
  718. {
  719. struct probe_arg *parg = &tp->args[i];
  720. const char *body;
  721. /* Increment count for freeing args in error case */
  722. tp->nr_args++;
  723. body = strchr(arg, '=');
  724. if (body) {
  725. if (body - arg > MAX_ARG_NAME_LEN) {
  726. trace_probe_log_err(0, ARG_NAME_TOO_LONG);
  727. return -EINVAL;
  728. } else if (body == arg) {
  729. trace_probe_log_err(0, NO_ARG_NAME);
  730. return -EINVAL;
  731. }
  732. parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL);
  733. body++;
  734. } else {
  735. /* If argument name is omitted, set "argN" */
  736. parg->name = kasprintf(GFP_KERNEL, "arg%d", i + 1);
  737. body = arg;
  738. }
  739. if (!parg->name)
  740. return -ENOMEM;
  741. if (!is_good_name(parg->name)) {
  742. trace_probe_log_err(0, BAD_ARG_NAME);
  743. return -EINVAL;
  744. }
  745. if (traceprobe_conflict_field_name(parg->name, tp->args, i)) {
  746. trace_probe_log_err(0, USED_ARG_NAME);
  747. return -EINVAL;
  748. }
  749. /* Parse fetch argument */
  750. return traceprobe_parse_probe_arg_body(body, &tp->size, parg, flags,
  751. body - arg);
  752. }
  753. void traceprobe_free_probe_arg(struct probe_arg *arg)
  754. {
  755. struct fetch_insn *code = arg->code;
  756. while (code && code->op != FETCH_OP_END) {
  757. if (code->op == FETCH_NOP_SYMBOL ||
  758. code->op == FETCH_OP_DATA)
  759. kfree(code->data);
  760. code++;
  761. }
  762. kfree(arg->code);
  763. kfree(arg->name);
  764. kfree(arg->comm);
  765. kfree(arg->fmt);
  766. }
  767. int traceprobe_update_arg(struct probe_arg *arg)
  768. {
  769. struct fetch_insn *code = arg->code;
  770. long offset;
  771. char *tmp;
  772. char c;
  773. int ret = 0;
  774. while (code && code->op != FETCH_OP_END) {
  775. if (code->op == FETCH_NOP_SYMBOL) {
  776. if (code[1].op != FETCH_OP_IMM)
  777. return -EINVAL;
  778. tmp = strpbrk(code->data, "+-");
  779. if (tmp)
  780. c = *tmp;
  781. ret = traceprobe_split_symbol_offset(code->data,
  782. &offset);
  783. if (ret)
  784. return ret;
  785. code[1].immediate =
  786. (unsigned long)kallsyms_lookup_name(code->data);
  787. if (tmp)
  788. *tmp = c;
  789. if (!code[1].immediate)
  790. return -ENOENT;
  791. code[1].immediate += offset;
  792. }
  793. code++;
  794. }
  795. return 0;
  796. }
  797. /* When len=0, we just calculate the needed length */
  798. #define LEN_OR_ZERO (len ? len - pos : 0)
  799. static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
  800. enum probe_print_type ptype)
  801. {
  802. struct probe_arg *parg;
  803. int i, j;
  804. int pos = 0;
  805. const char *fmt, *arg;
  806. switch (ptype) {
  807. case PROBE_PRINT_NORMAL:
  808. fmt = "(%lx)";
  809. arg = ", REC->" FIELD_STRING_IP;
  810. break;
  811. case PROBE_PRINT_RETURN:
  812. fmt = "(%lx <- %lx)";
  813. arg = ", REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
  814. break;
  815. case PROBE_PRINT_EVENT:
  816. fmt = "";
  817. arg = "";
  818. break;
  819. default:
  820. WARN_ON_ONCE(1);
  821. return 0;
  822. }
  823. pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
  824. for (i = 0; i < tp->nr_args; i++) {
  825. parg = tp->args + i;
  826. pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name);
  827. if (parg->count) {
  828. pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s",
  829. parg->type->fmt);
  830. for (j = 1; j < parg->count; j++)
  831. pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s",
  832. parg->type->fmt);
  833. pos += snprintf(buf + pos, LEN_OR_ZERO, "}");
  834. } else
  835. pos += snprintf(buf + pos, LEN_OR_ZERO, "%s",
  836. parg->type->fmt);
  837. }
  838. pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", arg);
  839. for (i = 0; i < tp->nr_args; i++) {
  840. parg = tp->args + i;
  841. if (parg->count) {
  842. if (parg->type->is_string)
  843. fmt = ", __get_str(%s[%d])";
  844. else
  845. fmt = ", REC->%s[%d]";
  846. for (j = 0; j < parg->count; j++)
  847. pos += snprintf(buf + pos, LEN_OR_ZERO,
  848. fmt, parg->name, j);
  849. } else {
  850. if (parg->type->is_string)
  851. fmt = ", __get_str(%s)";
  852. else
  853. fmt = ", REC->%s";
  854. pos += snprintf(buf + pos, LEN_OR_ZERO,
  855. fmt, parg->name);
  856. }
  857. }
  858. /* return the length of print_fmt */
  859. return pos;
  860. }
  861. #undef LEN_OR_ZERO
  862. int traceprobe_set_print_fmt(struct trace_probe *tp, enum probe_print_type ptype)
  863. {
  864. struct trace_event_call *call = trace_probe_event_call(tp);
  865. int len;
  866. char *print_fmt;
  867. /* First: called with 0 length to calculate the needed length */
  868. len = __set_print_fmt(tp, NULL, 0, ptype);
  869. print_fmt = kmalloc(len + 1, GFP_KERNEL);
  870. if (!print_fmt)
  871. return -ENOMEM;
  872. /* Second: actually write the @print_fmt */
  873. __set_print_fmt(tp, print_fmt, len + 1, ptype);
  874. call->print_fmt = print_fmt;
  875. return 0;
  876. }
  877. int traceprobe_define_arg_fields(struct trace_event_call *event_call,
  878. size_t offset, struct trace_probe *tp)
  879. {
  880. int ret, i;
  881. /* Set argument names as fields */
  882. for (i = 0; i < tp->nr_args; i++) {
  883. struct probe_arg *parg = &tp->args[i];
  884. const char *fmt = parg->type->fmttype;
  885. int size = parg->type->size;
  886. if (parg->fmt)
  887. fmt = parg->fmt;
  888. if (parg->count)
  889. size *= parg->count;
  890. ret = trace_define_field(event_call, fmt, parg->name,
  891. offset + parg->offset, size,
  892. parg->type->is_signed,
  893. FILTER_OTHER);
  894. if (ret)
  895. return ret;
  896. }
  897. return 0;
  898. }
  899. static void trace_probe_event_free(struct trace_probe_event *tpe)
  900. {
  901. kfree(tpe->class.system);
  902. kfree(tpe->call.name);
  903. kfree(tpe->call.print_fmt);
  904. kfree(tpe);
  905. }
  906. int trace_probe_append(struct trace_probe *tp, struct trace_probe *to)
  907. {
  908. if (trace_probe_has_sibling(tp))
  909. return -EBUSY;
  910. list_del_init(&tp->list);
  911. trace_probe_event_free(tp->event);
  912. tp->event = to->event;
  913. list_add_tail(&tp->list, trace_probe_probe_list(to));
  914. return 0;
  915. }
  916. void trace_probe_unlink(struct trace_probe *tp)
  917. {
  918. list_del_init(&tp->list);
  919. if (list_empty(trace_probe_probe_list(tp)))
  920. trace_probe_event_free(tp->event);
  921. tp->event = NULL;
  922. }
  923. void trace_probe_cleanup(struct trace_probe *tp)
  924. {
  925. int i;
  926. for (i = 0; i < tp->nr_args; i++)
  927. traceprobe_free_probe_arg(&tp->args[i]);
  928. if (tp->event)
  929. trace_probe_unlink(tp);
  930. }
  931. int trace_probe_init(struct trace_probe *tp, const char *event,
  932. const char *group, bool alloc_filter)
  933. {
  934. struct trace_event_call *call;
  935. size_t size = sizeof(struct trace_probe_event);
  936. int ret = 0;
  937. if (!event || !group)
  938. return -EINVAL;
  939. if (alloc_filter)
  940. size += sizeof(struct trace_uprobe_filter);
  941. tp->event = kzalloc(size, GFP_KERNEL);
  942. if (!tp->event)
  943. return -ENOMEM;
  944. INIT_LIST_HEAD(&tp->event->files);
  945. INIT_LIST_HEAD(&tp->event->class.fields);
  946. INIT_LIST_HEAD(&tp->event->probes);
  947. INIT_LIST_HEAD(&tp->list);
  948. list_add(&tp->list, &tp->event->probes);
  949. call = trace_probe_event_call(tp);
  950. call->class = &tp->event->class;
  951. call->name = kstrdup(event, GFP_KERNEL);
  952. if (!call->name) {
  953. ret = -ENOMEM;
  954. goto error;
  955. }
  956. tp->event->class.system = kstrdup(group, GFP_KERNEL);
  957. if (!tp->event->class.system) {
  958. ret = -ENOMEM;
  959. goto error;
  960. }
  961. return 0;
  962. error:
  963. trace_probe_cleanup(tp);
  964. return ret;
  965. }
  966. static struct trace_event_call *
  967. find_trace_event_call(const char *system, const char *event_name)
  968. {
  969. struct trace_event_call *tp_event;
  970. const char *name;
  971. list_for_each_entry(tp_event, &ftrace_events, list) {
  972. if (!tp_event->class->system ||
  973. strcmp(system, tp_event->class->system))
  974. continue;
  975. name = trace_event_name(tp_event);
  976. if (!name || strcmp(event_name, name))
  977. continue;
  978. return tp_event;
  979. }
  980. return NULL;
  981. }
  982. int trace_probe_register_event_call(struct trace_probe *tp)
  983. {
  984. struct trace_event_call *call = trace_probe_event_call(tp);
  985. int ret;
  986. lockdep_assert_held(&event_mutex);
  987. if (find_trace_event_call(trace_probe_group_name(tp),
  988. trace_probe_name(tp)))
  989. return -EEXIST;
  990. ret = register_trace_event(&call->event);
  991. if (!ret)
  992. return -ENODEV;
  993. ret = trace_add_event_call(call);
  994. if (ret)
  995. unregister_trace_event(&call->event);
  996. return ret;
  997. }
  998. int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file)
  999. {
  1000. struct event_file_link *link;
  1001. link = kmalloc(sizeof(*link), GFP_KERNEL);
  1002. if (!link)
  1003. return -ENOMEM;
  1004. link->file = file;
  1005. INIT_LIST_HEAD(&link->list);
  1006. list_add_tail_rcu(&link->list, &tp->event->files);
  1007. trace_probe_set_flag(tp, TP_FLAG_TRACE);
  1008. return 0;
  1009. }
  1010. struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
  1011. struct trace_event_file *file)
  1012. {
  1013. struct event_file_link *link;
  1014. trace_probe_for_each_link(link, tp) {
  1015. if (link->file == file)
  1016. return link;
  1017. }
  1018. return NULL;
  1019. }
  1020. int trace_probe_remove_file(struct trace_probe *tp,
  1021. struct trace_event_file *file)
  1022. {
  1023. struct event_file_link *link;
  1024. link = trace_probe_get_file_link(tp, file);
  1025. if (!link)
  1026. return -ENOENT;
  1027. list_del_rcu(&link->list);
  1028. kvfree_rcu(link);
  1029. if (list_empty(&tp->event->files))
  1030. trace_probe_clear_flag(tp, TP_FLAG_TRACE);
  1031. return 0;
  1032. }
  1033. /*
  1034. * Return the smallest index of different type argument (start from 1).
  1035. * If all argument types and name are same, return 0.
  1036. */
  1037. int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b)
  1038. {
  1039. int i;
  1040. /* In case of more arguments */
  1041. if (a->nr_args < b->nr_args)
  1042. return a->nr_args + 1;
  1043. if (a->nr_args > b->nr_args)
  1044. return b->nr_args + 1;
  1045. for (i = 0; i < a->nr_args; i++) {
  1046. if ((b->nr_args <= i) ||
  1047. ((a->args[i].type != b->args[i].type) ||
  1048. (a->args[i].count != b->args[i].count) ||
  1049. strcmp(a->args[i].name, b->args[i].name)))
  1050. return i + 1;
  1051. }
  1052. return 0;
  1053. }
  1054. bool trace_probe_match_command_args(struct trace_probe *tp,
  1055. int argc, const char **argv)
  1056. {
  1057. char buf[MAX_ARGSTR_LEN + 1];
  1058. int i;
  1059. if (tp->nr_args < argc)
  1060. return false;
  1061. for (i = 0; i < argc; i++) {
  1062. snprintf(buf, sizeof(buf), "%s=%s",
  1063. tp->args[i].name, tp->args[i].comm);
  1064. if (strcmp(buf, argv[i]))
  1065. return false;
  1066. }
  1067. return true;
  1068. }
  1069. int trace_probe_create(const char *raw_command, int (*createfn)(int, const char **))
  1070. {
  1071. int argc = 0, ret = 0;
  1072. char **argv;
  1073. argv = argv_split(GFP_KERNEL, raw_command, &argc);
  1074. if (!argv)
  1075. return -ENOMEM;
  1076. if (argc)
  1077. ret = createfn(argc, (const char **)argv);
  1078. argv_free(argv);
  1079. return ret;
  1080. }