trace_probe.h 16 KB

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