trace_probe.c 52 KB

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