trace_events_trigger.c 40 KB

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