trace_dynevent.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Common header file for generic dynamic events.
  4. */
  5. #ifndef _TRACE_DYNEVENT_H
  6. #define _TRACE_DYNEVENT_H
  7. #include <linux/kernel.h>
  8. #include <linux/list.h>
  9. #include <linux/mutex.h>
  10. #include <linux/seq_file.h>
  11. #include "trace.h"
  12. struct dyn_event;
  13. /**
  14. * struct dyn_event_operations - Methods for each type of dynamic events
  15. *
  16. * These methods must be set for each type, since there is no default method.
  17. * Before using this for dyn_event_init(), it must be registered by
  18. * dyn_event_register().
  19. *
  20. * @create: Parse and create event method. This is invoked when user passes
  21. * a event definition to dynamic_events interface. This must not destruct
  22. * the arguments and return -ECANCELED if given arguments doesn't match its
  23. * command prefix.
  24. * @show: Showing method. This is invoked when user reads the event definitions
  25. * via dynamic_events interface.
  26. * @is_busy: Check whether given event is busy so that it can not be deleted.
  27. * Return true if it is busy, otherwise false.
  28. * @free: Delete the given event. Return 0 if success, otherwise error.
  29. * @match: Check whether given event and system name match this event. The argc
  30. * and argv is used for exact match. Return true if it matches, otherwise
  31. * false.
  32. *
  33. * Except for @create, these methods are called under holding event_mutex.
  34. */
  35. struct dyn_event_operations {
  36. struct list_head list;
  37. int (*create)(const char *raw_command);
  38. int (*show)(struct seq_file *m, struct dyn_event *ev);
  39. bool (*is_busy)(struct dyn_event *ev);
  40. int (*free)(struct dyn_event *ev);
  41. bool (*match)(const char *system, const char *event,
  42. int argc, const char **argv, struct dyn_event *ev);
  43. };
  44. /* Register new dyn_event type -- must be called at first */
  45. int dyn_event_register(struct dyn_event_operations *ops);
  46. /**
  47. * struct dyn_event - Dynamic event list header
  48. *
  49. * The dyn_event structure encapsulates a list and a pointer to the operators
  50. * for making a global list of dynamic events.
  51. * User must includes this in each event structure, so that those events can
  52. * be added/removed via dynamic_events interface.
  53. */
  54. struct dyn_event {
  55. struct list_head list;
  56. struct dyn_event_operations *ops;
  57. };
  58. extern struct list_head dyn_event_list;
  59. static inline
  60. int dyn_event_init(struct dyn_event *ev, struct dyn_event_operations *ops)
  61. {
  62. if (!ev || !ops)
  63. return -EINVAL;
  64. INIT_LIST_HEAD(&ev->list);
  65. ev->ops = ops;
  66. return 0;
  67. }
  68. static inline int dyn_event_add(struct dyn_event *ev,
  69. struct trace_event_call *call)
  70. {
  71. lockdep_assert_held(&event_mutex);
  72. if (!ev || !ev->ops)
  73. return -EINVAL;
  74. call->flags |= TRACE_EVENT_FL_DYNAMIC;
  75. list_add_tail(&ev->list, &dyn_event_list);
  76. return 0;
  77. }
  78. static inline void dyn_event_remove(struct dyn_event *ev)
  79. {
  80. lockdep_assert_held(&event_mutex);
  81. list_del_init(&ev->list);
  82. }
  83. void *dyn_event_seq_start(struct seq_file *m, loff_t *pos);
  84. void *dyn_event_seq_next(struct seq_file *m, void *v, loff_t *pos);
  85. void dyn_event_seq_stop(struct seq_file *m, void *v);
  86. int dyn_events_release_all(struct dyn_event_operations *type);
  87. int dyn_event_release(const char *raw_command, struct dyn_event_operations *type);
  88. /*
  89. * for_each_dyn_event - iterate over the dyn_event list
  90. * @pos: the struct dyn_event * to use as a loop cursor
  91. *
  92. * This is just a basement of for_each macro. Wrap this for
  93. * each actual event structure with ops filtering.
  94. */
  95. #define for_each_dyn_event(pos) \
  96. list_for_each_entry(pos, &dyn_event_list, list)
  97. /*
  98. * for_each_dyn_event - iterate over the dyn_event list safely
  99. * @pos: the struct dyn_event * to use as a loop cursor
  100. * @n: the struct dyn_event * to use as temporary storage
  101. */
  102. #define for_each_dyn_event_safe(pos, n) \
  103. list_for_each_entry_safe(pos, n, &dyn_event_list, list)
  104. extern void dynevent_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen,
  105. enum dynevent_type type,
  106. dynevent_create_fn_t run_command);
  107. typedef int (*dynevent_check_arg_fn_t)(void *data);
  108. struct dynevent_arg {
  109. const char *str;
  110. char separator; /* e.g. ';', ',', or nothing */
  111. };
  112. extern void dynevent_arg_init(struct dynevent_arg *arg,
  113. char separator);
  114. extern int dynevent_arg_add(struct dynevent_cmd *cmd,
  115. struct dynevent_arg *arg,
  116. dynevent_check_arg_fn_t check_arg);
  117. struct dynevent_arg_pair {
  118. const char *lhs;
  119. const char *rhs;
  120. char operator; /* e.g. '=' or nothing */
  121. char separator; /* e.g. ';', ',', or nothing */
  122. };
  123. extern void dynevent_arg_pair_init(struct dynevent_arg_pair *arg_pair,
  124. char operator, char separator);
  125. extern int dynevent_arg_pair_add(struct dynevent_cmd *cmd,
  126. struct dynevent_arg_pair *arg_pair,
  127. dynevent_check_arg_fn_t check_arg);
  128. extern int dynevent_str_add(struct dynevent_cmd *cmd, const char *str);
  129. #endif