trace_dynevent.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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, otherwides false.
  28. * @free: Delete the given event. Return 0 if success, otherwides 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, otherwides
  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)(int argc, const char *argv[]);
  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. {
  70. lockdep_assert_held(&event_mutex);
  71. if (!ev || !ev->ops)
  72. return -EINVAL;
  73. list_add_tail(&ev->list, &dyn_event_list);
  74. return 0;
  75. }
  76. static inline void dyn_event_remove(struct dyn_event *ev)
  77. {
  78. lockdep_assert_held(&event_mutex);
  79. list_del_init(&ev->list);
  80. }
  81. void *dyn_event_seq_start(struct seq_file *m, loff_t *pos);
  82. void *dyn_event_seq_next(struct seq_file *m, void *v, loff_t *pos);
  83. void dyn_event_seq_stop(struct seq_file *m, void *v);
  84. int dyn_events_release_all(struct dyn_event_operations *type);
  85. int dyn_event_release(int argc, char **argv, struct dyn_event_operations *type);
  86. /*
  87. * for_each_dyn_event - iterate over the dyn_event list
  88. * @pos: the struct dyn_event * to use as a loop cursor
  89. *
  90. * This is just a basement of for_each macro. Wrap this for
  91. * each actual event structure with ops filtering.
  92. */
  93. #define for_each_dyn_event(pos) \
  94. list_for_each_entry(pos, &dyn_event_list, list)
  95. /*
  96. * for_each_dyn_event - iterate over the dyn_event list safely
  97. * @pos: the struct dyn_event * to use as a loop cursor
  98. * @n: the struct dyn_event * to use as temporary storage
  99. */
  100. #define for_each_dyn_event_safe(pos, n) \
  101. list_for_each_entry_safe(pos, n, &dyn_event_list, list)
  102. extern void dynevent_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen,
  103. enum dynevent_type type,
  104. dynevent_create_fn_t run_command);
  105. typedef int (*dynevent_check_arg_fn_t)(void *data);
  106. struct dynevent_arg {
  107. const char *str;
  108. char separator; /* e.g. ';', ',', or nothing */
  109. };
  110. extern void dynevent_arg_init(struct dynevent_arg *arg,
  111. char separator);
  112. extern int dynevent_arg_add(struct dynevent_cmd *cmd,
  113. struct dynevent_arg *arg,
  114. dynevent_check_arg_fn_t check_arg);
  115. struct dynevent_arg_pair {
  116. const char *lhs;
  117. const char *rhs;
  118. char operator; /* e.g. '=' or nothing */
  119. char separator; /* e.g. ';', ',', or nothing */
  120. };
  121. extern void dynevent_arg_pair_init(struct dynevent_arg_pair *arg_pair,
  122. char operator, char separator);
  123. extern int dynevent_arg_pair_add(struct dynevent_cmd *cmd,
  124. struct dynevent_arg_pair *arg_pair,
  125. dynevent_check_arg_fn_t check_arg);
  126. extern int dynevent_str_add(struct dynevent_cmd *cmd, const char *str);
  127. #endif