trace_events_synth.c 56 KB

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