trace_events_trigger.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * trace_events_trigger - trace event triggers
  4. *
  5. * Copyright (C) 2013 Tom Zanussi <tom.zanussi@linux.intel.com>
  6. */
  7. #include <linux/security.h>
  8. #include <linux/module.h>
  9. #include <linux/ctype.h>
  10. #include <linux/mutex.h>
  11. #include <linux/slab.h>
  12. #include <linux/rculist.h>
  13. #include "trace.h"
  14. static LIST_HEAD(trigger_commands);
  15. static DEFINE_MUTEX(trigger_cmd_mutex);
  16. void trigger_data_free(struct event_trigger_data *data)
  17. {
  18. if (data->cmd_ops->set_filter)
  19. data->cmd_ops->set_filter(NULL, data, NULL);
  20. /* make sure current triggers exit before free */
  21. tracepoint_synchronize_unregister();
  22. kfree(data);
  23. }
  24. /**
  25. * event_triggers_call - Call triggers associated with a trace event
  26. * @file: The trace_event_file associated with the event
  27. * @buffer: The ring buffer that the event is being written to
  28. * @rec: The trace entry for the event, NULL for unconditional invocation
  29. * @event: The event meta data in the ring buffer
  30. *
  31. * For each trigger associated with an event, invoke the trigger
  32. * function registered with the associated trigger command. If rec is
  33. * non-NULL, it means that the trigger requires further processing and
  34. * shouldn't be unconditionally invoked. If rec is non-NULL and the
  35. * trigger has a filter associated with it, rec will checked against
  36. * the filter and if the record matches the trigger will be invoked.
  37. * If the trigger is a 'post_trigger', meaning it shouldn't be invoked
  38. * in any case until the current event is written, the trigger
  39. * function isn't invoked but the bit associated with the deferred
  40. * trigger is set in the return value.
  41. *
  42. * Returns an enum event_trigger_type value containing a set bit for
  43. * any trigger that should be deferred, ETT_NONE if nothing to defer.
  44. *
  45. * Called from tracepoint handlers (with rcu_read_lock_sched() held).
  46. *
  47. * Return: an enum event_trigger_type value containing a set bit for
  48. * any trigger that should be deferred, ETT_NONE if nothing to defer.
  49. */
  50. enum event_trigger_type
  51. event_triggers_call(struct trace_event_file *file,
  52. struct trace_buffer *buffer, void *rec,
  53. struct ring_buffer_event *event)
  54. {
  55. struct event_trigger_data *data;
  56. enum event_trigger_type tt = ETT_NONE;
  57. struct event_filter *filter;
  58. if (list_empty(&file->triggers))
  59. return tt;
  60. list_for_each_entry_rcu(data, &file->triggers, list) {
  61. if (data->paused)
  62. continue;
  63. if (!rec) {
  64. data->ops->trigger(data, buffer, rec, event);
  65. continue;
  66. }
  67. filter = rcu_dereference_sched(data->filter);
  68. if (filter && !filter_match_preds(filter, rec))
  69. continue;
  70. if (event_command_post_trigger(data->cmd_ops)) {
  71. tt |= data->cmd_ops->trigger_type;
  72. continue;
  73. }
  74. data->ops->trigger(data, buffer, rec, event);
  75. }
  76. return tt;
  77. }
  78. EXPORT_SYMBOL_GPL(event_triggers_call);
  79. bool __trace_trigger_soft_disabled(struct trace_event_file *file)
  80. {
  81. unsigned long eflags = file->flags;
  82. if (eflags & EVENT_FILE_FL_TRIGGER_MODE)
  83. event_triggers_call(file, NULL, NULL, NULL);
  84. if (eflags & EVENT_FILE_FL_SOFT_DISABLED)
  85. return true;
  86. if (eflags & EVENT_FILE_FL_PID_FILTER)
  87. return trace_event_ignore_this_pid(file);
  88. return false;
  89. }
  90. EXPORT_SYMBOL_GPL(__trace_trigger_soft_disabled);
  91. /**
  92. * event_triggers_post_call - Call 'post_triggers' for a trace event
  93. * @file: The trace_event_file associated with the event
  94. * @tt: enum event_trigger_type containing a set bit for each trigger to invoke
  95. *
  96. * For each trigger associated with an event, invoke the trigger
  97. * function registered with the associated trigger command, if the
  98. * corresponding bit is set in the tt enum passed into this function.
  99. * See @event_triggers_call for details on how those bits are set.
  100. *
  101. * Called from tracepoint handlers (with rcu_read_lock_sched() held).
  102. */
  103. void
  104. event_triggers_post_call(struct trace_event_file *file,
  105. enum event_trigger_type tt)
  106. {
  107. struct event_trigger_data *data;
  108. list_for_each_entry_rcu(data, &file->triggers, list) {
  109. if (data->paused)
  110. continue;
  111. if (data->cmd_ops->trigger_type & tt)
  112. data->ops->trigger(data, NULL, NULL, NULL);
  113. }
  114. }
  115. EXPORT_SYMBOL_GPL(event_triggers_post_call);
  116. #define SHOW_AVAILABLE_TRIGGERS (void *)(1UL)
  117. static void *trigger_next(struct seq_file *m, void *t, loff_t *pos)
  118. {
  119. struct trace_event_file *event_file = event_file_data(m->private);
  120. if (t == SHOW_AVAILABLE_TRIGGERS) {
  121. (*pos)++;
  122. return NULL;
  123. }
  124. return seq_list_next(t, &event_file->triggers, pos);
  125. }
  126. static bool check_user_trigger(struct trace_event_file *file)
  127. {
  128. struct event_trigger_data *data;
  129. list_for_each_entry_rcu(data, &file->triggers, list,
  130. lockdep_is_held(&event_mutex)) {
  131. if (data->flags & EVENT_TRIGGER_FL_PROBE)
  132. continue;
  133. return true;
  134. }
  135. return false;
  136. }
  137. static void *trigger_start(struct seq_file *m, loff_t *pos)
  138. {
  139. struct trace_event_file *event_file;
  140. /* ->stop() is called even if ->start() fails */
  141. mutex_lock(&event_mutex);
  142. event_file = event_file_file(m->private);
  143. if (unlikely(!event_file))
  144. return ERR_PTR(-ENODEV);
  145. if (list_empty(&event_file->triggers) || !check_user_trigger(event_file))
  146. return *pos == 0 ? SHOW_AVAILABLE_TRIGGERS : NULL;
  147. return seq_list_start(&event_file->triggers, *pos);
  148. }
  149. static void trigger_stop(struct seq_file *m, void *t)
  150. {
  151. mutex_unlock(&event_mutex);
  152. }
  153. static int trigger_show(struct seq_file *m, void *v)
  154. {
  155. struct event_trigger_data *data;
  156. struct event_command *p;
  157. if (v == SHOW_AVAILABLE_TRIGGERS) {
  158. seq_puts(m, "# Available triggers:\n");
  159. seq_putc(m, '#');
  160. mutex_lock(&trigger_cmd_mutex);
  161. list_for_each_entry_reverse(p, &trigger_commands, list)
  162. seq_printf(m, " %s", p->name);
  163. seq_putc(m, '\n');
  164. mutex_unlock(&trigger_cmd_mutex);
  165. return 0;
  166. }
  167. data = list_entry(v, struct event_trigger_data, list);
  168. data->ops->print(m, data);
  169. return 0;
  170. }
  171. static const struct seq_operations event_triggers_seq_ops = {
  172. .start = trigger_start,
  173. .next = trigger_next,
  174. .stop = trigger_stop,
  175. .show = trigger_show,
  176. };
  177. static int event_trigger_regex_open(struct inode *inode, struct file *file)
  178. {
  179. int ret;
  180. ret = security_locked_down(LOCKDOWN_TRACEFS);
  181. if (ret)
  182. return ret;
  183. mutex_lock(&event_mutex);
  184. if (unlikely(!event_file_file(file))) {
  185. mutex_unlock(&event_mutex);
  186. return -ENODEV;
  187. }
  188. if ((file->f_mode & FMODE_WRITE) &&
  189. (file->f_flags & O_TRUNC)) {
  190. struct trace_event_file *event_file;
  191. struct event_command *p;
  192. event_file = event_file_data(file);
  193. list_for_each_entry(p, &trigger_commands, list) {
  194. if (p->unreg_all)
  195. p->unreg_all(event_file);
  196. }
  197. }
  198. if (file->f_mode & FMODE_READ) {
  199. ret = seq_open(file, &event_triggers_seq_ops);
  200. if (!ret) {
  201. struct seq_file *m = file->private_data;
  202. m->private = file;
  203. }
  204. }
  205. mutex_unlock(&event_mutex);
  206. return ret;
  207. }
  208. int trigger_process_regex(struct trace_event_file *file, char *buff)
  209. {
  210. char *command, *next;
  211. struct event_command *p;
  212. int ret = -EINVAL;
  213. next = buff = skip_spaces(buff);
  214. command = strsep(&next, ": \t");
  215. if (next) {
  216. next = skip_spaces(next);
  217. if (!*next)
  218. next = NULL;
  219. }
  220. command = (command[0] != '!') ? command : command + 1;
  221. mutex_lock(&trigger_cmd_mutex);
  222. list_for_each_entry(p, &trigger_commands, list) {
  223. if (strcmp(p->name, command) == 0) {
  224. ret = p->parse(p, file, buff, command, next);
  225. goto out_unlock;
  226. }
  227. }
  228. out_unlock:
  229. mutex_unlock(&trigger_cmd_mutex);
  230. return ret;
  231. }
  232. static ssize_t event_trigger_regex_write(struct file *file,
  233. const char __user *ubuf,
  234. size_t cnt, loff_t *ppos)
  235. {
  236. struct trace_event_file *event_file;
  237. ssize_t ret;
  238. char *buf;
  239. if (!cnt)
  240. return 0;
  241. if (cnt >= PAGE_SIZE)
  242. return -EINVAL;
  243. buf = memdup_user_nul(ubuf, cnt);
  244. if (IS_ERR(buf))
  245. return PTR_ERR(buf);
  246. strim(buf);
  247. mutex_lock(&event_mutex);
  248. event_file = event_file_file(file);
  249. if (unlikely(!event_file)) {
  250. mutex_unlock(&event_mutex);
  251. kfree(buf);
  252. return -ENODEV;
  253. }
  254. ret = trigger_process_regex(event_file, buf);
  255. mutex_unlock(&event_mutex);
  256. kfree(buf);
  257. if (ret < 0)
  258. goto out;
  259. *ppos += cnt;
  260. ret = cnt;
  261. out:
  262. return ret;
  263. }
  264. static int event_trigger_regex_release(struct inode *inode, struct file *file)
  265. {
  266. mutex_lock(&event_mutex);
  267. if (file->f_mode & FMODE_READ)
  268. seq_release(inode, file);
  269. mutex_unlock(&event_mutex);
  270. return 0;
  271. }
  272. static ssize_t
  273. event_trigger_write(struct file *filp, const char __user *ubuf,
  274. size_t cnt, loff_t *ppos)
  275. {
  276. return event_trigger_regex_write(filp, ubuf, cnt, ppos);
  277. }
  278. static int
  279. event_trigger_open(struct inode *inode, struct file *filp)
  280. {
  281. /* Checks for tracefs lockdown */
  282. return event_trigger_regex_open(inode, filp);
  283. }
  284. static int
  285. event_trigger_release(struct inode *inode, struct file *file)
  286. {
  287. return event_trigger_regex_release(inode, file);
  288. }
  289. const struct file_operations event_trigger_fops = {
  290. .open = event_trigger_open,
  291. .read = seq_read,
  292. .write = event_trigger_write,
  293. .llseek = tracing_lseek,
  294. .release = event_trigger_release,
  295. };
  296. /*
  297. * Currently we only register event commands from __init, so mark this
  298. * __init too.
  299. */
  300. __init int register_event_command(struct event_command *cmd)
  301. {
  302. struct event_command *p;
  303. int ret = 0;
  304. mutex_lock(&trigger_cmd_mutex);
  305. list_for_each_entry(p, &trigger_commands, list) {
  306. if (strcmp(cmd->name, p->name) == 0) {
  307. ret = -EBUSY;
  308. goto out_unlock;
  309. }
  310. }
  311. list_add(&cmd->list, &trigger_commands);
  312. out_unlock:
  313. mutex_unlock(&trigger_cmd_mutex);
  314. return ret;
  315. }
  316. /*
  317. * Currently we only unregister event commands from __init, so mark
  318. * this __init too.
  319. */
  320. __init int unregister_event_command(struct event_command *cmd)
  321. {
  322. struct event_command *p, *n;
  323. int ret = -ENODEV;
  324. mutex_lock(&trigger_cmd_mutex);
  325. list_for_each_entry_safe(p, n, &trigger_commands, list) {
  326. if (strcmp(cmd->name, p->name) == 0) {
  327. ret = 0;
  328. list_del_init(&p->list);
  329. goto out_unlock;
  330. }
  331. }
  332. out_unlock:
  333. mutex_unlock(&trigger_cmd_mutex);
  334. return ret;
  335. }
  336. /**
  337. * event_trigger_print - Generic event_trigger_ops @print implementation
  338. * @name: The name of the event trigger
  339. * @m: The seq_file being printed to
  340. * @data: Trigger-specific data
  341. * @filter_str: filter_str to print, if present
  342. *
  343. * Common implementation for event triggers to print themselves.
  344. *
  345. * Usually wrapped by a function that simply sets the @name of the
  346. * trigger command and then invokes this.
  347. *
  348. * Return: 0 on success, errno otherwise
  349. */
  350. static int
  351. event_trigger_print(const char *name, struct seq_file *m,
  352. void *data, char *filter_str)
  353. {
  354. long count = (long)data;
  355. seq_puts(m, name);
  356. if (count == -1)
  357. seq_puts(m, ":unlimited");
  358. else
  359. seq_printf(m, ":count=%ld", count);
  360. if (filter_str)
  361. seq_printf(m, " if %s\n", filter_str);
  362. else
  363. seq_putc(m, '\n');
  364. return 0;
  365. }
  366. /**
  367. * event_trigger_init - Generic event_trigger_ops @init implementation
  368. * @data: Trigger-specific data
  369. *
  370. * Common implementation of event trigger initialization.
  371. *
  372. * Usually used directly as the @init method in event trigger
  373. * implementations.
  374. *
  375. * Return: 0 on success, errno otherwise
  376. */
  377. int event_trigger_init(struct event_trigger_data *data)
  378. {
  379. data->ref++;
  380. return 0;
  381. }
  382. /**
  383. * event_trigger_free - Generic event_trigger_ops @free implementation
  384. * @data: Trigger-specific data
  385. *
  386. * Common implementation of event trigger de-initialization.
  387. *
  388. * Usually used directly as the @free method in event trigger
  389. * implementations.
  390. */
  391. static void
  392. event_trigger_free(struct event_trigger_data *data)
  393. {
  394. if (WARN_ON_ONCE(data->ref <= 0))
  395. return;
  396. data->ref--;
  397. if (!data->ref)
  398. trigger_data_free(data);
  399. }
  400. int trace_event_trigger_enable_disable(struct trace_event_file *file,
  401. int trigger_enable)
  402. {
  403. int ret = 0;
  404. if (trigger_enable) {
  405. if (atomic_inc_return(&file->tm_ref) > 1)
  406. return ret;
  407. set_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
  408. ret = trace_event_enable_disable(file, 1, 1);
  409. } else {
  410. if (atomic_dec_return(&file->tm_ref) > 0)
  411. return ret;
  412. clear_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
  413. ret = trace_event_enable_disable(file, 0, 1);
  414. }
  415. return ret;
  416. }
  417. /**
  418. * clear_event_triggers - Clear all triggers associated with a trace array
  419. * @tr: The trace array to clear
  420. *
  421. * For each trigger, the triggering event has its tm_ref decremented
  422. * via trace_event_trigger_enable_disable(), and any associated event
  423. * (in the case of enable/disable_event triggers) will have its sm_ref
  424. * decremented via free()->trace_event_enable_disable(). That
  425. * combination effectively reverses the soft-mode/trigger state added
  426. * by trigger registration.
  427. *
  428. * Must be called with event_mutex held.
  429. */
  430. void
  431. clear_event_triggers(struct trace_array *tr)
  432. {
  433. struct trace_event_file *file;
  434. list_for_each_entry(file, &tr->events, list) {
  435. struct event_trigger_data *data, *n;
  436. list_for_each_entry_safe(data, n, &file->triggers, list) {
  437. trace_event_trigger_enable_disable(file, 0);
  438. list_del_rcu(&data->list);
  439. if (data->ops->free)
  440. data->ops->free(data);
  441. }
  442. }
  443. }
  444. /**
  445. * update_cond_flag - Set or reset the TRIGGER_COND bit
  446. * @file: The trace_event_file associated with the event
  447. *
  448. * If an event has triggers and any of those triggers has a filter or
  449. * a post_trigger, trigger invocation needs to be deferred until after
  450. * the current event has logged its data, and the event should have
  451. * its TRIGGER_COND bit set, otherwise the TRIGGER_COND bit should be
  452. * cleared.
  453. */
  454. void update_cond_flag(struct trace_event_file *file)
  455. {
  456. struct event_trigger_data *data;
  457. bool set_cond = false;
  458. lockdep_assert_held(&event_mutex);
  459. list_for_each_entry(data, &file->triggers, list) {
  460. if (data->filter || event_command_post_trigger(data->cmd_ops) ||
  461. event_command_needs_rec(data->cmd_ops)) {
  462. set_cond = true;
  463. break;
  464. }
  465. }
  466. if (set_cond)
  467. set_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
  468. else
  469. clear_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
  470. }
  471. /**
  472. * register_trigger - Generic event_command @reg implementation
  473. * @glob: The raw string used to register the trigger
  474. * @data: Trigger-specific data to associate with the trigger
  475. * @file: The trace_event_file associated with the event
  476. *
  477. * Common implementation for event trigger registration.
  478. *
  479. * Usually used directly as the @reg method in event command
  480. * implementations.
  481. *
  482. * Return: 0 on success, errno otherwise
  483. */
  484. static int register_trigger(char *glob,
  485. struct event_trigger_data *data,
  486. struct trace_event_file *file)
  487. {
  488. struct event_trigger_data *test;
  489. int ret = 0;
  490. lockdep_assert_held(&event_mutex);
  491. list_for_each_entry(test, &file->triggers, list) {
  492. if (test->cmd_ops->trigger_type == data->cmd_ops->trigger_type) {
  493. ret = -EEXIST;
  494. goto out;
  495. }
  496. }
  497. if (data->ops->init) {
  498. ret = data->ops->init(data);
  499. if (ret < 0)
  500. goto out;
  501. }
  502. list_add_rcu(&data->list, &file->triggers);
  503. update_cond_flag(file);
  504. ret = trace_event_trigger_enable_disable(file, 1);
  505. if (ret < 0) {
  506. list_del_rcu(&data->list);
  507. update_cond_flag(file);
  508. }
  509. out:
  510. return ret;
  511. }
  512. /*
  513. * True if the trigger was found and unregistered, else false.
  514. */
  515. static bool try_unregister_trigger(char *glob,
  516. struct event_trigger_data *test,
  517. struct trace_event_file *file)
  518. {
  519. struct event_trigger_data *data = NULL, *iter;
  520. lockdep_assert_held(&event_mutex);
  521. list_for_each_entry(iter, &file->triggers, list) {
  522. if (iter->cmd_ops->trigger_type == test->cmd_ops->trigger_type) {
  523. data = iter;
  524. list_del_rcu(&data->list);
  525. trace_event_trigger_enable_disable(file, 0);
  526. update_cond_flag(file);
  527. break;
  528. }
  529. }
  530. if (data) {
  531. if (data->ops->free)
  532. data->ops->free(data);
  533. return true;
  534. }
  535. return false;
  536. }
  537. /**
  538. * unregister_trigger - Generic event_command @unreg implementation
  539. * @glob: The raw string used to register the trigger
  540. * @test: Trigger-specific data used to find the trigger to remove
  541. * @file: The trace_event_file associated with the event
  542. *
  543. * Common implementation for event trigger unregistration.
  544. *
  545. * Usually used directly as the @unreg method in event command
  546. * implementations.
  547. */
  548. static void unregister_trigger(char *glob,
  549. struct event_trigger_data *test,
  550. struct trace_event_file *file)
  551. {
  552. try_unregister_trigger(glob, test, file);
  553. }
  554. /*
  555. * Event trigger parsing helper functions.
  556. *
  557. * These functions help make it easier to write an event trigger
  558. * parsing function i.e. the struct event_command.parse() callback
  559. * function responsible for parsing and registering a trigger command
  560. * written to the 'trigger' file.
  561. *
  562. * A trigger command (or just 'trigger' for short) takes the form:
  563. * [trigger] [if filter]
  564. *
  565. * The struct event_command.parse() callback (and other struct
  566. * event_command functions) refer to several components of a trigger
  567. * command. Those same components are referenced by the event trigger
  568. * parsing helper functions defined below. These components are:
  569. *
  570. * cmd - the trigger command name
  571. * glob - the trigger command name optionally prefaced with '!'
  572. * param_and_filter - text following cmd and ':'
  573. * param - text following cmd and ':' and stripped of filter
  574. * filter - the optional filter text following (and including) 'if'
  575. *
  576. * To illustrate the use of these componenents, here are some concrete
  577. * examples. For the following triggers:
  578. *
  579. * echo 'traceon:5 if pid == 0' > trigger
  580. * - 'traceon' is both cmd and glob
  581. * - '5 if pid == 0' is the param_and_filter
  582. * - '5' is the param
  583. * - 'if pid == 0' is the filter
  584. *
  585. * echo 'enable_event:sys:event:n' > trigger
  586. * - 'enable_event' is both cmd and glob
  587. * - 'sys:event:n' is the param_and_filter
  588. * - 'sys:event:n' is the param
  589. * - there is no filter
  590. *
  591. * echo 'hist:keys=pid if prio > 50' > trigger
  592. * - 'hist' is both cmd and glob
  593. * - 'keys=pid if prio > 50' is the param_and_filter
  594. * - 'keys=pid' is the param
  595. * - 'if prio > 50' is the filter
  596. *
  597. * echo '!enable_event:sys:event:n' > trigger
  598. * - 'enable_event' the cmd
  599. * - '!enable_event' is the glob
  600. * - 'sys:event:n' is the param_and_filter
  601. * - 'sys:event:n' is the param
  602. * - there is no filter
  603. *
  604. * echo 'traceoff' > trigger
  605. * - 'traceoff' is both cmd and glob
  606. * - there is no param_and_filter
  607. * - there is no param
  608. * - there is no filter
  609. *
  610. * There are a few different categories of event trigger covered by
  611. * these helpers:
  612. *
  613. * - triggers that don't require a parameter e.g. traceon
  614. * - triggers that do require a parameter e.g. enable_event and hist
  615. * - triggers that though they may not require a param may support an
  616. * optional 'n' param (n = number of times the trigger should fire)
  617. * e.g.: traceon:5 or enable_event:sys:event:n
  618. * - triggers that do not support an 'n' param e.g. hist
  619. *
  620. * These functions can be used or ignored as necessary - it all
  621. * depends on the complexity of the trigger, and the granularity of
  622. * the functions supported reflects the fact that some implementations
  623. * may need to customize certain aspects of their implementations and
  624. * won't need certain functions. For instance, the hist trigger
  625. * implementation doesn't use event_trigger_separate_filter() because
  626. * it has special requirements for handling the filter.
  627. */
  628. /**
  629. * event_trigger_check_remove - check whether an event trigger specifies remove
  630. * @glob: The trigger command string, with optional remove(!) operator
  631. *
  632. * The event trigger callback implementations pass in 'glob' as a
  633. * parameter. This is the command name either with or without a
  634. * remove(!) operator. This function simply parses the glob and
  635. * determines whether the command corresponds to a trigger removal or
  636. * a trigger addition.
  637. *
  638. * Return: true if this is a remove command, false otherwise
  639. */
  640. bool event_trigger_check_remove(const char *glob)
  641. {
  642. return (glob && glob[0] == '!') ? true : false;
  643. }
  644. /**
  645. * event_trigger_empty_param - check whether the param is empty
  646. * @param: The trigger param string
  647. *
  648. * The event trigger callback implementations pass in 'param' as a
  649. * parameter. This corresponds to the string following the command
  650. * name minus the command name. This function can be called by a
  651. * callback implementation for any command that requires a param; a
  652. * callback that doesn't require a param can ignore it.
  653. *
  654. * Return: true if this is an empty param, false otherwise
  655. */
  656. bool event_trigger_empty_param(const char *param)
  657. {
  658. return !param;
  659. }
  660. /**
  661. * event_trigger_separate_filter - separate an event trigger from a filter
  662. * @param_and_filter: String containing trigger and possibly filter
  663. * @param: outparam, will be filled with a pointer to the trigger
  664. * @filter: outparam, will be filled with a pointer to the filter
  665. * @param_required: Specifies whether or not the param string is required
  666. *
  667. * Given a param string of the form '[trigger] [if filter]', this
  668. * function separates the filter from the trigger and returns the
  669. * trigger in @param and the filter in @filter. Either the @param
  670. * or the @filter may be set to NULL by this function - if not set to
  671. * NULL, they will contain strings corresponding to the trigger and
  672. * filter.
  673. *
  674. * There are two cases that need to be handled with respect to the
  675. * passed-in param: either the param is required, or it is not
  676. * required. If @param_required is set, and there's no param, it will
  677. * return -EINVAL. If @param_required is not set and there's a param
  678. * that starts with a number, that corresponds to the case of a
  679. * trigger with :n (n = number of times the trigger should fire) and
  680. * the parsing continues normally; otherwise the function just returns
  681. * and assumes param just contains a filter and there's nothing else
  682. * to do.
  683. *
  684. * Return: 0 on success, errno otherwise
  685. */
  686. int event_trigger_separate_filter(char *param_and_filter, char **param,
  687. char **filter, bool param_required)
  688. {
  689. int ret = 0;
  690. *param = *filter = NULL;
  691. if (!param_and_filter) {
  692. if (param_required)
  693. ret = -EINVAL;
  694. goto out;
  695. }
  696. /*
  697. * Here we check for an optional param. The only legal
  698. * optional param is :n, and if that's the case, continue
  699. * below. Otherwise we assume what's left is a filter and
  700. * return it as the filter string for the caller to deal with.
  701. */
  702. if (!param_required && param_and_filter && !isdigit(param_and_filter[0])) {
  703. *filter = param_and_filter;
  704. goto out;
  705. }
  706. /*
  707. * Separate the param from the filter (param [if filter]).
  708. * Here we have either an optional :n param or a required
  709. * param and an optional filter.
  710. */
  711. *param = strsep(&param_and_filter, " \t");
  712. /*
  713. * Here we have a filter, though it may be empty.
  714. */
  715. if (param_and_filter) {
  716. *filter = skip_spaces(param_and_filter);
  717. if (!**filter)
  718. *filter = NULL;
  719. }
  720. out:
  721. return ret;
  722. }
  723. /**
  724. * trigger_data_alloc - allocate and init event_trigger_data for a trigger
  725. * @cmd_ops: The event_command operations for the trigger
  726. * @cmd: The cmd string
  727. * @param: The param string
  728. * @private_data: User data to associate with the event trigger
  729. *
  730. * Allocate an event_trigger_data instance and initialize it. The
  731. * @cmd_ops are used along with the @cmd and @param to get the
  732. * trigger_ops to assign to the event_trigger_data. @private_data can
  733. * also be passed in and associated with the event_trigger_data.
  734. *
  735. * Use trigger_data_free() to free an event_trigger_data object.
  736. *
  737. * Return: The trigger_data object success, NULL otherwise
  738. */
  739. struct event_trigger_data *trigger_data_alloc(struct event_command *cmd_ops,
  740. char *cmd,
  741. char *param,
  742. void *private_data)
  743. {
  744. struct event_trigger_data *trigger_data;
  745. struct event_trigger_ops *trigger_ops;
  746. trigger_ops = cmd_ops->get_trigger_ops(cmd, param);
  747. trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
  748. if (!trigger_data)
  749. return NULL;
  750. trigger_data->count = -1;
  751. trigger_data->ops = trigger_ops;
  752. trigger_data->cmd_ops = cmd_ops;
  753. trigger_data->private_data = private_data;
  754. INIT_LIST_HEAD(&trigger_data->list);
  755. INIT_LIST_HEAD(&trigger_data->named_list);
  756. RCU_INIT_POINTER(trigger_data->filter, NULL);
  757. return trigger_data;
  758. }
  759. /**
  760. * event_trigger_parse_num - parse and return the number param for a trigger
  761. * @param: The param string
  762. * @trigger_data: The trigger_data for the trigger
  763. *
  764. * Parse the :n (n = number of times the trigger should fire) param
  765. * and set the count variable in the trigger_data to the parsed count.
  766. *
  767. * Return: 0 on success, errno otherwise
  768. */
  769. int event_trigger_parse_num(char *param,
  770. struct event_trigger_data *trigger_data)
  771. {
  772. char *number;
  773. int ret = 0;
  774. if (param) {
  775. number = strsep(&param, ":");
  776. if (!strlen(number))
  777. return -EINVAL;
  778. /*
  779. * We use the callback data field (which is a pointer)
  780. * as our counter.
  781. */
  782. ret = kstrtoul(number, 0, &trigger_data->count);
  783. }
  784. return ret;
  785. }
  786. /**
  787. * event_trigger_set_filter - set an event trigger's filter
  788. * @cmd_ops: The event_command operations for the trigger
  789. * @file: The event file for the trigger's event
  790. * @param: The string containing the filter
  791. * @trigger_data: The trigger_data for the trigger
  792. *
  793. * Set the filter for the trigger. If the filter is NULL, just return
  794. * without error.
  795. *
  796. * Return: 0 on success, errno otherwise
  797. */
  798. int event_trigger_set_filter(struct event_command *cmd_ops,
  799. struct trace_event_file *file,
  800. char *param,
  801. struct event_trigger_data *trigger_data)
  802. {
  803. if (param && cmd_ops->set_filter)
  804. return cmd_ops->set_filter(param, trigger_data, file);
  805. return 0;
  806. }
  807. /**
  808. * event_trigger_reset_filter - reset an event trigger's filter
  809. * @cmd_ops: The event_command operations for the trigger
  810. * @trigger_data: The trigger_data for the trigger
  811. *
  812. * Reset the filter for the trigger to no filter.
  813. */
  814. void event_trigger_reset_filter(struct event_command *cmd_ops,
  815. struct event_trigger_data *trigger_data)
  816. {
  817. if (cmd_ops->set_filter)
  818. cmd_ops->set_filter(NULL, trigger_data, NULL);
  819. }
  820. /**
  821. * event_trigger_register - register an event trigger
  822. * @cmd_ops: The event_command operations for the trigger
  823. * @file: The event file for the trigger's event
  824. * @glob: The trigger command string, with optional remove(!) operator
  825. * @trigger_data: The trigger_data for the trigger
  826. *
  827. * Register an event trigger. The @cmd_ops are used to call the
  828. * cmd_ops->reg() function which actually does the registration.
  829. *
  830. * Return: 0 on success, errno otherwise
  831. */
  832. int event_trigger_register(struct event_command *cmd_ops,
  833. struct trace_event_file *file,
  834. char *glob,
  835. struct event_trigger_data *trigger_data)
  836. {
  837. return cmd_ops->reg(glob, trigger_data, file);
  838. }
  839. /**
  840. * event_trigger_unregister - unregister an event trigger
  841. * @cmd_ops: The event_command operations for the trigger
  842. * @file: The event file for the trigger's event
  843. * @glob: The trigger command string, with optional remove(!) operator
  844. * @trigger_data: The trigger_data for the trigger
  845. *
  846. * Unregister an event trigger. The @cmd_ops are used to call the
  847. * cmd_ops->unreg() function which actually does the unregistration.
  848. */
  849. void event_trigger_unregister(struct event_command *cmd_ops,
  850. struct trace_event_file *file,
  851. char *glob,
  852. struct event_trigger_data *trigger_data)
  853. {
  854. cmd_ops->unreg(glob, trigger_data, file);
  855. }
  856. /*
  857. * End event trigger parsing helper functions.
  858. */
  859. /**
  860. * event_trigger_parse - Generic event_command @parse implementation
  861. * @cmd_ops: The command ops, used for trigger registration
  862. * @file: The trace_event_file associated with the event
  863. * @glob: The raw string used to register the trigger
  864. * @cmd: The cmd portion of the string used to register the trigger
  865. * @param_and_filter: The param and filter portion of the string used to register the trigger
  866. *
  867. * Common implementation for event command parsing and trigger
  868. * instantiation.
  869. *
  870. * Usually used directly as the @parse method in event command
  871. * implementations.
  872. *
  873. * Return: 0 on success, errno otherwise
  874. */
  875. static int
  876. event_trigger_parse(struct event_command *cmd_ops,
  877. struct trace_event_file *file,
  878. char *glob, char *cmd, char *param_and_filter)
  879. {
  880. struct event_trigger_data *trigger_data;
  881. char *param, *filter;
  882. bool remove;
  883. int ret;
  884. remove = event_trigger_check_remove(glob);
  885. ret = event_trigger_separate_filter(param_and_filter, &param, &filter, false);
  886. if (ret)
  887. return ret;
  888. ret = -ENOMEM;
  889. trigger_data = trigger_data_alloc(cmd_ops, cmd, param, file);
  890. if (!trigger_data)
  891. goto out;
  892. if (remove) {
  893. event_trigger_unregister(cmd_ops, file, glob+1, trigger_data);
  894. trigger_data_free(trigger_data);
  895. ret = 0;
  896. goto out;
  897. }
  898. ret = event_trigger_parse_num(param, trigger_data);
  899. if (ret)
  900. goto out_free;
  901. ret = event_trigger_set_filter(cmd_ops, file, filter, trigger_data);
  902. if (ret < 0)
  903. goto out_free;
  904. /* Up the trigger_data count to make sure reg doesn't free it on failure */
  905. event_trigger_init(trigger_data);
  906. ret = event_trigger_register(cmd_ops, file, glob, trigger_data);
  907. if (ret)
  908. goto out_free;
  909. /* Down the counter of trigger_data or free it if not used anymore */
  910. event_trigger_free(trigger_data);
  911. out:
  912. return ret;
  913. out_free:
  914. event_trigger_reset_filter(cmd_ops, trigger_data);
  915. trigger_data_free(trigger_data);
  916. goto out;
  917. }
  918. /**
  919. * set_trigger_filter - Generic event_command @set_filter implementation
  920. * @filter_str: The filter string for the trigger, NULL to remove filter
  921. * @trigger_data: Trigger-specific data
  922. * @file: The trace_event_file associated with the event
  923. *
  924. * Common implementation for event command filter parsing and filter
  925. * instantiation.
  926. *
  927. * Usually used directly as the @set_filter method in event command
  928. * implementations.
  929. *
  930. * Also used to remove a filter (if filter_str = NULL).
  931. *
  932. * Return: 0 on success, errno otherwise
  933. */
  934. int set_trigger_filter(char *filter_str,
  935. struct event_trigger_data *trigger_data,
  936. struct trace_event_file *file)
  937. {
  938. struct event_trigger_data *data = trigger_data;
  939. struct event_filter *filter = NULL, *tmp;
  940. int ret = -EINVAL;
  941. char *s;
  942. if (!filter_str) /* clear the current filter */
  943. goto assign;
  944. s = strsep(&filter_str, " \t");
  945. if (!strlen(s) || strcmp(s, "if") != 0)
  946. goto out;
  947. if (!filter_str)
  948. goto out;
  949. /* The filter is for the 'trigger' event, not the triggered event */
  950. ret = create_event_filter(file->tr, file->event_call,
  951. filter_str, true, &filter);
  952. /* Only enabled set_str for error handling */
  953. if (filter) {
  954. kfree(filter->filter_string);
  955. filter->filter_string = NULL;
  956. }
  957. /*
  958. * If create_event_filter() fails, filter still needs to be freed.
  959. * Which the calling code will do with data->filter.
  960. */
  961. assign:
  962. tmp = rcu_access_pointer(data->filter);
  963. rcu_assign_pointer(data->filter, filter);
  964. if (tmp) {
  965. /*
  966. * Make sure the call is done with the filter.
  967. * It is possible that a filter could fail at boot up,
  968. * and then this path will be called. Avoid the synchronization
  969. * in that case.
  970. */
  971. if (system_state != SYSTEM_BOOTING)
  972. tracepoint_synchronize_unregister();
  973. free_event_filter(tmp);
  974. }
  975. kfree(data->filter_str);
  976. data->filter_str = NULL;
  977. if (filter_str) {
  978. data->filter_str = kstrdup(filter_str, GFP_KERNEL);
  979. if (!data->filter_str) {
  980. free_event_filter(rcu_access_pointer(data->filter));
  981. data->filter = NULL;
  982. ret = -ENOMEM;
  983. }
  984. }
  985. out:
  986. return ret;
  987. }
  988. static LIST_HEAD(named_triggers);
  989. /**
  990. * find_named_trigger - Find the common named trigger associated with @name
  991. * @name: The name of the set of named triggers to find the common data for
  992. *
  993. * Named triggers are sets of triggers that share a common set of
  994. * trigger data. The first named trigger registered with a given name
  995. * owns the common trigger data that the others subsequently
  996. * registered with the same name will reference. This function
  997. * returns the common trigger data associated with that first
  998. * registered instance.
  999. *
  1000. * Return: the common trigger data for the given named trigger on
  1001. * success, NULL otherwise.
  1002. */
  1003. struct event_trigger_data *find_named_trigger(const char *name)
  1004. {
  1005. struct event_trigger_data *data;
  1006. if (!name)
  1007. return NULL;
  1008. list_for_each_entry(data, &named_triggers, named_list) {
  1009. if (data->named_data)
  1010. continue;
  1011. if (strcmp(data->name, name) == 0)
  1012. return data;
  1013. }
  1014. return NULL;
  1015. }
  1016. /**
  1017. * is_named_trigger - determine if a given trigger is a named trigger
  1018. * @test: The trigger data to test
  1019. *
  1020. * Return: true if 'test' is a named trigger, false otherwise.
  1021. */
  1022. bool is_named_trigger(struct event_trigger_data *test)
  1023. {
  1024. struct event_trigger_data *data;
  1025. list_for_each_entry(data, &named_triggers, named_list) {
  1026. if (test == data)
  1027. return true;
  1028. }
  1029. return false;
  1030. }
  1031. /**
  1032. * save_named_trigger - save the trigger in the named trigger list
  1033. * @name: The name of the named trigger set
  1034. * @data: The trigger data to save
  1035. *
  1036. * Return: 0 if successful, negative error otherwise.
  1037. */
  1038. int save_named_trigger(const char *name, struct event_trigger_data *data)
  1039. {
  1040. data->name = kstrdup(name, GFP_KERNEL);
  1041. if (!data->name)
  1042. return -ENOMEM;
  1043. list_add(&data->named_list, &named_triggers);
  1044. return 0;
  1045. }
  1046. /**
  1047. * del_named_trigger - delete a trigger from the named trigger list
  1048. * @data: The trigger data to delete
  1049. */
  1050. void del_named_trigger(struct event_trigger_data *data)
  1051. {
  1052. kfree(data->name);
  1053. data->name = NULL;
  1054. list_del(&data->named_list);
  1055. }
  1056. static void __pause_named_trigger(struct event_trigger_data *data, bool pause)
  1057. {
  1058. struct event_trigger_data *test;
  1059. list_for_each_entry(test, &named_triggers, named_list) {
  1060. if (strcmp(test->name, data->name) == 0) {
  1061. if (pause) {
  1062. test->paused_tmp = test->paused;
  1063. test->paused = true;
  1064. } else {
  1065. test->paused = test->paused_tmp;
  1066. }
  1067. }
  1068. }
  1069. }
  1070. /**
  1071. * pause_named_trigger - Pause all named triggers with the same name
  1072. * @data: The trigger data of a named trigger to pause
  1073. *
  1074. * Pauses a named trigger along with all other triggers having the
  1075. * same name. Because named triggers share a common set of data,
  1076. * pausing only one is meaningless, so pausing one named trigger needs
  1077. * to pause all triggers with the same name.
  1078. */
  1079. void pause_named_trigger(struct event_trigger_data *data)
  1080. {
  1081. __pause_named_trigger(data, true);
  1082. }
  1083. /**
  1084. * unpause_named_trigger - Un-pause all named triggers with the same name
  1085. * @data: The trigger data of a named trigger to unpause
  1086. *
  1087. * Un-pauses a named trigger along with all other triggers having the
  1088. * same name. Because named triggers share a common set of data,
  1089. * unpausing only one is meaningless, so unpausing one named trigger
  1090. * needs to unpause all triggers with the same name.
  1091. */
  1092. void unpause_named_trigger(struct event_trigger_data *data)
  1093. {
  1094. __pause_named_trigger(data, false);
  1095. }
  1096. /**
  1097. * set_named_trigger_data - Associate common named trigger data
  1098. * @data: The trigger data to associate
  1099. * @named_data: The common named trigger to be associated
  1100. *
  1101. * Named triggers are sets of triggers that share a common set of
  1102. * trigger data. The first named trigger registered with a given name
  1103. * owns the common trigger data that the others subsequently
  1104. * registered with the same name will reference. This function
  1105. * associates the common trigger data from the first trigger with the
  1106. * given trigger.
  1107. */
  1108. void set_named_trigger_data(struct event_trigger_data *data,
  1109. struct event_trigger_data *named_data)
  1110. {
  1111. data->named_data = named_data;
  1112. }
  1113. struct event_trigger_data *
  1114. get_named_trigger_data(struct event_trigger_data *data)
  1115. {
  1116. return data->named_data;
  1117. }
  1118. static void
  1119. traceon_trigger(struct event_trigger_data *data,
  1120. struct trace_buffer *buffer, void *rec,
  1121. struct ring_buffer_event *event)
  1122. {
  1123. struct trace_event_file *file = data->private_data;
  1124. if (file) {
  1125. if (tracer_tracing_is_on(file->tr))
  1126. return;
  1127. tracer_tracing_on(file->tr);
  1128. return;
  1129. }
  1130. if (tracing_is_on())
  1131. return;
  1132. tracing_on();
  1133. }
  1134. static void
  1135. traceon_count_trigger(struct event_trigger_data *data,
  1136. struct trace_buffer *buffer, void *rec,
  1137. struct ring_buffer_event *event)
  1138. {
  1139. struct trace_event_file *file = data->private_data;
  1140. if (file) {
  1141. if (tracer_tracing_is_on(file->tr))
  1142. return;
  1143. } else {
  1144. if (tracing_is_on())
  1145. return;
  1146. }
  1147. if (!data->count)
  1148. return;
  1149. if (data->count != -1)
  1150. (data->count)--;
  1151. if (file)
  1152. tracer_tracing_on(file->tr);
  1153. else
  1154. tracing_on();
  1155. }
  1156. static void
  1157. traceoff_trigger(struct event_trigger_data *data,
  1158. struct trace_buffer *buffer, void *rec,
  1159. struct ring_buffer_event *event)
  1160. {
  1161. struct trace_event_file *file = data->private_data;
  1162. if (file) {
  1163. if (!tracer_tracing_is_on(file->tr))
  1164. return;
  1165. tracer_tracing_off(file->tr);
  1166. return;
  1167. }
  1168. if (!tracing_is_on())
  1169. return;
  1170. tracing_off();
  1171. }
  1172. static void
  1173. traceoff_count_trigger(struct event_trigger_data *data,
  1174. struct trace_buffer *buffer, void *rec,
  1175. struct ring_buffer_event *event)
  1176. {
  1177. struct trace_event_file *file = data->private_data;
  1178. if (file) {
  1179. if (!tracer_tracing_is_on(file->tr))
  1180. return;
  1181. } else {
  1182. if (!tracing_is_on())
  1183. return;
  1184. }
  1185. if (!data->count)
  1186. return;
  1187. if (data->count != -1)
  1188. (data->count)--;
  1189. if (file)
  1190. tracer_tracing_off(file->tr);
  1191. else
  1192. tracing_off();
  1193. }
  1194. static int
  1195. traceon_trigger_print(struct seq_file *m, struct event_trigger_data *data)
  1196. {
  1197. return event_trigger_print("traceon", m, (void *)data->count,
  1198. data->filter_str);
  1199. }
  1200. static int
  1201. traceoff_trigger_print(struct seq_file *m, struct event_trigger_data *data)
  1202. {
  1203. return event_trigger_print("traceoff", m, (void *)data->count,
  1204. data->filter_str);
  1205. }
  1206. static struct event_trigger_ops traceon_trigger_ops = {
  1207. .trigger = traceon_trigger,
  1208. .print = traceon_trigger_print,
  1209. .init = event_trigger_init,
  1210. .free = event_trigger_free,
  1211. };
  1212. static struct event_trigger_ops traceon_count_trigger_ops = {
  1213. .trigger = traceon_count_trigger,
  1214. .print = traceon_trigger_print,
  1215. .init = event_trigger_init,
  1216. .free = event_trigger_free,
  1217. };
  1218. static struct event_trigger_ops traceoff_trigger_ops = {
  1219. .trigger = traceoff_trigger,
  1220. .print = traceoff_trigger_print,
  1221. .init = event_trigger_init,
  1222. .free = event_trigger_free,
  1223. };
  1224. static struct event_trigger_ops traceoff_count_trigger_ops = {
  1225. .trigger = traceoff_count_trigger,
  1226. .print = traceoff_trigger_print,
  1227. .init = event_trigger_init,
  1228. .free = event_trigger_free,
  1229. };
  1230. static struct event_trigger_ops *
  1231. onoff_get_trigger_ops(char *cmd, char *param)
  1232. {
  1233. struct event_trigger_ops *ops;
  1234. /* we register both traceon and traceoff to this callback */
  1235. if (strcmp(cmd, "traceon") == 0)
  1236. ops = param ? &traceon_count_trigger_ops :
  1237. &traceon_trigger_ops;
  1238. else
  1239. ops = param ? &traceoff_count_trigger_ops :
  1240. &traceoff_trigger_ops;
  1241. return ops;
  1242. }
  1243. static struct event_command trigger_traceon_cmd = {
  1244. .name = "traceon",
  1245. .trigger_type = ETT_TRACE_ONOFF,
  1246. .parse = event_trigger_parse,
  1247. .reg = register_trigger,
  1248. .unreg = unregister_trigger,
  1249. .get_trigger_ops = onoff_get_trigger_ops,
  1250. .set_filter = set_trigger_filter,
  1251. };
  1252. static struct event_command trigger_traceoff_cmd = {
  1253. .name = "traceoff",
  1254. .trigger_type = ETT_TRACE_ONOFF,
  1255. .flags = EVENT_CMD_FL_POST_TRIGGER,
  1256. .parse = event_trigger_parse,
  1257. .reg = register_trigger,
  1258. .unreg = unregister_trigger,
  1259. .get_trigger_ops = onoff_get_trigger_ops,
  1260. .set_filter = set_trigger_filter,
  1261. };
  1262. #ifdef CONFIG_TRACER_SNAPSHOT
  1263. static void
  1264. snapshot_trigger(struct event_trigger_data *data,
  1265. struct trace_buffer *buffer, void *rec,
  1266. struct ring_buffer_event *event)
  1267. {
  1268. struct trace_event_file *file = data->private_data;
  1269. if (file)
  1270. tracing_snapshot_instance(file->tr);
  1271. else
  1272. tracing_snapshot();
  1273. }
  1274. static void
  1275. snapshot_count_trigger(struct event_trigger_data *data,
  1276. struct trace_buffer *buffer, void *rec,
  1277. struct ring_buffer_event *event)
  1278. {
  1279. if (!data->count)
  1280. return;
  1281. if (data->count != -1)
  1282. (data->count)--;
  1283. snapshot_trigger(data, buffer, rec, event);
  1284. }
  1285. static int
  1286. register_snapshot_trigger(char *glob,
  1287. struct event_trigger_data *data,
  1288. struct trace_event_file *file)
  1289. {
  1290. int ret = tracing_arm_snapshot(file->tr);
  1291. if (ret < 0)
  1292. return ret;
  1293. ret = register_trigger(glob, data, file);
  1294. if (ret < 0)
  1295. tracing_disarm_snapshot(file->tr);
  1296. return ret;
  1297. }
  1298. static void unregister_snapshot_trigger(char *glob,
  1299. struct event_trigger_data *data,
  1300. struct trace_event_file *file)
  1301. {
  1302. if (try_unregister_trigger(glob, data, file))
  1303. tracing_disarm_snapshot(file->tr);
  1304. }
  1305. static int
  1306. snapshot_trigger_print(struct seq_file *m, struct event_trigger_data *data)
  1307. {
  1308. return event_trigger_print("snapshot", m, (void *)data->count,
  1309. data->filter_str);
  1310. }
  1311. static struct event_trigger_ops snapshot_trigger_ops = {
  1312. .trigger = snapshot_trigger,
  1313. .print = snapshot_trigger_print,
  1314. .init = event_trigger_init,
  1315. .free = event_trigger_free,
  1316. };
  1317. static struct event_trigger_ops snapshot_count_trigger_ops = {
  1318. .trigger = snapshot_count_trigger,
  1319. .print = snapshot_trigger_print,
  1320. .init = event_trigger_init,
  1321. .free = event_trigger_free,
  1322. };
  1323. static struct event_trigger_ops *
  1324. snapshot_get_trigger_ops(char *cmd, char *param)
  1325. {
  1326. return param ? &snapshot_count_trigger_ops : &snapshot_trigger_ops;
  1327. }
  1328. static struct event_command trigger_snapshot_cmd = {
  1329. .name = "snapshot",
  1330. .trigger_type = ETT_SNAPSHOT,
  1331. .parse = event_trigger_parse,
  1332. .reg = register_snapshot_trigger,
  1333. .unreg = unregister_snapshot_trigger,
  1334. .get_trigger_ops = snapshot_get_trigger_ops,
  1335. .set_filter = set_trigger_filter,
  1336. };
  1337. static __init int register_trigger_snapshot_cmd(void)
  1338. {
  1339. int ret;
  1340. ret = register_event_command(&trigger_snapshot_cmd);
  1341. WARN_ON(ret < 0);
  1342. return ret;
  1343. }
  1344. #else
  1345. static __init int register_trigger_snapshot_cmd(void) { return 0; }
  1346. #endif /* CONFIG_TRACER_SNAPSHOT */
  1347. #ifdef CONFIG_STACKTRACE
  1348. #ifdef CONFIG_UNWINDER_ORC
  1349. /* Skip 2:
  1350. * event_triggers_post_call()
  1351. * trace_event_raw_event_xxx()
  1352. */
  1353. # define STACK_SKIP 2
  1354. #else
  1355. /*
  1356. * Skip 4:
  1357. * stacktrace_trigger()
  1358. * event_triggers_post_call()
  1359. * trace_event_buffer_commit()
  1360. * trace_event_raw_event_xxx()
  1361. */
  1362. #define STACK_SKIP 4
  1363. #endif
  1364. static void
  1365. stacktrace_trigger(struct event_trigger_data *data,
  1366. struct trace_buffer *buffer, void *rec,
  1367. struct ring_buffer_event *event)
  1368. {
  1369. struct trace_event_file *file = data->private_data;
  1370. if (file)
  1371. __trace_stack(file->tr, tracing_gen_ctx_dec(), STACK_SKIP);
  1372. else
  1373. trace_dump_stack(STACK_SKIP);
  1374. }
  1375. static void
  1376. stacktrace_count_trigger(struct event_trigger_data *data,
  1377. struct trace_buffer *buffer, void *rec,
  1378. struct ring_buffer_event *event)
  1379. {
  1380. if (!data->count)
  1381. return;
  1382. if (data->count != -1)
  1383. (data->count)--;
  1384. stacktrace_trigger(data, buffer, rec, event);
  1385. }
  1386. static int
  1387. stacktrace_trigger_print(struct seq_file *m, struct event_trigger_data *data)
  1388. {
  1389. return event_trigger_print("stacktrace", m, (void *)data->count,
  1390. data->filter_str);
  1391. }
  1392. static struct event_trigger_ops stacktrace_trigger_ops = {
  1393. .trigger = stacktrace_trigger,
  1394. .print = stacktrace_trigger_print,
  1395. .init = event_trigger_init,
  1396. .free = event_trigger_free,
  1397. };
  1398. static struct event_trigger_ops stacktrace_count_trigger_ops = {
  1399. .trigger = stacktrace_count_trigger,
  1400. .print = stacktrace_trigger_print,
  1401. .init = event_trigger_init,
  1402. .free = event_trigger_free,
  1403. };
  1404. static struct event_trigger_ops *
  1405. stacktrace_get_trigger_ops(char *cmd, char *param)
  1406. {
  1407. return param ? &stacktrace_count_trigger_ops : &stacktrace_trigger_ops;
  1408. }
  1409. static struct event_command trigger_stacktrace_cmd = {
  1410. .name = "stacktrace",
  1411. .trigger_type = ETT_STACKTRACE,
  1412. .flags = EVENT_CMD_FL_POST_TRIGGER,
  1413. .parse = event_trigger_parse,
  1414. .reg = register_trigger,
  1415. .unreg = unregister_trigger,
  1416. .get_trigger_ops = stacktrace_get_trigger_ops,
  1417. .set_filter = set_trigger_filter,
  1418. };
  1419. static __init int register_trigger_stacktrace_cmd(void)
  1420. {
  1421. int ret;
  1422. ret = register_event_command(&trigger_stacktrace_cmd);
  1423. WARN_ON(ret < 0);
  1424. return ret;
  1425. }
  1426. #else
  1427. static __init int register_trigger_stacktrace_cmd(void) { return 0; }
  1428. #endif /* CONFIG_STACKTRACE */
  1429. static __init void unregister_trigger_traceon_traceoff_cmds(void)
  1430. {
  1431. unregister_event_command(&trigger_traceon_cmd);
  1432. unregister_event_command(&trigger_traceoff_cmd);
  1433. }
  1434. static void
  1435. event_enable_trigger(struct event_trigger_data *data,
  1436. struct trace_buffer *buffer, void *rec,
  1437. struct ring_buffer_event *event)
  1438. {
  1439. struct enable_trigger_data *enable_data = data->private_data;
  1440. if (enable_data->enable)
  1441. clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
  1442. else
  1443. set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
  1444. }
  1445. static void
  1446. event_enable_count_trigger(struct event_trigger_data *data,
  1447. struct trace_buffer *buffer, void *rec,
  1448. struct ring_buffer_event *event)
  1449. {
  1450. struct enable_trigger_data *enable_data = data->private_data;
  1451. if (!data->count)
  1452. return;
  1453. /* Skip if the event is in a state we want to switch to */
  1454. if (enable_data->enable == !(enable_data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
  1455. return;
  1456. if (data->count != -1)
  1457. (data->count)--;
  1458. event_enable_trigger(data, buffer, rec, event);
  1459. }
  1460. int event_enable_trigger_print(struct seq_file *m,
  1461. struct event_trigger_data *data)
  1462. {
  1463. struct enable_trigger_data *enable_data = data->private_data;
  1464. seq_printf(m, "%s:%s:%s",
  1465. enable_data->hist ?
  1466. (enable_data->enable ? ENABLE_HIST_STR : DISABLE_HIST_STR) :
  1467. (enable_data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR),
  1468. enable_data->file->event_call->class->system,
  1469. trace_event_name(enable_data->file->event_call));
  1470. if (data->count == -1)
  1471. seq_puts(m, ":unlimited");
  1472. else
  1473. seq_printf(m, ":count=%ld", data->count);
  1474. if (data->filter_str)
  1475. seq_printf(m, " if %s\n", data->filter_str);
  1476. else
  1477. seq_putc(m, '\n');
  1478. return 0;
  1479. }
  1480. void event_enable_trigger_free(struct event_trigger_data *data)
  1481. {
  1482. struct enable_trigger_data *enable_data = data->private_data;
  1483. if (WARN_ON_ONCE(data->ref <= 0))
  1484. return;
  1485. data->ref--;
  1486. if (!data->ref) {
  1487. /* Remove the SOFT_MODE flag */
  1488. trace_event_enable_disable(enable_data->file, 0, 1);
  1489. trace_event_put_ref(enable_data->file->event_call);
  1490. trigger_data_free(data);
  1491. kfree(enable_data);
  1492. }
  1493. }
  1494. static struct event_trigger_ops event_enable_trigger_ops = {
  1495. .trigger = event_enable_trigger,
  1496. .print = event_enable_trigger_print,
  1497. .init = event_trigger_init,
  1498. .free = event_enable_trigger_free,
  1499. };
  1500. static struct event_trigger_ops event_enable_count_trigger_ops = {
  1501. .trigger = event_enable_count_trigger,
  1502. .print = event_enable_trigger_print,
  1503. .init = event_trigger_init,
  1504. .free = event_enable_trigger_free,
  1505. };
  1506. static struct event_trigger_ops event_disable_trigger_ops = {
  1507. .trigger = event_enable_trigger,
  1508. .print = event_enable_trigger_print,
  1509. .init = event_trigger_init,
  1510. .free = event_enable_trigger_free,
  1511. };
  1512. static struct event_trigger_ops event_disable_count_trigger_ops = {
  1513. .trigger = event_enable_count_trigger,
  1514. .print = event_enable_trigger_print,
  1515. .init = event_trigger_init,
  1516. .free = event_enable_trigger_free,
  1517. };
  1518. int event_enable_trigger_parse(struct event_command *cmd_ops,
  1519. struct trace_event_file *file,
  1520. char *glob, char *cmd, char *param_and_filter)
  1521. {
  1522. struct trace_event_file *event_enable_file;
  1523. struct enable_trigger_data *enable_data;
  1524. struct event_trigger_data *trigger_data;
  1525. struct trace_array *tr = file->tr;
  1526. char *param, *filter;
  1527. bool enable, remove;
  1528. const char *system;
  1529. const char *event;
  1530. bool hist = false;
  1531. int ret;
  1532. remove = event_trigger_check_remove(glob);
  1533. if (event_trigger_empty_param(param_and_filter))
  1534. return -EINVAL;
  1535. ret = event_trigger_separate_filter(param_and_filter, &param, &filter, true);
  1536. if (ret)
  1537. return ret;
  1538. system = strsep(&param, ":");
  1539. if (!param)
  1540. return -EINVAL;
  1541. event = strsep(&param, ":");
  1542. ret = -EINVAL;
  1543. event_enable_file = find_event_file(tr, system, event);
  1544. if (!event_enable_file)
  1545. goto out;
  1546. #ifdef CONFIG_HIST_TRIGGERS
  1547. hist = ((strcmp(cmd, ENABLE_HIST_STR) == 0) ||
  1548. (strcmp(cmd, DISABLE_HIST_STR) == 0));
  1549. enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
  1550. (strcmp(cmd, ENABLE_HIST_STR) == 0));
  1551. #else
  1552. enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
  1553. #endif
  1554. ret = -ENOMEM;
  1555. enable_data = kzalloc(sizeof(*enable_data), GFP_KERNEL);
  1556. if (!enable_data)
  1557. goto out;
  1558. enable_data->hist = hist;
  1559. enable_data->enable = enable;
  1560. enable_data->file = event_enable_file;
  1561. trigger_data = trigger_data_alloc(cmd_ops, cmd, param, enable_data);
  1562. if (!trigger_data) {
  1563. kfree(enable_data);
  1564. goto out;
  1565. }
  1566. if (remove) {
  1567. event_trigger_unregister(cmd_ops, file, glob+1, trigger_data);
  1568. kfree(trigger_data);
  1569. kfree(enable_data);
  1570. ret = 0;
  1571. goto out;
  1572. }
  1573. /* Up the trigger_data count to make sure nothing frees it on failure */
  1574. event_trigger_init(trigger_data);
  1575. ret = event_trigger_parse_num(param, trigger_data);
  1576. if (ret)
  1577. goto out_free;
  1578. ret = event_trigger_set_filter(cmd_ops, file, filter, trigger_data);
  1579. if (ret < 0)
  1580. goto out_free;
  1581. /* Don't let event modules unload while probe registered */
  1582. ret = trace_event_try_get_ref(event_enable_file->event_call);
  1583. if (!ret) {
  1584. ret = -EBUSY;
  1585. goto out_free;
  1586. }
  1587. ret = trace_event_enable_disable(event_enable_file, 1, 1);
  1588. if (ret < 0)
  1589. goto out_put;
  1590. ret = event_trigger_register(cmd_ops, file, glob, trigger_data);
  1591. if (ret)
  1592. goto out_disable;
  1593. event_trigger_free(trigger_data);
  1594. out:
  1595. return ret;
  1596. out_disable:
  1597. trace_event_enable_disable(event_enable_file, 0, 1);
  1598. out_put:
  1599. trace_event_put_ref(event_enable_file->event_call);
  1600. out_free:
  1601. event_trigger_reset_filter(cmd_ops, trigger_data);
  1602. event_trigger_free(trigger_data);
  1603. kfree(enable_data);
  1604. goto out;
  1605. }
  1606. int event_enable_register_trigger(char *glob,
  1607. struct event_trigger_data *data,
  1608. struct trace_event_file *file)
  1609. {
  1610. struct enable_trigger_data *enable_data = data->private_data;
  1611. struct enable_trigger_data *test_enable_data;
  1612. struct event_trigger_data *test;
  1613. int ret = 0;
  1614. lockdep_assert_held(&event_mutex);
  1615. list_for_each_entry(test, &file->triggers, list) {
  1616. test_enable_data = test->private_data;
  1617. if (test_enable_data &&
  1618. (test->cmd_ops->trigger_type ==
  1619. data->cmd_ops->trigger_type) &&
  1620. (test_enable_data->file == enable_data->file)) {
  1621. ret = -EEXIST;
  1622. goto out;
  1623. }
  1624. }
  1625. if (data->ops->init) {
  1626. ret = data->ops->init(data);
  1627. if (ret < 0)
  1628. goto out;
  1629. }
  1630. list_add_rcu(&data->list, &file->triggers);
  1631. update_cond_flag(file);
  1632. ret = trace_event_trigger_enable_disable(file, 1);
  1633. if (ret < 0) {
  1634. list_del_rcu(&data->list);
  1635. update_cond_flag(file);
  1636. }
  1637. out:
  1638. return ret;
  1639. }
  1640. void event_enable_unregister_trigger(char *glob,
  1641. struct event_trigger_data *test,
  1642. struct trace_event_file *file)
  1643. {
  1644. struct enable_trigger_data *test_enable_data = test->private_data;
  1645. struct event_trigger_data *data = NULL, *iter;
  1646. struct enable_trigger_data *enable_data;
  1647. lockdep_assert_held(&event_mutex);
  1648. list_for_each_entry(iter, &file->triggers, list) {
  1649. enable_data = iter->private_data;
  1650. if (enable_data &&
  1651. (iter->cmd_ops->trigger_type ==
  1652. test->cmd_ops->trigger_type) &&
  1653. (enable_data->file == test_enable_data->file)) {
  1654. data = iter;
  1655. list_del_rcu(&data->list);
  1656. trace_event_trigger_enable_disable(file, 0);
  1657. update_cond_flag(file);
  1658. break;
  1659. }
  1660. }
  1661. if (data && data->ops->free)
  1662. data->ops->free(data);
  1663. }
  1664. static struct event_trigger_ops *
  1665. event_enable_get_trigger_ops(char *cmd, char *param)
  1666. {
  1667. struct event_trigger_ops *ops;
  1668. bool enable;
  1669. #ifdef CONFIG_HIST_TRIGGERS
  1670. enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
  1671. (strcmp(cmd, ENABLE_HIST_STR) == 0));
  1672. #else
  1673. enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
  1674. #endif
  1675. if (enable)
  1676. ops = param ? &event_enable_count_trigger_ops :
  1677. &event_enable_trigger_ops;
  1678. else
  1679. ops = param ? &event_disable_count_trigger_ops :
  1680. &event_disable_trigger_ops;
  1681. return ops;
  1682. }
  1683. static struct event_command trigger_enable_cmd = {
  1684. .name = ENABLE_EVENT_STR,
  1685. .trigger_type = ETT_EVENT_ENABLE,
  1686. .parse = event_enable_trigger_parse,
  1687. .reg = event_enable_register_trigger,
  1688. .unreg = event_enable_unregister_trigger,
  1689. .get_trigger_ops = event_enable_get_trigger_ops,
  1690. .set_filter = set_trigger_filter,
  1691. };
  1692. static struct event_command trigger_disable_cmd = {
  1693. .name = DISABLE_EVENT_STR,
  1694. .trigger_type = ETT_EVENT_ENABLE,
  1695. .parse = event_enable_trigger_parse,
  1696. .reg = event_enable_register_trigger,
  1697. .unreg = event_enable_unregister_trigger,
  1698. .get_trigger_ops = event_enable_get_trigger_ops,
  1699. .set_filter = set_trigger_filter,
  1700. };
  1701. static __init void unregister_trigger_enable_disable_cmds(void)
  1702. {
  1703. unregister_event_command(&trigger_enable_cmd);
  1704. unregister_event_command(&trigger_disable_cmd);
  1705. }
  1706. static __init int register_trigger_enable_disable_cmds(void)
  1707. {
  1708. int ret;
  1709. ret = register_event_command(&trigger_enable_cmd);
  1710. if (WARN_ON(ret < 0))
  1711. return ret;
  1712. ret = register_event_command(&trigger_disable_cmd);
  1713. if (WARN_ON(ret < 0))
  1714. unregister_trigger_enable_disable_cmds();
  1715. return ret;
  1716. }
  1717. static __init int register_trigger_traceon_traceoff_cmds(void)
  1718. {
  1719. int ret;
  1720. ret = register_event_command(&trigger_traceon_cmd);
  1721. if (WARN_ON(ret < 0))
  1722. return ret;
  1723. ret = register_event_command(&trigger_traceoff_cmd);
  1724. if (WARN_ON(ret < 0))
  1725. unregister_trigger_traceon_traceoff_cmds();
  1726. return ret;
  1727. }
  1728. __init int register_trigger_cmds(void)
  1729. {
  1730. register_trigger_traceon_traceoff_cmds();
  1731. register_trigger_snapshot_cmd();
  1732. register_trigger_stacktrace_cmd();
  1733. register_trigger_enable_disable_cmds();
  1734. register_trigger_hist_enable_disable_cmds();
  1735. register_trigger_hist_cmd();
  1736. return 0;
  1737. }