trace_probe.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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/ptrace.h>
  19. #include <linux/perf_event.h>
  20. #include <linux/kprobes.h>
  21. #include <linux/stringify.h>
  22. #include <linux/limits.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/bitops.h>
  25. #include <asm/bitsperlong.h>
  26. #include "trace.h"
  27. #include "trace_output.h"
  28. #define MAX_TRACE_ARGS 128
  29. #define MAX_ARGSTR_LEN 63
  30. #define MAX_ARRAY_LEN 64
  31. #define MAX_ARG_NAME_LEN 32
  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. /* data_loc: data location, compatible with u32 */
  51. #define make_data_loc(len, offs) \
  52. (((u32)(len) << 16) | ((u32)(offs) & 0xffff))
  53. #define get_loc_len(dl) ((u32)(dl) >> 16)
  54. #define get_loc_offs(dl) ((u32)(dl) & 0xffff)
  55. static nokprobe_inline void *get_loc_data(u32 *dl, void *ent)
  56. {
  57. return (u8 *)ent + get_loc_offs(*dl);
  58. }
  59. static nokprobe_inline u32 update_data_loc(u32 loc, int consumed)
  60. {
  61. u32 maxlen = get_loc_len(loc);
  62. u32 offset = get_loc_offs(loc);
  63. return make_data_loc(maxlen - consumed, offset + consumed);
  64. }
  65. /* Printing function type */
  66. typedef int (*print_type_func_t)(struct trace_seq *, void *, void *);
  67. enum fetch_op {
  68. FETCH_OP_NOP = 0,
  69. // Stage 1 (load) ops
  70. FETCH_OP_REG, /* Register : .param = offset */
  71. FETCH_OP_STACK, /* Stack : .param = index */
  72. FETCH_OP_STACKP, /* Stack pointer */
  73. FETCH_OP_RETVAL, /* Return value */
  74. FETCH_OP_IMM, /* Immediate : .immediate */
  75. FETCH_OP_COMM, /* Current comm */
  76. FETCH_OP_ARG, /* Function argument : .param */
  77. FETCH_OP_FOFFS, /* File offset: .immediate */
  78. FETCH_OP_DATA, /* Allocated data: .data */
  79. // Stage 2 (dereference) op
  80. FETCH_OP_DEREF, /* Dereference: .offset */
  81. FETCH_OP_UDEREF, /* User-space Dereference: .offset */
  82. // Stage 3 (store) ops
  83. FETCH_OP_ST_RAW, /* Raw: .size */
  84. FETCH_OP_ST_MEM, /* Mem: .offset, .size */
  85. FETCH_OP_ST_UMEM, /* Mem: .offset, .size */
  86. FETCH_OP_ST_STRING, /* String: .offset, .size */
  87. FETCH_OP_ST_USTRING, /* User String: .offset, .size */
  88. // Stage 4 (modify) op
  89. FETCH_OP_MOD_BF, /* Bitfield: .basesize, .lshift, .rshift */
  90. // Stage 5 (loop) op
  91. FETCH_OP_LP_ARRAY, /* Array: .param = loop count */
  92. FETCH_OP_END,
  93. FETCH_NOP_SYMBOL, /* Unresolved Symbol holder */
  94. };
  95. struct fetch_insn {
  96. enum fetch_op op;
  97. union {
  98. unsigned int param;
  99. struct {
  100. unsigned int size;
  101. int offset;
  102. };
  103. struct {
  104. unsigned char basesize;
  105. unsigned char lshift;
  106. unsigned char rshift;
  107. };
  108. unsigned long immediate;
  109. void *data;
  110. };
  111. };
  112. /* fetch + deref*N + store + mod + end <= 16, this allows N=12, enough */
  113. #define FETCH_INSN_MAX 16
  114. #define FETCH_TOKEN_COMM (-ECOMM)
  115. /* Fetch type information table */
  116. struct fetch_type {
  117. const char *name; /* Name of type */
  118. size_t size; /* Byte size of type */
  119. int is_signed; /* Signed flag */
  120. print_type_func_t print; /* Print functions */
  121. const char *fmt; /* Fromat string */
  122. const char *fmttype; /* Name in format file */
  123. };
  124. /* For defining macros, define string/string_size types */
  125. typedef u32 string;
  126. typedef u32 string_size;
  127. #define PRINT_TYPE_FUNC_NAME(type) print_type_##type
  128. #define PRINT_TYPE_FMT_NAME(type) print_type_format_##type
  129. /* Printing in basic type function template */
  130. #define DECLARE_BASIC_PRINT_TYPE_FUNC(type) \
  131. int PRINT_TYPE_FUNC_NAME(type)(struct trace_seq *s, void *data, void *ent);\
  132. extern const char PRINT_TYPE_FMT_NAME(type)[]
  133. DECLARE_BASIC_PRINT_TYPE_FUNC(u8);
  134. DECLARE_BASIC_PRINT_TYPE_FUNC(u16);
  135. DECLARE_BASIC_PRINT_TYPE_FUNC(u32);
  136. DECLARE_BASIC_PRINT_TYPE_FUNC(u64);
  137. DECLARE_BASIC_PRINT_TYPE_FUNC(s8);
  138. DECLARE_BASIC_PRINT_TYPE_FUNC(s16);
  139. DECLARE_BASIC_PRINT_TYPE_FUNC(s32);
  140. DECLARE_BASIC_PRINT_TYPE_FUNC(s64);
  141. DECLARE_BASIC_PRINT_TYPE_FUNC(x8);
  142. DECLARE_BASIC_PRINT_TYPE_FUNC(x16);
  143. DECLARE_BASIC_PRINT_TYPE_FUNC(x32);
  144. DECLARE_BASIC_PRINT_TYPE_FUNC(x64);
  145. DECLARE_BASIC_PRINT_TYPE_FUNC(string);
  146. DECLARE_BASIC_PRINT_TYPE_FUNC(symbol);
  147. /* Default (unsigned long) fetch type */
  148. #define __DEFAULT_FETCH_TYPE(t) x##t
  149. #define _DEFAULT_FETCH_TYPE(t) __DEFAULT_FETCH_TYPE(t)
  150. #define DEFAULT_FETCH_TYPE _DEFAULT_FETCH_TYPE(BITS_PER_LONG)
  151. #define DEFAULT_FETCH_TYPE_STR __stringify(DEFAULT_FETCH_TYPE)
  152. #define __ADDR_FETCH_TYPE(t) u##t
  153. #define _ADDR_FETCH_TYPE(t) __ADDR_FETCH_TYPE(t)
  154. #define ADDR_FETCH_TYPE _ADDR_FETCH_TYPE(BITS_PER_LONG)
  155. #define __ASSIGN_FETCH_TYPE(_name, ptype, ftype, _size, sign, _fmttype) \
  156. {.name = _name, \
  157. .size = _size, \
  158. .is_signed = sign, \
  159. .print = PRINT_TYPE_FUNC_NAME(ptype), \
  160. .fmt = PRINT_TYPE_FMT_NAME(ptype), \
  161. .fmttype = _fmttype, \
  162. }
  163. #define _ASSIGN_FETCH_TYPE(_name, ptype, ftype, _size, sign, _fmttype) \
  164. __ASSIGN_FETCH_TYPE(_name, ptype, ftype, _size, sign, #_fmttype)
  165. #define ASSIGN_FETCH_TYPE(ptype, ftype, sign) \
  166. _ASSIGN_FETCH_TYPE(#ptype, ptype, ftype, sizeof(ftype), sign, ptype)
  167. /* If ptype is an alias of atype, use this macro (show atype in format) */
  168. #define ASSIGN_FETCH_TYPE_ALIAS(ptype, atype, ftype, sign) \
  169. _ASSIGN_FETCH_TYPE(#ptype, ptype, ftype, sizeof(ftype), sign, atype)
  170. #define ASSIGN_FETCH_TYPE_END {}
  171. #define MAX_ARRAY_LEN 64
  172. #ifdef CONFIG_KPROBE_EVENTS
  173. bool trace_kprobe_on_func_entry(struct trace_event_call *call);
  174. bool trace_kprobe_error_injectable(struct trace_event_call *call);
  175. #else
  176. static inline bool trace_kprobe_on_func_entry(struct trace_event_call *call)
  177. {
  178. return false;
  179. }
  180. static inline bool trace_kprobe_error_injectable(struct trace_event_call *call)
  181. {
  182. return false;
  183. }
  184. #endif /* CONFIG_KPROBE_EVENTS */
  185. struct probe_arg {
  186. struct fetch_insn *code;
  187. bool dynamic;/* Dynamic array (string) is used */
  188. unsigned int offset; /* Offset from argument entry */
  189. unsigned int count; /* Array count */
  190. const char *name; /* Name of this argument */
  191. const char *comm; /* Command of this argument */
  192. char *fmt; /* Format string if needed */
  193. const struct fetch_type *type; /* Type of this argument */
  194. };
  195. struct trace_uprobe_filter {
  196. rwlock_t rwlock;
  197. int nr_systemwide;
  198. struct list_head perf_events;
  199. };
  200. /* Event call and class holder */
  201. struct trace_probe_event {
  202. unsigned int flags; /* For TP_FLAG_* */
  203. struct trace_event_class class;
  204. struct trace_event_call call;
  205. struct list_head files;
  206. struct list_head probes;
  207. struct trace_uprobe_filter filter[];
  208. };
  209. struct trace_probe {
  210. struct list_head list;
  211. struct trace_probe_event *event;
  212. ssize_t size; /* trace entry size */
  213. unsigned int nr_args;
  214. struct probe_arg args[];
  215. };
  216. struct event_file_link {
  217. struct trace_event_file *file;
  218. struct list_head list;
  219. };
  220. static inline bool trace_probe_test_flag(struct trace_probe *tp,
  221. unsigned int flag)
  222. {
  223. return !!(tp->event->flags & flag);
  224. }
  225. static inline void trace_probe_set_flag(struct trace_probe *tp,
  226. unsigned int flag)
  227. {
  228. tp->event->flags |= flag;
  229. }
  230. static inline void trace_probe_clear_flag(struct trace_probe *tp,
  231. unsigned int flag)
  232. {
  233. tp->event->flags &= ~flag;
  234. }
  235. static inline bool trace_probe_is_enabled(struct trace_probe *tp)
  236. {
  237. return trace_probe_test_flag(tp, TP_FLAG_TRACE | TP_FLAG_PROFILE);
  238. }
  239. static inline const char *trace_probe_name(struct trace_probe *tp)
  240. {
  241. return trace_event_name(&tp->event->call);
  242. }
  243. static inline const char *trace_probe_group_name(struct trace_probe *tp)
  244. {
  245. return tp->event->call.class->system;
  246. }
  247. static inline struct trace_event_call *
  248. trace_probe_event_call(struct trace_probe *tp)
  249. {
  250. return &tp->event->call;
  251. }
  252. static inline struct trace_probe_event *
  253. trace_probe_event_from_call(struct trace_event_call *event_call)
  254. {
  255. return container_of(event_call, struct trace_probe_event, call);
  256. }
  257. static inline struct trace_probe *
  258. trace_probe_primary_from_call(struct trace_event_call *call)
  259. {
  260. struct trace_probe_event *tpe = trace_probe_event_from_call(call);
  261. return list_first_entry(&tpe->probes, struct trace_probe, list);
  262. }
  263. static inline struct list_head *trace_probe_probe_list(struct trace_probe *tp)
  264. {
  265. return &tp->event->probes;
  266. }
  267. static inline bool trace_probe_has_sibling(struct trace_probe *tp)
  268. {
  269. struct list_head *list = trace_probe_probe_list(tp);
  270. return !list_empty(list) && !list_is_singular(list);
  271. }
  272. static inline int trace_probe_unregister_event_call(struct trace_probe *tp)
  273. {
  274. /* tp->event is unregistered in trace_remove_event_call() */
  275. return trace_remove_event_call(&tp->event->call);
  276. }
  277. static inline bool trace_probe_has_single_file(struct trace_probe *tp)
  278. {
  279. return !!list_is_singular(&tp->event->files);
  280. }
  281. int trace_probe_init(struct trace_probe *tp, const char *event,
  282. const char *group, bool alloc_filter);
  283. void trace_probe_cleanup(struct trace_probe *tp);
  284. int trace_probe_append(struct trace_probe *tp, struct trace_probe *to);
  285. void trace_probe_unlink(struct trace_probe *tp);
  286. int trace_probe_register_event_call(struct trace_probe *tp);
  287. int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file);
  288. int trace_probe_remove_file(struct trace_probe *tp,
  289. struct trace_event_file *file);
  290. struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
  291. struct trace_event_file *file);
  292. int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b);
  293. bool trace_probe_match_command_args(struct trace_probe *tp,
  294. int argc, const char **argv);
  295. #define trace_probe_for_each_link(pos, tp) \
  296. list_for_each_entry(pos, &(tp)->event->files, list)
  297. #define trace_probe_for_each_link_rcu(pos, tp) \
  298. list_for_each_entry_rcu(pos, &(tp)->event->files, list)
  299. #define TPARG_FL_RETURN BIT(0)
  300. #define TPARG_FL_KERNEL BIT(1)
  301. #define TPARG_FL_FENTRY BIT(2)
  302. #define TPARG_FL_MASK GENMASK(2, 0)
  303. extern int traceprobe_parse_probe_arg(struct trace_probe *tp, int i,
  304. char *arg, unsigned int flags);
  305. extern int traceprobe_update_arg(struct probe_arg *arg);
  306. extern void traceprobe_free_probe_arg(struct probe_arg *arg);
  307. extern int traceprobe_split_symbol_offset(char *symbol, long *offset);
  308. int traceprobe_parse_event_name(const char **pevent, const char **pgroup,
  309. char *buf, int offset);
  310. extern int traceprobe_set_print_fmt(struct trace_probe *tp, bool is_return);
  311. #ifdef CONFIG_PERF_EVENTS
  312. extern struct trace_event_call *
  313. create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
  314. bool is_return);
  315. extern void destroy_local_trace_kprobe(struct trace_event_call *event_call);
  316. extern struct trace_event_call *
  317. create_local_trace_uprobe(char *name, unsigned long offs,
  318. unsigned long ref_ctr_offset, bool is_return);
  319. extern void destroy_local_trace_uprobe(struct trace_event_call *event_call);
  320. #endif
  321. extern int traceprobe_define_arg_fields(struct trace_event_call *event_call,
  322. size_t offset, struct trace_probe *tp);
  323. #undef ERRORS
  324. #define ERRORS \
  325. C(FILE_NOT_FOUND, "Failed to find the given file"), \
  326. C(NO_REGULAR_FILE, "Not a regular file"), \
  327. C(BAD_REFCNT, "Invalid reference counter offset"), \
  328. C(REFCNT_OPEN_BRACE, "Reference counter brace is not closed"), \
  329. C(BAD_REFCNT_SUFFIX, "Reference counter has wrong suffix"), \
  330. C(BAD_UPROBE_OFFS, "Invalid uprobe offset"), \
  331. C(MAXACT_NO_KPROBE, "Maxactive is not for kprobe"), \
  332. C(BAD_MAXACT, "Invalid maxactive number"), \
  333. C(MAXACT_TOO_BIG, "Maxactive is too big"), \
  334. C(BAD_PROBE_ADDR, "Invalid probed address or symbol"), \
  335. C(BAD_RETPROBE, "Retprobe address must be an function entry"), \
  336. C(BAD_ADDR_SUFFIX, "Invalid probed address suffix"), \
  337. C(NO_GROUP_NAME, "Group name is not specified"), \
  338. C(GROUP_TOO_LONG, "Group name is too long"), \
  339. C(BAD_GROUP_NAME, "Group name must follow the same rules as C identifiers"), \
  340. C(NO_EVENT_NAME, "Event name is not specified"), \
  341. C(EVENT_TOO_LONG, "Event name is too long"), \
  342. C(BAD_EVENT_NAME, "Event name must follow the same rules as C identifiers"), \
  343. C(EVENT_EXIST, "Given group/event name is already used by another event"), \
  344. C(RETVAL_ON_PROBE, "$retval is not available on probe"), \
  345. C(BAD_STACK_NUM, "Invalid stack number"), \
  346. C(BAD_ARG_NUM, "Invalid argument number"), \
  347. C(BAD_VAR, "Invalid $-valiable specified"), \
  348. C(BAD_REG_NAME, "Invalid register name"), \
  349. C(BAD_MEM_ADDR, "Invalid memory address"), \
  350. C(BAD_IMM, "Invalid immediate value"), \
  351. C(IMMSTR_NO_CLOSE, "String is not closed with '\"'"), \
  352. C(FILE_ON_KPROBE, "File offset is not available with kprobe"), \
  353. C(BAD_FILE_OFFS, "Invalid file offset value"), \
  354. C(SYM_ON_UPROBE, "Symbol is not available with uprobe"), \
  355. C(TOO_MANY_OPS, "Dereference is too much nested"), \
  356. C(DEREF_NEED_BRACE, "Dereference needs a brace"), \
  357. C(BAD_DEREF_OFFS, "Invalid dereference offset"), \
  358. C(DEREF_OPEN_BRACE, "Dereference brace is not closed"), \
  359. C(COMM_CANT_DEREF, "$comm can not be dereferenced"), \
  360. C(BAD_FETCH_ARG, "Invalid fetch argument"), \
  361. C(ARRAY_NO_CLOSE, "Array is not closed"), \
  362. C(BAD_ARRAY_SUFFIX, "Array has wrong suffix"), \
  363. C(BAD_ARRAY_NUM, "Invalid array size"), \
  364. C(ARRAY_TOO_BIG, "Array number is too big"), \
  365. C(BAD_TYPE, "Unknown type is specified"), \
  366. C(BAD_STRING, "String accepts only memory argument"), \
  367. C(BAD_BITFIELD, "Invalid bitfield"), \
  368. C(ARG_NAME_TOO_LONG, "Argument name is too long"), \
  369. C(NO_ARG_NAME, "Argument name is not specified"), \
  370. C(BAD_ARG_NAME, "Argument name must follow the same rules as C identifiers"), \
  371. C(USED_ARG_NAME, "This argument name is already used"), \
  372. C(ARG_TOO_LONG, "Argument expression is too long"), \
  373. C(NO_ARG_BODY, "No argument expression"), \
  374. C(BAD_INSN_BNDRY, "Probe point is not an instruction boundary"),\
  375. C(FAIL_REG_PROBE, "Failed to register probe event"),\
  376. C(DIFF_PROBE_TYPE, "Probe type is different from existing probe"),\
  377. C(DIFF_ARG_TYPE, "Argument type or name is different from existing probe"),\
  378. C(SAME_PROBE, "There is already the exact same probe event"),
  379. #undef C
  380. #define C(a, b) TP_ERR_##a
  381. /* Define TP_ERR_ */
  382. enum { ERRORS };
  383. /* Error text is defined in trace_probe.c */
  384. struct trace_probe_log {
  385. const char *subsystem;
  386. const char **argv;
  387. int argc;
  388. int index;
  389. };
  390. void trace_probe_log_init(const char *subsystem, int argc, const char **argv);
  391. void trace_probe_log_set_index(int index);
  392. void trace_probe_log_clear(void);
  393. void __trace_probe_log_err(int offset, int err);
  394. #define trace_probe_log_err(offs, err) \
  395. __trace_probe_log_err(offs, TP_ERR_##err)