trace_events_trigger.c 41 KB

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