trace_events_trigger.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * trace_events_trigger - trace event triggers
  4. *
  5. * Copyright (C) 2013 Tom Zanussi <[email protected]>
  6. */
  7. #include <linux/security.h>
  8. #include <linux/module.h>
  9. #include <linux/ctype.h>
  10. #include <linux/mutex.h>
  11. #include <linux/slab.h>
  12. #include <linux/rculist.h>
  13. #include "trace.h"
  14. static LIST_HEAD(trigger_commands);
  15. static DEFINE_MUTEX(trigger_cmd_mutex);
  16. void trigger_data_free(struct event_trigger_data *data)
  17. {
  18. if (data->cmd_ops->set_filter)
  19. data->cmd_ops->set_filter(NULL, data, NULL);
  20. /* make sure current triggers exit before free */
  21. tracepoint_synchronize_unregister();
  22. kfree(data);
  23. }
  24. /**
  25. * event_triggers_call - Call triggers associated with a trace event
  26. * @file: The trace_event_file associated with the event
  27. * @rec: The trace entry for the event, NULL for unconditional invocation
  28. *
  29. * For each trigger associated with an event, invoke the trigger
  30. * function registered with the associated trigger command. If rec is
  31. * non-NULL, it means that the trigger requires further processing and
  32. * shouldn't be unconditionally invoked. If rec is non-NULL and the
  33. * trigger has a filter associated with it, rec will checked against
  34. * the filter and if the record matches the trigger will be invoked.
  35. * If the trigger is a 'post_trigger', meaning it shouldn't be invoked
  36. * in any case until the current event is written, the trigger
  37. * function isn't invoked but the bit associated with the deferred
  38. * trigger is set in the return value.
  39. *
  40. * Returns an enum event_trigger_type value containing a set bit for
  41. * any trigger that should be deferred, ETT_NONE if nothing to defer.
  42. *
  43. * Called from tracepoint handlers (with rcu_read_lock_sched() held).
  44. *
  45. * Return: an enum event_trigger_type value containing a set bit for
  46. * any trigger that should be deferred, ETT_NONE if nothing to defer.
  47. */
  48. enum event_trigger_type
  49. event_triggers_call(struct trace_event_file *file,
  50. struct trace_buffer *buffer, void *rec,
  51. struct ring_buffer_event *event)
  52. {
  53. struct event_trigger_data *data;
  54. enum event_trigger_type tt = ETT_NONE;
  55. struct event_filter *filter;
  56. if (list_empty(&file->triggers))
  57. return tt;
  58. list_for_each_entry_rcu(data, &file->triggers, list) {
  59. if (data->paused)
  60. continue;
  61. if (!rec) {
  62. data->ops->trigger(data, buffer, rec, event);
  63. continue;
  64. }
  65. filter = rcu_dereference_sched(data->filter);
  66. if (filter && !filter_match_preds(filter, rec))
  67. continue;
  68. if (event_command_post_trigger(data->cmd_ops)) {
  69. tt |= data->cmd_ops->trigger_type;
  70. continue;
  71. }
  72. data->ops->trigger(data, buffer, rec, event);
  73. }
  74. return tt;
  75. }
  76. EXPORT_SYMBOL_GPL(event_triggers_call);
  77. bool __trace_trigger_soft_disabled(struct trace_event_file *file)
  78. {
  79. unsigned long eflags = file->flags;
  80. if (eflags & EVENT_FILE_FL_TRIGGER_MODE)
  81. event_triggers_call(file, NULL, NULL, NULL);
  82. if (eflags & EVENT_FILE_FL_SOFT_DISABLED)
  83. return true;
  84. if (eflags & EVENT_FILE_FL_PID_FILTER)
  85. return trace_event_ignore_this_pid(file);
  86. return false;
  87. }
  88. EXPORT_SYMBOL_GPL(__trace_trigger_soft_disabled);
  89. /**
  90. * event_triggers_post_call - Call 'post_triggers' for a trace event
  91. * @file: The trace_event_file associated with the event
  92. * @tt: enum event_trigger_type containing a set bit for each trigger to invoke
  93. *
  94. * For each trigger associated with an event, invoke the trigger
  95. * function registered with the associated trigger command, if the
  96. * corresponding bit is set in the tt enum passed into this function.
  97. * See @event_triggers_call for details on how those bits are set.
  98. *
  99. * Called from tracepoint handlers (with rcu_read_lock_sched() held).
  100. */
  101. void
  102. event_triggers_post_call(struct trace_event_file *file,
  103. enum event_trigger_type tt)
  104. {
  105. struct event_trigger_data *data;
  106. list_for_each_entry_rcu(data, &file->triggers, list) {
  107. if (data->paused)
  108. continue;
  109. if (data->cmd_ops->trigger_type & tt)
  110. data->ops->trigger(data, NULL, NULL, NULL);
  111. }
  112. }
  113. EXPORT_SYMBOL_GPL(event_triggers_post_call);
  114. #define SHOW_AVAILABLE_TRIGGERS (void *)(1UL)
  115. static void *trigger_next(struct seq_file *m, void *t, loff_t *pos)
  116. {
  117. struct trace_event_file *event_file = event_file_data(m->private);
  118. if (t == SHOW_AVAILABLE_TRIGGERS) {
  119. (*pos)++;
  120. return NULL;
  121. }
  122. return seq_list_next(t, &event_file->triggers, pos);
  123. }
  124. static bool check_user_trigger(struct trace_event_file *file)
  125. {
  126. struct event_trigger_data *data;
  127. list_for_each_entry_rcu(data, &file->triggers, list,
  128. lockdep_is_held(&event_mutex)) {
  129. if (data->flags & EVENT_TRIGGER_FL_PROBE)
  130. continue;
  131. return true;
  132. }
  133. return false;
  134. }
  135. static void *trigger_start(struct seq_file *m, loff_t *pos)
  136. {
  137. struct trace_event_file *event_file;
  138. /* ->stop() is called even if ->start() fails */
  139. mutex_lock(&event_mutex);
  140. event_file = event_file_data(m->private);
  141. if (unlikely(!event_file))
  142. return ERR_PTR(-ENODEV);
  143. if (list_empty(&event_file->triggers) || !check_user_trigger(event_file))
  144. return *pos == 0 ? SHOW_AVAILABLE_TRIGGERS : NULL;
  145. return seq_list_start(&event_file->triggers, *pos);
  146. }
  147. static void trigger_stop(struct seq_file *m, void *t)
  148. {
  149. mutex_unlock(&event_mutex);
  150. }
  151. static int trigger_show(struct seq_file *m, void *v)
  152. {
  153. struct event_trigger_data *data;
  154. struct event_command *p;
  155. if (v == SHOW_AVAILABLE_TRIGGERS) {
  156. seq_puts(m, "# Available triggers:\n");
  157. seq_putc(m, '#');
  158. mutex_lock(&trigger_cmd_mutex);
  159. list_for_each_entry_reverse(p, &trigger_commands, list)
  160. seq_printf(m, " %s", p->name);
  161. seq_putc(m, '\n');
  162. mutex_unlock(&trigger_cmd_mutex);
  163. return 0;
  164. }
  165. data = list_entry(v, struct event_trigger_data, list);
  166. data->ops->print(m, data);
  167. return 0;
  168. }
  169. static const struct seq_operations event_triggers_seq_ops = {
  170. .start = trigger_start,
  171. .next = trigger_next,
  172. .stop = trigger_stop,
  173. .show = trigger_show,
  174. };
  175. static int event_trigger_regex_open(struct inode *inode, struct file *file)
  176. {
  177. int ret;
  178. ret = security_locked_down(LOCKDOWN_TRACEFS);
  179. if (ret)
  180. return ret;
  181. mutex_lock(&event_mutex);
  182. if (unlikely(!event_file_data(file))) {
  183. mutex_unlock(&event_mutex);
  184. return -ENODEV;
  185. }
  186. if ((file->f_mode & FMODE_WRITE) &&
  187. (file->f_flags & O_TRUNC)) {
  188. struct trace_event_file *event_file;
  189. struct event_command *p;
  190. event_file = event_file_data(file);
  191. list_for_each_entry(p, &trigger_commands, list) {
  192. if (p->unreg_all)
  193. p->unreg_all(event_file);
  194. }
  195. }
  196. if (file->f_mode & FMODE_READ) {
  197. ret = seq_open(file, &event_triggers_seq_ops);
  198. if (!ret) {
  199. struct seq_file *m = file->private_data;
  200. m->private = file;
  201. }
  202. }
  203. mutex_unlock(&event_mutex);
  204. return ret;
  205. }
  206. int trigger_process_regex(struct trace_event_file *file, char *buff)
  207. {
  208. char *command, *next;
  209. struct event_command *p;
  210. int ret = -EINVAL;
  211. next = buff = skip_spaces(buff);
  212. command = strsep(&next, ": \t");
  213. if (next) {
  214. next = skip_spaces(next);
  215. if (!*next)
  216. next = NULL;
  217. }
  218. command = (command[0] != '!') ? command : command + 1;
  219. mutex_lock(&trigger_cmd_mutex);
  220. list_for_each_entry(p, &trigger_commands, list) {
  221. if (strcmp(p->name, command) == 0) {
  222. ret = p->parse(p, file, buff, command, next);
  223. goto out_unlock;
  224. }
  225. }
  226. out_unlock:
  227. mutex_unlock(&trigger_cmd_mutex);
  228. return ret;
  229. }
  230. static ssize_t event_trigger_regex_write(struct file *file,
  231. const char __user *ubuf,
  232. size_t cnt, loff_t *ppos)
  233. {
  234. struct trace_event_file *event_file;
  235. ssize_t ret;
  236. char *buf;
  237. if (!cnt)
  238. return 0;
  239. if (cnt >= PAGE_SIZE)
  240. return -EINVAL;
  241. buf = memdup_user_nul(ubuf, cnt);
  242. if (IS_ERR(buf))
  243. return PTR_ERR(buf);
  244. strim(buf);
  245. mutex_lock(&event_mutex);
  246. event_file = event_file_data(file);
  247. if (unlikely(!event_file)) {
  248. mutex_unlock(&event_mutex);
  249. kfree(buf);
  250. return -ENODEV;
  251. }
  252. ret = trigger_process_regex(event_file, buf);
  253. mutex_unlock(&event_mutex);
  254. kfree(buf);
  255. if (ret < 0)
  256. goto out;
  257. *ppos += cnt;
  258. ret = cnt;
  259. out:
  260. return ret;
  261. }
  262. static int event_trigger_regex_release(struct inode *inode, struct file *file)
  263. {
  264. mutex_lock(&event_mutex);
  265. if (file->f_mode & FMODE_READ)
  266. seq_release(inode, file);
  267. mutex_unlock(&event_mutex);
  268. return 0;
  269. }
  270. static ssize_t
  271. event_trigger_write(struct file *filp, const char __user *ubuf,
  272. size_t cnt, loff_t *ppos)
  273. {
  274. return event_trigger_regex_write(filp, ubuf, cnt, ppos);
  275. }
  276. static int
  277. event_trigger_open(struct inode *inode, struct file *filp)
  278. {
  279. /* Checks for tracefs lockdown */
  280. return event_trigger_regex_open(inode, filp);
  281. }
  282. static int
  283. event_trigger_release(struct inode *inode, struct file *file)
  284. {
  285. return event_trigger_regex_release(inode, file);
  286. }
  287. const struct file_operations event_trigger_fops = {
  288. .open = event_trigger_open,
  289. .read = seq_read,
  290. .write = event_trigger_write,
  291. .llseek = tracing_lseek,
  292. .release = event_trigger_release,
  293. };
  294. /*
  295. * Currently we only register event commands from __init, so mark this
  296. * __init too.
  297. */
  298. __init int register_event_command(struct event_command *cmd)
  299. {
  300. struct event_command *p;
  301. int ret = 0;
  302. mutex_lock(&trigger_cmd_mutex);
  303. list_for_each_entry(p, &trigger_commands, list) {
  304. if (strcmp(cmd->name, p->name) == 0) {
  305. ret = -EBUSY;
  306. goto out_unlock;
  307. }
  308. }
  309. list_add(&cmd->list, &trigger_commands);
  310. out_unlock:
  311. mutex_unlock(&trigger_cmd_mutex);
  312. return ret;
  313. }
  314. /*
  315. * Currently we only unregister event commands from __init, so mark
  316. * this __init too.
  317. */
  318. __init int unregister_event_command(struct event_command *cmd)
  319. {
  320. struct event_command *p, *n;
  321. int ret = -ENODEV;
  322. mutex_lock(&trigger_cmd_mutex);
  323. list_for_each_entry_safe(p, n, &trigger_commands, list) {
  324. if (strcmp(cmd->name, p->name) == 0) {
  325. ret = 0;
  326. list_del_init(&p->list);
  327. goto out_unlock;
  328. }
  329. }
  330. out_unlock:
  331. mutex_unlock(&trigger_cmd_mutex);
  332. return ret;
  333. }
  334. /**
  335. * event_trigger_print - Generic event_trigger_ops @print implementation
  336. * @name: The name of the event trigger
  337. * @m: The seq_file being printed to
  338. * @data: Trigger-specific data
  339. * @filter_str: filter_str to print, if present
  340. *
  341. * Common implementation for event triggers to print themselves.
  342. *
  343. * Usually wrapped by a function that simply sets the @name of the
  344. * trigger command and then invokes this.
  345. *
  346. * Return: 0 on success, errno otherwise
  347. */
  348. static int
  349. event_trigger_print(const char *name, struct seq_file *m,
  350. void *data, char *filter_str)
  351. {
  352. long count = (long)data;
  353. seq_puts(m, name);
  354. if (count == -1)
  355. seq_puts(m, ":unlimited");
  356. else
  357. seq_printf(m, ":count=%ld", count);
  358. if (filter_str)
  359. seq_printf(m, " if %s\n", filter_str);
  360. else
  361. seq_putc(m, '\n');
  362. return 0;
  363. }
  364. /**
  365. * event_trigger_init - Generic event_trigger_ops @init implementation
  366. * @data: Trigger-specific data
  367. *
  368. * Common implementation of event trigger initialization.
  369. *
  370. * Usually used directly as the @init method in event trigger
  371. * implementations.
  372. *
  373. * Return: 0 on success, errno otherwise
  374. */
  375. int event_trigger_init(struct event_trigger_data *data)
  376. {
  377. data->ref++;
  378. return 0;
  379. }
  380. /**
  381. * event_trigger_free - Generic event_trigger_ops @free implementation
  382. * @data: Trigger-specific data
  383. *
  384. * Common implementation of event trigger de-initialization.
  385. *
  386. * Usually used directly as the @free method in event trigger
  387. * implementations.
  388. */
  389. static void
  390. event_trigger_free(struct event_trigger_data *data)
  391. {
  392. if (WARN_ON_ONCE(data->ref <= 0))
  393. return;
  394. data->ref--;
  395. if (!data->ref)
  396. trigger_data_free(data);
  397. }
  398. int trace_event_trigger_enable_disable(struct trace_event_file *file,
  399. int trigger_enable)
  400. {
  401. int ret = 0;
  402. if (trigger_enable) {
  403. if (atomic_inc_return(&file->tm_ref) > 1)
  404. return ret;
  405. set_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
  406. ret = trace_event_enable_disable(file, 1, 1);
  407. } else {
  408. if (atomic_dec_return(&file->tm_ref) > 0)
  409. return ret;
  410. clear_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
  411. ret = trace_event_enable_disable(file, 0, 1);
  412. }
  413. return ret;
  414. }
  415. /**
  416. * clear_event_triggers - Clear all triggers associated with a trace array
  417. * @tr: The trace array to clear
  418. *
  419. * For each trigger, the triggering event has its tm_ref decremented
  420. * via trace_event_trigger_enable_disable(), and any associated event
  421. * (in the case of enable/disable_event triggers) will have its sm_ref
  422. * decremented via free()->trace_event_enable_disable(). That
  423. * combination effectively reverses the soft-mode/trigger state added
  424. * by trigger registration.
  425. *
  426. * Must be called with event_mutex held.
  427. */
  428. void
  429. clear_event_triggers(struct trace_array *tr)
  430. {
  431. struct trace_event_file *file;
  432. list_for_each_entry(file, &tr->events, list) {
  433. struct event_trigger_data *data, *n;
  434. list_for_each_entry_safe(data, n, &file->triggers, list) {
  435. trace_event_trigger_enable_disable(file, 0);
  436. list_del_rcu(&data->list);
  437. if (data->ops->free)
  438. data->ops->free(data);
  439. }
  440. }
  441. }
  442. /**
  443. * update_cond_flag - Set or reset the TRIGGER_COND bit
  444. * @file: The trace_event_file associated with the event
  445. *
  446. * If an event has triggers and any of those triggers has a filter or
  447. * a post_trigger, trigger invocation needs to be deferred until after
  448. * the current event has logged its data, and the event should have
  449. * its TRIGGER_COND bit set, otherwise the TRIGGER_COND bit should be
  450. * cleared.
  451. */
  452. void update_cond_flag(struct trace_event_file *file)
  453. {
  454. struct event_trigger_data *data;
  455. bool set_cond = false;
  456. lockdep_assert_held(&event_mutex);
  457. list_for_each_entry(data, &file->triggers, list) {
  458. if (data->filter || event_command_post_trigger(data->cmd_ops) ||
  459. event_command_needs_rec(data->cmd_ops)) {
  460. set_cond = true;
  461. break;
  462. }
  463. }
  464. if (set_cond)
  465. set_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
  466. else
  467. clear_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
  468. }
  469. /**
  470. * register_trigger - Generic event_command @reg implementation
  471. * @glob: The raw string used to register the trigger
  472. * @data: Trigger-specific data to associate with the trigger
  473. * @file: The trace_event_file associated with the event
  474. *
  475. * Common implementation for event trigger registration.
  476. *
  477. * Usually used directly as the @reg method in event command
  478. * implementations.
  479. *
  480. * Return: 0 on success, errno otherwise
  481. */
  482. static int register_trigger(char *glob,
  483. struct event_trigger_data *data,
  484. struct trace_event_file *file)
  485. {
  486. struct event_trigger_data *test;
  487. int ret = 0;
  488. lockdep_assert_held(&event_mutex);
  489. list_for_each_entry(test, &file->triggers, list) {
  490. if (test->cmd_ops->trigger_type == data->cmd_ops->trigger_type) {
  491. ret = -EEXIST;
  492. goto out;
  493. }
  494. }
  495. if (data->ops->init) {
  496. ret = data->ops->init(data);
  497. if (ret < 0)
  498. goto out;
  499. }
  500. list_add_rcu(&data->list, &file->triggers);
  501. update_cond_flag(file);
  502. ret = trace_event_trigger_enable_disable(file, 1);
  503. if (ret < 0) {
  504. list_del_rcu(&data->list);
  505. update_cond_flag(file);
  506. }
  507. out:
  508. return ret;
  509. }
  510. /**
  511. * unregister_trigger - Generic event_command @unreg implementation
  512. * @glob: The raw string used to register the trigger
  513. * @test: Trigger-specific data used to find the trigger to remove
  514. * @file: The trace_event_file associated with the event
  515. *
  516. * Common implementation for event trigger unregistration.
  517. *
  518. * Usually used directly as the @unreg method in event command
  519. * implementations.
  520. */
  521. static void unregister_trigger(char *glob,
  522. struct event_trigger_data *test,
  523. struct trace_event_file *file)
  524. {
  525. struct event_trigger_data *data = NULL, *iter;
  526. lockdep_assert_held(&event_mutex);
  527. list_for_each_entry(iter, &file->triggers, list) {
  528. if (iter->cmd_ops->trigger_type == test->cmd_ops->trigger_type) {
  529. data = iter;
  530. list_del_rcu(&data->list);
  531. trace_event_trigger_enable_disable(file, 0);
  532. update_cond_flag(file);
  533. break;
  534. }
  535. }
  536. if (data && data->ops->free)
  537. data->ops->free(data);
  538. }
  539. /*
  540. * Event trigger parsing helper functions.
  541. *
  542. * These functions help make it easier to write an event trigger
  543. * parsing function i.e. the struct event_command.parse() callback
  544. * function responsible for parsing and registering a trigger command
  545. * written to the 'trigger' file.
  546. *
  547. * A trigger command (or just 'trigger' for short) takes the form:
  548. * [trigger] [if filter]
  549. *
  550. * The struct event_command.parse() callback (and other struct
  551. * event_command functions) refer to several components of a trigger
  552. * command. Those same components are referenced by the event trigger
  553. * parsing helper functions defined below. These components are:
  554. *
  555. * cmd - the trigger command name
  556. * glob - the trigger command name optionally prefaced with '!'
  557. * param_and_filter - text following cmd and ':'
  558. * param - text following cmd and ':' and stripped of filter
  559. * filter - the optional filter text following (and including) 'if'
  560. *
  561. * To illustrate the use of these componenents, here are some concrete
  562. * examples. For the following triggers:
  563. *
  564. * echo 'traceon:5 if pid == 0' > trigger
  565. * - 'traceon' is both cmd and glob
  566. * - '5 if pid == 0' is the param_and_filter
  567. * - '5' is the param
  568. * - 'if pid == 0' is the filter
  569. *
  570. * echo 'enable_event:sys:event:n' > trigger
  571. * - 'enable_event' is both cmd and glob
  572. * - 'sys:event:n' is the param_and_filter
  573. * - 'sys:event:n' is the param
  574. * - there is no filter
  575. *
  576. * echo 'hist:keys=pid if prio > 50' > trigger
  577. * - 'hist' is both cmd and glob
  578. * - 'keys=pid if prio > 50' is the param_and_filter
  579. * - 'keys=pid' is the param
  580. * - 'if prio > 50' is the filter
  581. *
  582. * echo '!enable_event:sys:event:n' > trigger
  583. * - 'enable_event' the cmd
  584. * - '!enable_event' is the glob
  585. * - 'sys:event:n' is the param_and_filter
  586. * - 'sys:event:n' is the param
  587. * - there is no filter
  588. *
  589. * echo 'traceoff' > trigger
  590. * - 'traceoff' is both cmd and glob
  591. * - there is no param_and_filter
  592. * - there is no param
  593. * - there is no filter
  594. *
  595. * There are a few different categories of event trigger covered by
  596. * these helpers:
  597. *
  598. * - triggers that don't require a parameter e.g. traceon
  599. * - triggers that do require a parameter e.g. enable_event and hist
  600. * - triggers that though they may not require a param may support an
  601. * optional 'n' param (n = number of times the trigger should fire)
  602. * e.g.: traceon:5 or enable_event:sys:event:n
  603. * - triggers that do not support an 'n' param e.g. hist
  604. *
  605. * These functions can be used or ignored as necessary - it all
  606. * depends on the complexity of the trigger, and the granularity of
  607. * the functions supported reflects the fact that some implementations
  608. * may need to customize certain aspects of their implementations and
  609. * won't need certain functions. For instance, the hist trigger
  610. * implementation doesn't use event_trigger_separate_filter() because
  611. * it has special requirements for handling the filter.
  612. */
  613. /**
  614. * event_trigger_check_remove - check whether an event trigger specifies remove
  615. * @glob: The trigger command string, with optional remove(!) operator
  616. *
  617. * The event trigger callback implementations pass in 'glob' as a
  618. * parameter. This is the command name either with or without a
  619. * remove(!) operator. This function simply parses the glob and
  620. * determines whether the command corresponds to a trigger removal or
  621. * a trigger addition.
  622. *
  623. * Return: true if this is a remove command, false otherwise
  624. */
  625. bool event_trigger_check_remove(const char *glob)
  626. {
  627. return (glob && glob[0] == '!') ? true : false;
  628. }
  629. /**
  630. * event_trigger_empty_param - check whether the param is empty
  631. * @param: The trigger param string
  632. *
  633. * The event trigger callback implementations pass in 'param' as a
  634. * parameter. This corresponds to the string following the command
  635. * name minus the command name. This function can be called by a
  636. * callback implementation for any command that requires a param; a
  637. * callback that doesn't require a param can ignore it.
  638. *
  639. * Return: true if this is an empty param, false otherwise
  640. */
  641. bool event_trigger_empty_param(const char *param)
  642. {
  643. return !param;
  644. }
  645. /**
  646. * event_trigger_separate_filter - separate an event trigger from a filter
  647. * @param_and_filter: String containing trigger and possibly filter
  648. * @param: outparam, will be filled with a pointer to the trigger
  649. * @filter: outparam, will be filled with a pointer to the filter
  650. * @param_required: Specifies whether or not the param string is required
  651. *
  652. * Given a param string of the form '[trigger] [if filter]', this
  653. * function separates the filter from the trigger and returns the
  654. * trigger in @param and the filter in @filter. Either the @param
  655. * or the @filter may be set to NULL by this function - if not set to
  656. * NULL, they will contain strings corresponding to the trigger and
  657. * filter.
  658. *
  659. * There are two cases that need to be handled with respect to the
  660. * passed-in param: either the param is required, or it is not
  661. * required. If @param_required is set, and there's no param, it will
  662. * return -EINVAL. If @param_required is not set and there's a param
  663. * that starts with a number, that corresponds to the case of a
  664. * trigger with :n (n = number of times the trigger should fire) and
  665. * the parsing continues normally; otherwise the function just returns
  666. * and assumes param just contains a filter and there's nothing else
  667. * to do.
  668. *
  669. * Return: 0 on success, errno otherwise
  670. */
  671. int event_trigger_separate_filter(char *param_and_filter, char **param,
  672. char **filter, bool param_required)
  673. {
  674. int ret = 0;
  675. *param = *filter = NULL;
  676. if (!param_and_filter) {
  677. if (param_required)
  678. ret = -EINVAL;
  679. goto out;
  680. }
  681. /*
  682. * Here we check for an optional param. The only legal
  683. * optional param is :n, and if that's the case, continue
  684. * below. Otherwise we assume what's left is a filter and
  685. * return it as the filter string for the caller to deal with.
  686. */
  687. if (!param_required && param_and_filter && !isdigit(param_and_filter[0])) {
  688. *filter = param_and_filter;
  689. goto out;
  690. }
  691. /*
  692. * Separate the param from the filter (param [if filter]).
  693. * Here we have either an optional :n param or a required
  694. * param and an optional filter.
  695. */
  696. *param = strsep(&param_and_filter, " \t");
  697. /*
  698. * Here we have a filter, though it may be empty.
  699. */
  700. if (param_and_filter) {
  701. *filter = skip_spaces(param_and_filter);
  702. if (!**filter)
  703. *filter = NULL;
  704. }
  705. out:
  706. return ret;
  707. }
  708. /**
  709. * event_trigger_alloc - allocate and init event_trigger_data for a trigger
  710. * @cmd_ops: The event_command operations for the trigger
  711. * @cmd: The cmd string
  712. * @param: The param string
  713. * @private_data: User data to associate with the event trigger
  714. *
  715. * Allocate an event_trigger_data instance and initialize it. The
  716. * @cmd_ops are used along with the @cmd and @param to get the
  717. * trigger_ops to assign to the event_trigger_data. @private_data can
  718. * also be passed in and associated with the event_trigger_data.
  719. *
  720. * Use event_trigger_free() to free an event_trigger_data object.
  721. *
  722. * Return: The trigger_data object success, NULL otherwise
  723. */
  724. struct event_trigger_data *event_trigger_alloc(struct event_command *cmd_ops,
  725. char *cmd,
  726. char *param,
  727. void *private_data)
  728. {
  729. struct event_trigger_data *trigger_data;
  730. struct event_trigger_ops *trigger_ops;
  731. trigger_ops = cmd_ops->get_trigger_ops(cmd, param);
  732. trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
  733. if (!trigger_data)
  734. return NULL;
  735. trigger_data->count = -1;
  736. trigger_data->ops = trigger_ops;
  737. trigger_data->cmd_ops = cmd_ops;
  738. trigger_data->private_data = private_data;
  739. INIT_LIST_HEAD(&trigger_data->list);
  740. INIT_LIST_HEAD(&trigger_data->named_list);
  741. RCU_INIT_POINTER(trigger_data->filter, NULL);
  742. return trigger_data;
  743. }
  744. /**
  745. * event_trigger_parse_num - parse and return the number param for a trigger
  746. * @param: The param string
  747. * @trigger_data: The trigger_data for the trigger
  748. *
  749. * Parse the :n (n = number of times the trigger should fire) param
  750. * and set the count variable in the trigger_data to the parsed count.
  751. *
  752. * Return: 0 on success, errno otherwise
  753. */
  754. int event_trigger_parse_num(char *param,
  755. struct event_trigger_data *trigger_data)
  756. {
  757. char *number;
  758. int ret = 0;
  759. if (param) {
  760. number = strsep(&param, ":");
  761. if (!strlen(number))
  762. return -EINVAL;
  763. /*
  764. * We use the callback data field (which is a pointer)
  765. * as our counter.
  766. */
  767. ret = kstrtoul(number, 0, &trigger_data->count);
  768. }
  769. return ret;
  770. }
  771. /**
  772. * event_trigger_set_filter - set an event trigger's filter
  773. * @cmd_ops: The event_command operations for the trigger
  774. * @file: The event file for the trigger's event
  775. * @param: The string containing the filter
  776. * @trigger_data: The trigger_data for the trigger
  777. *
  778. * Set the filter for the trigger. If the filter is NULL, just return
  779. * without error.
  780. *
  781. * Return: 0 on success, errno otherwise
  782. */
  783. int event_trigger_set_filter(struct event_command *cmd_ops,
  784. struct trace_event_file *file,
  785. char *param,
  786. struct event_trigger_data *trigger_data)
  787. {
  788. if (param && cmd_ops->set_filter)
  789. return cmd_ops->set_filter(param, trigger_data, file);
  790. return 0;
  791. }
  792. /**
  793. * event_trigger_reset_filter - reset an event trigger's filter
  794. * @cmd_ops: The event_command operations for the trigger
  795. * @trigger_data: The trigger_data for the trigger
  796. *
  797. * Reset the filter for the trigger to no filter.
  798. */
  799. void event_trigger_reset_filter(struct event_command *cmd_ops,
  800. struct event_trigger_data *trigger_data)
  801. {
  802. if (cmd_ops->set_filter)
  803. cmd_ops->set_filter(NULL, trigger_data, NULL);
  804. }
  805. /**
  806. * event_trigger_register - register an event trigger
  807. * @cmd_ops: The event_command operations for the trigger
  808. * @file: The event file for the trigger's event
  809. * @glob: The trigger command string, with optional remove(!) operator
  810. * @trigger_data: The trigger_data for the trigger
  811. *
  812. * Register an event trigger. The @cmd_ops are used to call the
  813. * cmd_ops->reg() function which actually does the registration.
  814. *
  815. * Return: 0 on success, errno otherwise
  816. */
  817. int event_trigger_register(struct event_command *cmd_ops,
  818. struct trace_event_file *file,
  819. char *glob,
  820. struct event_trigger_data *trigger_data)
  821. {
  822. return cmd_ops->reg(glob, trigger_data, file);
  823. }
  824. /**
  825. * event_trigger_unregister - unregister an event trigger
  826. * @cmd_ops: The event_command operations for the trigger
  827. * @file: The event file for the trigger's event
  828. * @glob: The trigger command string, with optional remove(!) operator
  829. * @trigger_data: The trigger_data for the trigger
  830. *
  831. * Unregister an event trigger. The @cmd_ops are used to call the
  832. * cmd_ops->unreg() function which actually does the unregistration.
  833. */
  834. void event_trigger_unregister(struct event_command *cmd_ops,
  835. struct trace_event_file *file,
  836. char *glob,
  837. struct event_trigger_data *trigger_data)
  838. {
  839. cmd_ops->unreg(glob, trigger_data, file);
  840. }
  841. /*
  842. * End event trigger parsing helper functions.
  843. */
  844. /**
  845. * event_trigger_parse - Generic event_command @parse implementation
  846. * @cmd_ops: The command ops, used for trigger registration
  847. * @file: The trace_event_file associated with the event
  848. * @glob: The raw string used to register the trigger
  849. * @cmd: The cmd portion of the string used to register the trigger
  850. * @param_and_filter: The param and filter portion of the string used to register the trigger
  851. *
  852. * Common implementation for event command parsing and trigger
  853. * instantiation.
  854. *
  855. * Usually used directly as the @parse method in event command
  856. * implementations.
  857. *
  858. * Return: 0 on success, errno otherwise
  859. */
  860. static int
  861. event_trigger_parse(struct event_command *cmd_ops,
  862. struct trace_event_file *file,
  863. char *glob, char *cmd, char *param_and_filter)
  864. {
  865. struct event_trigger_data *trigger_data;
  866. char *param, *filter;
  867. bool remove;
  868. int ret;
  869. remove = event_trigger_check_remove(glob);
  870. ret = event_trigger_separate_filter(param_and_filter, &param, &filter, false);
  871. if (ret)
  872. return ret;
  873. ret = -ENOMEM;
  874. trigger_data = event_trigger_alloc(cmd_ops, cmd, param, file);
  875. if (!trigger_data)
  876. goto out;
  877. if (remove) {
  878. event_trigger_unregister(cmd_ops, file, glob+1, trigger_data);
  879. kfree(trigger_data);
  880. ret = 0;
  881. goto out;
  882. }
  883. ret = event_trigger_parse_num(param, trigger_data);
  884. if (ret)
  885. goto out_free;
  886. ret = event_trigger_set_filter(cmd_ops, file, filter, trigger_data);
  887. if (ret < 0)
  888. goto out_free;
  889. /* Up the trigger_data count to make sure reg doesn't free it on failure */
  890. event_trigger_init(trigger_data);
  891. ret = event_trigger_register(cmd_ops, file, glob, trigger_data);
  892. if (ret)
  893. goto out_free;
  894. /* Down the counter of trigger_data or free it if not used anymore */
  895. event_trigger_free(trigger_data);
  896. out:
  897. return ret;
  898. out_free:
  899. event_trigger_reset_filter(cmd_ops, trigger_data);
  900. kfree(trigger_data);
  901. goto out;
  902. }
  903. /**
  904. * set_trigger_filter - Generic event_command @set_filter implementation
  905. * @filter_str: The filter string for the trigger, NULL to remove filter
  906. * @trigger_data: Trigger-specific data
  907. * @file: The trace_event_file associated with the event
  908. *
  909. * Common implementation for event command filter parsing and filter
  910. * instantiation.
  911. *
  912. * Usually used directly as the @set_filter method in event command
  913. * implementations.
  914. *
  915. * Also used to remove a filter (if filter_str = NULL).
  916. *
  917. * Return: 0 on success, errno otherwise
  918. */
  919. int set_trigger_filter(char *filter_str,
  920. struct event_trigger_data *trigger_data,
  921. struct trace_event_file *file)
  922. {
  923. struct event_trigger_data *data = trigger_data;
  924. struct event_filter *filter = NULL, *tmp;
  925. int ret = -EINVAL;
  926. char *s;
  927. if (!filter_str) /* clear the current filter */
  928. goto assign;
  929. s = strsep(&filter_str, " \t");
  930. if (!strlen(s) || strcmp(s, "if") != 0)
  931. goto out;
  932. if (!filter_str)
  933. goto out;
  934. /* The filter is for the 'trigger' event, not the triggered event */
  935. ret = create_event_filter(file->tr, file->event_call,
  936. filter_str, false, &filter);
  937. /*
  938. * If create_event_filter() fails, filter still needs to be freed.
  939. * Which the calling code will do with data->filter.
  940. */
  941. assign:
  942. tmp = rcu_access_pointer(data->filter);
  943. rcu_assign_pointer(data->filter, filter);
  944. if (tmp) {
  945. /* Make sure the call is done with the filter */
  946. tracepoint_synchronize_unregister();
  947. free_event_filter(tmp);
  948. }
  949. kfree(data->filter_str);
  950. data->filter_str = NULL;
  951. if (filter_str) {
  952. data->filter_str = kstrdup(filter_str, GFP_KERNEL);
  953. if (!data->filter_str) {
  954. free_event_filter(rcu_access_pointer(data->filter));
  955. data->filter = NULL;
  956. ret = -ENOMEM;
  957. }
  958. }
  959. out:
  960. return ret;
  961. }
  962. static LIST_HEAD(named_triggers);
  963. /**
  964. * find_named_trigger - Find the common named trigger associated with @name
  965. * @name: The name of the set of named triggers to find the common data for
  966. *
  967. * Named triggers are sets of triggers that share a common set of
  968. * trigger data. The first named trigger registered with a given name
  969. * owns the common trigger data that the others subsequently
  970. * registered with the same name will reference. This function
  971. * returns the common trigger data associated with that first
  972. * registered instance.
  973. *
  974. * Return: the common trigger data for the given named trigger on
  975. * success, NULL otherwise.
  976. */
  977. struct event_trigger_data *find_named_trigger(const char *name)
  978. {
  979. struct event_trigger_data *data;
  980. if (!name)
  981. return NULL;
  982. list_for_each_entry(data, &named_triggers, named_list) {
  983. if (data->named_data)
  984. continue;
  985. if (strcmp(data->name, name) == 0)
  986. return data;
  987. }
  988. return NULL;
  989. }
  990. /**
  991. * is_named_trigger - determine if a given trigger is a named trigger
  992. * @test: The trigger data to test
  993. *
  994. * Return: true if 'test' is a named trigger, false otherwise.
  995. */
  996. bool is_named_trigger(struct event_trigger_data *test)
  997. {
  998. struct event_trigger_data *data;
  999. list_for_each_entry(data, &named_triggers, named_list) {
  1000. if (test == data)
  1001. return true;
  1002. }
  1003. return false;
  1004. }
  1005. /**
  1006. * save_named_trigger - save the trigger in the named trigger list
  1007. * @name: The name of the named trigger set
  1008. * @data: The trigger data to save
  1009. *
  1010. * Return: 0 if successful, negative error otherwise.
  1011. */
  1012. int save_named_trigger(const char *name, struct event_trigger_data *data)
  1013. {
  1014. data->name = kstrdup(name, GFP_KERNEL);
  1015. if (!data->name)
  1016. return -ENOMEM;
  1017. list_add(&data->named_list, &named_triggers);
  1018. return 0;
  1019. }
  1020. /**
  1021. * del_named_trigger - delete a trigger from the named trigger list
  1022. * @data: The trigger data to delete
  1023. */
  1024. void del_named_trigger(struct event_trigger_data *data)
  1025. {
  1026. kfree(data->name);
  1027. data->name = NULL;
  1028. list_del(&data->named_list);
  1029. }
  1030. static void __pause_named_trigger(struct event_trigger_data *data, bool pause)
  1031. {
  1032. struct event_trigger_data *test;
  1033. list_for_each_entry(test, &named_triggers, named_list) {
  1034. if (strcmp(test->name, data->name) == 0) {
  1035. if (pause) {
  1036. test->paused_tmp = test->paused;
  1037. test->paused = true;
  1038. } else {
  1039. test->paused = test->paused_tmp;
  1040. }
  1041. }
  1042. }
  1043. }
  1044. /**
  1045. * pause_named_trigger - Pause all named triggers with the same name
  1046. * @data: The trigger data of a named trigger to pause
  1047. *
  1048. * Pauses a named trigger along with all other triggers having the
  1049. * same name. Because named triggers share a common set of data,
  1050. * pausing only one is meaningless, so pausing one named trigger needs
  1051. * to pause all triggers with the same name.
  1052. */
  1053. void pause_named_trigger(struct event_trigger_data *data)
  1054. {
  1055. __pause_named_trigger(data, true);
  1056. }
  1057. /**
  1058. * unpause_named_trigger - Un-pause all named triggers with the same name
  1059. * @data: The trigger data of a named trigger to unpause
  1060. *
  1061. * Un-pauses a named trigger along with all other triggers having the
  1062. * same name. Because named triggers share a common set of data,
  1063. * unpausing only one is meaningless, so unpausing one named trigger
  1064. * needs to unpause all triggers with the same name.
  1065. */
  1066. void unpause_named_trigger(struct event_trigger_data *data)
  1067. {
  1068. __pause_named_trigger(data, false);
  1069. }
  1070. /**
  1071. * set_named_trigger_data - Associate common named trigger data
  1072. * @data: The trigger data to associate
  1073. * @named_data: The common named trigger to be associated
  1074. *
  1075. * Named triggers are sets of triggers that share a common set of
  1076. * trigger data. The first named trigger registered with a given name
  1077. * owns the common trigger data that the others subsequently
  1078. * registered with the same name will reference. This function
  1079. * associates the common trigger data from the first trigger with the
  1080. * given trigger.
  1081. */
  1082. void set_named_trigger_data(struct event_trigger_data *data,
  1083. struct event_trigger_data *named_data)
  1084. {
  1085. data->named_data = named_data;
  1086. }
  1087. struct event_trigger_data *
  1088. get_named_trigger_data(struct event_trigger_data *data)
  1089. {
  1090. return data->named_data;
  1091. }
  1092. static void
  1093. traceon_trigger(struct event_trigger_data *data,
  1094. struct trace_buffer *buffer, void *rec,
  1095. struct ring_buffer_event *event)
  1096. {
  1097. struct trace_event_file *file = data->private_data;
  1098. if (file) {
  1099. if (tracer_tracing_is_on(file->tr))
  1100. return;
  1101. tracer_tracing_on(file->tr);
  1102. return;
  1103. }
  1104. if (tracing_is_on())
  1105. return;
  1106. tracing_on();
  1107. }
  1108. static void
  1109. traceon_count_trigger(struct event_trigger_data *data,
  1110. struct trace_buffer *buffer, void *rec,
  1111. struct ring_buffer_event *event)
  1112. {
  1113. struct trace_event_file *file = data->private_data;
  1114. if (file) {
  1115. if (tracer_tracing_is_on(file->tr))
  1116. return;
  1117. } else {
  1118. if (tracing_is_on())
  1119. return;
  1120. }
  1121. if (!data->count)
  1122. return;
  1123. if (data->count != -1)
  1124. (data->count)--;
  1125. if (file)
  1126. tracer_tracing_on(file->tr);
  1127. else
  1128. tracing_on();
  1129. }
  1130. static void
  1131. traceoff_trigger(struct event_trigger_data *data,
  1132. struct trace_buffer *buffer, void *rec,
  1133. struct ring_buffer_event *event)
  1134. {
  1135. struct trace_event_file *file = data->private_data;
  1136. if (file) {
  1137. if (!tracer_tracing_is_on(file->tr))
  1138. return;
  1139. tracer_tracing_off(file->tr);
  1140. return;
  1141. }
  1142. if (!tracing_is_on())
  1143. return;
  1144. tracing_off();
  1145. }
  1146. static void
  1147. traceoff_count_trigger(struct event_trigger_data *data,
  1148. struct trace_buffer *buffer, void *rec,
  1149. struct ring_buffer_event *event)
  1150. {
  1151. struct trace_event_file *file = data->private_data;
  1152. if (file) {
  1153. if (!tracer_tracing_is_on(file->tr))
  1154. return;
  1155. } else {
  1156. if (!tracing_is_on())
  1157. return;
  1158. }
  1159. if (!data->count)
  1160. return;
  1161. if (data->count != -1)
  1162. (data->count)--;
  1163. if (file)
  1164. tracer_tracing_off(file->tr);
  1165. else
  1166. tracing_off();
  1167. }
  1168. static int
  1169. traceon_trigger_print(struct seq_file *m, struct event_trigger_data *data)
  1170. {
  1171. return event_trigger_print("traceon", m, (void *)data->count,
  1172. data->filter_str);
  1173. }
  1174. static int
  1175. traceoff_trigger_print(struct seq_file *m, struct event_trigger_data *data)
  1176. {
  1177. return event_trigger_print("traceoff", m, (void *)data->count,
  1178. data->filter_str);
  1179. }
  1180. static struct event_trigger_ops traceon_trigger_ops = {
  1181. .trigger = traceon_trigger,
  1182. .print = traceon_trigger_print,
  1183. .init = event_trigger_init,
  1184. .free = event_trigger_free,
  1185. };
  1186. static struct event_trigger_ops traceon_count_trigger_ops = {
  1187. .trigger = traceon_count_trigger,
  1188. .print = traceon_trigger_print,
  1189. .init = event_trigger_init,
  1190. .free = event_trigger_free,
  1191. };
  1192. static struct event_trigger_ops traceoff_trigger_ops = {
  1193. .trigger = traceoff_trigger,
  1194. .print = traceoff_trigger_print,
  1195. .init = event_trigger_init,
  1196. .free = event_trigger_free,
  1197. };
  1198. static struct event_trigger_ops traceoff_count_trigger_ops = {
  1199. .trigger = traceoff_count_trigger,
  1200. .print = traceoff_trigger_print,
  1201. .init = event_trigger_init,
  1202. .free = event_trigger_free,
  1203. };
  1204. static struct event_trigger_ops *
  1205. onoff_get_trigger_ops(char *cmd, char *param)
  1206. {
  1207. struct event_trigger_ops *ops;
  1208. /* we register both traceon and traceoff to this callback */
  1209. if (strcmp(cmd, "traceon") == 0)
  1210. ops = param ? &traceon_count_trigger_ops :
  1211. &traceon_trigger_ops;
  1212. else
  1213. ops = param ? &traceoff_count_trigger_ops :
  1214. &traceoff_trigger_ops;
  1215. return ops;
  1216. }
  1217. static struct event_command trigger_traceon_cmd = {
  1218. .name = "traceon",
  1219. .trigger_type = ETT_TRACE_ONOFF,
  1220. .parse = event_trigger_parse,
  1221. .reg = register_trigger,
  1222. .unreg = unregister_trigger,
  1223. .get_trigger_ops = onoff_get_trigger_ops,
  1224. .set_filter = set_trigger_filter,
  1225. };
  1226. static struct event_command trigger_traceoff_cmd = {
  1227. .name = "traceoff",
  1228. .trigger_type = ETT_TRACE_ONOFF,
  1229. .flags = EVENT_CMD_FL_POST_TRIGGER,
  1230. .parse = event_trigger_parse,
  1231. .reg = register_trigger,
  1232. .unreg = unregister_trigger,
  1233. .get_trigger_ops = onoff_get_trigger_ops,
  1234. .set_filter = set_trigger_filter,
  1235. };
  1236. #ifdef CONFIG_TRACER_SNAPSHOT
  1237. static void
  1238. snapshot_trigger(struct event_trigger_data *data,
  1239. struct trace_buffer *buffer, void *rec,
  1240. struct ring_buffer_event *event)
  1241. {
  1242. struct trace_event_file *file = data->private_data;
  1243. if (file)
  1244. tracing_snapshot_instance(file->tr);
  1245. else
  1246. tracing_snapshot();
  1247. }
  1248. static void
  1249. snapshot_count_trigger(struct event_trigger_data *data,
  1250. struct trace_buffer *buffer, void *rec,
  1251. struct ring_buffer_event *event)
  1252. {
  1253. if (!data->count)
  1254. return;
  1255. if (data->count != -1)
  1256. (data->count)--;
  1257. snapshot_trigger(data, buffer, rec, event);
  1258. }
  1259. static int
  1260. register_snapshot_trigger(char *glob,
  1261. struct event_trigger_data *data,
  1262. struct trace_event_file *file)
  1263. {
  1264. if (tracing_alloc_snapshot_instance(file->tr) != 0)
  1265. return 0;
  1266. return register_trigger(glob, data, file);
  1267. }
  1268. static int
  1269. snapshot_trigger_print(struct seq_file *m, struct event_trigger_data *data)
  1270. {
  1271. return event_trigger_print("snapshot", m, (void *)data->count,
  1272. data->filter_str);
  1273. }
  1274. static struct event_trigger_ops snapshot_trigger_ops = {
  1275. .trigger = snapshot_trigger,
  1276. .print = snapshot_trigger_print,
  1277. .init = event_trigger_init,
  1278. .free = event_trigger_free,
  1279. };
  1280. static struct event_trigger_ops snapshot_count_trigger_ops = {
  1281. .trigger = snapshot_count_trigger,
  1282. .print = snapshot_trigger_print,
  1283. .init = event_trigger_init,
  1284. .free = event_trigger_free,
  1285. };
  1286. static struct event_trigger_ops *
  1287. snapshot_get_trigger_ops(char *cmd, char *param)
  1288. {
  1289. return param ? &snapshot_count_trigger_ops : &snapshot_trigger_ops;
  1290. }
  1291. static struct event_command trigger_snapshot_cmd = {
  1292. .name = "snapshot",
  1293. .trigger_type = ETT_SNAPSHOT,
  1294. .parse = event_trigger_parse,
  1295. .reg = register_snapshot_trigger,
  1296. .unreg = unregister_trigger,
  1297. .get_trigger_ops = snapshot_get_trigger_ops,
  1298. .set_filter = set_trigger_filter,
  1299. };
  1300. static __init int register_trigger_snapshot_cmd(void)
  1301. {
  1302. int ret;
  1303. ret = register_event_command(&trigger_snapshot_cmd);
  1304. WARN_ON(ret < 0);
  1305. return ret;
  1306. }
  1307. #else
  1308. static __init int register_trigger_snapshot_cmd(void) { return 0; }
  1309. #endif /* CONFIG_TRACER_SNAPSHOT */
  1310. #ifdef CONFIG_STACKTRACE
  1311. #ifdef CONFIG_UNWINDER_ORC
  1312. /* Skip 2:
  1313. * event_triggers_post_call()
  1314. * trace_event_raw_event_xxx()
  1315. */
  1316. # define STACK_SKIP 2
  1317. #else
  1318. /*
  1319. * Skip 4:
  1320. * stacktrace_trigger()
  1321. * event_triggers_post_call()
  1322. * trace_event_buffer_commit()
  1323. * trace_event_raw_event_xxx()
  1324. */
  1325. #define STACK_SKIP 4
  1326. #endif
  1327. static void
  1328. stacktrace_trigger(struct event_trigger_data *data,
  1329. struct trace_buffer *buffer, void *rec,
  1330. struct ring_buffer_event *event)
  1331. {
  1332. struct trace_event_file *file = data->private_data;
  1333. if (file)
  1334. __trace_stack(file->tr, tracing_gen_ctx(), STACK_SKIP);
  1335. else
  1336. trace_dump_stack(STACK_SKIP);
  1337. }
  1338. static void
  1339. stacktrace_count_trigger(struct event_trigger_data *data,
  1340. struct trace_buffer *buffer, void *rec,
  1341. struct ring_buffer_event *event)
  1342. {
  1343. if (!data->count)
  1344. return;
  1345. if (data->count != -1)
  1346. (data->count)--;
  1347. stacktrace_trigger(data, buffer, rec, event);
  1348. }
  1349. static int
  1350. stacktrace_trigger_print(struct seq_file *m, struct event_trigger_data *data)
  1351. {
  1352. return event_trigger_print("stacktrace", m, (void *)data->count,
  1353. data->filter_str);
  1354. }
  1355. static struct event_trigger_ops stacktrace_trigger_ops = {
  1356. .trigger = stacktrace_trigger,
  1357. .print = stacktrace_trigger_print,
  1358. .init = event_trigger_init,
  1359. .free = event_trigger_free,
  1360. };
  1361. static struct event_trigger_ops stacktrace_count_trigger_ops = {
  1362. .trigger = stacktrace_count_trigger,
  1363. .print = stacktrace_trigger_print,
  1364. .init = event_trigger_init,
  1365. .free = event_trigger_free,
  1366. };
  1367. static struct event_trigger_ops *
  1368. stacktrace_get_trigger_ops(char *cmd, char *param)
  1369. {
  1370. return param ? &stacktrace_count_trigger_ops : &stacktrace_trigger_ops;
  1371. }
  1372. static struct event_command trigger_stacktrace_cmd = {
  1373. .name = "stacktrace",
  1374. .trigger_type = ETT_STACKTRACE,
  1375. .flags = EVENT_CMD_FL_POST_TRIGGER,
  1376. .parse = event_trigger_parse,
  1377. .reg = register_trigger,
  1378. .unreg = unregister_trigger,
  1379. .get_trigger_ops = stacktrace_get_trigger_ops,
  1380. .set_filter = set_trigger_filter,
  1381. };
  1382. static __init int register_trigger_stacktrace_cmd(void)
  1383. {
  1384. int ret;
  1385. ret = register_event_command(&trigger_stacktrace_cmd);
  1386. WARN_ON(ret < 0);
  1387. return ret;
  1388. }
  1389. #else
  1390. static __init int register_trigger_stacktrace_cmd(void) { return 0; }
  1391. #endif /* CONFIG_STACKTRACE */
  1392. static __init void unregister_trigger_traceon_traceoff_cmds(void)
  1393. {
  1394. unregister_event_command(&trigger_traceon_cmd);
  1395. unregister_event_command(&trigger_traceoff_cmd);
  1396. }
  1397. static void
  1398. event_enable_trigger(struct event_trigger_data *data,
  1399. struct trace_buffer *buffer, void *rec,
  1400. struct ring_buffer_event *event)
  1401. {
  1402. struct enable_trigger_data *enable_data = data->private_data;
  1403. if (enable_data->enable)
  1404. clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
  1405. else
  1406. set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
  1407. }
  1408. static void
  1409. event_enable_count_trigger(struct event_trigger_data *data,
  1410. struct trace_buffer *buffer, void *rec,
  1411. struct ring_buffer_event *event)
  1412. {
  1413. struct enable_trigger_data *enable_data = data->private_data;
  1414. if (!data->count)
  1415. return;
  1416. /* Skip if the event is in a state we want to switch to */
  1417. if (enable_data->enable == !(enable_data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
  1418. return;
  1419. if (data->count != -1)
  1420. (data->count)--;
  1421. event_enable_trigger(data, buffer, rec, event);
  1422. }
  1423. int event_enable_trigger_print(struct seq_file *m,
  1424. struct event_trigger_data *data)
  1425. {
  1426. struct enable_trigger_data *enable_data = data->private_data;
  1427. seq_printf(m, "%s:%s:%s",
  1428. enable_data->hist ?
  1429. (enable_data->enable ? ENABLE_HIST_STR : DISABLE_HIST_STR) :
  1430. (enable_data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR),
  1431. enable_data->file->event_call->class->system,
  1432. trace_event_name(enable_data->file->event_call));
  1433. if (data->count == -1)
  1434. seq_puts(m, ":unlimited");
  1435. else
  1436. seq_printf(m, ":count=%ld", data->count);
  1437. if (data->filter_str)
  1438. seq_printf(m, " if %s\n", data->filter_str);
  1439. else
  1440. seq_putc(m, '\n');
  1441. return 0;
  1442. }
  1443. void event_enable_trigger_free(struct event_trigger_data *data)
  1444. {
  1445. struct enable_trigger_data *enable_data = data->private_data;
  1446. if (WARN_ON_ONCE(data->ref <= 0))
  1447. return;
  1448. data->ref--;
  1449. if (!data->ref) {
  1450. /* Remove the SOFT_MODE flag */
  1451. trace_event_enable_disable(enable_data->file, 0, 1);
  1452. trace_event_put_ref(enable_data->file->event_call);
  1453. trigger_data_free(data);
  1454. kfree(enable_data);
  1455. }
  1456. }
  1457. static struct event_trigger_ops event_enable_trigger_ops = {
  1458. .trigger = event_enable_trigger,
  1459. .print = event_enable_trigger_print,
  1460. .init = event_trigger_init,
  1461. .free = event_enable_trigger_free,
  1462. };
  1463. static struct event_trigger_ops event_enable_count_trigger_ops = {
  1464. .trigger = event_enable_count_trigger,
  1465. .print = event_enable_trigger_print,
  1466. .init = event_trigger_init,
  1467. .free = event_enable_trigger_free,
  1468. };
  1469. static struct event_trigger_ops event_disable_trigger_ops = {
  1470. .trigger = event_enable_trigger,
  1471. .print = event_enable_trigger_print,
  1472. .init = event_trigger_init,
  1473. .free = event_enable_trigger_free,
  1474. };
  1475. static struct event_trigger_ops event_disable_count_trigger_ops = {
  1476. .trigger = event_enable_count_trigger,
  1477. .print = event_enable_trigger_print,
  1478. .init = event_trigger_init,
  1479. .free = event_enable_trigger_free,
  1480. };
  1481. int event_enable_trigger_parse(struct event_command *cmd_ops,
  1482. struct trace_event_file *file,
  1483. char *glob, char *cmd, char *param_and_filter)
  1484. {
  1485. struct trace_event_file *event_enable_file;
  1486. struct enable_trigger_data *enable_data;
  1487. struct event_trigger_data *trigger_data;
  1488. struct trace_array *tr = file->tr;
  1489. char *param, *filter;
  1490. bool enable, remove;
  1491. const char *system;
  1492. const char *event;
  1493. bool hist = false;
  1494. int ret;
  1495. remove = event_trigger_check_remove(glob);
  1496. if (event_trigger_empty_param(param_and_filter))
  1497. return -EINVAL;
  1498. ret = event_trigger_separate_filter(param_and_filter, &param, &filter, true);
  1499. if (ret)
  1500. return ret;
  1501. system = strsep(&param, ":");
  1502. if (!param)
  1503. return -EINVAL;
  1504. event = strsep(&param, ":");
  1505. ret = -EINVAL;
  1506. event_enable_file = find_event_file(tr, system, event);
  1507. if (!event_enable_file)
  1508. goto out;
  1509. #ifdef CONFIG_HIST_TRIGGERS
  1510. hist = ((strcmp(cmd, ENABLE_HIST_STR) == 0) ||
  1511. (strcmp(cmd, DISABLE_HIST_STR) == 0));
  1512. enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
  1513. (strcmp(cmd, ENABLE_HIST_STR) == 0));
  1514. #else
  1515. enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
  1516. #endif
  1517. ret = -ENOMEM;
  1518. enable_data = kzalloc(sizeof(*enable_data), GFP_KERNEL);
  1519. if (!enable_data)
  1520. goto out;
  1521. enable_data->hist = hist;
  1522. enable_data->enable = enable;
  1523. enable_data->file = event_enable_file;
  1524. trigger_data = event_trigger_alloc(cmd_ops, cmd, param, enable_data);
  1525. if (!trigger_data) {
  1526. kfree(enable_data);
  1527. goto out;
  1528. }
  1529. if (remove) {
  1530. event_trigger_unregister(cmd_ops, file, glob+1, trigger_data);
  1531. kfree(trigger_data);
  1532. kfree(enable_data);
  1533. ret = 0;
  1534. goto out;
  1535. }
  1536. /* Up the trigger_data count to make sure nothing frees it on failure */
  1537. event_trigger_init(trigger_data);
  1538. ret = event_trigger_parse_num(param, trigger_data);
  1539. if (ret)
  1540. goto out_free;
  1541. ret = event_trigger_set_filter(cmd_ops, file, filter, trigger_data);
  1542. if (ret < 0)
  1543. goto out_free;
  1544. /* Don't let event modules unload while probe registered */
  1545. ret = trace_event_try_get_ref(event_enable_file->event_call);
  1546. if (!ret) {
  1547. ret = -EBUSY;
  1548. goto out_free;
  1549. }
  1550. ret = trace_event_enable_disable(event_enable_file, 1, 1);
  1551. if (ret < 0)
  1552. goto out_put;
  1553. ret = event_trigger_register(cmd_ops, file, glob, trigger_data);
  1554. if (ret)
  1555. goto out_disable;
  1556. event_trigger_free(trigger_data);
  1557. out:
  1558. return ret;
  1559. out_disable:
  1560. trace_event_enable_disable(event_enable_file, 0, 1);
  1561. out_put:
  1562. trace_event_put_ref(event_enable_file->event_call);
  1563. out_free:
  1564. event_trigger_reset_filter(cmd_ops, trigger_data);
  1565. event_trigger_free(trigger_data);
  1566. kfree(enable_data);
  1567. goto out;
  1568. }
  1569. int event_enable_register_trigger(char *glob,
  1570. struct event_trigger_data *data,
  1571. struct trace_event_file *file)
  1572. {
  1573. struct enable_trigger_data *enable_data = data->private_data;
  1574. struct enable_trigger_data *test_enable_data;
  1575. struct event_trigger_data *test;
  1576. int ret = 0;
  1577. lockdep_assert_held(&event_mutex);
  1578. list_for_each_entry(test, &file->triggers, list) {
  1579. test_enable_data = test->private_data;
  1580. if (test_enable_data &&
  1581. (test->cmd_ops->trigger_type ==
  1582. data->cmd_ops->trigger_type) &&
  1583. (test_enable_data->file == enable_data->file)) {
  1584. ret = -EEXIST;
  1585. goto out;
  1586. }
  1587. }
  1588. if (data->ops->init) {
  1589. ret = data->ops->init(data);
  1590. if (ret < 0)
  1591. goto out;
  1592. }
  1593. list_add_rcu(&data->list, &file->triggers);
  1594. update_cond_flag(file);
  1595. ret = trace_event_trigger_enable_disable(file, 1);
  1596. if (ret < 0) {
  1597. list_del_rcu(&data->list);
  1598. update_cond_flag(file);
  1599. }
  1600. out:
  1601. return ret;
  1602. }
  1603. void event_enable_unregister_trigger(char *glob,
  1604. struct event_trigger_data *test,
  1605. struct trace_event_file *file)
  1606. {
  1607. struct enable_trigger_data *test_enable_data = test->private_data;
  1608. struct event_trigger_data *data = NULL, *iter;
  1609. struct enable_trigger_data *enable_data;
  1610. lockdep_assert_held(&event_mutex);
  1611. list_for_each_entry(iter, &file->triggers, list) {
  1612. enable_data = iter->private_data;
  1613. if (enable_data &&
  1614. (iter->cmd_ops->trigger_type ==
  1615. test->cmd_ops->trigger_type) &&
  1616. (enable_data->file == test_enable_data->file)) {
  1617. data = iter;
  1618. list_del_rcu(&data->list);
  1619. trace_event_trigger_enable_disable(file, 0);
  1620. update_cond_flag(file);
  1621. break;
  1622. }
  1623. }
  1624. if (data && data->ops->free)
  1625. data->ops->free(data);
  1626. }
  1627. static struct event_trigger_ops *
  1628. event_enable_get_trigger_ops(char *cmd, char *param)
  1629. {
  1630. struct event_trigger_ops *ops;
  1631. bool enable;
  1632. #ifdef CONFIG_HIST_TRIGGERS
  1633. enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
  1634. (strcmp(cmd, ENABLE_HIST_STR) == 0));
  1635. #else
  1636. enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
  1637. #endif
  1638. if (enable)
  1639. ops = param ? &event_enable_count_trigger_ops :
  1640. &event_enable_trigger_ops;
  1641. else
  1642. ops = param ? &event_disable_count_trigger_ops :
  1643. &event_disable_trigger_ops;
  1644. return ops;
  1645. }
  1646. static struct event_command trigger_enable_cmd = {
  1647. .name = ENABLE_EVENT_STR,
  1648. .trigger_type = ETT_EVENT_ENABLE,
  1649. .parse = event_enable_trigger_parse,
  1650. .reg = event_enable_register_trigger,
  1651. .unreg = event_enable_unregister_trigger,
  1652. .get_trigger_ops = event_enable_get_trigger_ops,
  1653. .set_filter = set_trigger_filter,
  1654. };
  1655. static struct event_command trigger_disable_cmd = {
  1656. .name = DISABLE_EVENT_STR,
  1657. .trigger_type = ETT_EVENT_ENABLE,
  1658. .parse = event_enable_trigger_parse,
  1659. .reg = event_enable_register_trigger,
  1660. .unreg = event_enable_unregister_trigger,
  1661. .get_trigger_ops = event_enable_get_trigger_ops,
  1662. .set_filter = set_trigger_filter,
  1663. };
  1664. static __init void unregister_trigger_enable_disable_cmds(void)
  1665. {
  1666. unregister_event_command(&trigger_enable_cmd);
  1667. unregister_event_command(&trigger_disable_cmd);
  1668. }
  1669. static __init int register_trigger_enable_disable_cmds(void)
  1670. {
  1671. int ret;
  1672. ret = register_event_command(&trigger_enable_cmd);
  1673. if (WARN_ON(ret < 0))
  1674. return ret;
  1675. ret = register_event_command(&trigger_disable_cmd);
  1676. if (WARN_ON(ret < 0))
  1677. unregister_trigger_enable_disable_cmds();
  1678. return ret;
  1679. }
  1680. static __init int register_trigger_traceon_traceoff_cmds(void)
  1681. {
  1682. int ret;
  1683. ret = register_event_command(&trigger_traceon_cmd);
  1684. if (WARN_ON(ret < 0))
  1685. return ret;
  1686. ret = register_event_command(&trigger_traceoff_cmd);
  1687. if (WARN_ON(ret < 0))
  1688. unregister_trigger_traceon_traceoff_cmds();
  1689. return ret;
  1690. }
  1691. __init int register_trigger_cmds(void)
  1692. {
  1693. register_trigger_traceon_traceoff_cmds();
  1694. register_trigger_snapshot_cmd();
  1695. register_trigger_stacktrace_cmd();
  1696. register_trigger_enable_disable_cmds();
  1697. register_trigger_hist_enable_disable_cmds();
  1698. register_trigger_hist_cmd();
  1699. return 0;
  1700. }