trace_probe.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Common header file for probe-based Dynamic events.
  4. *
  5. * This code was copied from kernel/trace/trace_kprobe.h written by
  6. * Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
  7. *
  8. * Updates to make this generic:
  9. * Copyright (C) IBM Corporation, 2010-2011
  10. * Author: Srikar Dronamraju
  11. */
  12. #include <linux/seq_file.h>
  13. #include <linux/slab.h>
  14. #include <linux/smp.h>
  15. #include <linux/tracefs.h>
  16. #include <linux/types.h>
  17. #include <linux/string.h>
  18. #include <linux/ctype.h>
  19. #include <linux/ptrace.h>
  20. #include <linux/perf_event.h>
  21. #include <linux/kprobes.h>
  22. #include <linux/stringify.h>
  23. #include <linux/limits.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/bitops.h>
  26. #include <asm/bitsperlong.h>
  27. #include "trace.h"
  28. #include "trace_output.h"
  29. #define MAX_TRACE_ARGS 128
  30. #define MAX_ARGSTR_LEN 63
  31. #define MAX_ARRAY_LEN 64
  32. #define MAX_STRING_SIZE PATH_MAX
  33. /* Reserved field names */
  34. #define FIELD_STRING_IP "__probe_ip"
  35. #define FIELD_STRING_RETIP "__probe_ret_ip"
  36. #define FIELD_STRING_FUNC "__probe_func"
  37. #undef DEFINE_FIELD
  38. #define DEFINE_FIELD(type, item, name, is_signed) \
  39. do { \
  40. ret = trace_define_field(event_call, #type, name, \
  41. offsetof(typeof(field), item), \
  42. sizeof(field.item), is_signed, \
  43. FILTER_OTHER); \
  44. if (ret) \
  45. return ret; \
  46. } while (0)
  47. /* Flags for trace_probe */
  48. #define TP_FLAG_TRACE 1
  49. #define TP_FLAG_PROFILE 2
  50. #define TP_FLAG_REGISTERED 4
  51. /* data_loc: data location, compatible with u32 */
  52. #define make_data_loc(len, offs) \
  53. (((u32)(len) << 16) | ((u32)(offs) & 0xffff))
  54. #define get_loc_len(dl) ((u32)(dl) >> 16)
  55. #define get_loc_offs(dl) ((u32)(dl) & 0xffff)
  56. static nokprobe_inline void *get_loc_data(u32 *dl, void *ent)
  57. {
  58. return (u8 *)ent + get_loc_offs(*dl);
  59. }
  60. static nokprobe_inline u32 update_data_loc(u32 loc, int consumed)
  61. {
  62. u32 maxlen = get_loc_len(loc);
  63. u32 offset = get_loc_offs(loc);
  64. return make_data_loc(maxlen - consumed, offset + consumed);
  65. }
  66. /* Printing function type */
  67. typedef int (*print_type_func_t)(struct trace_seq *, void *, void *);
  68. enum fetch_op {
  69. FETCH_OP_NOP = 0,
  70. // Stage 1 (load) ops
  71. FETCH_OP_REG, /* Register : .param = offset */
  72. FETCH_OP_STACK, /* Stack : .param = index */
  73. FETCH_OP_STACKP, /* Stack pointer */
  74. FETCH_OP_RETVAL, /* Return value */
  75. FETCH_OP_IMM, /* Immediate : .immediate */
  76. FETCH_OP_COMM, /* Current comm */
  77. FETCH_OP_ARG, /* Function argument : .param */
  78. FETCH_OP_FOFFS, /* File offset: .immediate */
  79. // Stage 2 (dereference) op
  80. FETCH_OP_DEREF, /* Dereference: .offset */
  81. // Stage 3 (store) ops
  82. FETCH_OP_ST_RAW, /* Raw: .size */
  83. FETCH_OP_ST_MEM, /* Mem: .offset, .size */
  84. FETCH_OP_ST_STRING, /* String: .offset, .size */
  85. // Stage 4 (modify) op
  86. FETCH_OP_MOD_BF, /* Bitfield: .basesize, .lshift, .rshift */
  87. // Stage 5 (loop) op
  88. FETCH_OP_LP_ARRAY, /* Array: .param = loop count */
  89. FETCH_OP_END,
  90. FETCH_NOP_SYMBOL, /* Unresolved Symbol holder */
  91. };
  92. struct fetch_insn {
  93. enum fetch_op op;
  94. union {
  95. unsigned int param;
  96. struct {
  97. unsigned int size;
  98. int offset;
  99. };
  100. struct {
  101. unsigned char basesize;
  102. unsigned char lshift;
  103. unsigned char rshift;
  104. };
  105. unsigned long immediate;
  106. void *data;
  107. };
  108. };
  109. /* fetch + deref*N + store + mod + end <= 16, this allows N=12, enough */
  110. #define FETCH_INSN_MAX 16
  111. /* Fetch type information table */
  112. struct fetch_type {
  113. const char *name; /* Name of type */
  114. size_t size; /* Byte size of type */
  115. int is_signed; /* Signed flag */
  116. print_type_func_t print; /* Print functions */
  117. const char *fmt; /* Fromat string */
  118. const char *fmttype; /* Name in format file */
  119. };
  120. /* For defining macros, define string/string_size types */
  121. typedef u32 string;
  122. typedef u32 string_size;
  123. #define PRINT_TYPE_FUNC_NAME(type) print_type_##type
  124. #define PRINT_TYPE_FMT_NAME(type) print_type_format_##type
  125. /* Printing in basic type function template */
  126. #define DECLARE_BASIC_PRINT_TYPE_FUNC(type) \
  127. int PRINT_TYPE_FUNC_NAME(type)(struct trace_seq *s, void *data, void *ent);\
  128. extern const char PRINT_TYPE_FMT_NAME(type)[]
  129. DECLARE_BASIC_PRINT_TYPE_FUNC(u8);
  130. DECLARE_BASIC_PRINT_TYPE_FUNC(u16);
  131. DECLARE_BASIC_PRINT_TYPE_FUNC(u32);
  132. DECLARE_BASIC_PRINT_TYPE_FUNC(u64);
  133. DECLARE_BASIC_PRINT_TYPE_FUNC(s8);
  134. DECLARE_BASIC_PRINT_TYPE_FUNC(s16);
  135. DECLARE_BASIC_PRINT_TYPE_FUNC(s32);
  136. DECLARE_BASIC_PRINT_TYPE_FUNC(s64);
  137. DECLARE_BASIC_PRINT_TYPE_FUNC(x8);
  138. DECLARE_BASIC_PRINT_TYPE_FUNC(x16);
  139. DECLARE_BASIC_PRINT_TYPE_FUNC(x32);
  140. DECLARE_BASIC_PRINT_TYPE_FUNC(x64);
  141. DECLARE_BASIC_PRINT_TYPE_FUNC(string);
  142. DECLARE_BASIC_PRINT_TYPE_FUNC(symbol);
  143. /* Default (unsigned long) fetch type */
  144. #define __DEFAULT_FETCH_TYPE(t) x##t
  145. #define _DEFAULT_FETCH_TYPE(t) __DEFAULT_FETCH_TYPE(t)
  146. #define DEFAULT_FETCH_TYPE _DEFAULT_FETCH_TYPE(BITS_PER_LONG)
  147. #define DEFAULT_FETCH_TYPE_STR __stringify(DEFAULT_FETCH_TYPE)
  148. #define __ADDR_FETCH_TYPE(t) u##t
  149. #define _ADDR_FETCH_TYPE(t) __ADDR_FETCH_TYPE(t)
  150. #define ADDR_FETCH_TYPE _ADDR_FETCH_TYPE(BITS_PER_LONG)
  151. #define __ASSIGN_FETCH_TYPE(_name, ptype, ftype, _size, sign, _fmttype) \
  152. {.name = _name, \
  153. .size = _size, \
  154. .is_signed = sign, \
  155. .print = PRINT_TYPE_FUNC_NAME(ptype), \
  156. .fmt = PRINT_TYPE_FMT_NAME(ptype), \
  157. .fmttype = _fmttype, \
  158. }
  159. #define _ASSIGN_FETCH_TYPE(_name, ptype, ftype, _size, sign, _fmttype) \
  160. __ASSIGN_FETCH_TYPE(_name, ptype, ftype, _size, sign, #_fmttype)
  161. #define ASSIGN_FETCH_TYPE(ptype, ftype, sign) \
  162. _ASSIGN_FETCH_TYPE(#ptype, ptype, ftype, sizeof(ftype), sign, ptype)
  163. /* If ptype is an alias of atype, use this macro (show atype in format) */
  164. #define ASSIGN_FETCH_TYPE_ALIAS(ptype, atype, ftype, sign) \
  165. _ASSIGN_FETCH_TYPE(#ptype, ptype, ftype, sizeof(ftype), sign, atype)
  166. #define ASSIGN_FETCH_TYPE_END {}
  167. #define MAX_ARRAY_LEN 64
  168. #ifdef CONFIG_KPROBE_EVENTS
  169. bool trace_kprobe_on_func_entry(struct trace_event_call *call);
  170. bool trace_kprobe_error_injectable(struct trace_event_call *call);
  171. #else
  172. static inline bool trace_kprobe_on_func_entry(struct trace_event_call *call)
  173. {
  174. return false;
  175. }
  176. static inline bool trace_kprobe_error_injectable(struct trace_event_call *call)
  177. {
  178. return false;
  179. }
  180. #endif /* CONFIG_KPROBE_EVENTS */
  181. struct probe_arg {
  182. struct fetch_insn *code;
  183. bool dynamic;/* Dynamic array (string) is used */
  184. unsigned int offset; /* Offset from argument entry */
  185. unsigned int count; /* Array count */
  186. const char *name; /* Name of this argument */
  187. const char *comm; /* Command of this argument */
  188. char *fmt; /* Format string if needed */
  189. const struct fetch_type *type; /* Type of this argument */
  190. };
  191. struct trace_probe {
  192. unsigned int flags; /* For TP_FLAG_* */
  193. struct trace_event_class class;
  194. struct trace_event_call call;
  195. struct list_head files;
  196. ssize_t size; /* trace entry size */
  197. unsigned int nr_args;
  198. struct probe_arg args[];
  199. };
  200. struct event_file_link {
  201. struct trace_event_file *file;
  202. struct list_head list;
  203. };
  204. static inline bool trace_probe_is_enabled(struct trace_probe *tp)
  205. {
  206. return !!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE));
  207. }
  208. static inline bool trace_probe_is_registered(struct trace_probe *tp)
  209. {
  210. return !!(tp->flags & TP_FLAG_REGISTERED);
  211. }
  212. /* Check the name is good for event/group/fields */
  213. static inline bool is_good_name(const char *name)
  214. {
  215. if (!isalpha(*name) && *name != '_')
  216. return false;
  217. while (*++name != '\0') {
  218. if (!isalpha(*name) && !isdigit(*name) && *name != '_')
  219. return false;
  220. }
  221. return true;
  222. }
  223. static inline struct event_file_link *
  224. find_event_file_link(struct trace_probe *tp, struct trace_event_file *file)
  225. {
  226. struct event_file_link *link;
  227. list_for_each_entry(link, &tp->files, list)
  228. if (link->file == file)
  229. return link;
  230. return NULL;
  231. }
  232. #define TPARG_FL_RETURN BIT(0)
  233. #define TPARG_FL_KERNEL BIT(1)
  234. #define TPARG_FL_FENTRY BIT(2)
  235. #define TPARG_FL_MASK GENMASK(2, 0)
  236. extern int traceprobe_parse_probe_arg(char *arg, ssize_t *size,
  237. struct probe_arg *parg, unsigned int flags);
  238. extern int traceprobe_conflict_field_name(const char *name,
  239. struct probe_arg *args, int narg);
  240. extern int traceprobe_update_arg(struct probe_arg *arg);
  241. extern void traceprobe_free_probe_arg(struct probe_arg *arg);
  242. extern int traceprobe_split_symbol_offset(char *symbol, long *offset);
  243. extern int traceprobe_set_print_fmt(struct trace_probe *tp, bool is_return);
  244. #ifdef CONFIG_PERF_EVENTS
  245. extern struct trace_event_call *
  246. create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
  247. bool is_return);
  248. extern void destroy_local_trace_kprobe(struct trace_event_call *event_call);
  249. extern struct trace_event_call *
  250. create_local_trace_uprobe(char *name, unsigned long offs,
  251. unsigned long ref_ctr_offset, bool is_return);
  252. extern void destroy_local_trace_uprobe(struct trace_event_call *event_call);
  253. #endif
  254. extern int traceprobe_define_arg_fields(struct trace_event_call *event_call,
  255. size_t offset, struct trace_probe *tp);