trace_probe.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230
  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 <masami.hiramatsu.pt@hitachi.com>
  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 <linux/bpf.h>
  14. #include <linux/fs.h>
  15. #include "trace_btf.h"
  16. #include "trace_probe.h"
  17. #undef C
  18. #define C(a, b) b
  19. static const char *trace_probe_err_text[] = { ERRORS };
  20. static const char *reserved_field_names[] = {
  21. "common_type",
  22. "common_flags",
  23. "common_preempt_count",
  24. "common_pid",
  25. "common_tgid",
  26. FIELD_STRING_IP,
  27. FIELD_STRING_RETIP,
  28. FIELD_STRING_FUNC,
  29. };
  30. /* Printing in basic type function template */
  31. #define DEFINE_BASIC_PRINT_TYPE_FUNC(tname, type, fmt) \
  32. int PRINT_TYPE_FUNC_NAME(tname)(struct trace_seq *s, void *data, void *ent)\
  33. { \
  34. trace_seq_printf(s, fmt, *(type *)data); \
  35. return !trace_seq_has_overflowed(s); \
  36. } \
  37. const char PRINT_TYPE_FMT_NAME(tname)[] = fmt;
  38. DEFINE_BASIC_PRINT_TYPE_FUNC(u8, u8, "%u")
  39. DEFINE_BASIC_PRINT_TYPE_FUNC(u16, u16, "%u")
  40. DEFINE_BASIC_PRINT_TYPE_FUNC(u32, u32, "%u")
  41. DEFINE_BASIC_PRINT_TYPE_FUNC(u64, u64, "%Lu")
  42. DEFINE_BASIC_PRINT_TYPE_FUNC(s8, s8, "%d")
  43. DEFINE_BASIC_PRINT_TYPE_FUNC(s16, s16, "%d")
  44. DEFINE_BASIC_PRINT_TYPE_FUNC(s32, s32, "%d")
  45. DEFINE_BASIC_PRINT_TYPE_FUNC(s64, s64, "%Ld")
  46. DEFINE_BASIC_PRINT_TYPE_FUNC(x8, u8, "0x%x")
  47. DEFINE_BASIC_PRINT_TYPE_FUNC(x16, u16, "0x%x")
  48. DEFINE_BASIC_PRINT_TYPE_FUNC(x32, u32, "0x%x")
  49. DEFINE_BASIC_PRINT_TYPE_FUNC(x64, u64, "0x%Lx")
  50. DEFINE_BASIC_PRINT_TYPE_FUNC(char, u8, "'%c'")
  51. int PRINT_TYPE_FUNC_NAME(symbol)(struct trace_seq *s, void *data, void *ent)
  52. {
  53. trace_seq_printf(s, "%pS", (void *)*(unsigned long *)data);
  54. return !trace_seq_has_overflowed(s);
  55. }
  56. const char PRINT_TYPE_FMT_NAME(symbol)[] = "%pS";
  57. /* Print type function for string type */
  58. int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, void *data, void *ent)
  59. {
  60. int len = *(u32 *)data >> 16;
  61. if (!len)
  62. trace_seq_puts(s, FAULT_STRING);
  63. else
  64. trace_seq_printf(s, "\"%s\"",
  65. (const char *)get_loc_data(data, ent));
  66. return !trace_seq_has_overflowed(s);
  67. }
  68. const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
  69. /* Fetch type information table */
  70. static const struct fetch_type probe_fetch_types[] = {
  71. /* Special types */
  72. __ASSIGN_FETCH_TYPE("string", string, string, sizeof(u32), 1, 1,
  73. "__data_loc char[]"),
  74. __ASSIGN_FETCH_TYPE("ustring", string, string, sizeof(u32), 1, 1,
  75. "__data_loc char[]"),
  76. __ASSIGN_FETCH_TYPE("symstr", string, string, sizeof(u32), 1, 1,
  77. "__data_loc char[]"),
  78. /* Basic types */
  79. ASSIGN_FETCH_TYPE(u8, u8, 0),
  80. ASSIGN_FETCH_TYPE(u16, u16, 0),
  81. ASSIGN_FETCH_TYPE(u32, u32, 0),
  82. ASSIGN_FETCH_TYPE(u64, u64, 0),
  83. ASSIGN_FETCH_TYPE(s8, u8, 1),
  84. ASSIGN_FETCH_TYPE(s16, u16, 1),
  85. ASSIGN_FETCH_TYPE(s32, u32, 1),
  86. ASSIGN_FETCH_TYPE(s64, u64, 1),
  87. ASSIGN_FETCH_TYPE_ALIAS(x8, u8, u8, 0),
  88. ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
  89. ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
  90. ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
  91. ASSIGN_FETCH_TYPE_ALIAS(char, u8, u8, 0),
  92. ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0),
  93. ASSIGN_FETCH_TYPE_END
  94. };
  95. static const struct fetch_type *find_fetch_type(const char *type, unsigned long flags)
  96. {
  97. int i;
  98. /* Reject the symbol/symstr for uprobes */
  99. if (type && (flags & TPARG_FL_USER) &&
  100. (!strcmp(type, "symbol") || !strcmp(type, "symstr")))
  101. return NULL;
  102. if (!type)
  103. type = DEFAULT_FETCH_TYPE_STR;
  104. /* Special case: bitfield */
  105. if (*type == 'b') {
  106. unsigned long bs;
  107. type = strchr(type, '/');
  108. if (!type)
  109. goto fail;
  110. type++;
  111. if (kstrtoul(type, 0, &bs))
  112. goto fail;
  113. switch (bs) {
  114. case 8:
  115. return find_fetch_type("u8", flags);
  116. case 16:
  117. return find_fetch_type("u16", flags);
  118. case 32:
  119. return find_fetch_type("u32", flags);
  120. case 64:
  121. return find_fetch_type("u64", flags);
  122. default:
  123. goto fail;
  124. }
  125. }
  126. for (i = 0; probe_fetch_types[i].name; i++) {
  127. if (strcmp(type, probe_fetch_types[i].name) == 0)
  128. return &probe_fetch_types[i];
  129. }
  130. fail:
  131. return NULL;
  132. }
  133. static struct trace_probe_log trace_probe_log;
  134. void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
  135. {
  136. trace_probe_log.subsystem = subsystem;
  137. trace_probe_log.argc = argc;
  138. trace_probe_log.argv = argv;
  139. trace_probe_log.index = 0;
  140. }
  141. void trace_probe_log_clear(void)
  142. {
  143. memset(&trace_probe_log, 0, sizeof(trace_probe_log));
  144. }
  145. void trace_probe_log_set_index(int index)
  146. {
  147. trace_probe_log.index = index;
  148. }
  149. void __trace_probe_log_err(int offset, int err_type)
  150. {
  151. char *command, *p;
  152. int i, len = 0, pos = 0;
  153. if (!trace_probe_log.argv)
  154. return;
  155. /* Recalculate the length and allocate buffer */
  156. for (i = 0; i < trace_probe_log.argc; i++) {
  157. if (i == trace_probe_log.index)
  158. pos = len;
  159. len += strlen(trace_probe_log.argv[i]) + 1;
  160. }
  161. command = kzalloc(len, GFP_KERNEL);
  162. if (!command)
  163. return;
  164. if (trace_probe_log.index >= trace_probe_log.argc) {
  165. /**
  166. * Set the error position is next to the last arg + space.
  167. * Note that len includes the terminal null and the cursor
  168. * appears at pos + 1.
  169. */
  170. pos = len;
  171. offset = 0;
  172. }
  173. /* And make a command string from argv array */
  174. p = command;
  175. for (i = 0; i < trace_probe_log.argc; i++) {
  176. len = strlen(trace_probe_log.argv[i]);
  177. strcpy(p, trace_probe_log.argv[i]);
  178. p[len] = ' ';
  179. p += len + 1;
  180. }
  181. *(p - 1) = '\0';
  182. tracing_log_err(NULL, trace_probe_log.subsystem, command,
  183. trace_probe_err_text, err_type, pos + offset);
  184. kfree(command);
  185. }
  186. /* Split symbol and offset. */
  187. int traceprobe_split_symbol_offset(char *symbol, long *offset)
  188. {
  189. char *tmp;
  190. int ret;
  191. if (!offset)
  192. return -EINVAL;
  193. tmp = strpbrk(symbol, "+-");
  194. if (tmp) {
  195. ret = kstrtol(tmp, 0, offset);
  196. if (ret)
  197. return ret;
  198. *tmp = '\0';
  199. } else
  200. *offset = 0;
  201. return 0;
  202. }
  203. /* @buf must has MAX_EVENT_NAME_LEN size */
  204. int traceprobe_parse_event_name(const char **pevent, const char **pgroup,
  205. char *buf, int offset)
  206. {
  207. const char *slash, *event = *pevent;
  208. int len;
  209. slash = strchr(event, '/');
  210. if (!slash)
  211. slash = strchr(event, '.');
  212. if (slash) {
  213. if (slash == event) {
  214. trace_probe_log_err(offset, NO_GROUP_NAME);
  215. return -EINVAL;
  216. }
  217. if (slash - event + 1 > MAX_EVENT_NAME_LEN) {
  218. trace_probe_log_err(offset, GROUP_TOO_LONG);
  219. return -EINVAL;
  220. }
  221. strscpy(buf, event, slash - event + 1);
  222. if (!is_good_system_name(buf)) {
  223. trace_probe_log_err(offset, BAD_GROUP_NAME);
  224. return -EINVAL;
  225. }
  226. *pgroup = buf;
  227. *pevent = slash + 1;
  228. offset += slash - event + 1;
  229. event = *pevent;
  230. }
  231. len = strlen(event);
  232. if (len == 0) {
  233. if (slash) {
  234. *pevent = NULL;
  235. return 0;
  236. }
  237. trace_probe_log_err(offset, NO_EVENT_NAME);
  238. return -EINVAL;
  239. } else if (len >= MAX_EVENT_NAME_LEN) {
  240. trace_probe_log_err(offset, EVENT_TOO_LONG);
  241. return -EINVAL;
  242. }
  243. if (!is_good_name(event)) {
  244. trace_probe_log_err(offset, BAD_EVENT_NAME);
  245. return -EINVAL;
  246. }
  247. return 0;
  248. }
  249. static int parse_trace_event_arg(char *arg, struct fetch_insn *code,
  250. struct traceprobe_parse_context *ctx)
  251. {
  252. struct ftrace_event_field *field;
  253. struct list_head *head;
  254. head = trace_get_fields(ctx->event);
  255. list_for_each_entry(field, head, link) {
  256. if (!strcmp(arg, field->name)) {
  257. code->op = FETCH_OP_TP_ARG;
  258. code->data = field;
  259. return 0;
  260. }
  261. }
  262. return -ENOENT;
  263. }
  264. #ifdef CONFIG_PROBE_EVENTS_BTF_ARGS
  265. static u32 btf_type_int(const struct btf_type *t)
  266. {
  267. return *(u32 *)(t + 1);
  268. }
  269. static bool btf_type_is_char_ptr(struct btf *btf, const struct btf_type *type)
  270. {
  271. const struct btf_type *real_type;
  272. u32 intdata;
  273. s32 tid;
  274. real_type = btf_type_skip_modifiers(btf, type->type, &tid);
  275. if (!real_type)
  276. return false;
  277. if (BTF_INFO_KIND(real_type->info) != BTF_KIND_INT)
  278. return false;
  279. intdata = btf_type_int(real_type);
  280. return !(BTF_INT_ENCODING(intdata) & BTF_INT_SIGNED)
  281. && BTF_INT_BITS(intdata) == 8;
  282. }
  283. static bool btf_type_is_char_array(struct btf *btf, const struct btf_type *type)
  284. {
  285. const struct btf_type *real_type;
  286. const struct btf_array *array;
  287. u32 intdata;
  288. s32 tid;
  289. if (BTF_INFO_KIND(type->info) != BTF_KIND_ARRAY)
  290. return false;
  291. array = (const struct btf_array *)(type + 1);
  292. real_type = btf_type_skip_modifiers(btf, array->type, &tid);
  293. intdata = btf_type_int(real_type);
  294. return !(BTF_INT_ENCODING(intdata) & BTF_INT_SIGNED)
  295. && BTF_INT_BITS(intdata) == 8;
  296. }
  297. static int check_prepare_btf_string_fetch(char *typename,
  298. struct fetch_insn **pcode,
  299. struct traceprobe_parse_context *ctx)
  300. {
  301. struct btf *btf = ctx->btf;
  302. if (!btf || !ctx->last_type)
  303. return 0;
  304. /* char [] does not need any change. */
  305. if (btf_type_is_char_array(btf, ctx->last_type))
  306. return 0;
  307. /* char * requires dereference the pointer. */
  308. if (btf_type_is_char_ptr(btf, ctx->last_type)) {
  309. struct fetch_insn *code = *pcode + 1;
  310. if (code->op == FETCH_OP_END) {
  311. trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
  312. return -E2BIG;
  313. }
  314. if (typename[0] == 'u')
  315. code->op = FETCH_OP_UDEREF;
  316. else
  317. code->op = FETCH_OP_DEREF;
  318. code->offset = 0;
  319. *pcode = code;
  320. return 0;
  321. }
  322. /* Other types are not available for string */
  323. trace_probe_log_err(ctx->offset, BAD_TYPE4STR);
  324. return -EINVAL;
  325. }
  326. static const char *fetch_type_from_btf_type(struct btf *btf,
  327. const struct btf_type *type,
  328. struct traceprobe_parse_context *ctx)
  329. {
  330. u32 intdata;
  331. /* TODO: const char * could be converted as a string */
  332. switch (BTF_INFO_KIND(type->info)) {
  333. case BTF_KIND_ENUM:
  334. /* enum is "int", so convert to "s32" */
  335. return "s32";
  336. case BTF_KIND_ENUM64:
  337. return "s64";
  338. case BTF_KIND_PTR:
  339. /* pointer will be converted to "x??" */
  340. if (IS_ENABLED(CONFIG_64BIT))
  341. return "x64";
  342. else
  343. return "x32";
  344. case BTF_KIND_INT:
  345. intdata = btf_type_int(type);
  346. if (BTF_INT_ENCODING(intdata) & BTF_INT_SIGNED) {
  347. switch (BTF_INT_BITS(intdata)) {
  348. case 8:
  349. return "s8";
  350. case 16:
  351. return "s16";
  352. case 32:
  353. return "s32";
  354. case 64:
  355. return "s64";
  356. }
  357. } else { /* unsigned */
  358. switch (BTF_INT_BITS(intdata)) {
  359. case 8:
  360. return "u8";
  361. case 16:
  362. return "u16";
  363. case 32:
  364. return "u32";
  365. case 64:
  366. return "u64";
  367. }
  368. /* bitfield, size is encoded in the type */
  369. ctx->last_bitsize = BTF_INT_BITS(intdata);
  370. ctx->last_bitoffs += BTF_INT_OFFSET(intdata);
  371. return "u64";
  372. }
  373. }
  374. /* TODO: support other types */
  375. return NULL;
  376. }
  377. static int query_btf_context(struct traceprobe_parse_context *ctx)
  378. {
  379. const struct btf_param *param;
  380. const struct btf_type *type;
  381. struct btf *btf;
  382. s32 nr;
  383. if (ctx->btf)
  384. return 0;
  385. if (!ctx->funcname)
  386. return -EINVAL;
  387. type = btf_find_func_proto(ctx->funcname, &btf);
  388. if (!type)
  389. return -ENOENT;
  390. ctx->btf = btf;
  391. ctx->proto = type;
  392. /* ctx->params is optional, since func(void) will not have params. */
  393. nr = 0;
  394. param = btf_get_func_param(type, &nr);
  395. if (!IS_ERR_OR_NULL(param)) {
  396. /* Hide the first 'data' argument of tracepoint */
  397. if (ctx->flags & TPARG_FL_TPOINT) {
  398. nr--;
  399. param++;
  400. }
  401. }
  402. if (nr > 0) {
  403. ctx->nr_params = nr;
  404. ctx->params = param;
  405. } else {
  406. ctx->nr_params = 0;
  407. ctx->params = NULL;
  408. }
  409. return 0;
  410. }
  411. static void clear_btf_context(struct traceprobe_parse_context *ctx)
  412. {
  413. if (ctx->btf) {
  414. btf_put(ctx->btf);
  415. ctx->btf = NULL;
  416. ctx->proto = NULL;
  417. ctx->params = NULL;
  418. ctx->nr_params = 0;
  419. }
  420. }
  421. /* Return 1 if the field separater is arrow operator ('->') */
  422. static int split_next_field(char *varname, char **next_field,
  423. struct traceprobe_parse_context *ctx)
  424. {
  425. char *field;
  426. int ret = 0;
  427. field = strpbrk(varname, ".-");
  428. if (field) {
  429. if (field[0] == '-' && field[1] == '>') {
  430. field[0] = '\0';
  431. field += 2;
  432. ret = 1;
  433. } else if (field[0] == '.') {
  434. field[0] = '\0';
  435. field += 1;
  436. } else {
  437. trace_probe_log_err(ctx->offset + field - varname, BAD_HYPHEN);
  438. return -EINVAL;
  439. }
  440. *next_field = field;
  441. }
  442. return ret;
  443. }
  444. /*
  445. * Parse the field of data structure. The @type must be a pointer type
  446. * pointing the target data structure type.
  447. */
  448. static int parse_btf_field(char *fieldname, const struct btf_type *type,
  449. struct fetch_insn **pcode, struct fetch_insn *end,
  450. struct traceprobe_parse_context *ctx)
  451. {
  452. struct fetch_insn *code = *pcode;
  453. const struct btf_member *field;
  454. u32 bitoffs, anon_offs;
  455. char *next;
  456. int is_ptr;
  457. s32 tid;
  458. do {
  459. /* Outer loop for solving arrow operator ('->') */
  460. if (BTF_INFO_KIND(type->info) != BTF_KIND_PTR) {
  461. trace_probe_log_err(ctx->offset, NO_PTR_STRCT);
  462. return -EINVAL;
  463. }
  464. /* Convert a struct pointer type to a struct type */
  465. type = btf_type_skip_modifiers(ctx->btf, type->type, &tid);
  466. if (!type) {
  467. trace_probe_log_err(ctx->offset, BAD_BTF_TID);
  468. return -EINVAL;
  469. }
  470. bitoffs = 0;
  471. do {
  472. /* Inner loop for solving dot operator ('.') */
  473. next = NULL;
  474. is_ptr = split_next_field(fieldname, &next, ctx);
  475. if (is_ptr < 0)
  476. return is_ptr;
  477. anon_offs = 0;
  478. field = btf_find_struct_member(ctx->btf, type, fieldname,
  479. &anon_offs);
  480. if (IS_ERR(field)) {
  481. trace_probe_log_err(ctx->offset, BAD_BTF_TID);
  482. return PTR_ERR(field);
  483. }
  484. if (!field) {
  485. trace_probe_log_err(ctx->offset, NO_BTF_FIELD);
  486. return -ENOENT;
  487. }
  488. /* Add anonymous structure/union offset */
  489. bitoffs += anon_offs;
  490. /* Accumulate the bit-offsets of the dot-connected fields */
  491. if (btf_type_kflag(type)) {
  492. bitoffs += BTF_MEMBER_BIT_OFFSET(field->offset);
  493. ctx->last_bitsize = BTF_MEMBER_BITFIELD_SIZE(field->offset);
  494. } else {
  495. bitoffs += field->offset;
  496. ctx->last_bitsize = 0;
  497. }
  498. type = btf_type_skip_modifiers(ctx->btf, field->type, &tid);
  499. if (!type) {
  500. trace_probe_log_err(ctx->offset, BAD_BTF_TID);
  501. return -EINVAL;
  502. }
  503. ctx->offset += next - fieldname;
  504. fieldname = next;
  505. } while (!is_ptr && fieldname);
  506. if (++code == end) {
  507. trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
  508. return -EINVAL;
  509. }
  510. code->op = FETCH_OP_DEREF; /* TODO: user deref support */
  511. code->offset = bitoffs / 8;
  512. *pcode = code;
  513. ctx->last_bitoffs = bitoffs % 8;
  514. ctx->last_type = type;
  515. } while (fieldname);
  516. return 0;
  517. }
  518. static int __store_entry_arg(struct trace_probe *tp, int argnum);
  519. static int parse_btf_arg(char *varname,
  520. struct fetch_insn **pcode, struct fetch_insn *end,
  521. struct traceprobe_parse_context *ctx)
  522. {
  523. struct fetch_insn *code = *pcode;
  524. const struct btf_param *params;
  525. const struct btf_type *type;
  526. char *field = NULL;
  527. int i, is_ptr, ret;
  528. u32 tid;
  529. if (WARN_ON_ONCE(!ctx->funcname))
  530. return -EINVAL;
  531. is_ptr = split_next_field(varname, &field, ctx);
  532. if (is_ptr < 0)
  533. return is_ptr;
  534. if (!is_ptr && field) {
  535. /* dot-connected field on an argument is not supported. */
  536. trace_probe_log_err(ctx->offset + field - varname,
  537. NOSUP_DAT_ARG);
  538. return -EOPNOTSUPP;
  539. }
  540. if (ctx->flags & TPARG_FL_RETURN && !strcmp(varname, "$retval")) {
  541. code->op = FETCH_OP_RETVAL;
  542. /* Check whether the function return type is not void */
  543. if (query_btf_context(ctx) == 0) {
  544. if (ctx->proto->type == 0) {
  545. trace_probe_log_err(ctx->offset, NO_RETVAL);
  546. return -ENOENT;
  547. }
  548. tid = ctx->proto->type;
  549. goto found;
  550. }
  551. if (field) {
  552. trace_probe_log_err(ctx->offset + field - varname,
  553. NO_BTF_ENTRY);
  554. return -ENOENT;
  555. }
  556. return 0;
  557. }
  558. if (!ctx->btf) {
  559. ret = query_btf_context(ctx);
  560. if (ret < 0 || ctx->nr_params == 0) {
  561. trace_probe_log_err(ctx->offset, NO_BTF_ENTRY);
  562. return PTR_ERR(params);
  563. }
  564. }
  565. params = ctx->params;
  566. for (i = 0; i < ctx->nr_params; i++) {
  567. const char *name = btf_name_by_offset(ctx->btf, params[i].name_off);
  568. if (name && !strcmp(name, varname)) {
  569. if (tparg_is_function_entry(ctx->flags)) {
  570. code->op = FETCH_OP_ARG;
  571. if (ctx->flags & TPARG_FL_TPOINT)
  572. code->param = i + 1;
  573. else
  574. code->param = i;
  575. } else if (tparg_is_function_return(ctx->flags)) {
  576. code->op = FETCH_OP_EDATA;
  577. ret = __store_entry_arg(ctx->tp, i);
  578. if (ret < 0) {
  579. /* internal error */
  580. return ret;
  581. }
  582. code->offset = ret;
  583. }
  584. tid = params[i].type;
  585. goto found;
  586. }
  587. }
  588. trace_probe_log_err(ctx->offset, NO_BTFARG);
  589. return -ENOENT;
  590. found:
  591. type = btf_type_skip_modifiers(ctx->btf, tid, &tid);
  592. if (!type) {
  593. trace_probe_log_err(ctx->offset, BAD_BTF_TID);
  594. return -EINVAL;
  595. }
  596. /* Initialize the last type information */
  597. ctx->last_type = type;
  598. ctx->last_bitoffs = 0;
  599. ctx->last_bitsize = 0;
  600. if (field) {
  601. ctx->offset += field - varname;
  602. return parse_btf_field(field, type, pcode, end, ctx);
  603. }
  604. return 0;
  605. }
  606. static const struct fetch_type *find_fetch_type_from_btf_type(
  607. struct traceprobe_parse_context *ctx)
  608. {
  609. struct btf *btf = ctx->btf;
  610. const char *typestr = NULL;
  611. if (btf && ctx->last_type)
  612. typestr = fetch_type_from_btf_type(btf, ctx->last_type, ctx);
  613. return find_fetch_type(typestr, ctx->flags);
  614. }
  615. static int parse_btf_bitfield(struct fetch_insn **pcode,
  616. struct traceprobe_parse_context *ctx)
  617. {
  618. struct fetch_insn *code = *pcode;
  619. if ((ctx->last_bitsize % 8 == 0) && ctx->last_bitoffs == 0)
  620. return 0;
  621. code++;
  622. if (code->op != FETCH_OP_NOP) {
  623. trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
  624. return -EINVAL;
  625. }
  626. *pcode = code;
  627. code->op = FETCH_OP_MOD_BF;
  628. code->lshift = 64 - (ctx->last_bitsize + ctx->last_bitoffs);
  629. code->rshift = 64 - ctx->last_bitsize;
  630. code->basesize = 64 / 8;
  631. return 0;
  632. }
  633. #else
  634. static void clear_btf_context(struct traceprobe_parse_context *ctx)
  635. {
  636. ctx->btf = NULL;
  637. }
  638. static int query_btf_context(struct traceprobe_parse_context *ctx)
  639. {
  640. return -EOPNOTSUPP;
  641. }
  642. static int parse_btf_arg(char *varname,
  643. struct fetch_insn **pcode, struct fetch_insn *end,
  644. struct traceprobe_parse_context *ctx)
  645. {
  646. trace_probe_log_err(ctx->offset, NOSUP_BTFARG);
  647. return -EOPNOTSUPP;
  648. }
  649. static int parse_btf_bitfield(struct fetch_insn **pcode,
  650. struct traceprobe_parse_context *ctx)
  651. {
  652. trace_probe_log_err(ctx->offset, NOSUP_BTFARG);
  653. return -EOPNOTSUPP;
  654. }
  655. #define find_fetch_type_from_btf_type(ctx) \
  656. find_fetch_type(NULL, ctx->flags)
  657. static int check_prepare_btf_string_fetch(char *typename,
  658. struct fetch_insn **pcode,
  659. struct traceprobe_parse_context *ctx)
  660. {
  661. return 0;
  662. }
  663. #endif
  664. #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
  665. static int __store_entry_arg(struct trace_probe *tp, int argnum)
  666. {
  667. struct probe_entry_arg *earg = tp->entry_arg;
  668. bool match = false;
  669. int i, offset;
  670. if (!earg) {
  671. earg = kzalloc(sizeof(*tp->entry_arg), GFP_KERNEL);
  672. if (!earg)
  673. return -ENOMEM;
  674. earg->size = 2 * tp->nr_args + 1;
  675. earg->code = kcalloc(earg->size, sizeof(struct fetch_insn),
  676. GFP_KERNEL);
  677. if (!earg->code) {
  678. kfree(earg);
  679. return -ENOMEM;
  680. }
  681. /* Fill the code buffer with 'end' to simplify it */
  682. for (i = 0; i < earg->size; i++)
  683. earg->code[i].op = FETCH_OP_END;
  684. tp->entry_arg = earg;
  685. }
  686. offset = 0;
  687. for (i = 0; i < earg->size - 1; i++) {
  688. switch (earg->code[i].op) {
  689. case FETCH_OP_END:
  690. earg->code[i].op = FETCH_OP_ARG;
  691. earg->code[i].param = argnum;
  692. earg->code[i + 1].op = FETCH_OP_ST_EDATA;
  693. earg->code[i + 1].offset = offset;
  694. return offset;
  695. case FETCH_OP_ARG:
  696. match = (earg->code[i].param == argnum);
  697. break;
  698. case FETCH_OP_ST_EDATA:
  699. offset = earg->code[i].offset;
  700. if (match)
  701. return offset;
  702. offset += sizeof(unsigned long);
  703. break;
  704. default:
  705. break;
  706. }
  707. }
  708. return -ENOSPC;
  709. }
  710. int traceprobe_get_entry_data_size(struct trace_probe *tp)
  711. {
  712. struct probe_entry_arg *earg = tp->entry_arg;
  713. int i, size = 0;
  714. if (!earg)
  715. return 0;
  716. for (i = 0; i < earg->size; i++) {
  717. switch (earg->code[i].op) {
  718. case FETCH_OP_END:
  719. goto out;
  720. case FETCH_OP_ST_EDATA:
  721. size = earg->code[i].offset + sizeof(unsigned long);
  722. break;
  723. default:
  724. break;
  725. }
  726. }
  727. out:
  728. return size;
  729. }
  730. void store_trace_entry_data(void *edata, struct trace_probe *tp, struct pt_regs *regs)
  731. {
  732. struct probe_entry_arg *earg = tp->entry_arg;
  733. unsigned long val = 0;
  734. int i;
  735. if (!earg)
  736. return;
  737. for (i = 0; i < earg->size; i++) {
  738. struct fetch_insn *code = &earg->code[i];
  739. switch (code->op) {
  740. case FETCH_OP_ARG:
  741. val = regs_get_kernel_argument(regs, code->param);
  742. break;
  743. case FETCH_OP_ST_EDATA:
  744. *(unsigned long *)((unsigned long)edata + code->offset) = val;
  745. break;
  746. case FETCH_OP_END:
  747. goto end;
  748. default:
  749. break;
  750. }
  751. }
  752. end:
  753. return;
  754. }
  755. NOKPROBE_SYMBOL(store_trace_entry_data)
  756. #endif
  757. #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
  758. /* Parse $vars. @orig_arg points '$', which syncs to @ctx->offset */
  759. static int parse_probe_vars(char *orig_arg, const struct fetch_type *t,
  760. struct fetch_insn **pcode,
  761. struct fetch_insn *end,
  762. struct traceprobe_parse_context *ctx)
  763. {
  764. struct fetch_insn *code = *pcode;
  765. int err = TP_ERR_BAD_VAR;
  766. char *arg = orig_arg + 1;
  767. unsigned long param;
  768. int ret = 0;
  769. int len;
  770. if (ctx->flags & TPARG_FL_TEVENT) {
  771. if (code->data)
  772. return -EFAULT;
  773. ret = parse_trace_event_arg(arg, code, ctx);
  774. if (!ret)
  775. return 0;
  776. if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) {
  777. code->op = FETCH_OP_COMM;
  778. return 0;
  779. }
  780. /* backward compatibility */
  781. ctx->offset = 0;
  782. goto inval;
  783. }
  784. if (str_has_prefix(arg, "retval")) {
  785. if (!(ctx->flags & TPARG_FL_RETURN)) {
  786. err = TP_ERR_RETVAL_ON_PROBE;
  787. goto inval;
  788. }
  789. if (!(ctx->flags & TPARG_FL_KERNEL) ||
  790. !IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS)) {
  791. code->op = FETCH_OP_RETVAL;
  792. return 0;
  793. }
  794. return parse_btf_arg(orig_arg, pcode, end, ctx);
  795. }
  796. len = str_has_prefix(arg, "stack");
  797. if (len) {
  798. if (arg[len] == '\0') {
  799. code->op = FETCH_OP_STACKP;
  800. return 0;
  801. }
  802. if (isdigit(arg[len])) {
  803. ret = kstrtoul(arg + len, 10, &param);
  804. if (ret)
  805. goto inval;
  806. if ((ctx->flags & TPARG_FL_KERNEL) &&
  807. param > PARAM_MAX_STACK) {
  808. err = TP_ERR_BAD_STACK_NUM;
  809. goto inval;
  810. }
  811. code->op = FETCH_OP_STACK;
  812. code->param = (unsigned int)param;
  813. return 0;
  814. }
  815. goto inval;
  816. }
  817. if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) {
  818. code->op = FETCH_OP_COMM;
  819. return 0;
  820. }
  821. #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
  822. len = str_has_prefix(arg, "arg");
  823. if (len) {
  824. ret = kstrtoul(arg + len, 10, &param);
  825. if (ret)
  826. goto inval;
  827. if (!param || param > PARAM_MAX_STACK) {
  828. err = TP_ERR_BAD_ARG_NUM;
  829. goto inval;
  830. }
  831. param--; /* argN starts from 1, but internal arg[N] starts from 0 */
  832. if (tparg_is_function_entry(ctx->flags)) {
  833. code->op = FETCH_OP_ARG;
  834. code->param = (unsigned int)param;
  835. /*
  836. * The tracepoint probe will probe a stub function, and the
  837. * first parameter of the stub is a dummy and should be ignored.
  838. */
  839. if (ctx->flags & TPARG_FL_TPOINT)
  840. code->param++;
  841. } else if (tparg_is_function_return(ctx->flags)) {
  842. /* function entry argument access from return probe */
  843. ret = __store_entry_arg(ctx->tp, param);
  844. if (ret < 0) /* This error should be an internal error */
  845. return ret;
  846. code->op = FETCH_OP_EDATA;
  847. code->offset = ret;
  848. } else {
  849. err = TP_ERR_NOFENTRY_ARGS;
  850. goto inval;
  851. }
  852. return 0;
  853. }
  854. #endif
  855. inval:
  856. __trace_probe_log_err(ctx->offset, err);
  857. return -EINVAL;
  858. }
  859. static int str_to_immediate(char *str, unsigned long *imm)
  860. {
  861. if (isdigit(str[0]))
  862. return kstrtoul(str, 0, imm);
  863. else if (str[0] == '-')
  864. return kstrtol(str, 0, (long *)imm);
  865. else if (str[0] == '+')
  866. return kstrtol(str + 1, 0, (long *)imm);
  867. return -EINVAL;
  868. }
  869. static int __parse_imm_string(char *str, char **pbuf, int offs)
  870. {
  871. size_t len = strlen(str);
  872. if (str[len - 1] != '"') {
  873. trace_probe_log_err(offs + len, IMMSTR_NO_CLOSE);
  874. return -EINVAL;
  875. }
  876. *pbuf = kstrndup(str, len - 1, GFP_KERNEL);
  877. if (!*pbuf)
  878. return -ENOMEM;
  879. return 0;
  880. }
  881. /* Recursive argument parser */
  882. static int
  883. parse_probe_arg(char *arg, const struct fetch_type *type,
  884. struct fetch_insn **pcode, struct fetch_insn *end,
  885. struct traceprobe_parse_context *ctx)
  886. {
  887. struct fetch_insn *code = *pcode;
  888. unsigned long param;
  889. int deref = FETCH_OP_DEREF;
  890. long offset = 0;
  891. char *tmp;
  892. int ret = 0;
  893. switch (arg[0]) {
  894. case '$':
  895. ret = parse_probe_vars(arg, type, pcode, end, ctx);
  896. break;
  897. case '%': /* named register */
  898. if (ctx->flags & (TPARG_FL_TEVENT | TPARG_FL_FPROBE)) {
  899. /* eprobe and fprobe do not handle registers */
  900. trace_probe_log_err(ctx->offset, BAD_VAR);
  901. break;
  902. }
  903. ret = regs_query_register_offset(arg + 1);
  904. if (ret >= 0) {
  905. code->op = FETCH_OP_REG;
  906. code->param = (unsigned int)ret;
  907. ret = 0;
  908. } else
  909. trace_probe_log_err(ctx->offset, BAD_REG_NAME);
  910. break;
  911. case '@': /* memory, file-offset or symbol */
  912. if (isdigit(arg[1])) {
  913. ret = kstrtoul(arg + 1, 0, &param);
  914. if (ret) {
  915. trace_probe_log_err(ctx->offset, BAD_MEM_ADDR);
  916. break;
  917. }
  918. /* load address */
  919. code->op = FETCH_OP_IMM;
  920. code->immediate = param;
  921. } else if (arg[1] == '+') {
  922. /* kprobes don't support file offsets */
  923. if (ctx->flags & TPARG_FL_KERNEL) {
  924. trace_probe_log_err(ctx->offset, FILE_ON_KPROBE);
  925. return -EINVAL;
  926. }
  927. ret = kstrtol(arg + 2, 0, &offset);
  928. if (ret) {
  929. trace_probe_log_err(ctx->offset, BAD_FILE_OFFS);
  930. break;
  931. }
  932. code->op = FETCH_OP_FOFFS;
  933. code->immediate = (unsigned long)offset; // imm64?
  934. } else {
  935. /* uprobes don't support symbols */
  936. if (!(ctx->flags & TPARG_FL_KERNEL)) {
  937. trace_probe_log_err(ctx->offset, SYM_ON_UPROBE);
  938. return -EINVAL;
  939. }
  940. /* Preserve symbol for updating */
  941. code->op = FETCH_NOP_SYMBOL;
  942. code->data = kstrdup(arg + 1, GFP_KERNEL);
  943. if (!code->data)
  944. return -ENOMEM;
  945. if (++code == end) {
  946. trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
  947. return -EINVAL;
  948. }
  949. code->op = FETCH_OP_IMM;
  950. code->immediate = 0;
  951. }
  952. /* These are fetching from memory */
  953. if (++code == end) {
  954. trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
  955. return -EINVAL;
  956. }
  957. *pcode = code;
  958. code->op = FETCH_OP_DEREF;
  959. code->offset = offset;
  960. break;
  961. case '+': /* deref memory */
  962. case '-':
  963. if (arg[1] == 'u') {
  964. deref = FETCH_OP_UDEREF;
  965. arg[1] = arg[0];
  966. arg++;
  967. }
  968. if (arg[0] == '+')
  969. arg++; /* Skip '+', because kstrtol() rejects it. */
  970. tmp = strchr(arg, '(');
  971. if (!tmp) {
  972. trace_probe_log_err(ctx->offset, DEREF_NEED_BRACE);
  973. return -EINVAL;
  974. }
  975. *tmp = '\0';
  976. ret = kstrtol(arg, 0, &offset);
  977. if (ret) {
  978. trace_probe_log_err(ctx->offset, BAD_DEREF_OFFS);
  979. break;
  980. }
  981. ctx->offset += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0);
  982. arg = tmp + 1;
  983. tmp = strrchr(arg, ')');
  984. if (!tmp) {
  985. trace_probe_log_err(ctx->offset + strlen(arg),
  986. DEREF_OPEN_BRACE);
  987. return -EINVAL;
  988. } else {
  989. const struct fetch_type *t2 = find_fetch_type(NULL, ctx->flags);
  990. int cur_offs = ctx->offset;
  991. *tmp = '\0';
  992. ret = parse_probe_arg(arg, t2, &code, end, ctx);
  993. if (ret)
  994. break;
  995. ctx->offset = cur_offs;
  996. if (code->op == FETCH_OP_COMM ||
  997. code->op == FETCH_OP_DATA) {
  998. trace_probe_log_err(ctx->offset, COMM_CANT_DEREF);
  999. return -EINVAL;
  1000. }
  1001. if (++code == end) {
  1002. trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
  1003. return -EINVAL;
  1004. }
  1005. *pcode = code;
  1006. code->op = deref;
  1007. code->offset = offset;
  1008. /* Reset the last type if used */
  1009. ctx->last_type = NULL;
  1010. }
  1011. break;
  1012. case '\\': /* Immediate value */
  1013. if (arg[1] == '"') { /* Immediate string */
  1014. ret = __parse_imm_string(arg + 2, &tmp, ctx->offset + 2);
  1015. if (ret)
  1016. break;
  1017. code->op = FETCH_OP_DATA;
  1018. code->data = tmp;
  1019. } else {
  1020. ret = str_to_immediate(arg + 1, &code->immediate);
  1021. if (ret)
  1022. trace_probe_log_err(ctx->offset + 1, BAD_IMM);
  1023. else
  1024. code->op = FETCH_OP_IMM;
  1025. }
  1026. break;
  1027. default:
  1028. if (isalpha(arg[0]) || arg[0] == '_') { /* BTF variable */
  1029. if (!tparg_is_function_entry(ctx->flags) &&
  1030. !tparg_is_function_return(ctx->flags)) {
  1031. trace_probe_log_err(ctx->offset, NOSUP_BTFARG);
  1032. return -EINVAL;
  1033. }
  1034. ret = parse_btf_arg(arg, pcode, end, ctx);
  1035. break;
  1036. }
  1037. }
  1038. if (!ret && code->op == FETCH_OP_NOP) {
  1039. /* Parsed, but do not find fetch method */
  1040. trace_probe_log_err(ctx->offset, BAD_FETCH_ARG);
  1041. ret = -EINVAL;
  1042. }
  1043. return ret;
  1044. }
  1045. /* Bitfield type needs to be parsed into a fetch function */
  1046. static int __parse_bitfield_probe_arg(const char *bf,
  1047. const struct fetch_type *t,
  1048. struct fetch_insn **pcode)
  1049. {
  1050. struct fetch_insn *code = *pcode;
  1051. unsigned long bw, bo;
  1052. char *tail;
  1053. if (*bf != 'b')
  1054. return 0;
  1055. bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
  1056. if (bw == 0 || *tail != '@')
  1057. return -EINVAL;
  1058. bf = tail + 1;
  1059. bo = simple_strtoul(bf, &tail, 0);
  1060. if (tail == bf || *tail != '/')
  1061. return -EINVAL;
  1062. code++;
  1063. if (code->op != FETCH_OP_NOP)
  1064. return -EINVAL;
  1065. *pcode = code;
  1066. code->op = FETCH_OP_MOD_BF;
  1067. code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
  1068. code->rshift = BYTES_TO_BITS(t->size) - bw;
  1069. code->basesize = t->size;
  1070. return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
  1071. }
  1072. /* Split type part from @arg and return it. */
  1073. static char *parse_probe_arg_type(char *arg, struct probe_arg *parg,
  1074. struct traceprobe_parse_context *ctx)
  1075. {
  1076. char *t = NULL, *t2, *t3;
  1077. int offs;
  1078. t = strchr(arg, ':');
  1079. if (t) {
  1080. *t++ = '\0';
  1081. t2 = strchr(t, '[');
  1082. if (t2) {
  1083. *t2++ = '\0';
  1084. t3 = strchr(t2, ']');
  1085. if (!t3) {
  1086. offs = t2 + strlen(t2) - arg;
  1087. trace_probe_log_err(ctx->offset + offs,
  1088. ARRAY_NO_CLOSE);
  1089. return ERR_PTR(-EINVAL);
  1090. } else if (t3[1] != '\0') {
  1091. trace_probe_log_err(ctx->offset + t3 + 1 - arg,
  1092. BAD_ARRAY_SUFFIX);
  1093. return ERR_PTR(-EINVAL);
  1094. }
  1095. *t3 = '\0';
  1096. if (kstrtouint(t2, 0, &parg->count) || !parg->count) {
  1097. trace_probe_log_err(ctx->offset + t2 - arg,
  1098. BAD_ARRAY_NUM);
  1099. return ERR_PTR(-EINVAL);
  1100. }
  1101. if (parg->count > MAX_ARRAY_LEN) {
  1102. trace_probe_log_err(ctx->offset + t2 - arg,
  1103. ARRAY_TOO_BIG);
  1104. return ERR_PTR(-EINVAL);
  1105. }
  1106. }
  1107. }
  1108. offs = t ? t - arg : 0;
  1109. /*
  1110. * Since $comm and immediate string can not be dereferenced,
  1111. * we can find those by strcmp. But ignore for eprobes.
  1112. */
  1113. if (!(ctx->flags & TPARG_FL_TEVENT) &&
  1114. (strcmp(arg, "$comm") == 0 || strcmp(arg, "$COMM") == 0 ||
  1115. strncmp(arg, "\\\"", 2) == 0)) {
  1116. /* The type of $comm must be "string", and not an array type. */
  1117. if (parg->count || (t && strcmp(t, "string"))) {
  1118. trace_probe_log_err(ctx->offset + offs, NEED_STRING_TYPE);
  1119. return ERR_PTR(-EINVAL);
  1120. }
  1121. parg->type = find_fetch_type("string", ctx->flags);
  1122. } else
  1123. parg->type = find_fetch_type(t, ctx->flags);
  1124. if (!parg->type) {
  1125. trace_probe_log_err(ctx->offset + offs, BAD_TYPE);
  1126. return ERR_PTR(-EINVAL);
  1127. }
  1128. return t;
  1129. }
  1130. /* After parsing, adjust the fetch_insn according to the probe_arg */
  1131. static int finalize_fetch_insn(struct fetch_insn *code,
  1132. struct probe_arg *parg,
  1133. char *type,
  1134. int type_offset,
  1135. struct traceprobe_parse_context *ctx)
  1136. {
  1137. struct fetch_insn *scode;
  1138. int ret;
  1139. /* Store operation */
  1140. if (parg->type->is_string) {
  1141. /* Check bad combination of the type and the last fetch_insn. */
  1142. if (!strcmp(parg->type->name, "symstr")) {
  1143. if (code->op != FETCH_OP_REG && code->op != FETCH_OP_STACK &&
  1144. code->op != FETCH_OP_RETVAL && code->op != FETCH_OP_ARG &&
  1145. code->op != FETCH_OP_DEREF && code->op != FETCH_OP_TP_ARG) {
  1146. trace_probe_log_err(ctx->offset + type_offset,
  1147. BAD_SYMSTRING);
  1148. return -EINVAL;
  1149. }
  1150. } else {
  1151. if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF &&
  1152. code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM &&
  1153. code->op != FETCH_OP_DATA && code->op != FETCH_OP_TP_ARG) {
  1154. trace_probe_log_err(ctx->offset + type_offset,
  1155. BAD_STRING);
  1156. return -EINVAL;
  1157. }
  1158. }
  1159. if (!strcmp(parg->type->name, "symstr") ||
  1160. (code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM ||
  1161. code->op == FETCH_OP_DATA) || code->op == FETCH_OP_TP_ARG ||
  1162. parg->count) {
  1163. /*
  1164. * IMM, DATA and COMM is pointing actual address, those
  1165. * must be kept, and if parg->count != 0, this is an
  1166. * array of string pointers instead of string address
  1167. * itself.
  1168. * For the symstr, it doesn't need to dereference, thus
  1169. * it just get the value.
  1170. */
  1171. code++;
  1172. if (code->op != FETCH_OP_NOP) {
  1173. trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
  1174. return -EINVAL;
  1175. }
  1176. }
  1177. /* If op == DEREF, replace it with STRING */
  1178. if (!strcmp(parg->type->name, "ustring") ||
  1179. code->op == FETCH_OP_UDEREF)
  1180. code->op = FETCH_OP_ST_USTRING;
  1181. else if (!strcmp(parg->type->name, "symstr"))
  1182. code->op = FETCH_OP_ST_SYMSTR;
  1183. else
  1184. code->op = FETCH_OP_ST_STRING;
  1185. code->size = parg->type->size;
  1186. parg->dynamic = true;
  1187. } else if (code->op == FETCH_OP_DEREF) {
  1188. code->op = FETCH_OP_ST_MEM;
  1189. code->size = parg->type->size;
  1190. } else if (code->op == FETCH_OP_UDEREF) {
  1191. code->op = FETCH_OP_ST_UMEM;
  1192. code->size = parg->type->size;
  1193. } else {
  1194. code++;
  1195. if (code->op != FETCH_OP_NOP) {
  1196. trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
  1197. return -E2BIG;
  1198. }
  1199. code->op = FETCH_OP_ST_RAW;
  1200. code->size = parg->type->size;
  1201. }
  1202. /* Save storing fetch_insn. */
  1203. scode = code;
  1204. /* Modify operation */
  1205. if (type != NULL) {
  1206. /* Bitfield needs a special fetch_insn. */
  1207. ret = __parse_bitfield_probe_arg(type, parg->type, &code);
  1208. if (ret) {
  1209. trace_probe_log_err(ctx->offset + type_offset, BAD_BITFIELD);
  1210. return ret;
  1211. }
  1212. } else if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS) &&
  1213. ctx->last_type) {
  1214. /* If user not specified the type, try parsing BTF bitfield. */
  1215. ret = parse_btf_bitfield(&code, ctx);
  1216. if (ret)
  1217. return ret;
  1218. }
  1219. /* Loop(Array) operation */
  1220. if (parg->count) {
  1221. if (scode->op != FETCH_OP_ST_MEM &&
  1222. scode->op != FETCH_OP_ST_STRING &&
  1223. scode->op != FETCH_OP_ST_USTRING) {
  1224. trace_probe_log_err(ctx->offset + type_offset, BAD_STRING);
  1225. return -EINVAL;
  1226. }
  1227. code++;
  1228. if (code->op != FETCH_OP_NOP) {
  1229. trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
  1230. return -E2BIG;
  1231. }
  1232. code->op = FETCH_OP_LP_ARRAY;
  1233. code->param = parg->count;
  1234. }
  1235. /* Finalize the fetch_insn array. */
  1236. code++;
  1237. code->op = FETCH_OP_END;
  1238. return 0;
  1239. }
  1240. /* String length checking wrapper */
  1241. static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size,
  1242. struct probe_arg *parg,
  1243. struct traceprobe_parse_context *ctx)
  1244. {
  1245. struct fetch_insn *code, *tmp = NULL;
  1246. char *type, *arg;
  1247. int ret, len;
  1248. len = strlen(argv);
  1249. if (len > MAX_ARGSTR_LEN) {
  1250. trace_probe_log_err(ctx->offset, ARG_TOO_LONG);
  1251. return -E2BIG;
  1252. } else if (len == 0) {
  1253. trace_probe_log_err(ctx->offset, NO_ARG_BODY);
  1254. return -EINVAL;
  1255. }
  1256. arg = kstrdup(argv, GFP_KERNEL);
  1257. if (!arg)
  1258. return -ENOMEM;
  1259. parg->comm = kstrdup(arg, GFP_KERNEL);
  1260. if (!parg->comm) {
  1261. ret = -ENOMEM;
  1262. goto out;
  1263. }
  1264. type = parse_probe_arg_type(arg, parg, ctx);
  1265. if (IS_ERR(type)) {
  1266. ret = PTR_ERR(type);
  1267. goto out;
  1268. }
  1269. code = tmp = kcalloc(FETCH_INSN_MAX, sizeof(*code), GFP_KERNEL);
  1270. if (!code) {
  1271. ret = -ENOMEM;
  1272. goto out;
  1273. }
  1274. code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
  1275. ctx->last_type = NULL;
  1276. ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
  1277. ctx);
  1278. if (ret < 0)
  1279. goto fail;
  1280. /* Update storing type if BTF is available */
  1281. if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS) &&
  1282. ctx->last_type) {
  1283. if (!type) {
  1284. parg->type = find_fetch_type_from_btf_type(ctx);
  1285. } else if (strstr(type, "string")) {
  1286. ret = check_prepare_btf_string_fetch(type, &code, ctx);
  1287. if (ret)
  1288. goto fail;
  1289. }
  1290. }
  1291. parg->offset = *size;
  1292. *size += parg->type->size * (parg->count ?: 1);
  1293. if (parg->count) {
  1294. len = strlen(parg->type->fmttype) + 6;
  1295. parg->fmt = kmalloc(len, GFP_KERNEL);
  1296. if (!parg->fmt) {
  1297. ret = -ENOMEM;
  1298. goto fail;
  1299. }
  1300. snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype,
  1301. parg->count);
  1302. }
  1303. ret = finalize_fetch_insn(code, parg, type, type ? type - arg : 0, ctx);
  1304. if (ret < 0)
  1305. goto fail;
  1306. for (; code < tmp + FETCH_INSN_MAX; code++)
  1307. if (code->op == FETCH_OP_END)
  1308. break;
  1309. /* Shrink down the code buffer */
  1310. parg->code = kcalloc(code - tmp + 1, sizeof(*code), GFP_KERNEL);
  1311. if (!parg->code)
  1312. ret = -ENOMEM;
  1313. else
  1314. memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
  1315. fail:
  1316. if (ret < 0) {
  1317. for (code = tmp; code < tmp + FETCH_INSN_MAX; code++)
  1318. if (code->op == FETCH_NOP_SYMBOL ||
  1319. code->op == FETCH_OP_DATA)
  1320. kfree(code->data);
  1321. }
  1322. kfree(tmp);
  1323. out:
  1324. kfree(arg);
  1325. return ret;
  1326. }
  1327. /* Return 1 if name is reserved or already used by another argument */
  1328. static int traceprobe_conflict_field_name(const char *name,
  1329. struct probe_arg *args, int narg)
  1330. {
  1331. int i;
  1332. for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
  1333. if (strcmp(reserved_field_names[i], name) == 0)
  1334. return 1;
  1335. for (i = 0; i < narg; i++)
  1336. if (strcmp(args[i].name, name) == 0)
  1337. return 1;
  1338. return 0;
  1339. }
  1340. static char *generate_probe_arg_name(const char *arg, int idx)
  1341. {
  1342. char *name = NULL;
  1343. const char *end;
  1344. /*
  1345. * If argument name is omitted, try arg as a name (BTF variable)
  1346. * or "argN".
  1347. */
  1348. if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS)) {
  1349. end = strchr(arg, ':');
  1350. if (!end)
  1351. end = arg + strlen(arg);
  1352. name = kmemdup_nul(arg, end - arg, GFP_KERNEL);
  1353. if (!name || !is_good_name(name)) {
  1354. kfree(name);
  1355. name = NULL;
  1356. }
  1357. }
  1358. if (!name)
  1359. name = kasprintf(GFP_KERNEL, "arg%d", idx + 1);
  1360. return name;
  1361. }
  1362. int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, const char *arg,
  1363. struct traceprobe_parse_context *ctx)
  1364. {
  1365. struct probe_arg *parg = &tp->args[i];
  1366. const char *body;
  1367. ctx->tp = tp;
  1368. body = strchr(arg, '=');
  1369. if (body) {
  1370. if (body - arg > MAX_ARG_NAME_LEN) {
  1371. trace_probe_log_err(0, ARG_NAME_TOO_LONG);
  1372. return -EINVAL;
  1373. } else if (body == arg) {
  1374. trace_probe_log_err(0, NO_ARG_NAME);
  1375. return -EINVAL;
  1376. }
  1377. parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL);
  1378. body++;
  1379. } else {
  1380. parg->name = generate_probe_arg_name(arg, i);
  1381. body = arg;
  1382. }
  1383. if (!parg->name)
  1384. return -ENOMEM;
  1385. if (!is_good_name(parg->name)) {
  1386. trace_probe_log_err(0, BAD_ARG_NAME);
  1387. return -EINVAL;
  1388. }
  1389. if (traceprobe_conflict_field_name(parg->name, tp->args, i)) {
  1390. trace_probe_log_err(0, USED_ARG_NAME);
  1391. return -EINVAL;
  1392. }
  1393. ctx->offset = body - arg;
  1394. /* Parse fetch argument */
  1395. return traceprobe_parse_probe_arg_body(body, &tp->size, parg, ctx);
  1396. }
  1397. void traceprobe_free_probe_arg(struct probe_arg *arg)
  1398. {
  1399. struct fetch_insn *code = arg->code;
  1400. while (code && code->op != FETCH_OP_END) {
  1401. if (code->op == FETCH_NOP_SYMBOL ||
  1402. code->op == FETCH_OP_DATA)
  1403. kfree(code->data);
  1404. code++;
  1405. }
  1406. kfree(arg->code);
  1407. kfree(arg->name);
  1408. kfree(arg->comm);
  1409. kfree(arg->fmt);
  1410. }
  1411. static int argv_has_var_arg(int argc, const char *argv[], int *args_idx,
  1412. struct traceprobe_parse_context *ctx)
  1413. {
  1414. int i, found = 0;
  1415. for (i = 0; i < argc; i++)
  1416. if (str_has_prefix(argv[i], "$arg")) {
  1417. trace_probe_log_set_index(i + 2);
  1418. if (!tparg_is_function_entry(ctx->flags) &&
  1419. !tparg_is_function_return(ctx->flags)) {
  1420. trace_probe_log_err(0, NOFENTRY_ARGS);
  1421. return -EINVAL;
  1422. }
  1423. if (isdigit(argv[i][4])) {
  1424. found = 1;
  1425. continue;
  1426. }
  1427. if (argv[i][4] != '*') {
  1428. trace_probe_log_err(0, BAD_VAR);
  1429. return -EINVAL;
  1430. }
  1431. if (*args_idx >= 0 && *args_idx < argc) {
  1432. trace_probe_log_err(0, DOUBLE_ARGS);
  1433. return -EINVAL;
  1434. }
  1435. found = 1;
  1436. *args_idx = i;
  1437. }
  1438. return found;
  1439. }
  1440. static int sprint_nth_btf_arg(int idx, const char *type,
  1441. char *buf, int bufsize,
  1442. struct traceprobe_parse_context *ctx)
  1443. {
  1444. const char *name;
  1445. int ret;
  1446. if (idx >= ctx->nr_params) {
  1447. trace_probe_log_err(0, NO_BTFARG);
  1448. return -ENOENT;
  1449. }
  1450. name = btf_name_by_offset(ctx->btf, ctx->params[idx].name_off);
  1451. if (!name) {
  1452. trace_probe_log_err(0, NO_BTF_ENTRY);
  1453. return -ENOENT;
  1454. }
  1455. ret = snprintf(buf, bufsize, "%s%s", name, type);
  1456. if (ret >= bufsize) {
  1457. trace_probe_log_err(0, ARGS_2LONG);
  1458. return -E2BIG;
  1459. }
  1460. return ret;
  1461. }
  1462. /* Return new_argv which must be freed after use */
  1463. const char **traceprobe_expand_meta_args(int argc, const char *argv[],
  1464. int *new_argc, char *buf, int bufsize,
  1465. struct traceprobe_parse_context *ctx)
  1466. {
  1467. const struct btf_param *params = NULL;
  1468. int i, j, n, used, ret, args_idx = -1;
  1469. const char **new_argv = NULL;
  1470. ret = argv_has_var_arg(argc, argv, &args_idx, ctx);
  1471. if (ret < 0)
  1472. return ERR_PTR(ret);
  1473. if (!ret) {
  1474. *new_argc = argc;
  1475. return NULL;
  1476. }
  1477. ret = query_btf_context(ctx);
  1478. if (ret < 0 || ctx->nr_params == 0) {
  1479. if (args_idx != -1) {
  1480. /* $arg* requires BTF info */
  1481. trace_probe_log_err(0, NOSUP_BTFARG);
  1482. return (const char **)params;
  1483. }
  1484. *new_argc = argc;
  1485. return NULL;
  1486. }
  1487. if (args_idx >= 0)
  1488. *new_argc = argc + ctx->nr_params - 1;
  1489. else
  1490. *new_argc = argc;
  1491. new_argv = kcalloc(*new_argc, sizeof(char *), GFP_KERNEL);
  1492. if (!new_argv)
  1493. return ERR_PTR(-ENOMEM);
  1494. used = 0;
  1495. for (i = 0, j = 0; i < argc; i++) {
  1496. trace_probe_log_set_index(i + 2);
  1497. if (i == args_idx) {
  1498. for (n = 0; n < ctx->nr_params; n++) {
  1499. ret = sprint_nth_btf_arg(n, "", buf + used,
  1500. bufsize - used, ctx);
  1501. if (ret < 0)
  1502. goto error;
  1503. new_argv[j++] = buf + used;
  1504. used += ret + 1;
  1505. }
  1506. continue;
  1507. }
  1508. if (str_has_prefix(argv[i], "$arg")) {
  1509. char *type = NULL;
  1510. n = simple_strtoul(argv[i] + 4, &type, 10);
  1511. if (type && !(*type == ':' || *type == '\0')) {
  1512. trace_probe_log_err(0, BAD_VAR);
  1513. ret = -ENOENT;
  1514. goto error;
  1515. }
  1516. /* Note: $argN starts from $arg1 */
  1517. ret = sprint_nth_btf_arg(n - 1, type, buf + used,
  1518. bufsize - used, ctx);
  1519. if (ret < 0)
  1520. goto error;
  1521. new_argv[j++] = buf + used;
  1522. used += ret + 1;
  1523. } else
  1524. new_argv[j++] = argv[i];
  1525. }
  1526. return new_argv;
  1527. error:
  1528. kfree(new_argv);
  1529. return ERR_PTR(ret);
  1530. }
  1531. /* @buf: *buf must be equal to NULL. Caller must to free *buf */
  1532. int traceprobe_expand_dentry_args(int argc, const char *argv[], char **buf)
  1533. {
  1534. int i, used, ret;
  1535. const int bufsize = MAX_DENTRY_ARGS_LEN;
  1536. char *tmpbuf = NULL;
  1537. if (*buf)
  1538. return -EINVAL;
  1539. used = 0;
  1540. for (i = 0; i < argc; i++) {
  1541. char *tmp;
  1542. char *equal;
  1543. size_t arg_len;
  1544. if (!glob_match("*:%p[dD]", argv[i]))
  1545. continue;
  1546. if (!tmpbuf) {
  1547. tmpbuf = kmalloc(bufsize, GFP_KERNEL);
  1548. if (!tmpbuf)
  1549. return -ENOMEM;
  1550. }
  1551. tmp = kstrdup(argv[i], GFP_KERNEL);
  1552. if (!tmp)
  1553. goto nomem;
  1554. equal = strchr(tmp, '=');
  1555. if (equal)
  1556. *equal = '\0';
  1557. arg_len = strlen(argv[i]);
  1558. tmp[arg_len - 4] = '\0';
  1559. if (argv[i][arg_len - 1] == 'd')
  1560. ret = snprintf(tmpbuf + used, bufsize - used,
  1561. "%s%s+0x0(+0x%zx(%s)):string",
  1562. equal ? tmp : "", equal ? "=" : "",
  1563. offsetof(struct dentry, d_name.name),
  1564. equal ? equal + 1 : tmp);
  1565. else
  1566. ret = snprintf(tmpbuf + used, bufsize - used,
  1567. "%s%s+0x0(+0x%zx(+0x%zx(%s))):string",
  1568. equal ? tmp : "", equal ? "=" : "",
  1569. offsetof(struct dentry, d_name.name),
  1570. offsetof(struct file, f_path.dentry),
  1571. equal ? equal + 1 : tmp);
  1572. kfree(tmp);
  1573. if (ret >= bufsize - used)
  1574. goto nomem;
  1575. argv[i] = tmpbuf + used;
  1576. used += ret + 1;
  1577. }
  1578. *buf = tmpbuf;
  1579. return 0;
  1580. nomem:
  1581. kfree(tmpbuf);
  1582. return -ENOMEM;
  1583. }
  1584. void traceprobe_finish_parse(struct traceprobe_parse_context *ctx)
  1585. {
  1586. clear_btf_context(ctx);
  1587. }
  1588. int traceprobe_update_arg(struct probe_arg *arg)
  1589. {
  1590. struct fetch_insn *code = arg->code;
  1591. long offset;
  1592. char *tmp;
  1593. char c;
  1594. int ret = 0;
  1595. while (code && code->op != FETCH_OP_END) {
  1596. if (code->op == FETCH_NOP_SYMBOL) {
  1597. if (code[1].op != FETCH_OP_IMM)
  1598. return -EINVAL;
  1599. tmp = strpbrk(code->data, "+-");
  1600. if (tmp)
  1601. c = *tmp;
  1602. ret = traceprobe_split_symbol_offset(code->data,
  1603. &offset);
  1604. if (ret)
  1605. return ret;
  1606. code[1].immediate =
  1607. (unsigned long)kallsyms_lookup_name(code->data);
  1608. if (tmp)
  1609. *tmp = c;
  1610. if (!code[1].immediate)
  1611. return -ENOENT;
  1612. code[1].immediate += offset;
  1613. }
  1614. code++;
  1615. }
  1616. return 0;
  1617. }
  1618. /* When len=0, we just calculate the needed length */
  1619. #define LEN_OR_ZERO (len ? len - pos : 0)
  1620. static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
  1621. enum probe_print_type ptype)
  1622. {
  1623. struct probe_arg *parg;
  1624. int i, j;
  1625. int pos = 0;
  1626. const char *fmt, *arg;
  1627. switch (ptype) {
  1628. case PROBE_PRINT_NORMAL:
  1629. fmt = "(%lx)";
  1630. arg = ", REC->" FIELD_STRING_IP;
  1631. break;
  1632. case PROBE_PRINT_RETURN:
  1633. fmt = "(%lx <- %lx)";
  1634. arg = ", REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
  1635. break;
  1636. case PROBE_PRINT_EVENT:
  1637. fmt = "";
  1638. arg = "";
  1639. break;
  1640. default:
  1641. WARN_ON_ONCE(1);
  1642. return 0;
  1643. }
  1644. pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
  1645. for (i = 0; i < tp->nr_args; i++) {
  1646. parg = tp->args + i;
  1647. pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name);
  1648. if (parg->count) {
  1649. pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s",
  1650. parg->type->fmt);
  1651. for (j = 1; j < parg->count; j++)
  1652. pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s",
  1653. parg->type->fmt);
  1654. pos += snprintf(buf + pos, LEN_OR_ZERO, "}");
  1655. } else
  1656. pos += snprintf(buf + pos, LEN_OR_ZERO, "%s",
  1657. parg->type->fmt);
  1658. }
  1659. pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", arg);
  1660. for (i = 0; i < tp->nr_args; i++) {
  1661. parg = tp->args + i;
  1662. if (parg->count) {
  1663. if (parg->type->is_string)
  1664. fmt = ", __get_str(%s[%d])";
  1665. else
  1666. fmt = ", REC->%s[%d]";
  1667. for (j = 0; j < parg->count; j++)
  1668. pos += snprintf(buf + pos, LEN_OR_ZERO,
  1669. fmt, parg->name, j);
  1670. } else {
  1671. if (parg->type->is_string)
  1672. fmt = ", __get_str(%s)";
  1673. else
  1674. fmt = ", REC->%s";
  1675. pos += snprintf(buf + pos, LEN_OR_ZERO,
  1676. fmt, parg->name);
  1677. }
  1678. }
  1679. /* return the length of print_fmt */
  1680. return pos;
  1681. }
  1682. #undef LEN_OR_ZERO
  1683. int traceprobe_set_print_fmt(struct trace_probe *tp, enum probe_print_type ptype)
  1684. {
  1685. struct trace_event_call *call = trace_probe_event_call(tp);
  1686. int len;
  1687. char *print_fmt;
  1688. /* First: called with 0 length to calculate the needed length */
  1689. len = __set_print_fmt(tp, NULL, 0, ptype);
  1690. print_fmt = kmalloc(len + 1, GFP_KERNEL);
  1691. if (!print_fmt)
  1692. return -ENOMEM;
  1693. /* Second: actually write the @print_fmt */
  1694. __set_print_fmt(tp, print_fmt, len + 1, ptype);
  1695. call->print_fmt = print_fmt;
  1696. return 0;
  1697. }
  1698. int traceprobe_define_arg_fields(struct trace_event_call *event_call,
  1699. size_t offset, struct trace_probe *tp)
  1700. {
  1701. int ret, i;
  1702. /* Set argument names as fields */
  1703. for (i = 0; i < tp->nr_args; i++) {
  1704. struct probe_arg *parg = &tp->args[i];
  1705. const char *fmt = parg->type->fmttype;
  1706. int size = parg->type->size;
  1707. if (parg->fmt)
  1708. fmt = parg->fmt;
  1709. if (parg->count)
  1710. size *= parg->count;
  1711. ret = trace_define_field(event_call, fmt, parg->name,
  1712. offset + parg->offset, size,
  1713. parg->type->is_signed,
  1714. FILTER_OTHER);
  1715. if (ret)
  1716. return ret;
  1717. }
  1718. return 0;
  1719. }
  1720. static void trace_probe_event_free(struct trace_probe_event *tpe)
  1721. {
  1722. kfree(tpe->class.system);
  1723. kfree(tpe->call.name);
  1724. kfree(tpe->call.print_fmt);
  1725. kfree(tpe);
  1726. }
  1727. int trace_probe_append(struct trace_probe *tp, struct trace_probe *to)
  1728. {
  1729. if (trace_probe_has_sibling(tp))
  1730. return -EBUSY;
  1731. list_del_init(&tp->list);
  1732. trace_probe_event_free(tp->event);
  1733. tp->event = to->event;
  1734. list_add_tail(&tp->list, trace_probe_probe_list(to));
  1735. return 0;
  1736. }
  1737. void trace_probe_unlink(struct trace_probe *tp)
  1738. {
  1739. list_del_init(&tp->list);
  1740. if (list_empty(trace_probe_probe_list(tp)))
  1741. trace_probe_event_free(tp->event);
  1742. tp->event = NULL;
  1743. }
  1744. void trace_probe_cleanup(struct trace_probe *tp)
  1745. {
  1746. int i;
  1747. for (i = 0; i < tp->nr_args; i++)
  1748. traceprobe_free_probe_arg(&tp->args[i]);
  1749. if (tp->entry_arg) {
  1750. kfree(tp->entry_arg->code);
  1751. kfree(tp->entry_arg);
  1752. tp->entry_arg = NULL;
  1753. }
  1754. if (tp->event)
  1755. trace_probe_unlink(tp);
  1756. }
  1757. int trace_probe_init(struct trace_probe *tp, const char *event,
  1758. const char *group, bool alloc_filter, int nargs)
  1759. {
  1760. struct trace_event_call *call;
  1761. size_t size = sizeof(struct trace_probe_event);
  1762. int ret = 0;
  1763. if (!event || !group)
  1764. return -EINVAL;
  1765. if (alloc_filter)
  1766. size += sizeof(struct trace_uprobe_filter);
  1767. tp->event = kzalloc(size, GFP_KERNEL);
  1768. if (!tp->event)
  1769. return -ENOMEM;
  1770. INIT_LIST_HEAD(&tp->event->files);
  1771. INIT_LIST_HEAD(&tp->event->class.fields);
  1772. INIT_LIST_HEAD(&tp->event->probes);
  1773. INIT_LIST_HEAD(&tp->list);
  1774. list_add(&tp->list, &tp->event->probes);
  1775. call = trace_probe_event_call(tp);
  1776. call->class = &tp->event->class;
  1777. call->name = kstrdup(event, GFP_KERNEL);
  1778. if (!call->name) {
  1779. ret = -ENOMEM;
  1780. goto error;
  1781. }
  1782. tp->event->class.system = kstrdup(group, GFP_KERNEL);
  1783. if (!tp->event->class.system) {
  1784. ret = -ENOMEM;
  1785. goto error;
  1786. }
  1787. tp->nr_args = nargs;
  1788. /* Make sure pointers in args[] are NULL */
  1789. if (nargs)
  1790. memset(tp->args, 0, sizeof(tp->args[0]) * nargs);
  1791. return 0;
  1792. error:
  1793. trace_probe_cleanup(tp);
  1794. return ret;
  1795. }
  1796. static struct trace_event_call *
  1797. find_trace_event_call(const char *system, const char *event_name)
  1798. {
  1799. struct trace_event_call *tp_event;
  1800. const char *name;
  1801. list_for_each_entry(tp_event, &ftrace_events, list) {
  1802. if (!tp_event->class->system ||
  1803. strcmp(system, tp_event->class->system))
  1804. continue;
  1805. name = trace_event_name(tp_event);
  1806. if (!name || strcmp(event_name, name))
  1807. continue;
  1808. return tp_event;
  1809. }
  1810. return NULL;
  1811. }
  1812. int trace_probe_register_event_call(struct trace_probe *tp)
  1813. {
  1814. struct trace_event_call *call = trace_probe_event_call(tp);
  1815. int ret;
  1816. lockdep_assert_held(&event_mutex);
  1817. if (find_trace_event_call(trace_probe_group_name(tp),
  1818. trace_probe_name(tp)))
  1819. return -EEXIST;
  1820. ret = register_trace_event(&call->event);
  1821. if (!ret)
  1822. return -ENODEV;
  1823. ret = trace_add_event_call(call);
  1824. if (ret)
  1825. unregister_trace_event(&call->event);
  1826. return ret;
  1827. }
  1828. int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file)
  1829. {
  1830. struct event_file_link *link;
  1831. link = kmalloc(sizeof(*link), GFP_KERNEL);
  1832. if (!link)
  1833. return -ENOMEM;
  1834. link->file = file;
  1835. INIT_LIST_HEAD(&link->list);
  1836. list_add_tail_rcu(&link->list, &tp->event->files);
  1837. trace_probe_set_flag(tp, TP_FLAG_TRACE);
  1838. return 0;
  1839. }
  1840. struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
  1841. struct trace_event_file *file)
  1842. {
  1843. struct event_file_link *link;
  1844. trace_probe_for_each_link(link, tp) {
  1845. if (link->file == file)
  1846. return link;
  1847. }
  1848. return NULL;
  1849. }
  1850. int trace_probe_remove_file(struct trace_probe *tp,
  1851. struct trace_event_file *file)
  1852. {
  1853. struct event_file_link *link;
  1854. link = trace_probe_get_file_link(tp, file);
  1855. if (!link)
  1856. return -ENOENT;
  1857. list_del_rcu(&link->list);
  1858. kvfree_rcu_mightsleep(link);
  1859. if (list_empty(&tp->event->files))
  1860. trace_probe_clear_flag(tp, TP_FLAG_TRACE);
  1861. return 0;
  1862. }
  1863. /*
  1864. * Return the smallest index of different type argument (start from 1).
  1865. * If all argument types and name are same, return 0.
  1866. */
  1867. int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b)
  1868. {
  1869. int i;
  1870. /* In case of more arguments */
  1871. if (a->nr_args < b->nr_args)
  1872. return a->nr_args + 1;
  1873. if (a->nr_args > b->nr_args)
  1874. return b->nr_args + 1;
  1875. for (i = 0; i < a->nr_args; i++) {
  1876. if ((b->nr_args <= i) ||
  1877. ((a->args[i].type != b->args[i].type) ||
  1878. (a->args[i].count != b->args[i].count) ||
  1879. strcmp(a->args[i].name, b->args[i].name)))
  1880. return i + 1;
  1881. }
  1882. return 0;
  1883. }
  1884. bool trace_probe_match_command_args(struct trace_probe *tp,
  1885. int argc, const char **argv)
  1886. {
  1887. char buf[MAX_ARGSTR_LEN + 1];
  1888. int i;
  1889. if (tp->nr_args < argc)
  1890. return false;
  1891. for (i = 0; i < argc; i++) {
  1892. snprintf(buf, sizeof(buf), "%s=%s",
  1893. tp->args[i].name, tp->args[i].comm);
  1894. if (strcmp(buf, argv[i]))
  1895. return false;
  1896. }
  1897. return true;
  1898. }
  1899. int trace_probe_create(const char *raw_command, int (*createfn)(int, const char **))
  1900. {
  1901. int argc = 0, ret = 0;
  1902. char **argv;
  1903. argv = argv_split(GFP_KERNEL, raw_command, &argc);
  1904. if (!argv)
  1905. return -ENOMEM;
  1906. if (argc)
  1907. ret = createfn(argc, (const char **)argv);
  1908. argv_free(argv);
  1909. return ret;
  1910. }
  1911. int trace_probe_print_args(struct trace_seq *s, struct probe_arg *args, int nr_args,
  1912. u8 *data, void *field)
  1913. {
  1914. void *p;
  1915. int i, j;
  1916. for (i = 0; i < nr_args; i++) {
  1917. struct probe_arg *a = args + i;
  1918. trace_seq_printf(s, " %s=", a->name);
  1919. if (likely(!a->count)) {
  1920. if (!a->type->print(s, data + a->offset, field))
  1921. return -ENOMEM;
  1922. continue;
  1923. }
  1924. trace_seq_putc(s, '{');
  1925. p = data + a->offset;
  1926. for (j = 0; j < a->count; j++) {
  1927. if (!a->type->print(s, p, field))
  1928. return -ENOMEM;
  1929. trace_seq_putc(s, j == a->count - 1 ? '}' : ',');
  1930. p += a->type->size;
  1931. }
  1932. }
  1933. return 0;
  1934. }