trace_events_trigger.c 40 KB

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