Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Common header file for probe-based Dynamic events.
0004  *
0005  * This code was copied from kernel/trace/trace_kprobe.h written by
0006  * Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
0007  *
0008  * Updates to make this generic:
0009  * Copyright (C) IBM Corporation, 2010-2011
0010  * Author:     Srikar Dronamraju
0011  */
0012 
0013 #include <linux/seq_file.h>
0014 #include <linux/slab.h>
0015 #include <linux/smp.h>
0016 #include <linux/tracefs.h>
0017 #include <linux/types.h>
0018 #include <linux/string.h>
0019 #include <linux/ptrace.h>
0020 #include <linux/perf_event.h>
0021 #include <linux/kprobes.h>
0022 #include <linux/stringify.h>
0023 #include <linux/limits.h>
0024 #include <linux/uaccess.h>
0025 #include <linux/bitops.h>
0026 #include <asm/bitsperlong.h>
0027 
0028 #include "trace.h"
0029 #include "trace_output.h"
0030 
0031 #define MAX_TRACE_ARGS      128
0032 #define MAX_ARGSTR_LEN      63
0033 #define MAX_ARRAY_LEN       64
0034 #define MAX_ARG_NAME_LEN    32
0035 #define MAX_STRING_SIZE     PATH_MAX
0036 
0037 /* Reserved field names */
0038 #define FIELD_STRING_IP     "__probe_ip"
0039 #define FIELD_STRING_RETIP  "__probe_ret_ip"
0040 #define FIELD_STRING_FUNC   "__probe_func"
0041 
0042 #undef DEFINE_FIELD
0043 #define DEFINE_FIELD(type, item, name, is_signed)           \
0044     do {                                \
0045         ret = trace_define_field(event_call, #type, name,   \
0046                      offsetof(typeof(field), item), \
0047                      sizeof(field.item), is_signed, \
0048                      FILTER_OTHER);         \
0049         if (ret)                        \
0050             return ret;                 \
0051     } while (0)
0052 
0053 
0054 /* Flags for trace_probe */
0055 #define TP_FLAG_TRACE       1
0056 #define TP_FLAG_PROFILE     2
0057 
0058 /* data_loc: data location, compatible with u32 */
0059 #define make_data_loc(len, offs)    \
0060     (((u32)(len) << 16) | ((u32)(offs) & 0xffff))
0061 #define get_loc_len(dl)     ((u32)(dl) >> 16)
0062 #define get_loc_offs(dl)    ((u32)(dl) & 0xffff)
0063 
0064 static nokprobe_inline void *get_loc_data(u32 *dl, void *ent)
0065 {
0066     return (u8 *)ent + get_loc_offs(*dl);
0067 }
0068 
0069 static nokprobe_inline u32 update_data_loc(u32 loc, int consumed)
0070 {
0071     u32 maxlen = get_loc_len(loc);
0072     u32 offset = get_loc_offs(loc);
0073 
0074     return make_data_loc(maxlen - consumed, offset + consumed);
0075 }
0076 
0077 /* Printing function type */
0078 typedef int (*print_type_func_t)(struct trace_seq *, void *, void *);
0079 
0080 enum fetch_op {
0081     FETCH_OP_NOP = 0,
0082     // Stage 1 (load) ops
0083     FETCH_OP_REG,       /* Register : .param = offset */
0084     FETCH_OP_STACK,     /* Stack : .param = index */
0085     FETCH_OP_STACKP,    /* Stack pointer */
0086     FETCH_OP_RETVAL,    /* Return value */
0087     FETCH_OP_IMM,       /* Immediate : .immediate */
0088     FETCH_OP_COMM,      /* Current comm */
0089     FETCH_OP_ARG,       /* Function argument : .param */
0090     FETCH_OP_FOFFS,     /* File offset: .immediate */
0091     FETCH_OP_DATA,      /* Allocated data: .data */
0092     // Stage 2 (dereference) op
0093     FETCH_OP_DEREF,     /* Dereference: .offset */
0094     FETCH_OP_UDEREF,    /* User-space Dereference: .offset */
0095     // Stage 3 (store) ops
0096     FETCH_OP_ST_RAW,    /* Raw: .size */
0097     FETCH_OP_ST_MEM,    /* Mem: .offset, .size */
0098     FETCH_OP_ST_UMEM,   /* Mem: .offset, .size */
0099     FETCH_OP_ST_STRING, /* String: .offset, .size */
0100     FETCH_OP_ST_USTRING,    /* User String: .offset, .size */
0101     // Stage 4 (modify) op
0102     FETCH_OP_MOD_BF,    /* Bitfield: .basesize, .lshift, .rshift */
0103     // Stage 5 (loop) op
0104     FETCH_OP_LP_ARRAY,  /* Array: .param = loop count */
0105     FETCH_OP_TP_ARG,    /* Trace Point argument */
0106     FETCH_OP_END,
0107     FETCH_NOP_SYMBOL,   /* Unresolved Symbol holder */
0108 };
0109 
0110 struct fetch_insn {
0111     enum fetch_op op;
0112     union {
0113         unsigned int param;
0114         struct {
0115             unsigned int size;
0116             int offset;
0117         };
0118         struct {
0119             unsigned char basesize;
0120             unsigned char lshift;
0121             unsigned char rshift;
0122         };
0123         unsigned long immediate;
0124         void *data;
0125     };
0126 };
0127 
0128 /* fetch + deref*N + store + mod + end <= 16, this allows N=12, enough */
0129 #define FETCH_INSN_MAX  16
0130 #define FETCH_TOKEN_COMM    (-ECOMM)
0131 
0132 /* Fetch type information table */
0133 struct fetch_type {
0134     const char      *name;      /* Name of type */
0135     size_t          size;       /* Byte size of type */
0136     int         is_signed;  /* Signed flag */
0137     print_type_func_t   print;      /* Print functions */
0138     const char      *fmt;       /* Format string */
0139     const char      *fmttype;   /* Name in format file */
0140 };
0141 
0142 /* For defining macros, define string/string_size types */
0143 typedef u32 string;
0144 typedef u32 string_size;
0145 
0146 #define PRINT_TYPE_FUNC_NAME(type)  print_type_##type
0147 #define PRINT_TYPE_FMT_NAME(type)   print_type_format_##type
0148 
0149 /* Printing  in basic type function template */
0150 #define DECLARE_BASIC_PRINT_TYPE_FUNC(type)             \
0151 int PRINT_TYPE_FUNC_NAME(type)(struct trace_seq *s, void *data, void *ent);\
0152 extern const char PRINT_TYPE_FMT_NAME(type)[]
0153 
0154 DECLARE_BASIC_PRINT_TYPE_FUNC(u8);
0155 DECLARE_BASIC_PRINT_TYPE_FUNC(u16);
0156 DECLARE_BASIC_PRINT_TYPE_FUNC(u32);
0157 DECLARE_BASIC_PRINT_TYPE_FUNC(u64);
0158 DECLARE_BASIC_PRINT_TYPE_FUNC(s8);
0159 DECLARE_BASIC_PRINT_TYPE_FUNC(s16);
0160 DECLARE_BASIC_PRINT_TYPE_FUNC(s32);
0161 DECLARE_BASIC_PRINT_TYPE_FUNC(s64);
0162 DECLARE_BASIC_PRINT_TYPE_FUNC(x8);
0163 DECLARE_BASIC_PRINT_TYPE_FUNC(x16);
0164 DECLARE_BASIC_PRINT_TYPE_FUNC(x32);
0165 DECLARE_BASIC_PRINT_TYPE_FUNC(x64);
0166 
0167 DECLARE_BASIC_PRINT_TYPE_FUNC(string);
0168 DECLARE_BASIC_PRINT_TYPE_FUNC(symbol);
0169 
0170 /* Default (unsigned long) fetch type */
0171 #define __DEFAULT_FETCH_TYPE(t) x##t
0172 #define _DEFAULT_FETCH_TYPE(t) __DEFAULT_FETCH_TYPE(t)
0173 #define DEFAULT_FETCH_TYPE _DEFAULT_FETCH_TYPE(BITS_PER_LONG)
0174 #define DEFAULT_FETCH_TYPE_STR __stringify(DEFAULT_FETCH_TYPE)
0175 
0176 #define __ADDR_FETCH_TYPE(t) u##t
0177 #define _ADDR_FETCH_TYPE(t) __ADDR_FETCH_TYPE(t)
0178 #define ADDR_FETCH_TYPE _ADDR_FETCH_TYPE(BITS_PER_LONG)
0179 
0180 #define __ASSIGN_FETCH_TYPE(_name, ptype, ftype, _size, sign, _fmttype) \
0181     {.name = _name,             \
0182      .size = _size,                 \
0183      .is_signed = sign,             \
0184      .print = PRINT_TYPE_FUNC_NAME(ptype),      \
0185      .fmt = PRINT_TYPE_FMT_NAME(ptype),     \
0186      .fmttype = _fmttype,               \
0187     }
0188 #define _ASSIGN_FETCH_TYPE(_name, ptype, ftype, _size, sign, _fmttype)  \
0189     __ASSIGN_FETCH_TYPE(_name, ptype, ftype, _size, sign, #_fmttype)
0190 #define ASSIGN_FETCH_TYPE(ptype, ftype, sign)           \
0191     _ASSIGN_FETCH_TYPE(#ptype, ptype, ftype, sizeof(ftype), sign, ptype)
0192 
0193 /* If ptype is an alias of atype, use this macro (show atype in format) */
0194 #define ASSIGN_FETCH_TYPE_ALIAS(ptype, atype, ftype, sign)      \
0195     _ASSIGN_FETCH_TYPE(#ptype, ptype, ftype, sizeof(ftype), sign, atype)
0196 
0197 #define ASSIGN_FETCH_TYPE_END {}
0198 #define MAX_ARRAY_LEN   64
0199 
0200 #ifdef CONFIG_KPROBE_EVENTS
0201 bool trace_kprobe_on_func_entry(struct trace_event_call *call);
0202 bool trace_kprobe_error_injectable(struct trace_event_call *call);
0203 #else
0204 static inline bool trace_kprobe_on_func_entry(struct trace_event_call *call)
0205 {
0206     return false;
0207 }
0208 
0209 static inline bool trace_kprobe_error_injectable(struct trace_event_call *call)
0210 {
0211     return false;
0212 }
0213 #endif /* CONFIG_KPROBE_EVENTS */
0214 
0215 struct probe_arg {
0216     struct fetch_insn   *code;
0217     bool            dynamic;/* Dynamic array (string) is used */
0218     unsigned int        offset; /* Offset from argument entry */
0219     unsigned int        count;  /* Array count */
0220     const char      *name;  /* Name of this argument */
0221     const char      *comm;  /* Command of this argument */
0222     char            *fmt;   /* Format string if needed */
0223     const struct fetch_type *type;  /* Type of this argument */
0224 };
0225 
0226 struct trace_uprobe_filter {
0227     rwlock_t        rwlock;
0228     int         nr_systemwide;
0229     struct list_head    perf_events;
0230 };
0231 
0232 /* Event call and class holder */
0233 struct trace_probe_event {
0234     unsigned int            flags;  /* For TP_FLAG_* */
0235     struct trace_event_class    class;
0236     struct trace_event_call     call;
0237     struct list_head        files;
0238     struct list_head        probes;
0239     struct trace_uprobe_filter  filter[];
0240 };
0241 
0242 struct trace_probe {
0243     struct list_head        list;
0244     struct trace_probe_event    *event;
0245     ssize_t             size;   /* trace entry size */
0246     unsigned int            nr_args;
0247     struct probe_arg        args[];
0248 };
0249 
0250 struct event_file_link {
0251     struct trace_event_file     *file;
0252     struct list_head        list;
0253 };
0254 
0255 static inline bool trace_probe_test_flag(struct trace_probe *tp,
0256                      unsigned int flag)
0257 {
0258     return !!(tp->event->flags & flag);
0259 }
0260 
0261 static inline void trace_probe_set_flag(struct trace_probe *tp,
0262                     unsigned int flag)
0263 {
0264     tp->event->flags |= flag;
0265 }
0266 
0267 static inline void trace_probe_clear_flag(struct trace_probe *tp,
0268                       unsigned int flag)
0269 {
0270     tp->event->flags &= ~flag;
0271 }
0272 
0273 static inline bool trace_probe_is_enabled(struct trace_probe *tp)
0274 {
0275     return trace_probe_test_flag(tp, TP_FLAG_TRACE | TP_FLAG_PROFILE);
0276 }
0277 
0278 static inline const char *trace_probe_name(struct trace_probe *tp)
0279 {
0280     return trace_event_name(&tp->event->call);
0281 }
0282 
0283 static inline const char *trace_probe_group_name(struct trace_probe *tp)
0284 {
0285     return tp->event->call.class->system;
0286 }
0287 
0288 static inline struct trace_event_call *
0289     trace_probe_event_call(struct trace_probe *tp)
0290 {
0291     return &tp->event->call;
0292 }
0293 
0294 static inline struct trace_probe_event *
0295 trace_probe_event_from_call(struct trace_event_call *event_call)
0296 {
0297     return container_of(event_call, struct trace_probe_event, call);
0298 }
0299 
0300 static inline struct trace_probe *
0301 trace_probe_primary_from_call(struct trace_event_call *call)
0302 {
0303     struct trace_probe_event *tpe = trace_probe_event_from_call(call);
0304 
0305     return list_first_entry(&tpe->probes, struct trace_probe, list);
0306 }
0307 
0308 static inline struct list_head *trace_probe_probe_list(struct trace_probe *tp)
0309 {
0310     return &tp->event->probes;
0311 }
0312 
0313 static inline bool trace_probe_has_sibling(struct trace_probe *tp)
0314 {
0315     struct list_head *list = trace_probe_probe_list(tp);
0316 
0317     return !list_empty(list) && !list_is_singular(list);
0318 }
0319 
0320 static inline int trace_probe_unregister_event_call(struct trace_probe *tp)
0321 {
0322     /* tp->event is unregistered in trace_remove_event_call() */
0323     return trace_remove_event_call(&tp->event->call);
0324 }
0325 
0326 static inline bool trace_probe_has_single_file(struct trace_probe *tp)
0327 {
0328     return !!list_is_singular(&tp->event->files);
0329 }
0330 
0331 int trace_probe_init(struct trace_probe *tp, const char *event,
0332              const char *group, bool alloc_filter);
0333 void trace_probe_cleanup(struct trace_probe *tp);
0334 int trace_probe_append(struct trace_probe *tp, struct trace_probe *to);
0335 void trace_probe_unlink(struct trace_probe *tp);
0336 int trace_probe_register_event_call(struct trace_probe *tp);
0337 int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file);
0338 int trace_probe_remove_file(struct trace_probe *tp,
0339                 struct trace_event_file *file);
0340 struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
0341                         struct trace_event_file *file);
0342 int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b);
0343 bool trace_probe_match_command_args(struct trace_probe *tp,
0344                     int argc, const char **argv);
0345 int trace_probe_create(const char *raw_command, int (*createfn)(int, const char **));
0346 
0347 #define trace_probe_for_each_link(pos, tp)  \
0348     list_for_each_entry(pos, &(tp)->event->files, list)
0349 #define trace_probe_for_each_link_rcu(pos, tp)  \
0350     list_for_each_entry_rcu(pos, &(tp)->event->files, list)
0351 
0352 #define TPARG_FL_RETURN BIT(0)
0353 #define TPARG_FL_KERNEL BIT(1)
0354 #define TPARG_FL_FENTRY BIT(2)
0355 #define TPARG_FL_TPOINT BIT(3)
0356 #define TPARG_FL_MASK   GENMASK(3, 0)
0357 
0358 extern int traceprobe_parse_probe_arg(struct trace_probe *tp, int i,
0359                 const char *argv, unsigned int flags);
0360 
0361 extern int traceprobe_update_arg(struct probe_arg *arg);
0362 extern void traceprobe_free_probe_arg(struct probe_arg *arg);
0363 
0364 extern int traceprobe_split_symbol_offset(char *symbol, long *offset);
0365 int traceprobe_parse_event_name(const char **pevent, const char **pgroup,
0366                 char *buf, int offset);
0367 
0368 enum probe_print_type {
0369     PROBE_PRINT_NORMAL,
0370     PROBE_PRINT_RETURN,
0371     PROBE_PRINT_EVENT,
0372 };
0373 
0374 extern int traceprobe_set_print_fmt(struct trace_probe *tp, enum probe_print_type ptype);
0375 
0376 #ifdef CONFIG_PERF_EVENTS
0377 extern struct trace_event_call *
0378 create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
0379               bool is_return);
0380 extern void destroy_local_trace_kprobe(struct trace_event_call *event_call);
0381 
0382 extern struct trace_event_call *
0383 create_local_trace_uprobe(char *name, unsigned long offs,
0384               unsigned long ref_ctr_offset, bool is_return);
0385 extern void destroy_local_trace_uprobe(struct trace_event_call *event_call);
0386 #endif
0387 extern int traceprobe_define_arg_fields(struct trace_event_call *event_call,
0388                     size_t offset, struct trace_probe *tp);
0389 
0390 #undef ERRORS
0391 #define ERRORS  \
0392     C(FILE_NOT_FOUND,   "Failed to find the given file"),   \
0393     C(NO_REGULAR_FILE,  "Not a regular file"),          \
0394     C(BAD_REFCNT,       "Invalid reference counter offset"),    \
0395     C(REFCNT_OPEN_BRACE,    "Reference counter brace is not closed"), \
0396     C(BAD_REFCNT_SUFFIX,    "Reference counter has wrong suffix"),  \
0397     C(BAD_UPROBE_OFFS,  "Invalid uprobe offset"),       \
0398     C(MAXACT_NO_KPROBE, "Maxactive is not for kprobe"),     \
0399     C(BAD_MAXACT,       "Invalid maxactive number"),        \
0400     C(MAXACT_TOO_BIG,   "Maxactive is too big"),        \
0401     C(BAD_PROBE_ADDR,   "Invalid probed address or symbol"),    \
0402     C(BAD_RETPROBE,     "Retprobe address must be an function entry"), \
0403     C(BAD_ADDR_SUFFIX,  "Invalid probed address suffix"), \
0404     C(NO_GROUP_NAME,    "Group name is not specified"),     \
0405     C(GROUP_TOO_LONG,   "Group name is too long"),      \
0406     C(BAD_GROUP_NAME,   "Group name must follow the same rules as C identifiers"), \
0407     C(NO_EVENT_NAME,    "Event name is not specified"),     \
0408     C(EVENT_TOO_LONG,   "Event name is too long"),      \
0409     C(BAD_EVENT_NAME,   "Event name must follow the same rules as C identifiers"), \
0410     C(EVENT_EXIST,      "Given group/event name is already used by another event"), \
0411     C(RETVAL_ON_PROBE,  "$retval is not available on probe"),   \
0412     C(BAD_STACK_NUM,    "Invalid stack number"),        \
0413     C(BAD_ARG_NUM,      "Invalid argument number"),     \
0414     C(BAD_VAR,      "Invalid $-valiable specified"),    \
0415     C(BAD_REG_NAME,     "Invalid register name"),       \
0416     C(BAD_MEM_ADDR,     "Invalid memory address"),      \
0417     C(BAD_IMM,      "Invalid immediate value"),     \
0418     C(IMMSTR_NO_CLOSE,  "String is not closed with '\"'"),  \
0419     C(FILE_ON_KPROBE,   "File offset is not available with kprobe"), \
0420     C(BAD_FILE_OFFS,    "Invalid file offset value"),       \
0421     C(SYM_ON_UPROBE,    "Symbol is not available with uprobe"), \
0422     C(TOO_MANY_OPS,     "Dereference is too much nested"),  \
0423     C(DEREF_NEED_BRACE, "Dereference needs a brace"),       \
0424     C(BAD_DEREF_OFFS,   "Invalid dereference offset"),      \
0425     C(DEREF_OPEN_BRACE, "Dereference brace is not closed"), \
0426     C(COMM_CANT_DEREF,  "$comm can not be dereferenced"),   \
0427     C(BAD_FETCH_ARG,    "Invalid fetch argument"),      \
0428     C(ARRAY_NO_CLOSE,   "Array is not closed"),         \
0429     C(BAD_ARRAY_SUFFIX, "Array has wrong suffix"),      \
0430     C(BAD_ARRAY_NUM,    "Invalid array size"),          \
0431     C(ARRAY_TOO_BIG,    "Array number is too big"),     \
0432     C(BAD_TYPE,     "Unknown type is specified"),       \
0433     C(BAD_STRING,       "String accepts only memory argument"), \
0434     C(BAD_BITFIELD,     "Invalid bitfield"),            \
0435     C(ARG_NAME_TOO_LONG,    "Argument name is too long"),       \
0436     C(NO_ARG_NAME,      "Argument name is not specified"),  \
0437     C(BAD_ARG_NAME,     "Argument name must follow the same rules as C identifiers"), \
0438     C(USED_ARG_NAME,    "This argument name is already used"),  \
0439     C(ARG_TOO_LONG,     "Argument expression is too long"), \
0440     C(NO_ARG_BODY,      "No argument expression"),      \
0441     C(BAD_INSN_BNDRY,   "Probe point is not an instruction boundary"),\
0442     C(FAIL_REG_PROBE,   "Failed to register probe event"),\
0443     C(DIFF_PROBE_TYPE,  "Probe type is different from existing probe"),\
0444     C(DIFF_ARG_TYPE,    "Argument type or name is different from existing probe"),\
0445     C(SAME_PROBE,       "There is already the exact same probe event"),\
0446     C(NO_EVENT_INFO,    "This requires both group and event name to attach"),\
0447     C(BAD_ATTACH_EVENT, "Attached event does not exist"),\
0448     C(BAD_ATTACH_ARG,   "Attached event does not have this field"),
0449 
0450 #undef C
0451 #define C(a, b)     TP_ERR_##a
0452 
0453 /* Define TP_ERR_ */
0454 enum { ERRORS };
0455 
0456 /* Error text is defined in trace_probe.c */
0457 
0458 struct trace_probe_log {
0459     const char  *subsystem;
0460     const char  **argv;
0461     int     argc;
0462     int     index;
0463 };
0464 
0465 void trace_probe_log_init(const char *subsystem, int argc, const char **argv);
0466 void trace_probe_log_set_index(int index);
0467 void trace_probe_log_clear(void);
0468 void __trace_probe_log_err(int offset, int err);
0469 
0470 #define trace_probe_log_err(offs, err)  \
0471     __trace_probe_log_err(offs, TP_ERR_##err)