trace_events_trigger.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635
  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/module.h>
  8. #include <linux/ctype.h>
  9. #include <linux/mutex.h>
  10. #include <linux/slab.h>
  11. #include <linux/rculist.h>
  12. #include "trace.h"
  13. static LIST_HEAD(trigger_commands);
  14. static DEFINE_MUTEX(trigger_cmd_mutex);
  15. void trigger_data_free(struct event_trigger_data *data)
  16. {
  17. if (data->cmd_ops->set_filter)
  18. data->cmd_ops->set_filter(NULL, data, NULL);
  19. /* make sure current triggers exit before free */
  20. tracepoint_synchronize_unregister();
  21. kfree(data);
  22. }
  23. /**
  24. * event_triggers_call - Call triggers associated with a trace event
  25. * @file: The trace_event_file associated with the event
  26. * @rec: The trace entry for the event, NULL for unconditional invocation
  27. *
  28. * For each trigger associated with an event, invoke the trigger
  29. * function registered with the associated trigger command. If rec is
  30. * non-NULL, it means that the trigger requires further processing and
  31. * shouldn't be unconditionally invoked. If rec is non-NULL and the
  32. * trigger has a filter associated with it, rec will checked against
  33. * the filter and if the record matches the trigger will be invoked.
  34. * If the trigger is a 'post_trigger', meaning it shouldn't be invoked
  35. * in any case until the current event is written, the trigger
  36. * function isn't invoked but the bit associated with the deferred
  37. * trigger is set in the return value.
  38. *
  39. * Returns an enum event_trigger_type value containing a set bit for
  40. * any trigger that should be deferred, ETT_NONE if nothing to defer.
  41. *
  42. * Called from tracepoint handlers (with rcu_read_lock_sched() held).
  43. *
  44. * Return: an enum event_trigger_type value containing a set bit for
  45. * any trigger that should be deferred, ETT_NONE if nothing to defer.
  46. */
  47. enum event_trigger_type
  48. event_triggers_call(struct trace_event_file *file, void *rec,
  49. struct ring_buffer_event *event)
  50. {
  51. struct event_trigger_data *data;
  52. enum event_trigger_type tt = ETT_NONE;
  53. struct event_filter *filter;
  54. if (list_empty(&file->triggers))
  55. return tt;
  56. list_for_each_entry_rcu(data, &file->triggers, list) {
  57. if (data->paused)
  58. continue;
  59. if (!rec) {
  60. data->ops->func(data, rec, event);
  61. continue;
  62. }
  63. filter = rcu_dereference_sched(data->filter);
  64. if (filter && !filter_match_preds(filter, rec))
  65. continue;
  66. if (event_command_post_trigger(data->cmd_ops)) {
  67. tt |= data->cmd_ops->trigger_type;
  68. continue;
  69. }
  70. data->ops->func(data, rec, event);
  71. }
  72. return tt;
  73. }
  74. EXPORT_SYMBOL_GPL(event_triggers_call);
  75. /**
  76. * event_triggers_post_call - Call 'post_triggers' for a trace event
  77. * @file: The trace_event_file associated with the event
  78. * @tt: enum event_trigger_type containing a set bit for each trigger to invoke
  79. *
  80. * For each trigger associated with an event, invoke the trigger
  81. * function registered with the associated trigger command, if the
  82. * corresponding bit is set in the tt enum passed into this function.
  83. * See @event_triggers_call for details on how those bits are set.
  84. *
  85. * Called from tracepoint handlers (with rcu_read_lock_sched() held).
  86. */
  87. void
  88. event_triggers_post_call(struct trace_event_file *file,
  89. enum event_trigger_type tt)
  90. {
  91. struct event_trigger_data *data;
  92. list_for_each_entry_rcu(data, &file->triggers, list) {
  93. if (data->paused)
  94. continue;
  95. if (data->cmd_ops->trigger_type & tt)
  96. data->ops->func(data, NULL, NULL);
  97. }
  98. }
  99. EXPORT_SYMBOL_GPL(event_triggers_post_call);
  100. #define SHOW_AVAILABLE_TRIGGERS (void *)(1UL)
  101. static void *trigger_next(struct seq_file *m, void *t, loff_t *pos)
  102. {
  103. struct trace_event_file *event_file = event_file_data(m->private);
  104. if (t == SHOW_AVAILABLE_TRIGGERS)
  105. return NULL;
  106. return seq_list_next(t, &event_file->triggers, pos);
  107. }
  108. static void *trigger_start(struct seq_file *m, loff_t *pos)
  109. {
  110. struct trace_event_file *event_file;
  111. /* ->stop() is called even if ->start() fails */
  112. mutex_lock(&event_mutex);
  113. event_file = event_file_data(m->private);
  114. if (unlikely(!event_file))
  115. return ERR_PTR(-ENODEV);
  116. if (list_empty(&event_file->triggers))
  117. return *pos == 0 ? SHOW_AVAILABLE_TRIGGERS : NULL;
  118. return seq_list_start(&event_file->triggers, *pos);
  119. }
  120. static void trigger_stop(struct seq_file *m, void *t)
  121. {
  122. mutex_unlock(&event_mutex);
  123. }
  124. static int trigger_show(struct seq_file *m, void *v)
  125. {
  126. struct event_trigger_data *data;
  127. struct event_command *p;
  128. if (v == SHOW_AVAILABLE_TRIGGERS) {
  129. seq_puts(m, "# Available triggers:\n");
  130. seq_putc(m, '#');
  131. mutex_lock(&trigger_cmd_mutex);
  132. list_for_each_entry_reverse(p, &trigger_commands, list)
  133. seq_printf(m, " %s", p->name);
  134. seq_putc(m, '\n');
  135. mutex_unlock(&trigger_cmd_mutex);
  136. return 0;
  137. }
  138. data = list_entry(v, struct event_trigger_data, list);
  139. data->ops->print(m, data->ops, data);
  140. return 0;
  141. }
  142. static const struct seq_operations event_triggers_seq_ops = {
  143. .start = trigger_start,
  144. .next = trigger_next,
  145. .stop = trigger_stop,
  146. .show = trigger_show,
  147. };
  148. static int event_trigger_regex_open(struct inode *inode, struct file *file)
  149. {
  150. int ret = 0;
  151. mutex_lock(&event_mutex);
  152. if (unlikely(!event_file_data(file))) {
  153. mutex_unlock(&event_mutex);
  154. return -ENODEV;
  155. }
  156. if ((file->f_mode & FMODE_WRITE) &&
  157. (file->f_flags & O_TRUNC)) {
  158. struct trace_event_file *event_file;
  159. struct event_command *p;
  160. event_file = event_file_data(file);
  161. list_for_each_entry(p, &trigger_commands, list) {
  162. if (p->unreg_all)
  163. p->unreg_all(event_file);
  164. }
  165. }
  166. if (file->f_mode & FMODE_READ) {
  167. ret = seq_open(file, &event_triggers_seq_ops);
  168. if (!ret) {
  169. struct seq_file *m = file->private_data;
  170. m->private = file;
  171. }
  172. }
  173. mutex_unlock(&event_mutex);
  174. return ret;
  175. }
  176. static int trigger_process_regex(struct trace_event_file *file, char *buff)
  177. {
  178. char *command, *next = buff;
  179. struct event_command *p;
  180. int ret = -EINVAL;
  181. command = strsep(&next, ": \t");
  182. command = (command[0] != '!') ? command : command + 1;
  183. mutex_lock(&trigger_cmd_mutex);
  184. list_for_each_entry(p, &trigger_commands, list) {
  185. if (strcmp(p->name, command) == 0) {
  186. ret = p->func(p, file, buff, command, next);
  187. goto out_unlock;
  188. }
  189. }
  190. out_unlock:
  191. mutex_unlock(&trigger_cmd_mutex);
  192. return ret;
  193. }
  194. static ssize_t event_trigger_regex_write(struct file *file,
  195. const char __user *ubuf,
  196. size_t cnt, loff_t *ppos)
  197. {
  198. struct trace_event_file *event_file;
  199. ssize_t ret;
  200. char *buf;
  201. if (!cnt)
  202. return 0;
  203. if (cnt >= PAGE_SIZE)
  204. return -EINVAL;
  205. buf = memdup_user_nul(ubuf, cnt);
  206. if (IS_ERR(buf))
  207. return PTR_ERR(buf);
  208. strim(buf);
  209. mutex_lock(&event_mutex);
  210. event_file = event_file_data(file);
  211. if (unlikely(!event_file)) {
  212. mutex_unlock(&event_mutex);
  213. kfree(buf);
  214. return -ENODEV;
  215. }
  216. ret = trigger_process_regex(event_file, buf);
  217. mutex_unlock(&event_mutex);
  218. kfree(buf);
  219. if (ret < 0)
  220. goto out;
  221. *ppos += cnt;
  222. ret = cnt;
  223. out:
  224. return ret;
  225. }
  226. static int event_trigger_regex_release(struct inode *inode, struct file *file)
  227. {
  228. mutex_lock(&event_mutex);
  229. if (file->f_mode & FMODE_READ)
  230. seq_release(inode, file);
  231. mutex_unlock(&event_mutex);
  232. return 0;
  233. }
  234. static ssize_t
  235. event_trigger_write(struct file *filp, const char __user *ubuf,
  236. size_t cnt, loff_t *ppos)
  237. {
  238. return event_trigger_regex_write(filp, ubuf, cnt, ppos);
  239. }
  240. static int
  241. event_trigger_open(struct inode *inode, struct file *filp)
  242. {
  243. return event_trigger_regex_open(inode, filp);
  244. }
  245. static int
  246. event_trigger_release(struct inode *inode, struct file *file)
  247. {
  248. return event_trigger_regex_release(inode, file);
  249. }
  250. const struct file_operations event_trigger_fops = {
  251. .open = event_trigger_open,
  252. .read = seq_read,
  253. .write = event_trigger_write,
  254. .llseek = tracing_lseek,
  255. .release = event_trigger_release,
  256. };
  257. /*
  258. * Currently we only register event commands from __init, so mark this
  259. * __init too.
  260. */
  261. __init int register_event_command(struct event_command *cmd)
  262. {
  263. struct event_command *p;
  264. int ret = 0;
  265. mutex_lock(&trigger_cmd_mutex);
  266. list_for_each_entry(p, &trigger_commands, list) {
  267. if (strcmp(cmd->name, p->name) == 0) {
  268. ret = -EBUSY;
  269. goto out_unlock;
  270. }
  271. }
  272. list_add(&cmd->list, &trigger_commands);
  273. out_unlock:
  274. mutex_unlock(&trigger_cmd_mutex);
  275. return ret;
  276. }
  277. /*
  278. * Currently we only unregister event commands from __init, so mark
  279. * this __init too.
  280. */
  281. __init int unregister_event_command(struct event_command *cmd)
  282. {
  283. struct event_command *p, *n;
  284. int ret = -ENODEV;
  285. mutex_lock(&trigger_cmd_mutex);
  286. list_for_each_entry_safe(p, n, &trigger_commands, list) {
  287. if (strcmp(cmd->name, p->name) == 0) {
  288. ret = 0;
  289. list_del_init(&p->list);
  290. goto out_unlock;
  291. }
  292. }
  293. out_unlock:
  294. mutex_unlock(&trigger_cmd_mutex);
  295. return ret;
  296. }
  297. /**
  298. * event_trigger_print - Generic event_trigger_ops @print implementation
  299. * @name: The name of the event trigger
  300. * @m: The seq_file being printed to
  301. * @data: Trigger-specific data
  302. * @filter_str: filter_str to print, if present
  303. *
  304. * Common implementation for event triggers to print themselves.
  305. *
  306. * Usually wrapped by a function that simply sets the @name of the
  307. * trigger command and then invokes this.
  308. *
  309. * Return: 0 on success, errno otherwise
  310. */
  311. static int
  312. event_trigger_print(const char *name, struct seq_file *m,
  313. void *data, char *filter_str)
  314. {
  315. long count = (long)data;
  316. seq_puts(m, name);
  317. if (count == -1)
  318. seq_puts(m, ":unlimited");
  319. else
  320. seq_printf(m, ":count=%ld", count);
  321. if (filter_str)
  322. seq_printf(m, " if %s\n", filter_str);
  323. else
  324. seq_putc(m, '\n');
  325. return 0;
  326. }
  327. /**
  328. * event_trigger_init - Generic event_trigger_ops @init implementation
  329. * @ops: The trigger ops associated with the trigger
  330. * @data: Trigger-specific data
  331. *
  332. * Common implementation of event trigger initialization.
  333. *
  334. * Usually used directly as the @init method in event trigger
  335. * implementations.
  336. *
  337. * Return: 0 on success, errno otherwise
  338. */
  339. int event_trigger_init(struct event_trigger_ops *ops,
  340. struct event_trigger_data *data)
  341. {
  342. data->ref++;
  343. return 0;
  344. }
  345. /**
  346. * event_trigger_free - Generic event_trigger_ops @free implementation
  347. * @ops: The trigger ops associated with the trigger
  348. * @data: Trigger-specific data
  349. *
  350. * Common implementation of event trigger de-initialization.
  351. *
  352. * Usually used directly as the @free method in event trigger
  353. * implementations.
  354. */
  355. static void
  356. event_trigger_free(struct event_trigger_ops *ops,
  357. struct event_trigger_data *data)
  358. {
  359. if (WARN_ON_ONCE(data->ref <= 0))
  360. return;
  361. data->ref--;
  362. if (!data->ref)
  363. trigger_data_free(data);
  364. }
  365. int trace_event_trigger_enable_disable(struct trace_event_file *file,
  366. int trigger_enable)
  367. {
  368. int ret = 0;
  369. if (trigger_enable) {
  370. if (atomic_inc_return(&file->tm_ref) > 1)
  371. return ret;
  372. set_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
  373. ret = trace_event_enable_disable(file, 1, 1);
  374. } else {
  375. if (atomic_dec_return(&file->tm_ref) > 0)
  376. return ret;
  377. clear_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
  378. ret = trace_event_enable_disable(file, 0, 1);
  379. }
  380. return ret;
  381. }
  382. /**
  383. * clear_event_triggers - Clear all triggers associated with a trace array
  384. * @tr: The trace array to clear
  385. *
  386. * For each trigger, the triggering event has its tm_ref decremented
  387. * via trace_event_trigger_enable_disable(), and any associated event
  388. * (in the case of enable/disable_event triggers) will have its sm_ref
  389. * decremented via free()->trace_event_enable_disable(). That
  390. * combination effectively reverses the soft-mode/trigger state added
  391. * by trigger registration.
  392. *
  393. * Must be called with event_mutex held.
  394. */
  395. void
  396. clear_event_triggers(struct trace_array *tr)
  397. {
  398. struct trace_event_file *file;
  399. list_for_each_entry(file, &tr->events, list) {
  400. struct event_trigger_data *data, *n;
  401. list_for_each_entry_safe(data, n, &file->triggers, list) {
  402. trace_event_trigger_enable_disable(file, 0);
  403. list_del_rcu(&data->list);
  404. if (data->ops->free)
  405. data->ops->free(data->ops, data);
  406. }
  407. }
  408. }
  409. /**
  410. * update_cond_flag - Set or reset the TRIGGER_COND bit
  411. * @file: The trace_event_file associated with the event
  412. *
  413. * If an event has triggers and any of those triggers has a filter or
  414. * a post_trigger, trigger invocation needs to be deferred until after
  415. * the current event has logged its data, and the event should have
  416. * its TRIGGER_COND bit set, otherwise the TRIGGER_COND bit should be
  417. * cleared.
  418. */
  419. void update_cond_flag(struct trace_event_file *file)
  420. {
  421. struct event_trigger_data *data;
  422. bool set_cond = false;
  423. list_for_each_entry_rcu(data, &file->triggers, list) {
  424. if (data->filter || event_command_post_trigger(data->cmd_ops) ||
  425. event_command_needs_rec(data->cmd_ops)) {
  426. set_cond = true;
  427. break;
  428. }
  429. }
  430. if (set_cond)
  431. set_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
  432. else
  433. clear_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
  434. }
  435. /**
  436. * register_trigger - Generic event_command @reg implementation
  437. * @glob: The raw string used to register the trigger
  438. * @ops: The trigger ops associated with the trigger
  439. * @data: Trigger-specific data to associate with the trigger
  440. * @file: The trace_event_file associated with the event
  441. *
  442. * Common implementation for event trigger registration.
  443. *
  444. * Usually used directly as the @reg method in event command
  445. * implementations.
  446. *
  447. * Return: 0 on success, errno otherwise
  448. */
  449. static int register_trigger(char *glob, struct event_trigger_ops *ops,
  450. struct event_trigger_data *data,
  451. struct trace_event_file *file)
  452. {
  453. struct event_trigger_data *test;
  454. int ret = 0;
  455. list_for_each_entry_rcu(test, &file->triggers, list) {
  456. if (test->cmd_ops->trigger_type == data->cmd_ops->trigger_type) {
  457. ret = -EEXIST;
  458. goto out;
  459. }
  460. }
  461. if (data->ops->init) {
  462. ret = data->ops->init(data->ops, data);
  463. if (ret < 0)
  464. goto out;
  465. }
  466. list_add_rcu(&data->list, &file->triggers);
  467. ret++;
  468. update_cond_flag(file);
  469. if (trace_event_trigger_enable_disable(file, 1) < 0) {
  470. list_del_rcu(&data->list);
  471. update_cond_flag(file);
  472. ret--;
  473. }
  474. out:
  475. return ret;
  476. }
  477. /**
  478. * unregister_trigger - Generic event_command @unreg implementation
  479. * @glob: The raw string used to register the trigger
  480. * @ops: The trigger ops associated with the trigger
  481. * @test: Trigger-specific data used to find the trigger to remove
  482. * @file: The trace_event_file associated with the event
  483. *
  484. * Common implementation for event trigger unregistration.
  485. *
  486. * Usually used directly as the @unreg method in event command
  487. * implementations.
  488. */
  489. static void unregister_trigger(char *glob, struct event_trigger_ops *ops,
  490. struct event_trigger_data *test,
  491. struct trace_event_file *file)
  492. {
  493. struct event_trigger_data *data;
  494. bool unregistered = false;
  495. list_for_each_entry_rcu(data, &file->triggers, list) {
  496. if (data->cmd_ops->trigger_type == test->cmd_ops->trigger_type) {
  497. unregistered = true;
  498. list_del_rcu(&data->list);
  499. trace_event_trigger_enable_disable(file, 0);
  500. update_cond_flag(file);
  501. break;
  502. }
  503. }
  504. if (unregistered && data->ops->free)
  505. data->ops->free(data->ops, data);
  506. }
  507. /**
  508. * event_trigger_callback - Generic event_command @func implementation
  509. * @cmd_ops: The command ops, used for trigger registration
  510. * @file: The trace_event_file associated with the event
  511. * @glob: The raw string used to register the trigger
  512. * @cmd: The cmd portion of the string used to register the trigger
  513. * @param: The params portion of the string used to register the trigger
  514. *
  515. * Common implementation for event command parsing and trigger
  516. * instantiation.
  517. *
  518. * Usually used directly as the @func method in event command
  519. * implementations.
  520. *
  521. * Return: 0 on success, errno otherwise
  522. */
  523. static int
  524. event_trigger_callback(struct event_command *cmd_ops,
  525. struct trace_event_file *file,
  526. char *glob, char *cmd, char *param)
  527. {
  528. struct event_trigger_data *trigger_data;
  529. struct event_trigger_ops *trigger_ops;
  530. char *trigger = NULL;
  531. char *number;
  532. int ret;
  533. /* separate the trigger from the filter (t:n [if filter]) */
  534. if (param && isdigit(param[0]))
  535. trigger = strsep(&param, " \t");
  536. trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
  537. ret = -ENOMEM;
  538. trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
  539. if (!trigger_data)
  540. goto out;
  541. trigger_data->count = -1;
  542. trigger_data->ops = trigger_ops;
  543. trigger_data->cmd_ops = cmd_ops;
  544. trigger_data->private_data = file;
  545. INIT_LIST_HEAD(&trigger_data->list);
  546. INIT_LIST_HEAD(&trigger_data->named_list);
  547. if (glob[0] == '!') {
  548. cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
  549. kfree(trigger_data);
  550. ret = 0;
  551. goto out;
  552. }
  553. if (trigger) {
  554. number = strsep(&trigger, ":");
  555. ret = -EINVAL;
  556. if (!strlen(number))
  557. goto out_free;
  558. /*
  559. * We use the callback data field (which is a pointer)
  560. * as our counter.
  561. */
  562. ret = kstrtoul(number, 0, &trigger_data->count);
  563. if (ret)
  564. goto out_free;
  565. }
  566. if (!param) /* if param is non-empty, it's supposed to be a filter */
  567. goto out_reg;
  568. if (!cmd_ops->set_filter)
  569. goto out_reg;
  570. ret = cmd_ops->set_filter(param, trigger_data, file);
  571. if (ret < 0)
  572. goto out_free;
  573. out_reg:
  574. /* Up the trigger_data count to make sure reg doesn't free it on failure */
  575. event_trigger_init(trigger_ops, trigger_data);
  576. ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
  577. /*
  578. * The above returns on success the # of functions enabled,
  579. * but if it didn't find any functions it returns zero.
  580. * Consider no functions a failure too.
  581. */
  582. if (!ret) {
  583. cmd_ops->unreg(glob, trigger_ops, trigger_data, file);
  584. ret = -ENOENT;
  585. } else if (ret > 0)
  586. ret = 0;
  587. /* Down the counter of trigger_data or free it if not used anymore */
  588. event_trigger_free(trigger_ops, trigger_data);
  589. out:
  590. return ret;
  591. out_free:
  592. if (cmd_ops->set_filter)
  593. cmd_ops->set_filter(NULL, trigger_data, NULL);
  594. kfree(trigger_data);
  595. goto out;
  596. }
  597. /**
  598. * set_trigger_filter - Generic event_command @set_filter implementation
  599. * @filter_str: The filter string for the trigger, NULL to remove filter
  600. * @trigger_data: Trigger-specific data
  601. * @file: The trace_event_file associated with the event
  602. *
  603. * Common implementation for event command filter parsing and filter
  604. * instantiation.
  605. *
  606. * Usually used directly as the @set_filter method in event command
  607. * implementations.
  608. *
  609. * Also used to remove a filter (if filter_str = NULL).
  610. *
  611. * Return: 0 on success, errno otherwise
  612. */
  613. int set_trigger_filter(char *filter_str,
  614. struct event_trigger_data *trigger_data,
  615. struct trace_event_file *file)
  616. {
  617. struct event_trigger_data *data = trigger_data;
  618. struct event_filter *filter = NULL, *tmp;
  619. int ret = -EINVAL;
  620. char *s;
  621. if (!filter_str) /* clear the current filter */
  622. goto assign;
  623. s = strsep(&filter_str, " \t");
  624. if (!strlen(s) || strcmp(s, "if") != 0)
  625. goto out;
  626. if (!filter_str)
  627. goto out;
  628. /* The filter is for the 'trigger' event, not the triggered event */
  629. ret = create_event_filter(file->event_call, filter_str, false, &filter);
  630. if (ret)
  631. goto out;
  632. assign:
  633. tmp = rcu_access_pointer(data->filter);
  634. rcu_assign_pointer(data->filter, filter);
  635. if (tmp) {
  636. /* Make sure the call is done with the filter */
  637. tracepoint_synchronize_unregister();
  638. free_event_filter(tmp);
  639. }
  640. kfree(data->filter_str);
  641. data->filter_str = NULL;
  642. if (filter_str) {
  643. data->filter_str = kstrdup(filter_str, GFP_KERNEL);
  644. if (!data->filter_str) {
  645. free_event_filter(rcu_access_pointer(data->filter));
  646. data->filter = NULL;
  647. ret = -ENOMEM;
  648. }
  649. }
  650. out:
  651. return ret;
  652. }
  653. static LIST_HEAD(named_triggers);
  654. /**
  655. * find_named_trigger - Find the common named trigger associated with @name
  656. * @name: The name of the set of named triggers to find the common data for
  657. *
  658. * Named triggers are sets of triggers that share a common set of
  659. * trigger data. The first named trigger registered with a given name
  660. * owns the common trigger data that the others subsequently
  661. * registered with the same name will reference. This function
  662. * returns the common trigger data associated with that first
  663. * registered instance.
  664. *
  665. * Return: the common trigger data for the given named trigger on
  666. * success, NULL otherwise.
  667. */
  668. struct event_trigger_data *find_named_trigger(const char *name)
  669. {
  670. struct event_trigger_data *data;
  671. if (!name)
  672. return NULL;
  673. list_for_each_entry(data, &named_triggers, named_list) {
  674. if (data->named_data)
  675. continue;
  676. if (strcmp(data->name, name) == 0)
  677. return data;
  678. }
  679. return NULL;
  680. }
  681. /**
  682. * is_named_trigger - determine if a given trigger is a named trigger
  683. * @test: The trigger data to test
  684. *
  685. * Return: true if 'test' is a named trigger, false otherwise.
  686. */
  687. bool is_named_trigger(struct event_trigger_data *test)
  688. {
  689. struct event_trigger_data *data;
  690. list_for_each_entry(data, &named_triggers, named_list) {
  691. if (test == data)
  692. return true;
  693. }
  694. return false;
  695. }
  696. /**
  697. * save_named_trigger - save the trigger in the named trigger list
  698. * @name: The name of the named trigger set
  699. * @data: The trigger data to save
  700. *
  701. * Return: 0 if successful, negative error otherwise.
  702. */
  703. int save_named_trigger(const char *name, struct event_trigger_data *data)
  704. {
  705. data->name = kstrdup(name, GFP_KERNEL);
  706. if (!data->name)
  707. return -ENOMEM;
  708. list_add(&data->named_list, &named_triggers);
  709. return 0;
  710. }
  711. /**
  712. * del_named_trigger - delete a trigger from the named trigger list
  713. * @data: The trigger data to delete
  714. */
  715. void del_named_trigger(struct event_trigger_data *data)
  716. {
  717. kfree(data->name);
  718. data->name = NULL;
  719. list_del(&data->named_list);
  720. }
  721. static void __pause_named_trigger(struct event_trigger_data *data, bool pause)
  722. {
  723. struct event_trigger_data *test;
  724. list_for_each_entry(test, &named_triggers, named_list) {
  725. if (strcmp(test->name, data->name) == 0) {
  726. if (pause) {
  727. test->paused_tmp = test->paused;
  728. test->paused = true;
  729. } else {
  730. test->paused = test->paused_tmp;
  731. }
  732. }
  733. }
  734. }
  735. /**
  736. * pause_named_trigger - Pause all named triggers with the same name
  737. * @data: The trigger data of a named trigger to pause
  738. *
  739. * Pauses a named trigger along with all other triggers having the
  740. * same name. Because named triggers share a common set of data,
  741. * pausing only one is meaningless, so pausing one named trigger needs
  742. * to pause all triggers with the same name.
  743. */
  744. void pause_named_trigger(struct event_trigger_data *data)
  745. {
  746. __pause_named_trigger(data, true);
  747. }
  748. /**
  749. * unpause_named_trigger - Un-pause all named triggers with the same name
  750. * @data: The trigger data of a named trigger to unpause
  751. *
  752. * Un-pauses a named trigger along with all other triggers having the
  753. * same name. Because named triggers share a common set of data,
  754. * unpausing only one is meaningless, so unpausing one named trigger
  755. * needs to unpause all triggers with the same name.
  756. */
  757. void unpause_named_trigger(struct event_trigger_data *data)
  758. {
  759. __pause_named_trigger(data, false);
  760. }
  761. /**
  762. * set_named_trigger_data - Associate common named trigger data
  763. * @data: The trigger data of a named trigger to unpause
  764. *
  765. * Named triggers are sets of triggers that share a common set of
  766. * trigger data. The first named trigger registered with a given name
  767. * owns the common trigger data that the others subsequently
  768. * registered with the same name will reference. This function
  769. * associates the common trigger data from the first trigger with the
  770. * given trigger.
  771. */
  772. void set_named_trigger_data(struct event_trigger_data *data,
  773. struct event_trigger_data *named_data)
  774. {
  775. data->named_data = named_data;
  776. }
  777. struct event_trigger_data *
  778. get_named_trigger_data(struct event_trigger_data *data)
  779. {
  780. return data->named_data;
  781. }
  782. static void
  783. traceon_trigger(struct event_trigger_data *data, void *rec,
  784. struct ring_buffer_event *event)
  785. {
  786. if (tracing_is_on())
  787. return;
  788. tracing_on();
  789. }
  790. static void
  791. traceon_count_trigger(struct event_trigger_data *data, void *rec,
  792. struct ring_buffer_event *event)
  793. {
  794. if (tracing_is_on())
  795. return;
  796. if (!data->count)
  797. return;
  798. if (data->count != -1)
  799. (data->count)--;
  800. tracing_on();
  801. }
  802. static void
  803. traceoff_trigger(struct event_trigger_data *data, void *rec,
  804. struct ring_buffer_event *event)
  805. {
  806. if (!tracing_is_on())
  807. return;
  808. tracing_off();
  809. }
  810. static void
  811. traceoff_count_trigger(struct event_trigger_data *data, void *rec,
  812. struct ring_buffer_event *event)
  813. {
  814. if (!tracing_is_on())
  815. return;
  816. if (!data->count)
  817. return;
  818. if (data->count != -1)
  819. (data->count)--;
  820. tracing_off();
  821. }
  822. static int
  823. traceon_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
  824. struct event_trigger_data *data)
  825. {
  826. return event_trigger_print("traceon", m, (void *)data->count,
  827. data->filter_str);
  828. }
  829. static int
  830. traceoff_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
  831. struct event_trigger_data *data)
  832. {
  833. return event_trigger_print("traceoff", m, (void *)data->count,
  834. data->filter_str);
  835. }
  836. static struct event_trigger_ops traceon_trigger_ops = {
  837. .func = traceon_trigger,
  838. .print = traceon_trigger_print,
  839. .init = event_trigger_init,
  840. .free = event_trigger_free,
  841. };
  842. static struct event_trigger_ops traceon_count_trigger_ops = {
  843. .func = traceon_count_trigger,
  844. .print = traceon_trigger_print,
  845. .init = event_trigger_init,
  846. .free = event_trigger_free,
  847. };
  848. static struct event_trigger_ops traceoff_trigger_ops = {
  849. .func = traceoff_trigger,
  850. .print = traceoff_trigger_print,
  851. .init = event_trigger_init,
  852. .free = event_trigger_free,
  853. };
  854. static struct event_trigger_ops traceoff_count_trigger_ops = {
  855. .func = traceoff_count_trigger,
  856. .print = traceoff_trigger_print,
  857. .init = event_trigger_init,
  858. .free = event_trigger_free,
  859. };
  860. static struct event_trigger_ops *
  861. onoff_get_trigger_ops(char *cmd, char *param)
  862. {
  863. struct event_trigger_ops *ops;
  864. /* we register both traceon and traceoff to this callback */
  865. if (strcmp(cmd, "traceon") == 0)
  866. ops = param ? &traceon_count_trigger_ops :
  867. &traceon_trigger_ops;
  868. else
  869. ops = param ? &traceoff_count_trigger_ops :
  870. &traceoff_trigger_ops;
  871. return ops;
  872. }
  873. static struct event_command trigger_traceon_cmd = {
  874. .name = "traceon",
  875. .trigger_type = ETT_TRACE_ONOFF,
  876. .func = event_trigger_callback,
  877. .reg = register_trigger,
  878. .unreg = unregister_trigger,
  879. .get_trigger_ops = onoff_get_trigger_ops,
  880. .set_filter = set_trigger_filter,
  881. };
  882. static struct event_command trigger_traceoff_cmd = {
  883. .name = "traceoff",
  884. .trigger_type = ETT_TRACE_ONOFF,
  885. .flags = EVENT_CMD_FL_POST_TRIGGER,
  886. .func = event_trigger_callback,
  887. .reg = register_trigger,
  888. .unreg = unregister_trigger,
  889. .get_trigger_ops = onoff_get_trigger_ops,
  890. .set_filter = set_trigger_filter,
  891. };
  892. #ifdef CONFIG_TRACER_SNAPSHOT
  893. static void
  894. snapshot_trigger(struct event_trigger_data *data, void *rec,
  895. struct ring_buffer_event *event)
  896. {
  897. struct trace_event_file *file = data->private_data;
  898. if (file)
  899. tracing_snapshot_instance(file->tr);
  900. else
  901. tracing_snapshot();
  902. }
  903. static void
  904. snapshot_count_trigger(struct event_trigger_data *data, void *rec,
  905. struct ring_buffer_event *event)
  906. {
  907. if (!data->count)
  908. return;
  909. if (data->count != -1)
  910. (data->count)--;
  911. snapshot_trigger(data, rec, event);
  912. }
  913. static int
  914. register_snapshot_trigger(char *glob, struct event_trigger_ops *ops,
  915. struct event_trigger_data *data,
  916. struct trace_event_file *file)
  917. {
  918. int ret = register_trigger(glob, ops, data, file);
  919. if (ret > 0 && tracing_alloc_snapshot_instance(file->tr) != 0) {
  920. unregister_trigger(glob, ops, data, file);
  921. ret = 0;
  922. }
  923. return ret;
  924. }
  925. static int
  926. snapshot_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
  927. struct event_trigger_data *data)
  928. {
  929. return event_trigger_print("snapshot", m, (void *)data->count,
  930. data->filter_str);
  931. }
  932. static struct event_trigger_ops snapshot_trigger_ops = {
  933. .func = snapshot_trigger,
  934. .print = snapshot_trigger_print,
  935. .init = event_trigger_init,
  936. .free = event_trigger_free,
  937. };
  938. static struct event_trigger_ops snapshot_count_trigger_ops = {
  939. .func = snapshot_count_trigger,
  940. .print = snapshot_trigger_print,
  941. .init = event_trigger_init,
  942. .free = event_trigger_free,
  943. };
  944. static struct event_trigger_ops *
  945. snapshot_get_trigger_ops(char *cmd, char *param)
  946. {
  947. return param ? &snapshot_count_trigger_ops : &snapshot_trigger_ops;
  948. }
  949. static struct event_command trigger_snapshot_cmd = {
  950. .name = "snapshot",
  951. .trigger_type = ETT_SNAPSHOT,
  952. .func = event_trigger_callback,
  953. .reg = register_snapshot_trigger,
  954. .unreg = unregister_trigger,
  955. .get_trigger_ops = snapshot_get_trigger_ops,
  956. .set_filter = set_trigger_filter,
  957. };
  958. static __init int register_trigger_snapshot_cmd(void)
  959. {
  960. int ret;
  961. ret = register_event_command(&trigger_snapshot_cmd);
  962. WARN_ON(ret < 0);
  963. return ret;
  964. }
  965. #else
  966. static __init int register_trigger_snapshot_cmd(void) { return 0; }
  967. #endif /* CONFIG_TRACER_SNAPSHOT */
  968. #ifdef CONFIG_STACKTRACE
  969. #ifdef CONFIG_UNWINDER_ORC
  970. /* Skip 2:
  971. * event_triggers_post_call()
  972. * trace_event_raw_event_xxx()
  973. */
  974. # define STACK_SKIP 2
  975. #else
  976. /*
  977. * Skip 4:
  978. * stacktrace_trigger()
  979. * event_triggers_post_call()
  980. * trace_event_buffer_commit()
  981. * trace_event_raw_event_xxx()
  982. */
  983. #define STACK_SKIP 4
  984. #endif
  985. static void
  986. stacktrace_trigger(struct event_trigger_data *data, void *rec,
  987. struct ring_buffer_event *event)
  988. {
  989. trace_dump_stack(STACK_SKIP);
  990. }
  991. static void
  992. stacktrace_count_trigger(struct event_trigger_data *data, void *rec,
  993. struct ring_buffer_event *event)
  994. {
  995. if (!data->count)
  996. return;
  997. if (data->count != -1)
  998. (data->count)--;
  999. stacktrace_trigger(data, rec, event);
  1000. }
  1001. static int
  1002. stacktrace_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
  1003. struct event_trigger_data *data)
  1004. {
  1005. return event_trigger_print("stacktrace", m, (void *)data->count,
  1006. data->filter_str);
  1007. }
  1008. static struct event_trigger_ops stacktrace_trigger_ops = {
  1009. .func = stacktrace_trigger,
  1010. .print = stacktrace_trigger_print,
  1011. .init = event_trigger_init,
  1012. .free = event_trigger_free,
  1013. };
  1014. static struct event_trigger_ops stacktrace_count_trigger_ops = {
  1015. .func = stacktrace_count_trigger,
  1016. .print = stacktrace_trigger_print,
  1017. .init = event_trigger_init,
  1018. .free = event_trigger_free,
  1019. };
  1020. static struct event_trigger_ops *
  1021. stacktrace_get_trigger_ops(char *cmd, char *param)
  1022. {
  1023. return param ? &stacktrace_count_trigger_ops : &stacktrace_trigger_ops;
  1024. }
  1025. static struct event_command trigger_stacktrace_cmd = {
  1026. .name = "stacktrace",
  1027. .trigger_type = ETT_STACKTRACE,
  1028. .flags = EVENT_CMD_FL_POST_TRIGGER,
  1029. .func = event_trigger_callback,
  1030. .reg = register_trigger,
  1031. .unreg = unregister_trigger,
  1032. .get_trigger_ops = stacktrace_get_trigger_ops,
  1033. .set_filter = set_trigger_filter,
  1034. };
  1035. static __init int register_trigger_stacktrace_cmd(void)
  1036. {
  1037. int ret;
  1038. ret = register_event_command(&trigger_stacktrace_cmd);
  1039. WARN_ON(ret < 0);
  1040. return ret;
  1041. }
  1042. #else
  1043. static __init int register_trigger_stacktrace_cmd(void) { return 0; }
  1044. #endif /* CONFIG_STACKTRACE */
  1045. static __init void unregister_trigger_traceon_traceoff_cmds(void)
  1046. {
  1047. unregister_event_command(&trigger_traceon_cmd);
  1048. unregister_event_command(&trigger_traceoff_cmd);
  1049. }
  1050. static void
  1051. event_enable_trigger(struct event_trigger_data *data, void *rec,
  1052. struct ring_buffer_event *event)
  1053. {
  1054. struct enable_trigger_data *enable_data = data->private_data;
  1055. if (enable_data->enable)
  1056. clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
  1057. else
  1058. set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
  1059. }
  1060. static void
  1061. event_enable_count_trigger(struct event_trigger_data *data, void *rec,
  1062. struct ring_buffer_event *event)
  1063. {
  1064. struct enable_trigger_data *enable_data = data->private_data;
  1065. if (!data->count)
  1066. return;
  1067. /* Skip if the event is in a state we want to switch to */
  1068. if (enable_data->enable == !(enable_data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
  1069. return;
  1070. if (data->count != -1)
  1071. (data->count)--;
  1072. event_enable_trigger(data, rec, event);
  1073. }
  1074. int event_enable_trigger_print(struct seq_file *m,
  1075. struct event_trigger_ops *ops,
  1076. struct event_trigger_data *data)
  1077. {
  1078. struct enable_trigger_data *enable_data = data->private_data;
  1079. seq_printf(m, "%s:%s:%s",
  1080. enable_data->hist ?
  1081. (enable_data->enable ? ENABLE_HIST_STR : DISABLE_HIST_STR) :
  1082. (enable_data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR),
  1083. enable_data->file->event_call->class->system,
  1084. trace_event_name(enable_data->file->event_call));
  1085. if (data->count == -1)
  1086. seq_puts(m, ":unlimited");
  1087. else
  1088. seq_printf(m, ":count=%ld", data->count);
  1089. if (data->filter_str)
  1090. seq_printf(m, " if %s\n", data->filter_str);
  1091. else
  1092. seq_putc(m, '\n');
  1093. return 0;
  1094. }
  1095. void event_enable_trigger_free(struct event_trigger_ops *ops,
  1096. struct event_trigger_data *data)
  1097. {
  1098. struct enable_trigger_data *enable_data = data->private_data;
  1099. if (WARN_ON_ONCE(data->ref <= 0))
  1100. return;
  1101. data->ref--;
  1102. if (!data->ref) {
  1103. /* Remove the SOFT_MODE flag */
  1104. trace_event_enable_disable(enable_data->file, 0, 1);
  1105. module_put(enable_data->file->event_call->mod);
  1106. trigger_data_free(data);
  1107. kfree(enable_data);
  1108. }
  1109. }
  1110. static struct event_trigger_ops event_enable_trigger_ops = {
  1111. .func = event_enable_trigger,
  1112. .print = event_enable_trigger_print,
  1113. .init = event_trigger_init,
  1114. .free = event_enable_trigger_free,
  1115. };
  1116. static struct event_trigger_ops event_enable_count_trigger_ops = {
  1117. .func = event_enable_count_trigger,
  1118. .print = event_enable_trigger_print,
  1119. .init = event_trigger_init,
  1120. .free = event_enable_trigger_free,
  1121. };
  1122. static struct event_trigger_ops event_disable_trigger_ops = {
  1123. .func = event_enable_trigger,
  1124. .print = event_enable_trigger_print,
  1125. .init = event_trigger_init,
  1126. .free = event_enable_trigger_free,
  1127. };
  1128. static struct event_trigger_ops event_disable_count_trigger_ops = {
  1129. .func = event_enable_count_trigger,
  1130. .print = event_enable_trigger_print,
  1131. .init = event_trigger_init,
  1132. .free = event_enable_trigger_free,
  1133. };
  1134. int event_enable_trigger_func(struct event_command *cmd_ops,
  1135. struct trace_event_file *file,
  1136. char *glob, char *cmd, char *param)
  1137. {
  1138. struct trace_event_file *event_enable_file;
  1139. struct enable_trigger_data *enable_data;
  1140. struct event_trigger_data *trigger_data;
  1141. struct event_trigger_ops *trigger_ops;
  1142. struct trace_array *tr = file->tr;
  1143. const char *system;
  1144. const char *event;
  1145. bool hist = false;
  1146. char *trigger;
  1147. char *number;
  1148. bool enable;
  1149. int ret;
  1150. if (!param)
  1151. return -EINVAL;
  1152. /* separate the trigger from the filter (s:e:n [if filter]) */
  1153. trigger = strsep(&param, " \t");
  1154. if (!trigger)
  1155. return -EINVAL;
  1156. system = strsep(&trigger, ":");
  1157. if (!trigger)
  1158. return -EINVAL;
  1159. event = strsep(&trigger, ":");
  1160. ret = -EINVAL;
  1161. event_enable_file = find_event_file(tr, system, event);
  1162. if (!event_enable_file)
  1163. goto out;
  1164. #ifdef CONFIG_HIST_TRIGGERS
  1165. hist = ((strcmp(cmd, ENABLE_HIST_STR) == 0) ||
  1166. (strcmp(cmd, DISABLE_HIST_STR) == 0));
  1167. enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
  1168. (strcmp(cmd, ENABLE_HIST_STR) == 0));
  1169. #else
  1170. enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
  1171. #endif
  1172. trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
  1173. ret = -ENOMEM;
  1174. trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
  1175. if (!trigger_data)
  1176. goto out;
  1177. enable_data = kzalloc(sizeof(*enable_data), GFP_KERNEL);
  1178. if (!enable_data) {
  1179. kfree(trigger_data);
  1180. goto out;
  1181. }
  1182. trigger_data->count = -1;
  1183. trigger_data->ops = trigger_ops;
  1184. trigger_data->cmd_ops = cmd_ops;
  1185. INIT_LIST_HEAD(&trigger_data->list);
  1186. RCU_INIT_POINTER(trigger_data->filter, NULL);
  1187. enable_data->hist = hist;
  1188. enable_data->enable = enable;
  1189. enable_data->file = event_enable_file;
  1190. trigger_data->private_data = enable_data;
  1191. if (glob[0] == '!') {
  1192. cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
  1193. kfree(trigger_data);
  1194. kfree(enable_data);
  1195. ret = 0;
  1196. goto out;
  1197. }
  1198. /* Up the trigger_data count to make sure nothing frees it on failure */
  1199. event_trigger_init(trigger_ops, trigger_data);
  1200. if (trigger) {
  1201. number = strsep(&trigger, ":");
  1202. ret = -EINVAL;
  1203. if (!strlen(number))
  1204. goto out_free;
  1205. /*
  1206. * We use the callback data field (which is a pointer)
  1207. * as our counter.
  1208. */
  1209. ret = kstrtoul(number, 0, &trigger_data->count);
  1210. if (ret)
  1211. goto out_free;
  1212. }
  1213. if (!param) /* if param is non-empty, it's supposed to be a filter */
  1214. goto out_reg;
  1215. if (!cmd_ops->set_filter)
  1216. goto out_reg;
  1217. ret = cmd_ops->set_filter(param, trigger_data, file);
  1218. if (ret < 0)
  1219. goto out_free;
  1220. out_reg:
  1221. /* Don't let event modules unload while probe registered */
  1222. ret = try_module_get(event_enable_file->event_call->mod);
  1223. if (!ret) {
  1224. ret = -EBUSY;
  1225. goto out_free;
  1226. }
  1227. ret = trace_event_enable_disable(event_enable_file, 1, 1);
  1228. if (ret < 0)
  1229. goto out_put;
  1230. ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
  1231. /*
  1232. * The above returns on success the # of functions enabled,
  1233. * but if it didn't find any functions it returns zero.
  1234. * Consider no functions a failure too.
  1235. */
  1236. if (!ret) {
  1237. ret = -ENOENT;
  1238. goto out_disable;
  1239. } else if (ret < 0)
  1240. goto out_disable;
  1241. /* Just return zero, not the number of enabled functions */
  1242. ret = 0;
  1243. event_trigger_free(trigger_ops, trigger_data);
  1244. out:
  1245. return ret;
  1246. out_disable:
  1247. trace_event_enable_disable(event_enable_file, 0, 1);
  1248. out_put:
  1249. module_put(event_enable_file->event_call->mod);
  1250. out_free:
  1251. if (cmd_ops->set_filter)
  1252. cmd_ops->set_filter(NULL, trigger_data, NULL);
  1253. event_trigger_free(trigger_ops, trigger_data);
  1254. kfree(enable_data);
  1255. goto out;
  1256. }
  1257. int event_enable_register_trigger(char *glob,
  1258. struct event_trigger_ops *ops,
  1259. struct event_trigger_data *data,
  1260. struct trace_event_file *file)
  1261. {
  1262. struct enable_trigger_data *enable_data = data->private_data;
  1263. struct enable_trigger_data *test_enable_data;
  1264. struct event_trigger_data *test;
  1265. int ret = 0;
  1266. list_for_each_entry_rcu(test, &file->triggers, list) {
  1267. test_enable_data = test->private_data;
  1268. if (test_enable_data &&
  1269. (test->cmd_ops->trigger_type ==
  1270. data->cmd_ops->trigger_type) &&
  1271. (test_enable_data->file == enable_data->file)) {
  1272. ret = -EEXIST;
  1273. goto out;
  1274. }
  1275. }
  1276. if (data->ops->init) {
  1277. ret = data->ops->init(data->ops, data);
  1278. if (ret < 0)
  1279. goto out;
  1280. }
  1281. list_add_rcu(&data->list, &file->triggers);
  1282. ret++;
  1283. update_cond_flag(file);
  1284. if (trace_event_trigger_enable_disable(file, 1) < 0) {
  1285. list_del_rcu(&data->list);
  1286. update_cond_flag(file);
  1287. ret--;
  1288. }
  1289. out:
  1290. return ret;
  1291. }
  1292. void event_enable_unregister_trigger(char *glob,
  1293. struct event_trigger_ops *ops,
  1294. struct event_trigger_data *test,
  1295. struct trace_event_file *file)
  1296. {
  1297. struct enable_trigger_data *test_enable_data = test->private_data;
  1298. struct enable_trigger_data *enable_data;
  1299. struct event_trigger_data *data;
  1300. bool unregistered = false;
  1301. list_for_each_entry_rcu(data, &file->triggers, list) {
  1302. enable_data = data->private_data;
  1303. if (enable_data &&
  1304. (data->cmd_ops->trigger_type ==
  1305. test->cmd_ops->trigger_type) &&
  1306. (enable_data->file == test_enable_data->file)) {
  1307. unregistered = true;
  1308. list_del_rcu(&data->list);
  1309. trace_event_trigger_enable_disable(file, 0);
  1310. update_cond_flag(file);
  1311. break;
  1312. }
  1313. }
  1314. if (unregistered && data->ops->free)
  1315. data->ops->free(data->ops, data);
  1316. }
  1317. static struct event_trigger_ops *
  1318. event_enable_get_trigger_ops(char *cmd, char *param)
  1319. {
  1320. struct event_trigger_ops *ops;
  1321. bool enable;
  1322. #ifdef CONFIG_HIST_TRIGGERS
  1323. enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
  1324. (strcmp(cmd, ENABLE_HIST_STR) == 0));
  1325. #else
  1326. enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
  1327. #endif
  1328. if (enable)
  1329. ops = param ? &event_enable_count_trigger_ops :
  1330. &event_enable_trigger_ops;
  1331. else
  1332. ops = param ? &event_disable_count_trigger_ops :
  1333. &event_disable_trigger_ops;
  1334. return ops;
  1335. }
  1336. static struct event_command trigger_enable_cmd = {
  1337. .name = ENABLE_EVENT_STR,
  1338. .trigger_type = ETT_EVENT_ENABLE,
  1339. .func = event_enable_trigger_func,
  1340. .reg = event_enable_register_trigger,
  1341. .unreg = event_enable_unregister_trigger,
  1342. .get_trigger_ops = event_enable_get_trigger_ops,
  1343. .set_filter = set_trigger_filter,
  1344. };
  1345. static struct event_command trigger_disable_cmd = {
  1346. .name = DISABLE_EVENT_STR,
  1347. .trigger_type = ETT_EVENT_ENABLE,
  1348. .func = event_enable_trigger_func,
  1349. .reg = event_enable_register_trigger,
  1350. .unreg = event_enable_unregister_trigger,
  1351. .get_trigger_ops = event_enable_get_trigger_ops,
  1352. .set_filter = set_trigger_filter,
  1353. };
  1354. static __init void unregister_trigger_enable_disable_cmds(void)
  1355. {
  1356. unregister_event_command(&trigger_enable_cmd);
  1357. unregister_event_command(&trigger_disable_cmd);
  1358. }
  1359. static __init int register_trigger_enable_disable_cmds(void)
  1360. {
  1361. int ret;
  1362. ret = register_event_command(&trigger_enable_cmd);
  1363. if (WARN_ON(ret < 0))
  1364. return ret;
  1365. ret = register_event_command(&trigger_disable_cmd);
  1366. if (WARN_ON(ret < 0))
  1367. unregister_trigger_enable_disable_cmds();
  1368. return ret;
  1369. }
  1370. static __init int register_trigger_traceon_traceoff_cmds(void)
  1371. {
  1372. int ret;
  1373. ret = register_event_command(&trigger_traceon_cmd);
  1374. if (WARN_ON(ret < 0))
  1375. return ret;
  1376. ret = register_event_command(&trigger_traceoff_cmd);
  1377. if (WARN_ON(ret < 0))
  1378. unregister_trigger_traceon_traceoff_cmds();
  1379. return ret;
  1380. }
  1381. __init int register_trigger_cmds(void)
  1382. {
  1383. register_trigger_traceon_traceoff_cmds();
  1384. register_trigger_snapshot_cmd();
  1385. register_trigger_stacktrace_cmd();
  1386. register_trigger_enable_disable_cmds();
  1387. register_trigger_hist_enable_disable_cmds();
  1388. register_trigger_hist_cmd();
  1389. return 0;
  1390. }