trace_events_trigger.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637
  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. /*
  631. * If create_event_filter() fails, filter still needs to be freed.
  632. * Which the calling code will do with data->filter.
  633. */
  634. assign:
  635. tmp = rcu_access_pointer(data->filter);
  636. rcu_assign_pointer(data->filter, filter);
  637. if (tmp) {
  638. /* Make sure the call is done with the filter */
  639. tracepoint_synchronize_unregister();
  640. free_event_filter(tmp);
  641. }
  642. kfree(data->filter_str);
  643. data->filter_str = NULL;
  644. if (filter_str) {
  645. data->filter_str = kstrdup(filter_str, GFP_KERNEL);
  646. if (!data->filter_str) {
  647. free_event_filter(rcu_access_pointer(data->filter));
  648. data->filter = NULL;
  649. ret = -ENOMEM;
  650. }
  651. }
  652. out:
  653. return ret;
  654. }
  655. static LIST_HEAD(named_triggers);
  656. /**
  657. * find_named_trigger - Find the common named trigger associated with @name
  658. * @name: The name of the set of named triggers to find the common data for
  659. *
  660. * Named triggers are sets of triggers that share a common set of
  661. * trigger data. The first named trigger registered with a given name
  662. * owns the common trigger data that the others subsequently
  663. * registered with the same name will reference. This function
  664. * returns the common trigger data associated with that first
  665. * registered instance.
  666. *
  667. * Return: the common trigger data for the given named trigger on
  668. * success, NULL otherwise.
  669. */
  670. struct event_trigger_data *find_named_trigger(const char *name)
  671. {
  672. struct event_trigger_data *data;
  673. if (!name)
  674. return NULL;
  675. list_for_each_entry(data, &named_triggers, named_list) {
  676. if (data->named_data)
  677. continue;
  678. if (strcmp(data->name, name) == 0)
  679. return data;
  680. }
  681. return NULL;
  682. }
  683. /**
  684. * is_named_trigger - determine if a given trigger is a named trigger
  685. * @test: The trigger data to test
  686. *
  687. * Return: true if 'test' is a named trigger, false otherwise.
  688. */
  689. bool is_named_trigger(struct event_trigger_data *test)
  690. {
  691. struct event_trigger_data *data;
  692. list_for_each_entry(data, &named_triggers, named_list) {
  693. if (test == data)
  694. return true;
  695. }
  696. return false;
  697. }
  698. /**
  699. * save_named_trigger - save the trigger in the named trigger list
  700. * @name: The name of the named trigger set
  701. * @data: The trigger data to save
  702. *
  703. * Return: 0 if successful, negative error otherwise.
  704. */
  705. int save_named_trigger(const char *name, struct event_trigger_data *data)
  706. {
  707. data->name = kstrdup(name, GFP_KERNEL);
  708. if (!data->name)
  709. return -ENOMEM;
  710. list_add(&data->named_list, &named_triggers);
  711. return 0;
  712. }
  713. /**
  714. * del_named_trigger - delete a trigger from the named trigger list
  715. * @data: The trigger data to delete
  716. */
  717. void del_named_trigger(struct event_trigger_data *data)
  718. {
  719. kfree(data->name);
  720. data->name = NULL;
  721. list_del(&data->named_list);
  722. }
  723. static void __pause_named_trigger(struct event_trigger_data *data, bool pause)
  724. {
  725. struct event_trigger_data *test;
  726. list_for_each_entry(test, &named_triggers, named_list) {
  727. if (strcmp(test->name, data->name) == 0) {
  728. if (pause) {
  729. test->paused_tmp = test->paused;
  730. test->paused = true;
  731. } else {
  732. test->paused = test->paused_tmp;
  733. }
  734. }
  735. }
  736. }
  737. /**
  738. * pause_named_trigger - Pause all named triggers with the same name
  739. * @data: The trigger data of a named trigger to pause
  740. *
  741. * Pauses a named trigger along with all other triggers having the
  742. * same name. Because named triggers share a common set of data,
  743. * pausing only one is meaningless, so pausing one named trigger needs
  744. * to pause all triggers with the same name.
  745. */
  746. void pause_named_trigger(struct event_trigger_data *data)
  747. {
  748. __pause_named_trigger(data, true);
  749. }
  750. /**
  751. * unpause_named_trigger - Un-pause all named triggers with the same name
  752. * @data: The trigger data of a named trigger to unpause
  753. *
  754. * Un-pauses a named trigger along with all other triggers having the
  755. * same name. Because named triggers share a common set of data,
  756. * unpausing only one is meaningless, so unpausing one named trigger
  757. * needs to unpause all triggers with the same name.
  758. */
  759. void unpause_named_trigger(struct event_trigger_data *data)
  760. {
  761. __pause_named_trigger(data, false);
  762. }
  763. /**
  764. * set_named_trigger_data - Associate common named trigger data
  765. * @data: The trigger data of a named trigger to unpause
  766. *
  767. * Named triggers are sets of triggers that share a common set of
  768. * trigger data. The first named trigger registered with a given name
  769. * owns the common trigger data that the others subsequently
  770. * registered with the same name will reference. This function
  771. * associates the common trigger data from the first trigger with the
  772. * given trigger.
  773. */
  774. void set_named_trigger_data(struct event_trigger_data *data,
  775. struct event_trigger_data *named_data)
  776. {
  777. data->named_data = named_data;
  778. }
  779. struct event_trigger_data *
  780. get_named_trigger_data(struct event_trigger_data *data)
  781. {
  782. return data->named_data;
  783. }
  784. static void
  785. traceon_trigger(struct event_trigger_data *data, void *rec,
  786. struct ring_buffer_event *event)
  787. {
  788. if (tracing_is_on())
  789. return;
  790. tracing_on();
  791. }
  792. static void
  793. traceon_count_trigger(struct event_trigger_data *data, void *rec,
  794. struct ring_buffer_event *event)
  795. {
  796. if (tracing_is_on())
  797. return;
  798. if (!data->count)
  799. return;
  800. if (data->count != -1)
  801. (data->count)--;
  802. tracing_on();
  803. }
  804. static void
  805. traceoff_trigger(struct event_trigger_data *data, void *rec,
  806. struct ring_buffer_event *event)
  807. {
  808. if (!tracing_is_on())
  809. return;
  810. tracing_off();
  811. }
  812. static void
  813. traceoff_count_trigger(struct event_trigger_data *data, void *rec,
  814. struct ring_buffer_event *event)
  815. {
  816. if (!tracing_is_on())
  817. return;
  818. if (!data->count)
  819. return;
  820. if (data->count != -1)
  821. (data->count)--;
  822. tracing_off();
  823. }
  824. static int
  825. traceon_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
  826. struct event_trigger_data *data)
  827. {
  828. return event_trigger_print("traceon", m, (void *)data->count,
  829. data->filter_str);
  830. }
  831. static int
  832. traceoff_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
  833. struct event_trigger_data *data)
  834. {
  835. return event_trigger_print("traceoff", m, (void *)data->count,
  836. data->filter_str);
  837. }
  838. static struct event_trigger_ops traceon_trigger_ops = {
  839. .func = traceon_trigger,
  840. .print = traceon_trigger_print,
  841. .init = event_trigger_init,
  842. .free = event_trigger_free,
  843. };
  844. static struct event_trigger_ops traceon_count_trigger_ops = {
  845. .func = traceon_count_trigger,
  846. .print = traceon_trigger_print,
  847. .init = event_trigger_init,
  848. .free = event_trigger_free,
  849. };
  850. static struct event_trigger_ops traceoff_trigger_ops = {
  851. .func = traceoff_trigger,
  852. .print = traceoff_trigger_print,
  853. .init = event_trigger_init,
  854. .free = event_trigger_free,
  855. };
  856. static struct event_trigger_ops traceoff_count_trigger_ops = {
  857. .func = traceoff_count_trigger,
  858. .print = traceoff_trigger_print,
  859. .init = event_trigger_init,
  860. .free = event_trigger_free,
  861. };
  862. static struct event_trigger_ops *
  863. onoff_get_trigger_ops(char *cmd, char *param)
  864. {
  865. struct event_trigger_ops *ops;
  866. /* we register both traceon and traceoff to this callback */
  867. if (strcmp(cmd, "traceon") == 0)
  868. ops = param ? &traceon_count_trigger_ops :
  869. &traceon_trigger_ops;
  870. else
  871. ops = param ? &traceoff_count_trigger_ops :
  872. &traceoff_trigger_ops;
  873. return ops;
  874. }
  875. static struct event_command trigger_traceon_cmd = {
  876. .name = "traceon",
  877. .trigger_type = ETT_TRACE_ONOFF,
  878. .func = event_trigger_callback,
  879. .reg = register_trigger,
  880. .unreg = unregister_trigger,
  881. .get_trigger_ops = onoff_get_trigger_ops,
  882. .set_filter = set_trigger_filter,
  883. };
  884. static struct event_command trigger_traceoff_cmd = {
  885. .name = "traceoff",
  886. .trigger_type = ETT_TRACE_ONOFF,
  887. .flags = EVENT_CMD_FL_POST_TRIGGER,
  888. .func = event_trigger_callback,
  889. .reg = register_trigger,
  890. .unreg = unregister_trigger,
  891. .get_trigger_ops = onoff_get_trigger_ops,
  892. .set_filter = set_trigger_filter,
  893. };
  894. #ifdef CONFIG_TRACER_SNAPSHOT
  895. static void
  896. snapshot_trigger(struct event_trigger_data *data, void *rec,
  897. struct ring_buffer_event *event)
  898. {
  899. struct trace_event_file *file = data->private_data;
  900. if (file)
  901. tracing_snapshot_instance(file->tr);
  902. else
  903. tracing_snapshot();
  904. }
  905. static void
  906. snapshot_count_trigger(struct event_trigger_data *data, void *rec,
  907. struct ring_buffer_event *event)
  908. {
  909. if (!data->count)
  910. return;
  911. if (data->count != -1)
  912. (data->count)--;
  913. snapshot_trigger(data, rec, event);
  914. }
  915. static int
  916. register_snapshot_trigger(char *glob, struct event_trigger_ops *ops,
  917. struct event_trigger_data *data,
  918. struct trace_event_file *file)
  919. {
  920. int ret = register_trigger(glob, ops, data, file);
  921. if (ret > 0 && tracing_alloc_snapshot_instance(file->tr) != 0) {
  922. unregister_trigger(glob, ops, data, file);
  923. ret = 0;
  924. }
  925. return ret;
  926. }
  927. static int
  928. snapshot_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
  929. struct event_trigger_data *data)
  930. {
  931. return event_trigger_print("snapshot", m, (void *)data->count,
  932. data->filter_str);
  933. }
  934. static struct event_trigger_ops snapshot_trigger_ops = {
  935. .func = snapshot_trigger,
  936. .print = snapshot_trigger_print,
  937. .init = event_trigger_init,
  938. .free = event_trigger_free,
  939. };
  940. static struct event_trigger_ops snapshot_count_trigger_ops = {
  941. .func = snapshot_count_trigger,
  942. .print = snapshot_trigger_print,
  943. .init = event_trigger_init,
  944. .free = event_trigger_free,
  945. };
  946. static struct event_trigger_ops *
  947. snapshot_get_trigger_ops(char *cmd, char *param)
  948. {
  949. return param ? &snapshot_count_trigger_ops : &snapshot_trigger_ops;
  950. }
  951. static struct event_command trigger_snapshot_cmd = {
  952. .name = "snapshot",
  953. .trigger_type = ETT_SNAPSHOT,
  954. .func = event_trigger_callback,
  955. .reg = register_snapshot_trigger,
  956. .unreg = unregister_trigger,
  957. .get_trigger_ops = snapshot_get_trigger_ops,
  958. .set_filter = set_trigger_filter,
  959. };
  960. static __init int register_trigger_snapshot_cmd(void)
  961. {
  962. int ret;
  963. ret = register_event_command(&trigger_snapshot_cmd);
  964. WARN_ON(ret < 0);
  965. return ret;
  966. }
  967. #else
  968. static __init int register_trigger_snapshot_cmd(void) { return 0; }
  969. #endif /* CONFIG_TRACER_SNAPSHOT */
  970. #ifdef CONFIG_STACKTRACE
  971. #ifdef CONFIG_UNWINDER_ORC
  972. /* Skip 2:
  973. * event_triggers_post_call()
  974. * trace_event_raw_event_xxx()
  975. */
  976. # define STACK_SKIP 2
  977. #else
  978. /*
  979. * Skip 4:
  980. * stacktrace_trigger()
  981. * event_triggers_post_call()
  982. * trace_event_buffer_commit()
  983. * trace_event_raw_event_xxx()
  984. */
  985. #define STACK_SKIP 4
  986. #endif
  987. static void
  988. stacktrace_trigger(struct event_trigger_data *data, void *rec,
  989. struct ring_buffer_event *event)
  990. {
  991. trace_dump_stack(STACK_SKIP);
  992. }
  993. static void
  994. stacktrace_count_trigger(struct event_trigger_data *data, void *rec,
  995. struct ring_buffer_event *event)
  996. {
  997. if (!data->count)
  998. return;
  999. if (data->count != -1)
  1000. (data->count)--;
  1001. stacktrace_trigger(data, rec, event);
  1002. }
  1003. static int
  1004. stacktrace_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
  1005. struct event_trigger_data *data)
  1006. {
  1007. return event_trigger_print("stacktrace", m, (void *)data->count,
  1008. data->filter_str);
  1009. }
  1010. static struct event_trigger_ops stacktrace_trigger_ops = {
  1011. .func = stacktrace_trigger,
  1012. .print = stacktrace_trigger_print,
  1013. .init = event_trigger_init,
  1014. .free = event_trigger_free,
  1015. };
  1016. static struct event_trigger_ops stacktrace_count_trigger_ops = {
  1017. .func = stacktrace_count_trigger,
  1018. .print = stacktrace_trigger_print,
  1019. .init = event_trigger_init,
  1020. .free = event_trigger_free,
  1021. };
  1022. static struct event_trigger_ops *
  1023. stacktrace_get_trigger_ops(char *cmd, char *param)
  1024. {
  1025. return param ? &stacktrace_count_trigger_ops : &stacktrace_trigger_ops;
  1026. }
  1027. static struct event_command trigger_stacktrace_cmd = {
  1028. .name = "stacktrace",
  1029. .trigger_type = ETT_STACKTRACE,
  1030. .flags = EVENT_CMD_FL_POST_TRIGGER,
  1031. .func = event_trigger_callback,
  1032. .reg = register_trigger,
  1033. .unreg = unregister_trigger,
  1034. .get_trigger_ops = stacktrace_get_trigger_ops,
  1035. .set_filter = set_trigger_filter,
  1036. };
  1037. static __init int register_trigger_stacktrace_cmd(void)
  1038. {
  1039. int ret;
  1040. ret = register_event_command(&trigger_stacktrace_cmd);
  1041. WARN_ON(ret < 0);
  1042. return ret;
  1043. }
  1044. #else
  1045. static __init int register_trigger_stacktrace_cmd(void) { return 0; }
  1046. #endif /* CONFIG_STACKTRACE */
  1047. static __init void unregister_trigger_traceon_traceoff_cmds(void)
  1048. {
  1049. unregister_event_command(&trigger_traceon_cmd);
  1050. unregister_event_command(&trigger_traceoff_cmd);
  1051. }
  1052. static void
  1053. event_enable_trigger(struct event_trigger_data *data, void *rec,
  1054. struct ring_buffer_event *event)
  1055. {
  1056. struct enable_trigger_data *enable_data = data->private_data;
  1057. if (enable_data->enable)
  1058. clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
  1059. else
  1060. set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
  1061. }
  1062. static void
  1063. event_enable_count_trigger(struct event_trigger_data *data, void *rec,
  1064. struct ring_buffer_event *event)
  1065. {
  1066. struct enable_trigger_data *enable_data = data->private_data;
  1067. if (!data->count)
  1068. return;
  1069. /* Skip if the event is in a state we want to switch to */
  1070. if (enable_data->enable == !(enable_data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
  1071. return;
  1072. if (data->count != -1)
  1073. (data->count)--;
  1074. event_enable_trigger(data, rec, event);
  1075. }
  1076. int event_enable_trigger_print(struct seq_file *m,
  1077. struct event_trigger_ops *ops,
  1078. struct event_trigger_data *data)
  1079. {
  1080. struct enable_trigger_data *enable_data = data->private_data;
  1081. seq_printf(m, "%s:%s:%s",
  1082. enable_data->hist ?
  1083. (enable_data->enable ? ENABLE_HIST_STR : DISABLE_HIST_STR) :
  1084. (enable_data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR),
  1085. enable_data->file->event_call->class->system,
  1086. trace_event_name(enable_data->file->event_call));
  1087. if (data->count == -1)
  1088. seq_puts(m, ":unlimited");
  1089. else
  1090. seq_printf(m, ":count=%ld", data->count);
  1091. if (data->filter_str)
  1092. seq_printf(m, " if %s\n", data->filter_str);
  1093. else
  1094. seq_putc(m, '\n');
  1095. return 0;
  1096. }
  1097. void event_enable_trigger_free(struct event_trigger_ops *ops,
  1098. struct event_trigger_data *data)
  1099. {
  1100. struct enable_trigger_data *enable_data = data->private_data;
  1101. if (WARN_ON_ONCE(data->ref <= 0))
  1102. return;
  1103. data->ref--;
  1104. if (!data->ref) {
  1105. /* Remove the SOFT_MODE flag */
  1106. trace_event_enable_disable(enable_data->file, 0, 1);
  1107. module_put(enable_data->file->event_call->mod);
  1108. trigger_data_free(data);
  1109. kfree(enable_data);
  1110. }
  1111. }
  1112. static struct event_trigger_ops event_enable_trigger_ops = {
  1113. .func = event_enable_trigger,
  1114. .print = event_enable_trigger_print,
  1115. .init = event_trigger_init,
  1116. .free = event_enable_trigger_free,
  1117. };
  1118. static struct event_trigger_ops event_enable_count_trigger_ops = {
  1119. .func = event_enable_count_trigger,
  1120. .print = event_enable_trigger_print,
  1121. .init = event_trigger_init,
  1122. .free = event_enable_trigger_free,
  1123. };
  1124. static struct event_trigger_ops event_disable_trigger_ops = {
  1125. .func = event_enable_trigger,
  1126. .print = event_enable_trigger_print,
  1127. .init = event_trigger_init,
  1128. .free = event_enable_trigger_free,
  1129. };
  1130. static struct event_trigger_ops event_disable_count_trigger_ops = {
  1131. .func = event_enable_count_trigger,
  1132. .print = event_enable_trigger_print,
  1133. .init = event_trigger_init,
  1134. .free = event_enable_trigger_free,
  1135. };
  1136. int event_enable_trigger_func(struct event_command *cmd_ops,
  1137. struct trace_event_file *file,
  1138. char *glob, char *cmd, char *param)
  1139. {
  1140. struct trace_event_file *event_enable_file;
  1141. struct enable_trigger_data *enable_data;
  1142. struct event_trigger_data *trigger_data;
  1143. struct event_trigger_ops *trigger_ops;
  1144. struct trace_array *tr = file->tr;
  1145. const char *system;
  1146. const char *event;
  1147. bool hist = false;
  1148. char *trigger;
  1149. char *number;
  1150. bool enable;
  1151. int ret;
  1152. if (!param)
  1153. return -EINVAL;
  1154. /* separate the trigger from the filter (s:e:n [if filter]) */
  1155. trigger = strsep(&param, " \t");
  1156. if (!trigger)
  1157. return -EINVAL;
  1158. system = strsep(&trigger, ":");
  1159. if (!trigger)
  1160. return -EINVAL;
  1161. event = strsep(&trigger, ":");
  1162. ret = -EINVAL;
  1163. event_enable_file = find_event_file(tr, system, event);
  1164. if (!event_enable_file)
  1165. goto out;
  1166. #ifdef CONFIG_HIST_TRIGGERS
  1167. hist = ((strcmp(cmd, ENABLE_HIST_STR) == 0) ||
  1168. (strcmp(cmd, DISABLE_HIST_STR) == 0));
  1169. enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
  1170. (strcmp(cmd, ENABLE_HIST_STR) == 0));
  1171. #else
  1172. enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
  1173. #endif
  1174. trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
  1175. ret = -ENOMEM;
  1176. trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
  1177. if (!trigger_data)
  1178. goto out;
  1179. enable_data = kzalloc(sizeof(*enable_data), GFP_KERNEL);
  1180. if (!enable_data) {
  1181. kfree(trigger_data);
  1182. goto out;
  1183. }
  1184. trigger_data->count = -1;
  1185. trigger_data->ops = trigger_ops;
  1186. trigger_data->cmd_ops = cmd_ops;
  1187. INIT_LIST_HEAD(&trigger_data->list);
  1188. RCU_INIT_POINTER(trigger_data->filter, NULL);
  1189. enable_data->hist = hist;
  1190. enable_data->enable = enable;
  1191. enable_data->file = event_enable_file;
  1192. trigger_data->private_data = enable_data;
  1193. if (glob[0] == '!') {
  1194. cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
  1195. kfree(trigger_data);
  1196. kfree(enable_data);
  1197. ret = 0;
  1198. goto out;
  1199. }
  1200. /* Up the trigger_data count to make sure nothing frees it on failure */
  1201. event_trigger_init(trigger_ops, trigger_data);
  1202. if (trigger) {
  1203. number = strsep(&trigger, ":");
  1204. ret = -EINVAL;
  1205. if (!strlen(number))
  1206. goto out_free;
  1207. /*
  1208. * We use the callback data field (which is a pointer)
  1209. * as our counter.
  1210. */
  1211. ret = kstrtoul(number, 0, &trigger_data->count);
  1212. if (ret)
  1213. goto out_free;
  1214. }
  1215. if (!param) /* if param is non-empty, it's supposed to be a filter */
  1216. goto out_reg;
  1217. if (!cmd_ops->set_filter)
  1218. goto out_reg;
  1219. ret = cmd_ops->set_filter(param, trigger_data, file);
  1220. if (ret < 0)
  1221. goto out_free;
  1222. out_reg:
  1223. /* Don't let event modules unload while probe registered */
  1224. ret = try_module_get(event_enable_file->event_call->mod);
  1225. if (!ret) {
  1226. ret = -EBUSY;
  1227. goto out_free;
  1228. }
  1229. ret = trace_event_enable_disable(event_enable_file, 1, 1);
  1230. if (ret < 0)
  1231. goto out_put;
  1232. ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
  1233. /*
  1234. * The above returns on success the # of functions enabled,
  1235. * but if it didn't find any functions it returns zero.
  1236. * Consider no functions a failure too.
  1237. */
  1238. if (!ret) {
  1239. ret = -ENOENT;
  1240. goto out_disable;
  1241. } else if (ret < 0)
  1242. goto out_disable;
  1243. /* Just return zero, not the number of enabled functions */
  1244. ret = 0;
  1245. event_trigger_free(trigger_ops, trigger_data);
  1246. out:
  1247. return ret;
  1248. out_disable:
  1249. trace_event_enable_disable(event_enable_file, 0, 1);
  1250. out_put:
  1251. module_put(event_enable_file->event_call->mod);
  1252. out_free:
  1253. if (cmd_ops->set_filter)
  1254. cmd_ops->set_filter(NULL, trigger_data, NULL);
  1255. event_trigger_free(trigger_ops, trigger_data);
  1256. kfree(enable_data);
  1257. goto out;
  1258. }
  1259. int event_enable_register_trigger(char *glob,
  1260. struct event_trigger_ops *ops,
  1261. struct event_trigger_data *data,
  1262. struct trace_event_file *file)
  1263. {
  1264. struct enable_trigger_data *enable_data = data->private_data;
  1265. struct enable_trigger_data *test_enable_data;
  1266. struct event_trigger_data *test;
  1267. int ret = 0;
  1268. list_for_each_entry_rcu(test, &file->triggers, list) {
  1269. test_enable_data = test->private_data;
  1270. if (test_enable_data &&
  1271. (test->cmd_ops->trigger_type ==
  1272. data->cmd_ops->trigger_type) &&
  1273. (test_enable_data->file == enable_data->file)) {
  1274. ret = -EEXIST;
  1275. goto out;
  1276. }
  1277. }
  1278. if (data->ops->init) {
  1279. ret = data->ops->init(data->ops, data);
  1280. if (ret < 0)
  1281. goto out;
  1282. }
  1283. list_add_rcu(&data->list, &file->triggers);
  1284. ret++;
  1285. update_cond_flag(file);
  1286. if (trace_event_trigger_enable_disable(file, 1) < 0) {
  1287. list_del_rcu(&data->list);
  1288. update_cond_flag(file);
  1289. ret--;
  1290. }
  1291. out:
  1292. return ret;
  1293. }
  1294. void event_enable_unregister_trigger(char *glob,
  1295. struct event_trigger_ops *ops,
  1296. struct event_trigger_data *test,
  1297. struct trace_event_file *file)
  1298. {
  1299. struct enable_trigger_data *test_enable_data = test->private_data;
  1300. struct enable_trigger_data *enable_data;
  1301. struct event_trigger_data *data;
  1302. bool unregistered = false;
  1303. list_for_each_entry_rcu(data, &file->triggers, list) {
  1304. enable_data = data->private_data;
  1305. if (enable_data &&
  1306. (data->cmd_ops->trigger_type ==
  1307. test->cmd_ops->trigger_type) &&
  1308. (enable_data->file == test_enable_data->file)) {
  1309. unregistered = true;
  1310. list_del_rcu(&data->list);
  1311. trace_event_trigger_enable_disable(file, 0);
  1312. update_cond_flag(file);
  1313. break;
  1314. }
  1315. }
  1316. if (unregistered && data->ops->free)
  1317. data->ops->free(data->ops, data);
  1318. }
  1319. static struct event_trigger_ops *
  1320. event_enable_get_trigger_ops(char *cmd, char *param)
  1321. {
  1322. struct event_trigger_ops *ops;
  1323. bool enable;
  1324. #ifdef CONFIG_HIST_TRIGGERS
  1325. enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
  1326. (strcmp(cmd, ENABLE_HIST_STR) == 0));
  1327. #else
  1328. enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
  1329. #endif
  1330. if (enable)
  1331. ops = param ? &event_enable_count_trigger_ops :
  1332. &event_enable_trigger_ops;
  1333. else
  1334. ops = param ? &event_disable_count_trigger_ops :
  1335. &event_disable_trigger_ops;
  1336. return ops;
  1337. }
  1338. static struct event_command trigger_enable_cmd = {
  1339. .name = ENABLE_EVENT_STR,
  1340. .trigger_type = ETT_EVENT_ENABLE,
  1341. .func = event_enable_trigger_func,
  1342. .reg = event_enable_register_trigger,
  1343. .unreg = event_enable_unregister_trigger,
  1344. .get_trigger_ops = event_enable_get_trigger_ops,
  1345. .set_filter = set_trigger_filter,
  1346. };
  1347. static struct event_command trigger_disable_cmd = {
  1348. .name = DISABLE_EVENT_STR,
  1349. .trigger_type = ETT_EVENT_ENABLE,
  1350. .func = event_enable_trigger_func,
  1351. .reg = event_enable_register_trigger,
  1352. .unreg = event_enable_unregister_trigger,
  1353. .get_trigger_ops = event_enable_get_trigger_ops,
  1354. .set_filter = set_trigger_filter,
  1355. };
  1356. static __init void unregister_trigger_enable_disable_cmds(void)
  1357. {
  1358. unregister_event_command(&trigger_enable_cmd);
  1359. unregister_event_command(&trigger_disable_cmd);
  1360. }
  1361. static __init int register_trigger_enable_disable_cmds(void)
  1362. {
  1363. int ret;
  1364. ret = register_event_command(&trigger_enable_cmd);
  1365. if (WARN_ON(ret < 0))
  1366. return ret;
  1367. ret = register_event_command(&trigger_disable_cmd);
  1368. if (WARN_ON(ret < 0))
  1369. unregister_trigger_enable_disable_cmds();
  1370. return ret;
  1371. }
  1372. static __init int register_trigger_traceon_traceoff_cmds(void)
  1373. {
  1374. int ret;
  1375. ret = register_event_command(&trigger_traceon_cmd);
  1376. if (WARN_ON(ret < 0))
  1377. return ret;
  1378. ret = register_event_command(&trigger_traceoff_cmd);
  1379. if (WARN_ON(ret < 0))
  1380. unregister_trigger_traceon_traceoff_cmds();
  1381. return ret;
  1382. }
  1383. __init int register_trigger_cmds(void)
  1384. {
  1385. register_trigger_traceon_traceoff_cmds();
  1386. register_trigger_snapshot_cmd();
  1387. register_trigger_stacktrace_cmd();
  1388. register_trigger_enable_disable_cmds();
  1389. register_trigger_hist_enable_disable_cmds();
  1390. register_trigger_hist_cmd();
  1391. return 0;
  1392. }