trace_events_trigger.c 40 KB

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