trace_probe.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 <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_STRING_SIZE PATH_MAX
  31. /* Reserved field names */
  32. #define FIELD_STRING_IP "__probe_ip"
  33. #define FIELD_STRING_RETIP "__probe_ret_ip"
  34. #define FIELD_STRING_FUNC "__probe_func"
  35. #undef DEFINE_FIELD
  36. #define DEFINE_FIELD(type, item, name, is_signed) \
  37. do { \
  38. ret = trace_define_field(event_call, #type, name, \
  39. offsetof(typeof(field), item), \
  40. sizeof(field.item), is_signed, \
  41. FILTER_OTHER); \
  42. if (ret) \
  43. return ret; \
  44. } while (0)
  45. /* Flags for trace_probe */
  46. #define TP_FLAG_TRACE 1
  47. #define TP_FLAG_PROFILE 2
  48. #define TP_FLAG_REGISTERED 4
  49. /* data_rloc: data relative location, compatible with u32 */
  50. #define make_data_rloc(len, roffs) \
  51. (((u32)(len) << 16) | ((u32)(roffs) & 0xffff))
  52. #define get_rloc_len(dl) ((u32)(dl) >> 16)
  53. #define get_rloc_offs(dl) ((u32)(dl) & 0xffff)
  54. /*
  55. * Convert data_rloc to data_loc:
  56. * data_rloc stores the offset from data_rloc itself, but data_loc
  57. * stores the offset from event entry.
  58. */
  59. #define convert_rloc_to_loc(dl, offs) ((u32)(dl) + (offs))
  60. static nokprobe_inline void *get_rloc_data(u32 *dl)
  61. {
  62. return (u8 *)dl + get_rloc_offs(*dl);
  63. }
  64. /* For data_loc conversion */
  65. static nokprobe_inline void *get_loc_data(u32 *dl, void *ent)
  66. {
  67. return (u8 *)ent + get_rloc_offs(*dl);
  68. }
  69. /* Data fetch function type */
  70. typedef void (*fetch_func_t)(struct pt_regs *, void *, void *);
  71. /* Printing function type */
  72. typedef int (*print_type_func_t)(struct trace_seq *, const char *, void *, void *);
  73. /* Fetch types */
  74. enum {
  75. FETCH_MTD_reg = 0,
  76. FETCH_MTD_stack,
  77. FETCH_MTD_retval,
  78. FETCH_MTD_comm,
  79. FETCH_MTD_memory,
  80. FETCH_MTD_symbol,
  81. FETCH_MTD_deref,
  82. FETCH_MTD_bitfield,
  83. FETCH_MTD_file_offset,
  84. FETCH_MTD_END,
  85. };
  86. /* Fetch type information table */
  87. struct fetch_type {
  88. const char *name; /* Name of type */
  89. size_t size; /* Byte size of type */
  90. int is_signed; /* Signed flag */
  91. print_type_func_t print; /* Print functions */
  92. const char *fmt; /* Fromat string */
  93. const char *fmttype; /* Name in format file */
  94. /* Fetch functions */
  95. fetch_func_t fetch[FETCH_MTD_END];
  96. };
  97. struct fetch_param {
  98. fetch_func_t fn;
  99. void *data;
  100. };
  101. /* For defining macros, define string/string_size types */
  102. typedef u32 string;
  103. typedef u32 string_size;
  104. #define PRINT_TYPE_FUNC_NAME(type) print_type_##type
  105. #define PRINT_TYPE_FMT_NAME(type) print_type_format_##type
  106. /* Printing in basic type function template */
  107. #define DECLARE_BASIC_PRINT_TYPE_FUNC(type) \
  108. int PRINT_TYPE_FUNC_NAME(type)(struct trace_seq *s, const char *name, \
  109. void *data, void *ent); \
  110. extern const char PRINT_TYPE_FMT_NAME(type)[]
  111. DECLARE_BASIC_PRINT_TYPE_FUNC(u8);
  112. DECLARE_BASIC_PRINT_TYPE_FUNC(u16);
  113. DECLARE_BASIC_PRINT_TYPE_FUNC(u32);
  114. DECLARE_BASIC_PRINT_TYPE_FUNC(u64);
  115. DECLARE_BASIC_PRINT_TYPE_FUNC(s8);
  116. DECLARE_BASIC_PRINT_TYPE_FUNC(s16);
  117. DECLARE_BASIC_PRINT_TYPE_FUNC(s32);
  118. DECLARE_BASIC_PRINT_TYPE_FUNC(s64);
  119. DECLARE_BASIC_PRINT_TYPE_FUNC(x8);
  120. DECLARE_BASIC_PRINT_TYPE_FUNC(x16);
  121. DECLARE_BASIC_PRINT_TYPE_FUNC(x32);
  122. DECLARE_BASIC_PRINT_TYPE_FUNC(x64);
  123. DECLARE_BASIC_PRINT_TYPE_FUNC(string);
  124. #define FETCH_FUNC_NAME(method, type) fetch_##method##_##type
  125. /* Declare macro for basic types */
  126. #define DECLARE_FETCH_FUNC(method, type) \
  127. extern void FETCH_FUNC_NAME(method, type)(struct pt_regs *regs, \
  128. void *data, void *dest)
  129. #define DECLARE_BASIC_FETCH_FUNCS(method) \
  130. DECLARE_FETCH_FUNC(method, u8); \
  131. DECLARE_FETCH_FUNC(method, u16); \
  132. DECLARE_FETCH_FUNC(method, u32); \
  133. DECLARE_FETCH_FUNC(method, u64)
  134. DECLARE_BASIC_FETCH_FUNCS(reg);
  135. #define fetch_reg_string NULL
  136. #define fetch_reg_string_size NULL
  137. DECLARE_BASIC_FETCH_FUNCS(retval);
  138. #define fetch_retval_string NULL
  139. #define fetch_retval_string_size NULL
  140. DECLARE_BASIC_FETCH_FUNCS(symbol);
  141. DECLARE_FETCH_FUNC(symbol, string);
  142. DECLARE_FETCH_FUNC(symbol, string_size);
  143. DECLARE_BASIC_FETCH_FUNCS(deref);
  144. DECLARE_FETCH_FUNC(deref, string);
  145. DECLARE_FETCH_FUNC(deref, string_size);
  146. DECLARE_BASIC_FETCH_FUNCS(bitfield);
  147. #define fetch_bitfield_string NULL
  148. #define fetch_bitfield_string_size NULL
  149. /* comm only makes sense as a string */
  150. #define fetch_comm_u8 NULL
  151. #define fetch_comm_u16 NULL
  152. #define fetch_comm_u32 NULL
  153. #define fetch_comm_u64 NULL
  154. DECLARE_FETCH_FUNC(comm, string);
  155. DECLARE_FETCH_FUNC(comm, string_size);
  156. /*
  157. * Define macro for basic types - we don't need to define s* types, because
  158. * we have to care only about bitwidth at recording time.
  159. */
  160. #define DEFINE_BASIC_FETCH_FUNCS(method) \
  161. DEFINE_FETCH_##method(u8) \
  162. DEFINE_FETCH_##method(u16) \
  163. DEFINE_FETCH_##method(u32) \
  164. DEFINE_FETCH_##method(u64)
  165. /* Default (unsigned long) fetch type */
  166. #define __DEFAULT_FETCH_TYPE(t) x##t
  167. #define _DEFAULT_FETCH_TYPE(t) __DEFAULT_FETCH_TYPE(t)
  168. #define DEFAULT_FETCH_TYPE _DEFAULT_FETCH_TYPE(BITS_PER_LONG)
  169. #define DEFAULT_FETCH_TYPE_STR __stringify(DEFAULT_FETCH_TYPE)
  170. #define ASSIGN_FETCH_FUNC(method, type) \
  171. [FETCH_MTD_##method] = FETCH_FUNC_NAME(method, type)
  172. #define __ASSIGN_FETCH_TYPE(_name, ptype, ftype, _size, sign, _fmttype) \
  173. {.name = _name, \
  174. .size = _size, \
  175. .is_signed = sign, \
  176. .print = PRINT_TYPE_FUNC_NAME(ptype), \
  177. .fmt = PRINT_TYPE_FMT_NAME(ptype), \
  178. .fmttype = _fmttype, \
  179. .fetch = { \
  180. ASSIGN_FETCH_FUNC(reg, ftype), \
  181. ASSIGN_FETCH_FUNC(stack, ftype), \
  182. ASSIGN_FETCH_FUNC(retval, ftype), \
  183. ASSIGN_FETCH_FUNC(comm, ftype), \
  184. ASSIGN_FETCH_FUNC(memory, ftype), \
  185. ASSIGN_FETCH_FUNC(symbol, ftype), \
  186. ASSIGN_FETCH_FUNC(deref, ftype), \
  187. ASSIGN_FETCH_FUNC(bitfield, ftype), \
  188. ASSIGN_FETCH_FUNC(file_offset, ftype), \
  189. } \
  190. }
  191. #define ASSIGN_FETCH_TYPE(ptype, ftype, sign) \
  192. __ASSIGN_FETCH_TYPE(#ptype, ptype, ftype, sizeof(ftype), sign, #ptype)
  193. /* If ptype is an alias of atype, use this macro (show atype in format) */
  194. #define ASSIGN_FETCH_TYPE_ALIAS(ptype, atype, ftype, sign) \
  195. __ASSIGN_FETCH_TYPE(#ptype, ptype, ftype, sizeof(ftype), sign, #atype)
  196. #define ASSIGN_FETCH_TYPE_END {}
  197. #define FETCH_TYPE_STRING 0
  198. #define FETCH_TYPE_STRSIZE 1
  199. #ifdef CONFIG_KPROBE_EVENTS
  200. struct symbol_cache;
  201. unsigned long update_symbol_cache(struct symbol_cache *sc);
  202. void free_symbol_cache(struct symbol_cache *sc);
  203. struct symbol_cache *alloc_symbol_cache(const char *sym, long offset);
  204. bool trace_kprobe_on_func_entry(struct trace_event_call *call);
  205. bool trace_kprobe_error_injectable(struct trace_event_call *call);
  206. #else
  207. /* uprobes do not support symbol fetch methods */
  208. #define fetch_symbol_u8 NULL
  209. #define fetch_symbol_u16 NULL
  210. #define fetch_symbol_u32 NULL
  211. #define fetch_symbol_u64 NULL
  212. #define fetch_symbol_string NULL
  213. #define fetch_symbol_string_size NULL
  214. struct symbol_cache {
  215. };
  216. static inline unsigned long __used update_symbol_cache(struct symbol_cache *sc)
  217. {
  218. return 0;
  219. }
  220. static inline void __used free_symbol_cache(struct symbol_cache *sc)
  221. {
  222. }
  223. static inline struct symbol_cache * __used
  224. alloc_symbol_cache(const char *sym, long offset)
  225. {
  226. return NULL;
  227. }
  228. static inline bool trace_kprobe_on_func_entry(struct trace_event_call *call)
  229. {
  230. return false;
  231. }
  232. static inline bool trace_kprobe_error_injectable(struct trace_event_call *call)
  233. {
  234. return false;
  235. }
  236. #endif /* CONFIG_KPROBE_EVENTS */
  237. struct probe_arg {
  238. struct fetch_param fetch;
  239. struct fetch_param fetch_size;
  240. unsigned int offset; /* Offset from argument entry */
  241. const char *name; /* Name of this argument */
  242. const char *comm; /* Command of this argument */
  243. const struct fetch_type *type; /* Type of this argument */
  244. };
  245. struct trace_probe {
  246. unsigned int flags; /* For TP_FLAG_* */
  247. struct trace_event_class class;
  248. struct trace_event_call call;
  249. struct list_head files;
  250. ssize_t size; /* trace entry size */
  251. unsigned int nr_args;
  252. struct probe_arg args[];
  253. };
  254. struct event_file_link {
  255. struct trace_event_file *file;
  256. struct list_head list;
  257. };
  258. static inline bool trace_probe_is_enabled(struct trace_probe *tp)
  259. {
  260. return !!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE));
  261. }
  262. static inline bool trace_probe_is_registered(struct trace_probe *tp)
  263. {
  264. return !!(tp->flags & TP_FLAG_REGISTERED);
  265. }
  266. static nokprobe_inline void call_fetch(struct fetch_param *fprm,
  267. struct pt_regs *regs, void *dest)
  268. {
  269. return fprm->fn(regs, fprm->data, dest);
  270. }
  271. /* Check the name is good for event/group/fields */
  272. static inline bool is_good_name(const char *name)
  273. {
  274. if (!isalpha(*name) && *name != '_')
  275. return false;
  276. while (*++name != '\0') {
  277. if (!isalpha(*name) && !isdigit(*name) && *name != '_')
  278. return false;
  279. }
  280. return true;
  281. }
  282. static inline struct event_file_link *
  283. find_event_file_link(struct trace_probe *tp, struct trace_event_file *file)
  284. {
  285. struct event_file_link *link;
  286. list_for_each_entry(link, &tp->files, list)
  287. if (link->file == file)
  288. return link;
  289. return NULL;
  290. }
  291. extern int traceprobe_parse_probe_arg(char *arg, ssize_t *size,
  292. struct probe_arg *parg, bool is_return, bool is_kprobe,
  293. const struct fetch_type *ftbl);
  294. extern int traceprobe_conflict_field_name(const char *name,
  295. struct probe_arg *args, int narg);
  296. extern void traceprobe_update_arg(struct probe_arg *arg);
  297. extern void traceprobe_free_probe_arg(struct probe_arg *arg);
  298. extern int traceprobe_split_symbol_offset(char *symbol, long *offset);
  299. /* Sum up total data length for dynamic arraies (strings) */
  300. static nokprobe_inline int
  301. __get_data_size(struct trace_probe *tp, struct pt_regs *regs)
  302. {
  303. int i, ret = 0;
  304. u32 len;
  305. for (i = 0; i < tp->nr_args; i++)
  306. if (unlikely(tp->args[i].fetch_size.fn)) {
  307. call_fetch(&tp->args[i].fetch_size, regs, &len);
  308. ret += len;
  309. }
  310. return ret;
  311. }
  312. /* Store the value of each argument */
  313. static nokprobe_inline void
  314. store_trace_args(int ent_size, struct trace_probe *tp, struct pt_regs *regs,
  315. u8 *data, int maxlen)
  316. {
  317. int i;
  318. u32 end = tp->size;
  319. u32 *dl; /* Data (relative) location */
  320. for (i = 0; i < tp->nr_args; i++) {
  321. if (unlikely(tp->args[i].fetch_size.fn)) {
  322. /*
  323. * First, we set the relative location and
  324. * maximum data length to *dl
  325. */
  326. dl = (u32 *)(data + tp->args[i].offset);
  327. *dl = make_data_rloc(maxlen, end - tp->args[i].offset);
  328. /* Then try to fetch string or dynamic array data */
  329. call_fetch(&tp->args[i].fetch, regs, dl);
  330. /* Reduce maximum length */
  331. end += get_rloc_len(*dl);
  332. maxlen -= get_rloc_len(*dl);
  333. /* Trick here, convert data_rloc to data_loc */
  334. *dl = convert_rloc_to_loc(*dl,
  335. ent_size + tp->args[i].offset);
  336. } else
  337. /* Just fetching data normally */
  338. call_fetch(&tp->args[i].fetch, regs,
  339. data + tp->args[i].offset);
  340. }
  341. }
  342. extern int set_print_fmt(struct trace_probe *tp, bool is_return);
  343. #ifdef CONFIG_PERF_EVENTS
  344. extern struct trace_event_call *
  345. create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
  346. bool is_return);
  347. extern void destroy_local_trace_kprobe(struct trace_event_call *event_call);
  348. extern struct trace_event_call *
  349. create_local_trace_uprobe(char *name, unsigned long offs, bool is_return);
  350. extern void destroy_local_trace_uprobe(struct trace_event_call *event_call);
  351. #endif