trace_events_synth.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * trace_events_synth - synthetic trace events
  4. *
  5. * Copyright (C) 2015, 2020 Tom Zanussi <tom.zanussi@linux.intel.com>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/kallsyms.h>
  9. #include <linux/security.h>
  10. #include <linux/mutex.h>
  11. #include <linux/slab.h>
  12. #include <linux/stacktrace.h>
  13. #include <linux/rculist.h>
  14. #include <linux/tracefs.h>
  15. /* for gfp flag names */
  16. #include <linux/trace_events.h>
  17. #include <trace/events/mmflags.h>
  18. #include "trace_probe.h"
  19. #include "trace_probe_kernel.h"
  20. #include "trace_synth.h"
  21. #undef ERRORS
  22. #define ERRORS \
  23. C(BAD_NAME, "Illegal name"), \
  24. C(INVALID_CMD, "Command must be of the form: <name> field[;field] ..."),\
  25. C(INVALID_DYN_CMD, "Command must be of the form: s or -:[synthetic/]<name> field[;field] ..."),\
  26. C(EVENT_EXISTS, "Event already exists"), \
  27. C(TOO_MANY_FIELDS, "Too many fields"), \
  28. C(INCOMPLETE_TYPE, "Incomplete type"), \
  29. C(INVALID_TYPE, "Invalid type"), \
  30. C(INVALID_FIELD, "Invalid field"), \
  31. C(INVALID_ARRAY_SPEC, "Invalid array specification"),
  32. #undef C
  33. #define C(a, b) SYNTH_ERR_##a
  34. enum { ERRORS };
  35. #undef C
  36. #define C(a, b) b
  37. static const char *err_text[] = { ERRORS };
  38. static DEFINE_MUTEX(lastcmd_mutex);
  39. static char *last_cmd;
  40. static int errpos(const char *str)
  41. {
  42. int ret = 0;
  43. mutex_lock(&lastcmd_mutex);
  44. if (!str || !last_cmd)
  45. goto out;
  46. ret = err_pos(last_cmd, str);
  47. out:
  48. mutex_unlock(&lastcmd_mutex);
  49. return ret;
  50. }
  51. static void last_cmd_set(const char *str)
  52. {
  53. if (!str)
  54. return;
  55. mutex_lock(&lastcmd_mutex);
  56. kfree(last_cmd);
  57. last_cmd = kstrdup(str, GFP_KERNEL);
  58. mutex_unlock(&lastcmd_mutex);
  59. }
  60. static void synth_err(u8 err_type, u16 err_pos)
  61. {
  62. mutex_lock(&lastcmd_mutex);
  63. if (!last_cmd)
  64. goto out;
  65. tracing_log_err(NULL, "synthetic_events", last_cmd, err_text,
  66. err_type, err_pos);
  67. out:
  68. mutex_unlock(&lastcmd_mutex);
  69. }
  70. static int create_synth_event(const char *raw_command);
  71. static int synth_event_show(struct seq_file *m, struct dyn_event *ev);
  72. static int synth_event_release(struct dyn_event *ev);
  73. static bool synth_event_is_busy(struct dyn_event *ev);
  74. static bool synth_event_match(const char *system, const char *event,
  75. int argc, const char **argv, struct dyn_event *ev);
  76. static struct dyn_event_operations synth_event_ops = {
  77. .create = create_synth_event,
  78. .show = synth_event_show,
  79. .is_busy = synth_event_is_busy,
  80. .free = synth_event_release,
  81. .match = synth_event_match,
  82. };
  83. static bool is_synth_event(struct dyn_event *ev)
  84. {
  85. return ev->ops == &synth_event_ops;
  86. }
  87. static struct synth_event *to_synth_event(struct dyn_event *ev)
  88. {
  89. return container_of(ev, struct synth_event, devent);
  90. }
  91. static bool synth_event_is_busy(struct dyn_event *ev)
  92. {
  93. struct synth_event *event = to_synth_event(ev);
  94. return event->ref != 0;
  95. }
  96. static bool synth_event_match(const char *system, const char *event,
  97. int argc, const char **argv, struct dyn_event *ev)
  98. {
  99. struct synth_event *sev = to_synth_event(ev);
  100. return strcmp(sev->name, event) == 0 &&
  101. (!system || strcmp(system, SYNTH_SYSTEM) == 0);
  102. }
  103. struct synth_trace_event {
  104. struct trace_entry ent;
  105. union trace_synth_field fields[];
  106. };
  107. static int synth_event_define_fields(struct trace_event_call *call)
  108. {
  109. struct synth_trace_event trace;
  110. int offset = offsetof(typeof(trace), fields);
  111. struct synth_event *event = call->data;
  112. unsigned int i, size, n_u64;
  113. char *name, *type;
  114. bool is_signed;
  115. int ret = 0;
  116. for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
  117. size = event->fields[i]->size;
  118. is_signed = event->fields[i]->is_signed;
  119. type = event->fields[i]->type;
  120. name = event->fields[i]->name;
  121. ret = trace_define_field(call, type, name, offset, size,
  122. is_signed, FILTER_OTHER);
  123. if (ret)
  124. break;
  125. event->fields[i]->offset = n_u64;
  126. if (event->fields[i]->is_string && !event->fields[i]->is_dynamic) {
  127. offset += STR_VAR_LEN_MAX;
  128. n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
  129. } else {
  130. offset += sizeof(u64);
  131. n_u64++;
  132. }
  133. }
  134. event->n_u64 = n_u64;
  135. return ret;
  136. }
  137. static bool synth_field_signed(char *type)
  138. {
  139. if (str_has_prefix(type, "u"))
  140. return false;
  141. if (strcmp(type, "gfp_t") == 0)
  142. return false;
  143. return true;
  144. }
  145. static int synth_field_is_string(char *type)
  146. {
  147. if (strstr(type, "char[") != NULL)
  148. return true;
  149. return false;
  150. }
  151. static int synth_field_is_stack(char *type)
  152. {
  153. if (strstr(type, "long[") != NULL)
  154. return true;
  155. return false;
  156. }
  157. static int synth_field_string_size(char *type)
  158. {
  159. char buf[4], *end, *start;
  160. unsigned int len;
  161. int size, err;
  162. start = strstr(type, "char[");
  163. if (start == NULL)
  164. return -EINVAL;
  165. start += sizeof("char[") - 1;
  166. end = strchr(type, ']');
  167. if (!end || end < start || type + strlen(type) > end + 1)
  168. return -EINVAL;
  169. len = end - start;
  170. if (len > 3)
  171. return -EINVAL;
  172. if (len == 0)
  173. return 0; /* variable-length string */
  174. strncpy(buf, start, len);
  175. buf[len] = '\0';
  176. err = kstrtouint(buf, 0, &size);
  177. if (err)
  178. return err;
  179. if (size > STR_VAR_LEN_MAX)
  180. return -EINVAL;
  181. return size;
  182. }
  183. static int synth_field_size(char *type)
  184. {
  185. int size = 0;
  186. if (strcmp(type, "s64") == 0)
  187. size = sizeof(s64);
  188. else if (strcmp(type, "u64") == 0)
  189. size = sizeof(u64);
  190. else if (strcmp(type, "s32") == 0)
  191. size = sizeof(s32);
  192. else if (strcmp(type, "u32") == 0)
  193. size = sizeof(u32);
  194. else if (strcmp(type, "s16") == 0)
  195. size = sizeof(s16);
  196. else if (strcmp(type, "u16") == 0)
  197. size = sizeof(u16);
  198. else if (strcmp(type, "s8") == 0)
  199. size = sizeof(s8);
  200. else if (strcmp(type, "u8") == 0)
  201. size = sizeof(u8);
  202. else if (strcmp(type, "char") == 0)
  203. size = sizeof(char);
  204. else if (strcmp(type, "unsigned char") == 0)
  205. size = sizeof(unsigned char);
  206. else if (strcmp(type, "int") == 0)
  207. size = sizeof(int);
  208. else if (strcmp(type, "unsigned int") == 0)
  209. size = sizeof(unsigned int);
  210. else if (strcmp(type, "long") == 0)
  211. size = sizeof(long);
  212. else if (strcmp(type, "unsigned long") == 0)
  213. size = sizeof(unsigned long);
  214. else if (strcmp(type, "bool") == 0)
  215. size = sizeof(bool);
  216. else if (strcmp(type, "pid_t") == 0)
  217. size = sizeof(pid_t);
  218. else if (strcmp(type, "gfp_t") == 0)
  219. size = sizeof(gfp_t);
  220. else if (synth_field_is_string(type))
  221. size = synth_field_string_size(type);
  222. else if (synth_field_is_stack(type))
  223. size = 0;
  224. return size;
  225. }
  226. static const char *synth_field_fmt(char *type)
  227. {
  228. const char *fmt = "%llu";
  229. if (strcmp(type, "s64") == 0)
  230. fmt = "%lld";
  231. else if (strcmp(type, "u64") == 0)
  232. fmt = "%llu";
  233. else if (strcmp(type, "s32") == 0)
  234. fmt = "%d";
  235. else if (strcmp(type, "u32") == 0)
  236. fmt = "%u";
  237. else if (strcmp(type, "s16") == 0)
  238. fmt = "%d";
  239. else if (strcmp(type, "u16") == 0)
  240. fmt = "%u";
  241. else if (strcmp(type, "s8") == 0)
  242. fmt = "%d";
  243. else if (strcmp(type, "u8") == 0)
  244. fmt = "%u";
  245. else if (strcmp(type, "char") == 0)
  246. fmt = "%d";
  247. else if (strcmp(type, "unsigned char") == 0)
  248. fmt = "%u";
  249. else if (strcmp(type, "int") == 0)
  250. fmt = "%d";
  251. else if (strcmp(type, "unsigned int") == 0)
  252. fmt = "%u";
  253. else if (strcmp(type, "long") == 0)
  254. fmt = "%ld";
  255. else if (strcmp(type, "unsigned long") == 0)
  256. fmt = "%lu";
  257. else if (strcmp(type, "bool") == 0)
  258. fmt = "%d";
  259. else if (strcmp(type, "pid_t") == 0)
  260. fmt = "%d";
  261. else if (strcmp(type, "gfp_t") == 0)
  262. fmt = "%x";
  263. else if (synth_field_is_string(type))
  264. fmt = "%s";
  265. else if (synth_field_is_stack(type))
  266. fmt = "%s";
  267. return fmt;
  268. }
  269. static void print_synth_event_num_val(struct trace_seq *s,
  270. char *print_fmt, char *name,
  271. int size, union trace_synth_field *val, char *space)
  272. {
  273. switch (size) {
  274. case 1:
  275. trace_seq_printf(s, print_fmt, name, val->as_u8, space);
  276. break;
  277. case 2:
  278. trace_seq_printf(s, print_fmt, name, val->as_u16, space);
  279. break;
  280. case 4:
  281. trace_seq_printf(s, print_fmt, name, val->as_u32, space);
  282. break;
  283. default:
  284. trace_seq_printf(s, print_fmt, name, val->as_u64, space);
  285. break;
  286. }
  287. }
  288. static enum print_line_t print_synth_event(struct trace_iterator *iter,
  289. int flags,
  290. struct trace_event *event)
  291. {
  292. struct trace_array *tr = iter->tr;
  293. struct trace_seq *s = &iter->seq;
  294. struct synth_trace_event *entry;
  295. struct synth_event *se;
  296. unsigned int i, j, n_u64;
  297. char print_fmt[32];
  298. const char *fmt;
  299. entry = (struct synth_trace_event *)iter->ent;
  300. se = container_of(event, struct synth_event, call.event);
  301. trace_seq_printf(s, "%s: ", se->name);
  302. for (i = 0, n_u64 = 0; i < se->n_fields; i++) {
  303. if (trace_seq_has_overflowed(s))
  304. goto end;
  305. fmt = synth_field_fmt(se->fields[i]->type);
  306. /* parameter types */
  307. if (tr && tr->trace_flags & TRACE_ITER_VERBOSE)
  308. trace_seq_printf(s, "%s ", fmt);
  309. snprintf(print_fmt, sizeof(print_fmt), "%%s=%s%%s", fmt);
  310. /* parameter values */
  311. if (se->fields[i]->is_string) {
  312. if (se->fields[i]->is_dynamic) {
  313. union trace_synth_field *data = &entry->fields[n_u64];
  314. trace_seq_printf(s, print_fmt, se->fields[i]->name,
  315. (char *)entry + data->as_dynamic.offset,
  316. i == se->n_fields - 1 ? "" : " ");
  317. n_u64++;
  318. } else {
  319. trace_seq_printf(s, print_fmt, se->fields[i]->name,
  320. STR_VAR_LEN_MAX,
  321. (char *)&entry->fields[n_u64].as_u64,
  322. i == se->n_fields - 1 ? "" : " ");
  323. n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
  324. }
  325. } else if (se->fields[i]->is_stack) {
  326. union trace_synth_field *data = &entry->fields[n_u64];
  327. unsigned long *p = (void *)entry + data->as_dynamic.offset;
  328. trace_seq_printf(s, "%s=STACK:\n", se->fields[i]->name);
  329. for (j = 1; j < data->as_dynamic.len / sizeof(long); j++)
  330. trace_seq_printf(s, "=> %pS\n", (void *)p[j]);
  331. n_u64++;
  332. } else {
  333. struct trace_print_flags __flags[] = {
  334. __def_gfpflag_names, {-1, NULL} };
  335. char *space = (i == se->n_fields - 1 ? "" : " ");
  336. print_synth_event_num_val(s, print_fmt,
  337. se->fields[i]->name,
  338. se->fields[i]->size,
  339. &entry->fields[n_u64],
  340. space);
  341. if (strcmp(se->fields[i]->type, "gfp_t") == 0) {
  342. trace_seq_puts(s, " (");
  343. trace_print_flags_seq(s, "|",
  344. entry->fields[n_u64].as_u64,
  345. __flags);
  346. trace_seq_putc(s, ')');
  347. }
  348. n_u64++;
  349. }
  350. }
  351. end:
  352. trace_seq_putc(s, '\n');
  353. return trace_handle_return(s);
  354. }
  355. static struct trace_event_functions synth_event_funcs = {
  356. .trace = print_synth_event
  357. };
  358. static unsigned int trace_string(struct synth_trace_event *entry,
  359. struct synth_event *event,
  360. char *str_val,
  361. bool is_dynamic,
  362. unsigned int data_size,
  363. unsigned int *n_u64)
  364. {
  365. unsigned int len = 0;
  366. char *str_field;
  367. int ret;
  368. if (is_dynamic) {
  369. union trace_synth_field *data = &entry->fields[*n_u64];
  370. len = fetch_store_strlen((unsigned long)str_val);
  371. data->as_dynamic.offset = struct_size(entry, fields, event->n_u64) + data_size;
  372. data->as_dynamic.len = len;
  373. ret = fetch_store_string((unsigned long)str_val, &entry->fields[*n_u64], entry);
  374. (*n_u64)++;
  375. } else {
  376. str_field = (char *)&entry->fields[*n_u64].as_u64;
  377. #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
  378. if ((unsigned long)str_val < TASK_SIZE)
  379. ret = strncpy_from_user_nofault(str_field, (const void __user *)str_val, STR_VAR_LEN_MAX);
  380. else
  381. #endif
  382. ret = strncpy_from_kernel_nofault(str_field, str_val, STR_VAR_LEN_MAX);
  383. if (ret < 0)
  384. strcpy(str_field, FAULT_STRING);
  385. (*n_u64) += STR_VAR_LEN_MAX / sizeof(u64);
  386. }
  387. return len;
  388. }
  389. static unsigned int trace_stack(struct synth_trace_event *entry,
  390. struct synth_event *event,
  391. long *stack,
  392. unsigned int data_size,
  393. unsigned int *n_u64)
  394. {
  395. union trace_synth_field *data = &entry->fields[*n_u64];
  396. unsigned int len;
  397. u32 data_offset;
  398. void *data_loc;
  399. data_offset = struct_size(entry, fields, event->n_u64);
  400. data_offset += data_size;
  401. for (len = 0; len < HIST_STACKTRACE_DEPTH; len++) {
  402. if (!stack[len])
  403. break;
  404. }
  405. len *= sizeof(long);
  406. /* Find the dynamic section to copy the stack into. */
  407. data_loc = (void *)entry + data_offset;
  408. memcpy(data_loc, stack, len);
  409. /* Fill in the field that holds the offset/len combo */
  410. data->as_dynamic.offset = data_offset;
  411. data->as_dynamic.len = len;
  412. (*n_u64)++;
  413. return len;
  414. }
  415. static notrace void trace_event_raw_event_synth(void *__data,
  416. u64 *var_ref_vals,
  417. unsigned int *var_ref_idx)
  418. {
  419. unsigned int i, n_u64, val_idx, len, data_size = 0;
  420. struct trace_event_file *trace_file = __data;
  421. struct synth_trace_event *entry;
  422. struct trace_event_buffer fbuffer;
  423. struct trace_buffer *buffer;
  424. struct synth_event *event;
  425. int fields_size = 0;
  426. event = trace_file->event_call->data;
  427. if (trace_trigger_soft_disabled(trace_file))
  428. return;
  429. fields_size = event->n_u64 * sizeof(u64);
  430. for (i = 0; i < event->n_dynamic_fields; i++) {
  431. unsigned int field_pos = event->dynamic_fields[i]->field_pos;
  432. char *str_val;
  433. val_idx = var_ref_idx[field_pos];
  434. str_val = (char *)(long)var_ref_vals[val_idx];
  435. if (event->dynamic_fields[i]->is_stack) {
  436. /* reserve one extra element for size */
  437. len = *((unsigned long *)str_val) + 1;
  438. len *= sizeof(unsigned long);
  439. } else {
  440. len = fetch_store_strlen((unsigned long)str_val);
  441. }
  442. fields_size += len;
  443. }
  444. /*
  445. * Avoid ring buffer recursion detection, as this event
  446. * is being performed within another event.
  447. */
  448. buffer = trace_file->tr->array_buffer.buffer;
  449. ring_buffer_nest_start(buffer);
  450. entry = trace_event_buffer_reserve(&fbuffer, trace_file,
  451. sizeof(*entry) + fields_size);
  452. if (!entry)
  453. goto out;
  454. for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
  455. val_idx = var_ref_idx[i];
  456. if (event->fields[i]->is_string) {
  457. char *str_val = (char *)(long)var_ref_vals[val_idx];
  458. len = trace_string(entry, event, str_val,
  459. event->fields[i]->is_dynamic,
  460. data_size, &n_u64);
  461. data_size += len; /* only dynamic string increments */
  462. } else if (event->fields[i]->is_stack) {
  463. long *stack = (long *)(long)var_ref_vals[val_idx];
  464. len = trace_stack(entry, event, stack,
  465. data_size, &n_u64);
  466. data_size += len;
  467. } else {
  468. struct synth_field *field = event->fields[i];
  469. u64 val = var_ref_vals[val_idx];
  470. switch (field->size) {
  471. case 1:
  472. entry->fields[n_u64].as_u8 = (u8)val;
  473. break;
  474. case 2:
  475. entry->fields[n_u64].as_u16 = (u16)val;
  476. break;
  477. case 4:
  478. entry->fields[n_u64].as_u32 = (u32)val;
  479. break;
  480. default:
  481. entry->fields[n_u64].as_u64 = val;
  482. break;
  483. }
  484. n_u64++;
  485. }
  486. }
  487. trace_event_buffer_commit(&fbuffer);
  488. out:
  489. ring_buffer_nest_end(buffer);
  490. }
  491. static void free_synth_event_print_fmt(struct trace_event_call *call)
  492. {
  493. if (call) {
  494. kfree(call->print_fmt);
  495. call->print_fmt = NULL;
  496. }
  497. }
  498. static int __set_synth_event_print_fmt(struct synth_event *event,
  499. char *buf, int len)
  500. {
  501. const char *fmt;
  502. int pos = 0;
  503. int i;
  504. /* When len=0, we just calculate the needed length */
  505. #define LEN_OR_ZERO (len ? len - pos : 0)
  506. pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
  507. for (i = 0; i < event->n_fields; i++) {
  508. fmt = synth_field_fmt(event->fields[i]->type);
  509. pos += snprintf(buf + pos, LEN_OR_ZERO, "%s=%s%s",
  510. event->fields[i]->name, fmt,
  511. i == event->n_fields - 1 ? "" : ", ");
  512. }
  513. pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
  514. for (i = 0; i < event->n_fields; i++) {
  515. if (event->fields[i]->is_string &&
  516. event->fields[i]->is_dynamic)
  517. pos += snprintf(buf + pos, LEN_OR_ZERO,
  518. ", __get_str(%s)", event->fields[i]->name);
  519. else if (event->fields[i]->is_stack)
  520. pos += snprintf(buf + pos, LEN_OR_ZERO,
  521. ", __get_stacktrace(%s)", event->fields[i]->name);
  522. else
  523. pos += snprintf(buf + pos, LEN_OR_ZERO,
  524. ", REC->%s", event->fields[i]->name);
  525. }
  526. #undef LEN_OR_ZERO
  527. /* return the length of print_fmt */
  528. return pos;
  529. }
  530. static int set_synth_event_print_fmt(struct trace_event_call *call)
  531. {
  532. struct synth_event *event = call->data;
  533. char *print_fmt;
  534. int len;
  535. /* First: called with 0 length to calculate the needed length */
  536. len = __set_synth_event_print_fmt(event, NULL, 0);
  537. print_fmt = kmalloc(len + 1, GFP_KERNEL);
  538. if (!print_fmt)
  539. return -ENOMEM;
  540. /* Second: actually write the @print_fmt */
  541. __set_synth_event_print_fmt(event, print_fmt, len + 1);
  542. call->print_fmt = print_fmt;
  543. return 0;
  544. }
  545. static void free_synth_field(struct synth_field *field)
  546. {
  547. kfree(field->type);
  548. kfree(field->name);
  549. kfree(field);
  550. }
  551. static int check_field_version(const char *prefix, const char *field_type,
  552. const char *field_name)
  553. {
  554. /*
  555. * For backward compatibility, the old synthetic event command
  556. * format did not require semicolons, and in order to not
  557. * break user space, that old format must still work. If a new
  558. * feature is added, then the format that uses the new feature
  559. * will be required to have semicolons, as nothing that uses
  560. * the old format would be using the new, yet to be created,
  561. * feature. When a new feature is added, this will detect it,
  562. * and return a number greater than 1, and require the format
  563. * to use semicolons.
  564. */
  565. return 1;
  566. }
  567. static struct synth_field *parse_synth_field(int argc, char **argv,
  568. int *consumed, int *field_version)
  569. {
  570. const char *prefix = NULL, *field_type = argv[0], *field_name, *array;
  571. struct synth_field *field;
  572. int len, ret = -ENOMEM;
  573. struct seq_buf s;
  574. ssize_t size;
  575. if (!strcmp(field_type, "unsigned")) {
  576. if (argc < 3) {
  577. synth_err(SYNTH_ERR_INCOMPLETE_TYPE, errpos(field_type));
  578. return ERR_PTR(-EINVAL);
  579. }
  580. prefix = "unsigned ";
  581. field_type = argv[1];
  582. field_name = argv[2];
  583. *consumed += 3;
  584. } else {
  585. field_name = argv[1];
  586. *consumed += 2;
  587. }
  588. if (!field_name) {
  589. synth_err(SYNTH_ERR_INVALID_FIELD, errpos(field_type));
  590. return ERR_PTR(-EINVAL);
  591. }
  592. *field_version = check_field_version(prefix, field_type, field_name);
  593. field = kzalloc(sizeof(*field), GFP_KERNEL);
  594. if (!field)
  595. return ERR_PTR(-ENOMEM);
  596. len = strlen(field_name);
  597. array = strchr(field_name, '[');
  598. if (array)
  599. len -= strlen(array);
  600. field->name = kmemdup_nul(field_name, len, GFP_KERNEL);
  601. if (!field->name)
  602. goto free;
  603. if (!is_good_name(field->name)) {
  604. synth_err(SYNTH_ERR_BAD_NAME, errpos(field_name));
  605. ret = -EINVAL;
  606. goto free;
  607. }
  608. len = strlen(field_type) + 1;
  609. if (array)
  610. len += strlen(array);
  611. if (prefix)
  612. len += strlen(prefix);
  613. field->type = kzalloc(len, GFP_KERNEL);
  614. if (!field->type)
  615. goto free;
  616. seq_buf_init(&s, field->type, len);
  617. if (prefix)
  618. seq_buf_puts(&s, prefix);
  619. seq_buf_puts(&s, field_type);
  620. if (array)
  621. seq_buf_puts(&s, array);
  622. if (WARN_ON_ONCE(!seq_buf_buffer_left(&s)))
  623. goto free;
  624. s.buffer[s.len] = '\0';
  625. size = synth_field_size(field->type);
  626. if (size < 0) {
  627. if (array)
  628. synth_err(SYNTH_ERR_INVALID_ARRAY_SPEC, errpos(field_name));
  629. else
  630. synth_err(SYNTH_ERR_INVALID_TYPE, errpos(field_type));
  631. ret = -EINVAL;
  632. goto free;
  633. } else if (size == 0) {
  634. if (synth_field_is_string(field->type) ||
  635. synth_field_is_stack(field->type)) {
  636. char *type;
  637. len = sizeof("__data_loc ") + strlen(field->type) + 1;
  638. type = kzalloc(len, GFP_KERNEL);
  639. if (!type)
  640. goto free;
  641. seq_buf_init(&s, type, len);
  642. seq_buf_puts(&s, "__data_loc ");
  643. seq_buf_puts(&s, field->type);
  644. if (WARN_ON_ONCE(!seq_buf_buffer_left(&s)))
  645. goto free;
  646. s.buffer[s.len] = '\0';
  647. kfree(field->type);
  648. field->type = type;
  649. field->is_dynamic = true;
  650. size = sizeof(u64);
  651. } else {
  652. synth_err(SYNTH_ERR_INVALID_TYPE, errpos(field_type));
  653. ret = -EINVAL;
  654. goto free;
  655. }
  656. }
  657. field->size = size;
  658. if (synth_field_is_string(field->type))
  659. field->is_string = true;
  660. else if (synth_field_is_stack(field->type))
  661. field->is_stack = true;
  662. field->is_signed = synth_field_signed(field->type);
  663. out:
  664. return field;
  665. free:
  666. free_synth_field(field);
  667. field = ERR_PTR(ret);
  668. goto out;
  669. }
  670. static void free_synth_tracepoint(struct tracepoint *tp)
  671. {
  672. if (!tp)
  673. return;
  674. kfree(tp->name);
  675. kfree(tp);
  676. }
  677. static struct tracepoint *alloc_synth_tracepoint(char *name)
  678. {
  679. struct tracepoint *tp;
  680. tp = kzalloc(sizeof(*tp), GFP_KERNEL);
  681. if (!tp)
  682. return ERR_PTR(-ENOMEM);
  683. tp->name = kstrdup(name, GFP_KERNEL);
  684. if (!tp->name) {
  685. kfree(tp);
  686. return ERR_PTR(-ENOMEM);
  687. }
  688. return tp;
  689. }
  690. struct synth_event *find_synth_event(const char *name)
  691. {
  692. struct dyn_event *pos;
  693. struct synth_event *event;
  694. for_each_dyn_event(pos) {
  695. if (!is_synth_event(pos))
  696. continue;
  697. event = to_synth_event(pos);
  698. if (strcmp(event->name, name) == 0)
  699. return event;
  700. }
  701. return NULL;
  702. }
  703. static struct trace_event_fields synth_event_fields_array[] = {
  704. { .type = TRACE_FUNCTION_TYPE,
  705. .define_fields = synth_event_define_fields },
  706. {}
  707. };
  708. static int synth_event_reg(struct trace_event_call *call,
  709. enum trace_reg type, void *data)
  710. {
  711. struct synth_event *event = container_of(call, struct synth_event, call);
  712. switch (type) {
  713. #ifdef CONFIG_PERF_EVENTS
  714. case TRACE_REG_PERF_REGISTER:
  715. #endif
  716. case TRACE_REG_REGISTER:
  717. if (!try_module_get(event->mod))
  718. return -EBUSY;
  719. break;
  720. default:
  721. break;
  722. }
  723. int ret = trace_event_reg(call, type, data);
  724. switch (type) {
  725. #ifdef CONFIG_PERF_EVENTS
  726. case TRACE_REG_PERF_UNREGISTER:
  727. #endif
  728. case TRACE_REG_UNREGISTER:
  729. module_put(event->mod);
  730. break;
  731. default:
  732. break;
  733. }
  734. return ret;
  735. }
  736. static int register_synth_event(struct synth_event *event)
  737. {
  738. struct trace_event_call *call = &event->call;
  739. int ret = 0;
  740. event->call.class = &event->class;
  741. event->class.system = kstrdup(SYNTH_SYSTEM, GFP_KERNEL);
  742. if (!event->class.system) {
  743. ret = -ENOMEM;
  744. goto out;
  745. }
  746. event->tp = alloc_synth_tracepoint(event->name);
  747. if (IS_ERR(event->tp)) {
  748. ret = PTR_ERR(event->tp);
  749. event->tp = NULL;
  750. goto out;
  751. }
  752. INIT_LIST_HEAD(&call->class->fields);
  753. call->event.funcs = &synth_event_funcs;
  754. call->class->fields_array = synth_event_fields_array;
  755. ret = register_trace_event(&call->event);
  756. if (!ret) {
  757. ret = -ENODEV;
  758. goto out;
  759. }
  760. call->flags = TRACE_EVENT_FL_TRACEPOINT;
  761. call->class->reg = synth_event_reg;
  762. call->class->probe = trace_event_raw_event_synth;
  763. call->data = event;
  764. call->tp = event->tp;
  765. ret = trace_add_event_call(call);
  766. if (ret) {
  767. pr_warn("Failed to register synthetic event: %s\n",
  768. trace_event_name(call));
  769. goto err;
  770. }
  771. ret = set_synth_event_print_fmt(call);
  772. /* unregister_trace_event() will be called inside */
  773. if (ret < 0)
  774. trace_remove_event_call(call);
  775. out:
  776. return ret;
  777. err:
  778. unregister_trace_event(&call->event);
  779. goto out;
  780. }
  781. static int unregister_synth_event(struct synth_event *event)
  782. {
  783. struct trace_event_call *call = &event->call;
  784. int ret;
  785. ret = trace_remove_event_call(call);
  786. return ret;
  787. }
  788. static void free_synth_event(struct synth_event *event)
  789. {
  790. unsigned int i;
  791. if (!event)
  792. return;
  793. for (i = 0; i < event->n_fields; i++)
  794. free_synth_field(event->fields[i]);
  795. kfree(event->fields);
  796. kfree(event->dynamic_fields);
  797. kfree(event->name);
  798. kfree(event->class.system);
  799. free_synth_tracepoint(event->tp);
  800. free_synth_event_print_fmt(&event->call);
  801. kfree(event);
  802. }
  803. static struct synth_event *alloc_synth_event(const char *name, int n_fields,
  804. struct synth_field **fields)
  805. {
  806. unsigned int i, j, n_dynamic_fields = 0;
  807. struct synth_event *event;
  808. event = kzalloc(sizeof(*event), GFP_KERNEL);
  809. if (!event) {
  810. event = ERR_PTR(-ENOMEM);
  811. goto out;
  812. }
  813. event->name = kstrdup(name, GFP_KERNEL);
  814. if (!event->name) {
  815. kfree(event);
  816. event = ERR_PTR(-ENOMEM);
  817. goto out;
  818. }
  819. event->fields = kcalloc(n_fields, sizeof(*event->fields), GFP_KERNEL);
  820. if (!event->fields) {
  821. free_synth_event(event);
  822. event = ERR_PTR(-ENOMEM);
  823. goto out;
  824. }
  825. for (i = 0; i < n_fields; i++)
  826. if (fields[i]->is_dynamic)
  827. n_dynamic_fields++;
  828. if (n_dynamic_fields) {
  829. event->dynamic_fields = kcalloc(n_dynamic_fields,
  830. sizeof(*event->dynamic_fields),
  831. GFP_KERNEL);
  832. if (!event->dynamic_fields) {
  833. free_synth_event(event);
  834. event = ERR_PTR(-ENOMEM);
  835. goto out;
  836. }
  837. }
  838. dyn_event_init(&event->devent, &synth_event_ops);
  839. for (i = 0, j = 0; i < n_fields; i++) {
  840. fields[i]->field_pos = i;
  841. event->fields[i] = fields[i];
  842. if (fields[i]->is_dynamic)
  843. event->dynamic_fields[j++] = fields[i];
  844. }
  845. event->n_dynamic_fields = j;
  846. event->n_fields = n_fields;
  847. out:
  848. return event;
  849. }
  850. static int synth_event_check_arg_fn(void *data)
  851. {
  852. struct dynevent_arg_pair *arg_pair = data;
  853. int size;
  854. size = synth_field_size((char *)arg_pair->lhs);
  855. if (size == 0) {
  856. if (strstr((char *)arg_pair->lhs, "["))
  857. return 0;
  858. }
  859. return size ? 0 : -EINVAL;
  860. }
  861. /**
  862. * synth_event_add_field - Add a new field to a synthetic event cmd
  863. * @cmd: A pointer to the dynevent_cmd struct representing the new event
  864. * @type: The type of the new field to add
  865. * @name: The name of the new field to add
  866. *
  867. * Add a new field to a synthetic event cmd object. Field ordering is in
  868. * the same order the fields are added.
  869. *
  870. * See synth_field_size() for available types. If field_name contains
  871. * [n] the field is considered to be an array.
  872. *
  873. * Return: 0 if successful, error otherwise.
  874. */
  875. int synth_event_add_field(struct dynevent_cmd *cmd, const char *type,
  876. const char *name)
  877. {
  878. struct dynevent_arg_pair arg_pair;
  879. int ret;
  880. if (cmd->type != DYNEVENT_TYPE_SYNTH)
  881. return -EINVAL;
  882. if (!type || !name)
  883. return -EINVAL;
  884. dynevent_arg_pair_init(&arg_pair, 0, ';');
  885. arg_pair.lhs = type;
  886. arg_pair.rhs = name;
  887. ret = dynevent_arg_pair_add(cmd, &arg_pair, synth_event_check_arg_fn);
  888. if (ret)
  889. return ret;
  890. if (++cmd->n_fields > SYNTH_FIELDS_MAX)
  891. ret = -EINVAL;
  892. return ret;
  893. }
  894. EXPORT_SYMBOL_GPL(synth_event_add_field);
  895. /**
  896. * synth_event_add_field_str - Add a new field to a synthetic event cmd
  897. * @cmd: A pointer to the dynevent_cmd struct representing the new event
  898. * @type_name: The type and name of the new field to add, as a single string
  899. *
  900. * Add a new field to a synthetic event cmd object, as a single
  901. * string. The @type_name string is expected to be of the form 'type
  902. * name', which will be appended by ';'. No sanity checking is done -
  903. * what's passed in is assumed to already be well-formed. Field
  904. * ordering is in the same order the fields are added.
  905. *
  906. * See synth_field_size() for available types. If field_name contains
  907. * [n] the field is considered to be an array.
  908. *
  909. * Return: 0 if successful, error otherwise.
  910. */
  911. int synth_event_add_field_str(struct dynevent_cmd *cmd, const char *type_name)
  912. {
  913. struct dynevent_arg arg;
  914. int ret;
  915. if (cmd->type != DYNEVENT_TYPE_SYNTH)
  916. return -EINVAL;
  917. if (!type_name)
  918. return -EINVAL;
  919. dynevent_arg_init(&arg, ';');
  920. arg.str = type_name;
  921. ret = dynevent_arg_add(cmd, &arg, NULL);
  922. if (ret)
  923. return ret;
  924. if (++cmd->n_fields > SYNTH_FIELDS_MAX)
  925. ret = -EINVAL;
  926. return ret;
  927. }
  928. EXPORT_SYMBOL_GPL(synth_event_add_field_str);
  929. /**
  930. * synth_event_add_fields - Add multiple fields to a synthetic event cmd
  931. * @cmd: A pointer to the dynevent_cmd struct representing the new event
  932. * @fields: An array of type/name field descriptions
  933. * @n_fields: The number of field descriptions contained in the fields array
  934. *
  935. * Add a new set of fields to a synthetic event cmd object. The event
  936. * fields that will be defined for the event should be passed in as an
  937. * array of struct synth_field_desc, and the number of elements in the
  938. * array passed in as n_fields. Field ordering will retain the
  939. * ordering given in the fields array.
  940. *
  941. * See synth_field_size() for available types. If field_name contains
  942. * [n] the field is considered to be an array.
  943. *
  944. * Return: 0 if successful, error otherwise.
  945. */
  946. int synth_event_add_fields(struct dynevent_cmd *cmd,
  947. struct synth_field_desc *fields,
  948. unsigned int n_fields)
  949. {
  950. unsigned int i;
  951. int ret = 0;
  952. for (i = 0; i < n_fields; i++) {
  953. if (fields[i].type == NULL || fields[i].name == NULL) {
  954. ret = -EINVAL;
  955. break;
  956. }
  957. ret = synth_event_add_field(cmd, fields[i].type, fields[i].name);
  958. if (ret)
  959. break;
  960. }
  961. return ret;
  962. }
  963. EXPORT_SYMBOL_GPL(synth_event_add_fields);
  964. /**
  965. * __synth_event_gen_cmd_start - Start a synthetic event command from arg list
  966. * @cmd: A pointer to the dynevent_cmd struct representing the new event
  967. * @name: The name of the synthetic event
  968. * @mod: The module creating the event, NULL if not created from a module
  969. * @...: Variable number of arg (pairs), one pair for each field
  970. *
  971. * NOTE: Users normally won't want to call this function directly, but
  972. * rather use the synth_event_gen_cmd_start() wrapper, which
  973. * automatically adds a NULL to the end of the arg list. If this
  974. * function is used directly, make sure the last arg in the variable
  975. * arg list is NULL.
  976. *
  977. * Generate a synthetic event command to be executed by
  978. * synth_event_gen_cmd_end(). This function can be used to generate
  979. * the complete command or only the first part of it; in the latter
  980. * case, synth_event_add_field(), synth_event_add_field_str(), or
  981. * synth_event_add_fields() can be used to add more fields following
  982. * this.
  983. *
  984. * There should be an even number variable args, each pair consisting
  985. * of a type followed by a field name.
  986. *
  987. * See synth_field_size() for available types. If field_name contains
  988. * [n] the field is considered to be an array.
  989. *
  990. * Return: 0 if successful, error otherwise.
  991. */
  992. int __synth_event_gen_cmd_start(struct dynevent_cmd *cmd, const char *name,
  993. struct module *mod, ...)
  994. {
  995. struct dynevent_arg arg;
  996. va_list args;
  997. int ret;
  998. cmd->event_name = name;
  999. cmd->private_data = mod;
  1000. if (cmd->type != DYNEVENT_TYPE_SYNTH)
  1001. return -EINVAL;
  1002. dynevent_arg_init(&arg, 0);
  1003. arg.str = name;
  1004. ret = dynevent_arg_add(cmd, &arg, NULL);
  1005. if (ret)
  1006. return ret;
  1007. va_start(args, mod);
  1008. for (;;) {
  1009. const char *type, *name;
  1010. type = va_arg(args, const char *);
  1011. if (!type)
  1012. break;
  1013. name = va_arg(args, const char *);
  1014. if (!name)
  1015. break;
  1016. if (++cmd->n_fields > SYNTH_FIELDS_MAX) {
  1017. ret = -EINVAL;
  1018. break;
  1019. }
  1020. ret = synth_event_add_field(cmd, type, name);
  1021. if (ret)
  1022. break;
  1023. }
  1024. va_end(args);
  1025. return ret;
  1026. }
  1027. EXPORT_SYMBOL_GPL(__synth_event_gen_cmd_start);
  1028. /**
  1029. * synth_event_gen_cmd_array_start - Start synthetic event command from an array
  1030. * @cmd: A pointer to the dynevent_cmd struct representing the new event
  1031. * @name: The name of the synthetic event
  1032. * @mod: The module creating the event, NULL if not created from a module
  1033. * @fields: An array of type/name field descriptions
  1034. * @n_fields: The number of field descriptions contained in the fields array
  1035. *
  1036. * Generate a synthetic event command to be executed by
  1037. * synth_event_gen_cmd_end(). This function can be used to generate
  1038. * the complete command or only the first part of it; in the latter
  1039. * case, synth_event_add_field(), synth_event_add_field_str(), or
  1040. * synth_event_add_fields() can be used to add more fields following
  1041. * this.
  1042. *
  1043. * The event fields that will be defined for the event should be
  1044. * passed in as an array of struct synth_field_desc, and the number of
  1045. * elements in the array passed in as n_fields. Field ordering will
  1046. * retain the ordering given in the fields array.
  1047. *
  1048. * See synth_field_size() for available types. If field_name contains
  1049. * [n] the field is considered to be an array.
  1050. *
  1051. * Return: 0 if successful, error otherwise.
  1052. */
  1053. int synth_event_gen_cmd_array_start(struct dynevent_cmd *cmd, const char *name,
  1054. struct module *mod,
  1055. struct synth_field_desc *fields,
  1056. unsigned int n_fields)
  1057. {
  1058. struct dynevent_arg arg;
  1059. unsigned int i;
  1060. int ret = 0;
  1061. cmd->event_name = name;
  1062. cmd->private_data = mod;
  1063. if (cmd->type != DYNEVENT_TYPE_SYNTH)
  1064. return -EINVAL;
  1065. if (n_fields > SYNTH_FIELDS_MAX)
  1066. return -EINVAL;
  1067. dynevent_arg_init(&arg, 0);
  1068. arg.str = name;
  1069. ret = dynevent_arg_add(cmd, &arg, NULL);
  1070. if (ret)
  1071. return ret;
  1072. for (i = 0; i < n_fields; i++) {
  1073. if (fields[i].type == NULL || fields[i].name == NULL)
  1074. return -EINVAL;
  1075. ret = synth_event_add_field(cmd, fields[i].type, fields[i].name);
  1076. if (ret)
  1077. break;
  1078. }
  1079. return ret;
  1080. }
  1081. EXPORT_SYMBOL_GPL(synth_event_gen_cmd_array_start);
  1082. static int __create_synth_event(const char *name, const char *raw_fields)
  1083. {
  1084. char **argv, *field_str, *tmp_fields, *saved_fields = NULL;
  1085. struct synth_field *field, *fields[SYNTH_FIELDS_MAX];
  1086. int consumed, cmd_version = 1, n_fields_this_loop;
  1087. int i, argc, n_fields = 0, ret = 0;
  1088. struct synth_event *event = NULL;
  1089. /*
  1090. * Argument syntax:
  1091. * - Add synthetic event: <event_name> field[;field] ...
  1092. * - Remove synthetic event: !<event_name> field[;field] ...
  1093. * where 'field' = type field_name
  1094. */
  1095. if (name[0] == '\0') {
  1096. synth_err(SYNTH_ERR_INVALID_CMD, 0);
  1097. return -EINVAL;
  1098. }
  1099. if (!is_good_name(name)) {
  1100. synth_err(SYNTH_ERR_BAD_NAME, errpos(name));
  1101. return -EINVAL;
  1102. }
  1103. mutex_lock(&event_mutex);
  1104. event = find_synth_event(name);
  1105. if (event) {
  1106. synth_err(SYNTH_ERR_EVENT_EXISTS, errpos(name));
  1107. ret = -EEXIST;
  1108. goto err;
  1109. }
  1110. tmp_fields = saved_fields = kstrdup(raw_fields, GFP_KERNEL);
  1111. if (!tmp_fields) {
  1112. ret = -ENOMEM;
  1113. goto err;
  1114. }
  1115. while ((field_str = strsep(&tmp_fields, ";")) != NULL) {
  1116. argv = argv_split(GFP_KERNEL, field_str, &argc);
  1117. if (!argv) {
  1118. ret = -ENOMEM;
  1119. goto err;
  1120. }
  1121. if (!argc) {
  1122. argv_free(argv);
  1123. continue;
  1124. }
  1125. n_fields_this_loop = 0;
  1126. consumed = 0;
  1127. while (argc > consumed) {
  1128. int field_version;
  1129. field = parse_synth_field(argc - consumed,
  1130. argv + consumed, &consumed,
  1131. &field_version);
  1132. if (IS_ERR(field)) {
  1133. ret = PTR_ERR(field);
  1134. goto err_free_arg;
  1135. }
  1136. /*
  1137. * Track the highest version of any field we
  1138. * found in the command.
  1139. */
  1140. if (field_version > cmd_version)
  1141. cmd_version = field_version;
  1142. /*
  1143. * Now sort out what is and isn't valid for
  1144. * each supported version.
  1145. *
  1146. * If we see more than 1 field per loop, it
  1147. * means we have multiple fields between
  1148. * semicolons, and that's something we no
  1149. * longer support in a version 2 or greater
  1150. * command.
  1151. */
  1152. if (cmd_version > 1 && n_fields_this_loop >= 1) {
  1153. synth_err(SYNTH_ERR_INVALID_CMD, errpos(field_str));
  1154. ret = -EINVAL;
  1155. goto err_free_arg;
  1156. }
  1157. if (n_fields == SYNTH_FIELDS_MAX) {
  1158. synth_err(SYNTH_ERR_TOO_MANY_FIELDS, 0);
  1159. ret = -EINVAL;
  1160. goto err_free_arg;
  1161. }
  1162. fields[n_fields++] = field;
  1163. n_fields_this_loop++;
  1164. }
  1165. argv_free(argv);
  1166. if (consumed < argc) {
  1167. synth_err(SYNTH_ERR_INVALID_CMD, 0);
  1168. ret = -EINVAL;
  1169. goto err;
  1170. }
  1171. }
  1172. if (n_fields == 0) {
  1173. synth_err(SYNTH_ERR_INVALID_CMD, 0);
  1174. ret = -EINVAL;
  1175. goto err;
  1176. }
  1177. event = alloc_synth_event(name, n_fields, fields);
  1178. if (IS_ERR(event)) {
  1179. ret = PTR_ERR(event);
  1180. event = NULL;
  1181. goto err;
  1182. }
  1183. ret = register_synth_event(event);
  1184. if (!ret)
  1185. dyn_event_add(&event->devent, &event->call);
  1186. else
  1187. free_synth_event(event);
  1188. out:
  1189. mutex_unlock(&event_mutex);
  1190. kfree(saved_fields);
  1191. return ret;
  1192. err_free_arg:
  1193. argv_free(argv);
  1194. err:
  1195. for (i = 0; i < n_fields; i++)
  1196. free_synth_field(fields[i]);
  1197. goto out;
  1198. }
  1199. /**
  1200. * synth_event_create - Create a new synthetic event
  1201. * @name: The name of the new synthetic event
  1202. * @fields: An array of type/name field descriptions
  1203. * @n_fields: The number of field descriptions contained in the fields array
  1204. * @mod: The module creating the event, NULL if not created from a module
  1205. *
  1206. * Create a new synthetic event with the given name under the
  1207. * trace/events/synthetic/ directory. The event fields that will be
  1208. * defined for the event should be passed in as an array of struct
  1209. * synth_field_desc, and the number elements in the array passed in as
  1210. * n_fields. Field ordering will retain the ordering given in the
  1211. * fields array.
  1212. *
  1213. * If the new synthetic event is being created from a module, the mod
  1214. * param must be non-NULL. This will ensure that the trace buffer
  1215. * won't contain unreadable events.
  1216. *
  1217. * The new synth event should be deleted using synth_event_delete()
  1218. * function. The new synthetic event can be generated from modules or
  1219. * other kernel code using trace_synth_event() and related functions.
  1220. *
  1221. * Return: 0 if successful, error otherwise.
  1222. */
  1223. int synth_event_create(const char *name, struct synth_field_desc *fields,
  1224. unsigned int n_fields, struct module *mod)
  1225. {
  1226. struct dynevent_cmd cmd;
  1227. char *buf;
  1228. int ret;
  1229. buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL);
  1230. if (!buf)
  1231. return -ENOMEM;
  1232. synth_event_cmd_init(&cmd, buf, MAX_DYNEVENT_CMD_LEN);
  1233. ret = synth_event_gen_cmd_array_start(&cmd, name, mod,
  1234. fields, n_fields);
  1235. if (ret)
  1236. goto out;
  1237. ret = synth_event_gen_cmd_end(&cmd);
  1238. out:
  1239. kfree(buf);
  1240. return ret;
  1241. }
  1242. EXPORT_SYMBOL_GPL(synth_event_create);
  1243. static int destroy_synth_event(struct synth_event *se)
  1244. {
  1245. int ret;
  1246. if (se->ref)
  1247. return -EBUSY;
  1248. if (trace_event_dyn_busy(&se->call))
  1249. return -EBUSY;
  1250. ret = unregister_synth_event(se);
  1251. if (!ret) {
  1252. dyn_event_remove(&se->devent);
  1253. free_synth_event(se);
  1254. }
  1255. return ret;
  1256. }
  1257. /**
  1258. * synth_event_delete - Delete a synthetic event
  1259. * @event_name: The name of the new synthetic event
  1260. *
  1261. * Delete a synthetic event that was created with synth_event_create().
  1262. *
  1263. * Return: 0 if successful, error otherwise.
  1264. */
  1265. int synth_event_delete(const char *event_name)
  1266. {
  1267. struct synth_event *se = NULL;
  1268. struct module *mod = NULL;
  1269. int ret = -ENOENT;
  1270. mutex_lock(&event_mutex);
  1271. se = find_synth_event(event_name);
  1272. if (se) {
  1273. mod = se->mod;
  1274. ret = destroy_synth_event(se);
  1275. }
  1276. mutex_unlock(&event_mutex);
  1277. if (mod) {
  1278. /*
  1279. * It is safest to reset the ring buffer if the module
  1280. * being unloaded registered any events that were
  1281. * used. The only worry is if a new module gets
  1282. * loaded, and takes on the same id as the events of
  1283. * this module. When printing out the buffer, traced
  1284. * events left over from this module may be passed to
  1285. * the new module events and unexpected results may
  1286. * occur.
  1287. */
  1288. tracing_reset_all_online_cpus();
  1289. }
  1290. return ret;
  1291. }
  1292. EXPORT_SYMBOL_GPL(synth_event_delete);
  1293. static int check_command(const char *raw_command)
  1294. {
  1295. char **argv = NULL, *cmd, *saved_cmd, *name_and_field;
  1296. int argc, ret = 0;
  1297. cmd = saved_cmd = kstrdup(raw_command, GFP_KERNEL);
  1298. if (!cmd)
  1299. return -ENOMEM;
  1300. name_and_field = strsep(&cmd, ";");
  1301. if (!name_and_field) {
  1302. ret = -EINVAL;
  1303. goto free;
  1304. }
  1305. if (name_and_field[0] == '!')
  1306. goto free;
  1307. argv = argv_split(GFP_KERNEL, name_and_field, &argc);
  1308. if (!argv) {
  1309. ret = -ENOMEM;
  1310. goto free;
  1311. }
  1312. argv_free(argv);
  1313. if (argc < 3)
  1314. ret = -EINVAL;
  1315. free:
  1316. kfree(saved_cmd);
  1317. return ret;
  1318. }
  1319. static int create_or_delete_synth_event(const char *raw_command)
  1320. {
  1321. char *name = NULL, *fields, *p;
  1322. int ret = 0;
  1323. raw_command = skip_spaces(raw_command);
  1324. if (raw_command[0] == '\0')
  1325. return ret;
  1326. last_cmd_set(raw_command);
  1327. ret = check_command(raw_command);
  1328. if (ret) {
  1329. synth_err(SYNTH_ERR_INVALID_CMD, 0);
  1330. return ret;
  1331. }
  1332. p = strpbrk(raw_command, " \t");
  1333. if (!p && raw_command[0] != '!') {
  1334. synth_err(SYNTH_ERR_INVALID_CMD, 0);
  1335. ret = -EINVAL;
  1336. goto free;
  1337. }
  1338. name = kmemdup_nul(raw_command, p ? p - raw_command : strlen(raw_command), GFP_KERNEL);
  1339. if (!name)
  1340. return -ENOMEM;
  1341. if (name[0] == '!') {
  1342. ret = synth_event_delete(name + 1);
  1343. goto free;
  1344. }
  1345. fields = skip_spaces(p);
  1346. ret = __create_synth_event(name, fields);
  1347. free:
  1348. kfree(name);
  1349. return ret;
  1350. }
  1351. static int synth_event_run_command(struct dynevent_cmd *cmd)
  1352. {
  1353. struct synth_event *se;
  1354. int ret;
  1355. ret = create_or_delete_synth_event(cmd->seq.buffer);
  1356. if (ret)
  1357. return ret;
  1358. se = find_synth_event(cmd->event_name);
  1359. if (WARN_ON(!se))
  1360. return -ENOENT;
  1361. se->mod = cmd->private_data;
  1362. return ret;
  1363. }
  1364. /**
  1365. * synth_event_cmd_init - Initialize a synthetic event command object
  1366. * @cmd: A pointer to the dynevent_cmd struct representing the new event
  1367. * @buf: A pointer to the buffer used to build the command
  1368. * @maxlen: The length of the buffer passed in @buf
  1369. *
  1370. * Initialize a synthetic event command object. Use this before
  1371. * calling any of the other dyenvent_cmd functions.
  1372. */
  1373. void synth_event_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen)
  1374. {
  1375. dynevent_cmd_init(cmd, buf, maxlen, DYNEVENT_TYPE_SYNTH,
  1376. synth_event_run_command);
  1377. }
  1378. EXPORT_SYMBOL_GPL(synth_event_cmd_init);
  1379. static inline int
  1380. __synth_event_trace_init(struct trace_event_file *file,
  1381. struct synth_event_trace_state *trace_state)
  1382. {
  1383. int ret = 0;
  1384. memset(trace_state, '\0', sizeof(*trace_state));
  1385. /*
  1386. * Normal event tracing doesn't get called at all unless the
  1387. * ENABLED bit is set (which attaches the probe thus allowing
  1388. * this code to be called, etc). Because this is called
  1389. * directly by the user, we don't have that but we still need
  1390. * to honor not logging when disabled. For the iterated
  1391. * trace case, we save the enabled state upon start and just
  1392. * ignore the following data calls.
  1393. */
  1394. if (!(file->flags & EVENT_FILE_FL_ENABLED) ||
  1395. trace_trigger_soft_disabled(file)) {
  1396. trace_state->disabled = true;
  1397. ret = -ENOENT;
  1398. goto out;
  1399. }
  1400. trace_state->event = file->event_call->data;
  1401. out:
  1402. return ret;
  1403. }
  1404. static inline int
  1405. __synth_event_trace_start(struct trace_event_file *file,
  1406. struct synth_event_trace_state *trace_state,
  1407. int dynamic_fields_size)
  1408. {
  1409. int entry_size, fields_size = 0;
  1410. int ret = 0;
  1411. fields_size = trace_state->event->n_u64 * sizeof(u64);
  1412. fields_size += dynamic_fields_size;
  1413. /*
  1414. * Avoid ring buffer recursion detection, as this event
  1415. * is being performed within another event.
  1416. */
  1417. trace_state->buffer = file->tr->array_buffer.buffer;
  1418. ring_buffer_nest_start(trace_state->buffer);
  1419. entry_size = sizeof(*trace_state->entry) + fields_size;
  1420. trace_state->entry = trace_event_buffer_reserve(&trace_state->fbuffer,
  1421. file,
  1422. entry_size);
  1423. if (!trace_state->entry) {
  1424. ring_buffer_nest_end(trace_state->buffer);
  1425. ret = -EINVAL;
  1426. }
  1427. return ret;
  1428. }
  1429. static inline void
  1430. __synth_event_trace_end(struct synth_event_trace_state *trace_state)
  1431. {
  1432. trace_event_buffer_commit(&trace_state->fbuffer);
  1433. ring_buffer_nest_end(trace_state->buffer);
  1434. }
  1435. /**
  1436. * synth_event_trace - Trace a synthetic event
  1437. * @file: The trace_event_file representing the synthetic event
  1438. * @n_vals: The number of values in vals
  1439. * @...: Variable number of args containing the event values
  1440. *
  1441. * Trace a synthetic event using the values passed in the variable
  1442. * argument list.
  1443. *
  1444. * The argument list should be a list 'n_vals' u64 values. The number
  1445. * of vals must match the number of field in the synthetic event, and
  1446. * must be in the same order as the synthetic event fields.
  1447. *
  1448. * All vals should be cast to u64, and string vals are just pointers
  1449. * to strings, cast to u64. Strings will be copied into space
  1450. * reserved in the event for the string, using these pointers.
  1451. *
  1452. * Return: 0 on success, err otherwise.
  1453. */
  1454. int synth_event_trace(struct trace_event_file *file, unsigned int n_vals, ...)
  1455. {
  1456. unsigned int i, n_u64, len, data_size = 0;
  1457. struct synth_event_trace_state state;
  1458. va_list args;
  1459. int ret;
  1460. ret = __synth_event_trace_init(file, &state);
  1461. if (ret) {
  1462. if (ret == -ENOENT)
  1463. ret = 0; /* just disabled, not really an error */
  1464. return ret;
  1465. }
  1466. if (state.event->n_dynamic_fields) {
  1467. va_start(args, n_vals);
  1468. for (i = 0; i < state.event->n_fields; i++) {
  1469. u64 val = va_arg(args, u64);
  1470. if (state.event->fields[i]->is_string &&
  1471. state.event->fields[i]->is_dynamic) {
  1472. char *str_val = (char *)(long)val;
  1473. data_size += strlen(str_val) + 1;
  1474. }
  1475. }
  1476. va_end(args);
  1477. }
  1478. ret = __synth_event_trace_start(file, &state, data_size);
  1479. if (ret)
  1480. return ret;
  1481. if (n_vals != state.event->n_fields) {
  1482. ret = -EINVAL;
  1483. goto out;
  1484. }
  1485. data_size = 0;
  1486. va_start(args, n_vals);
  1487. for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) {
  1488. u64 val;
  1489. val = va_arg(args, u64);
  1490. if (state.event->fields[i]->is_string) {
  1491. char *str_val = (char *)(long)val;
  1492. len = trace_string(state.entry, state.event, str_val,
  1493. state.event->fields[i]->is_dynamic,
  1494. data_size, &n_u64);
  1495. data_size += len; /* only dynamic string increments */
  1496. } else {
  1497. struct synth_field *field = state.event->fields[i];
  1498. switch (field->size) {
  1499. case 1:
  1500. state.entry->fields[n_u64].as_u8 = (u8)val;
  1501. break;
  1502. case 2:
  1503. state.entry->fields[n_u64].as_u16 = (u16)val;
  1504. break;
  1505. case 4:
  1506. state.entry->fields[n_u64].as_u32 = (u32)val;
  1507. break;
  1508. default:
  1509. state.entry->fields[n_u64].as_u64 = val;
  1510. break;
  1511. }
  1512. n_u64++;
  1513. }
  1514. }
  1515. va_end(args);
  1516. out:
  1517. __synth_event_trace_end(&state);
  1518. return ret;
  1519. }
  1520. EXPORT_SYMBOL_GPL(synth_event_trace);
  1521. /**
  1522. * synth_event_trace_array - Trace a synthetic event from an array
  1523. * @file: The trace_event_file representing the synthetic event
  1524. * @vals: Array of values
  1525. * @n_vals: The number of values in vals
  1526. *
  1527. * Trace a synthetic event using the values passed in as 'vals'.
  1528. *
  1529. * The 'vals' array is just an array of 'n_vals' u64. The number of
  1530. * vals must match the number of field in the synthetic event, and
  1531. * must be in the same order as the synthetic event fields.
  1532. *
  1533. * All vals should be cast to u64, and string vals are just pointers
  1534. * to strings, cast to u64. Strings will be copied into space
  1535. * reserved in the event for the string, using these pointers.
  1536. *
  1537. * Return: 0 on success, err otherwise.
  1538. */
  1539. int synth_event_trace_array(struct trace_event_file *file, u64 *vals,
  1540. unsigned int n_vals)
  1541. {
  1542. unsigned int i, n_u64, field_pos, len, data_size = 0;
  1543. struct synth_event_trace_state state;
  1544. char *str_val;
  1545. int ret;
  1546. ret = __synth_event_trace_init(file, &state);
  1547. if (ret) {
  1548. if (ret == -ENOENT)
  1549. ret = 0; /* just disabled, not really an error */
  1550. return ret;
  1551. }
  1552. if (state.event->n_dynamic_fields) {
  1553. for (i = 0; i < state.event->n_dynamic_fields; i++) {
  1554. field_pos = state.event->dynamic_fields[i]->field_pos;
  1555. str_val = (char *)(long)vals[field_pos];
  1556. len = strlen(str_val) + 1;
  1557. data_size += len;
  1558. }
  1559. }
  1560. ret = __synth_event_trace_start(file, &state, data_size);
  1561. if (ret)
  1562. return ret;
  1563. if (n_vals != state.event->n_fields) {
  1564. ret = -EINVAL;
  1565. goto out;
  1566. }
  1567. data_size = 0;
  1568. for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) {
  1569. if (state.event->fields[i]->is_string) {
  1570. char *str_val = (char *)(long)vals[i];
  1571. len = trace_string(state.entry, state.event, str_val,
  1572. state.event->fields[i]->is_dynamic,
  1573. data_size, &n_u64);
  1574. data_size += len; /* only dynamic string increments */
  1575. } else {
  1576. struct synth_field *field = state.event->fields[i];
  1577. u64 val = vals[i];
  1578. switch (field->size) {
  1579. case 1:
  1580. state.entry->fields[n_u64].as_u8 = (u8)val;
  1581. break;
  1582. case 2:
  1583. state.entry->fields[n_u64].as_u16 = (u16)val;
  1584. break;
  1585. case 4:
  1586. state.entry->fields[n_u64].as_u32 = (u32)val;
  1587. break;
  1588. default:
  1589. state.entry->fields[n_u64].as_u64 = val;
  1590. break;
  1591. }
  1592. n_u64++;
  1593. }
  1594. }
  1595. out:
  1596. __synth_event_trace_end(&state);
  1597. return ret;
  1598. }
  1599. EXPORT_SYMBOL_GPL(synth_event_trace_array);
  1600. /**
  1601. * synth_event_trace_start - Start piecewise synthetic event trace
  1602. * @file: The trace_event_file representing the synthetic event
  1603. * @trace_state: A pointer to object tracking the piecewise trace state
  1604. *
  1605. * Start the trace of a synthetic event field-by-field rather than all
  1606. * at once.
  1607. *
  1608. * This function 'opens' an event trace, which means space is reserved
  1609. * for the event in the trace buffer, after which the event's
  1610. * individual field values can be set through either
  1611. * synth_event_add_next_val() or synth_event_add_val().
  1612. *
  1613. * A pointer to a trace_state object is passed in, which will keep
  1614. * track of the current event trace state until the event trace is
  1615. * closed (and the event finally traced) using
  1616. * synth_event_trace_end().
  1617. *
  1618. * Note that synth_event_trace_end() must be called after all values
  1619. * have been added for each event trace, regardless of whether adding
  1620. * all field values succeeded or not.
  1621. *
  1622. * Note also that for a given event trace, all fields must be added
  1623. * using either synth_event_add_next_val() or synth_event_add_val()
  1624. * but not both together or interleaved.
  1625. *
  1626. * Return: 0 on success, err otherwise.
  1627. */
  1628. int synth_event_trace_start(struct trace_event_file *file,
  1629. struct synth_event_trace_state *trace_state)
  1630. {
  1631. int ret;
  1632. if (!trace_state)
  1633. return -EINVAL;
  1634. ret = __synth_event_trace_init(file, trace_state);
  1635. if (ret) {
  1636. if (ret == -ENOENT)
  1637. ret = 0; /* just disabled, not really an error */
  1638. return ret;
  1639. }
  1640. if (trace_state->event->n_dynamic_fields)
  1641. return -ENOTSUPP;
  1642. ret = __synth_event_trace_start(file, trace_state, 0);
  1643. return ret;
  1644. }
  1645. EXPORT_SYMBOL_GPL(synth_event_trace_start);
  1646. static int __synth_event_add_val(const char *field_name, u64 val,
  1647. struct synth_event_trace_state *trace_state)
  1648. {
  1649. struct synth_field *field = NULL;
  1650. struct synth_trace_event *entry;
  1651. struct synth_event *event;
  1652. int i, ret = 0;
  1653. if (!trace_state) {
  1654. ret = -EINVAL;
  1655. goto out;
  1656. }
  1657. /* can't mix add_next_synth_val() with add_synth_val() */
  1658. if (field_name) {
  1659. if (trace_state->add_next) {
  1660. ret = -EINVAL;
  1661. goto out;
  1662. }
  1663. trace_state->add_name = true;
  1664. } else {
  1665. if (trace_state->add_name) {
  1666. ret = -EINVAL;
  1667. goto out;
  1668. }
  1669. trace_state->add_next = true;
  1670. }
  1671. if (trace_state->disabled)
  1672. goto out;
  1673. event = trace_state->event;
  1674. if (trace_state->add_name) {
  1675. for (i = 0; i < event->n_fields; i++) {
  1676. field = event->fields[i];
  1677. if (strcmp(field->name, field_name) == 0)
  1678. break;
  1679. }
  1680. if (!field) {
  1681. ret = -EINVAL;
  1682. goto out;
  1683. }
  1684. } else {
  1685. if (trace_state->cur_field >= event->n_fields) {
  1686. ret = -EINVAL;
  1687. goto out;
  1688. }
  1689. field = event->fields[trace_state->cur_field++];
  1690. }
  1691. entry = trace_state->entry;
  1692. if (field->is_string) {
  1693. char *str_val = (char *)(long)val;
  1694. char *str_field;
  1695. if (field->is_dynamic) { /* add_val can't do dynamic strings */
  1696. ret = -EINVAL;
  1697. goto out;
  1698. }
  1699. if (!str_val) {
  1700. ret = -EINVAL;
  1701. goto out;
  1702. }
  1703. str_field = (char *)&entry->fields[field->offset];
  1704. strscpy(str_field, str_val, STR_VAR_LEN_MAX);
  1705. } else {
  1706. switch (field->size) {
  1707. case 1:
  1708. trace_state->entry->fields[field->offset].as_u8 = (u8)val;
  1709. break;
  1710. case 2:
  1711. trace_state->entry->fields[field->offset].as_u16 = (u16)val;
  1712. break;
  1713. case 4:
  1714. trace_state->entry->fields[field->offset].as_u32 = (u32)val;
  1715. break;
  1716. default:
  1717. trace_state->entry->fields[field->offset].as_u64 = val;
  1718. break;
  1719. }
  1720. }
  1721. out:
  1722. return ret;
  1723. }
  1724. /**
  1725. * synth_event_add_next_val - Add the next field's value to an open synth trace
  1726. * @val: The value to set the next field to
  1727. * @trace_state: A pointer to object tracking the piecewise trace state
  1728. *
  1729. * Set the value of the next field in an event that's been opened by
  1730. * synth_event_trace_start().
  1731. *
  1732. * The val param should be the value cast to u64. If the value points
  1733. * to a string, the val param should be a char * cast to u64.
  1734. *
  1735. * This function assumes all the fields in an event are to be set one
  1736. * after another - successive calls to this function are made, one for
  1737. * each field, in the order of the fields in the event, until all
  1738. * fields have been set. If you'd rather set each field individually
  1739. * without regard to ordering, synth_event_add_val() can be used
  1740. * instead.
  1741. *
  1742. * Note however that synth_event_add_next_val() and
  1743. * synth_event_add_val() can't be intermixed for a given event trace -
  1744. * one or the other but not both can be used at the same time.
  1745. *
  1746. * Note also that synth_event_trace_end() must be called after all
  1747. * values have been added for each event trace, regardless of whether
  1748. * adding all field values succeeded or not.
  1749. *
  1750. * Return: 0 on success, err otherwise.
  1751. */
  1752. int synth_event_add_next_val(u64 val,
  1753. struct synth_event_trace_state *trace_state)
  1754. {
  1755. return __synth_event_add_val(NULL, val, trace_state);
  1756. }
  1757. EXPORT_SYMBOL_GPL(synth_event_add_next_val);
  1758. /**
  1759. * synth_event_add_val - Add a named field's value to an open synth trace
  1760. * @field_name: The name of the synthetic event field value to set
  1761. * @val: The value to set the named field to
  1762. * @trace_state: A pointer to object tracking the piecewise trace state
  1763. *
  1764. * Set the value of the named field in an event that's been opened by
  1765. * synth_event_trace_start().
  1766. *
  1767. * The val param should be the value cast to u64. If the value points
  1768. * to a string, the val param should be a char * cast to u64.
  1769. *
  1770. * This function looks up the field name, and if found, sets the field
  1771. * to the specified value. This lookup makes this function more
  1772. * expensive than synth_event_add_next_val(), so use that or the
  1773. * none-piecewise synth_event_trace() instead if efficiency is more
  1774. * important.
  1775. *
  1776. * Note however that synth_event_add_next_val() and
  1777. * synth_event_add_val() can't be intermixed for a given event trace -
  1778. * one or the other but not both can be used at the same time.
  1779. *
  1780. * Note also that synth_event_trace_end() must be called after all
  1781. * values have been added for each event trace, regardless of whether
  1782. * adding all field values succeeded or not.
  1783. *
  1784. * Return: 0 on success, err otherwise.
  1785. */
  1786. int synth_event_add_val(const char *field_name, u64 val,
  1787. struct synth_event_trace_state *trace_state)
  1788. {
  1789. return __synth_event_add_val(field_name, val, trace_state);
  1790. }
  1791. EXPORT_SYMBOL_GPL(synth_event_add_val);
  1792. /**
  1793. * synth_event_trace_end - End piecewise synthetic event trace
  1794. * @trace_state: A pointer to object tracking the piecewise trace state
  1795. *
  1796. * End the trace of a synthetic event opened by
  1797. * synth_event_trace__start().
  1798. *
  1799. * This function 'closes' an event trace, which basically means that
  1800. * it commits the reserved event and cleans up other loose ends.
  1801. *
  1802. * A pointer to a trace_state object is passed in, which will keep
  1803. * track of the current event trace state opened with
  1804. * synth_event_trace_start().
  1805. *
  1806. * Note that this function must be called after all values have been
  1807. * added for each event trace, regardless of whether adding all field
  1808. * values succeeded or not.
  1809. *
  1810. * Return: 0 on success, err otherwise.
  1811. */
  1812. int synth_event_trace_end(struct synth_event_trace_state *trace_state)
  1813. {
  1814. if (!trace_state)
  1815. return -EINVAL;
  1816. __synth_event_trace_end(trace_state);
  1817. return 0;
  1818. }
  1819. EXPORT_SYMBOL_GPL(synth_event_trace_end);
  1820. static int create_synth_event(const char *raw_command)
  1821. {
  1822. char *fields, *p;
  1823. const char *name;
  1824. int len, ret = 0;
  1825. raw_command = skip_spaces(raw_command);
  1826. if (raw_command[0] == '\0')
  1827. return ret;
  1828. last_cmd_set(raw_command);
  1829. name = raw_command;
  1830. /* Don't try to process if not our system */
  1831. if (name[0] != 's' || name[1] != ':')
  1832. return -ECANCELED;
  1833. name += 2;
  1834. p = strpbrk(raw_command, " \t");
  1835. if (!p) {
  1836. synth_err(SYNTH_ERR_INVALID_CMD, 0);
  1837. return -EINVAL;
  1838. }
  1839. fields = skip_spaces(p);
  1840. /* This interface accepts group name prefix */
  1841. if (strchr(name, '/')) {
  1842. len = str_has_prefix(name, SYNTH_SYSTEM "/");
  1843. if (len == 0) {
  1844. synth_err(SYNTH_ERR_INVALID_DYN_CMD, 0);
  1845. return -EINVAL;
  1846. }
  1847. name += len;
  1848. }
  1849. len = name - raw_command;
  1850. ret = check_command(raw_command + len);
  1851. if (ret) {
  1852. synth_err(SYNTH_ERR_INVALID_CMD, 0);
  1853. return ret;
  1854. }
  1855. name = kmemdup_nul(raw_command + len, p - raw_command - len, GFP_KERNEL);
  1856. if (!name)
  1857. return -ENOMEM;
  1858. ret = __create_synth_event(name, fields);
  1859. kfree(name);
  1860. return ret;
  1861. }
  1862. static int synth_event_release(struct dyn_event *ev)
  1863. {
  1864. struct synth_event *event = to_synth_event(ev);
  1865. int ret;
  1866. if (event->ref)
  1867. return -EBUSY;
  1868. if (trace_event_dyn_busy(&event->call))
  1869. return -EBUSY;
  1870. ret = unregister_synth_event(event);
  1871. if (ret)
  1872. return ret;
  1873. dyn_event_remove(ev);
  1874. free_synth_event(event);
  1875. return 0;
  1876. }
  1877. static int __synth_event_show(struct seq_file *m, struct synth_event *event)
  1878. {
  1879. struct synth_field *field;
  1880. unsigned int i;
  1881. char *type, *t;
  1882. seq_printf(m, "%s\t", event->name);
  1883. for (i = 0; i < event->n_fields; i++) {
  1884. field = event->fields[i];
  1885. type = field->type;
  1886. t = strstr(type, "__data_loc");
  1887. if (t) { /* __data_loc belongs in format but not event desc */
  1888. t += sizeof("__data_loc");
  1889. type = t;
  1890. }
  1891. /* parameter values */
  1892. seq_printf(m, "%s %s%s", type, field->name,
  1893. i == event->n_fields - 1 ? "" : "; ");
  1894. }
  1895. seq_putc(m, '\n');
  1896. return 0;
  1897. }
  1898. static int synth_event_show(struct seq_file *m, struct dyn_event *ev)
  1899. {
  1900. struct synth_event *event = to_synth_event(ev);
  1901. seq_printf(m, "s:%s/", event->class.system);
  1902. return __synth_event_show(m, event);
  1903. }
  1904. static int synth_events_seq_show(struct seq_file *m, void *v)
  1905. {
  1906. struct dyn_event *ev = v;
  1907. if (!is_synth_event(ev))
  1908. return 0;
  1909. return __synth_event_show(m, to_synth_event(ev));
  1910. }
  1911. static const struct seq_operations synth_events_seq_op = {
  1912. .start = dyn_event_seq_start,
  1913. .next = dyn_event_seq_next,
  1914. .stop = dyn_event_seq_stop,
  1915. .show = synth_events_seq_show,
  1916. };
  1917. static int synth_events_open(struct inode *inode, struct file *file)
  1918. {
  1919. int ret;
  1920. ret = security_locked_down(LOCKDOWN_TRACEFS);
  1921. if (ret)
  1922. return ret;
  1923. if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
  1924. ret = dyn_events_release_all(&synth_event_ops);
  1925. if (ret < 0)
  1926. return ret;
  1927. }
  1928. return seq_open(file, &synth_events_seq_op);
  1929. }
  1930. static ssize_t synth_events_write(struct file *file,
  1931. const char __user *buffer,
  1932. size_t count, loff_t *ppos)
  1933. {
  1934. return trace_parse_run_command(file, buffer, count, ppos,
  1935. create_or_delete_synth_event);
  1936. }
  1937. static const struct file_operations synth_events_fops = {
  1938. .open = synth_events_open,
  1939. .write = synth_events_write,
  1940. .read = seq_read,
  1941. .llseek = seq_lseek,
  1942. .release = seq_release,
  1943. };
  1944. /*
  1945. * Register dynevent at core_initcall. This allows kernel to setup kprobe
  1946. * events in postcore_initcall without tracefs.
  1947. */
  1948. static __init int trace_events_synth_init_early(void)
  1949. {
  1950. int err = 0;
  1951. err = dyn_event_register(&synth_event_ops);
  1952. if (err)
  1953. pr_warn("Could not register synth_event_ops\n");
  1954. return err;
  1955. }
  1956. core_initcall(trace_events_synth_init_early);
  1957. static __init int trace_events_synth_init(void)
  1958. {
  1959. struct dentry *entry = NULL;
  1960. int err = 0;
  1961. err = tracing_init_dentry();
  1962. if (err)
  1963. goto err;
  1964. entry = tracefs_create_file("synthetic_events", TRACE_MODE_WRITE,
  1965. NULL, NULL, &synth_events_fops);
  1966. if (!entry) {
  1967. err = -ENODEV;
  1968. goto err;
  1969. }
  1970. return err;
  1971. err:
  1972. pr_warn("Could not create tracefs 'synthetic_events' entry\n");
  1973. return err;
  1974. }
  1975. fs_initcall(trace_events_synth_init);