trace_events_trigger.c 41 KB

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