trace_probe.h 12 KB

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