trace_events_synth.c 56 KB

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