Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __PERF_DSO
0003 #define __PERF_DSO
0004 
0005 #include <pthread.h>
0006 #include <linux/refcount.h>
0007 #include <linux/types.h>
0008 #include <linux/rbtree.h>
0009 #include <sys/types.h>
0010 #include <stdbool.h>
0011 #include <stdio.h>
0012 #include <linux/bitops.h>
0013 #include "build-id.h"
0014 
0015 struct machine;
0016 struct map;
0017 struct perf_env;
0018 
0019 #define DSO__NAME_KALLSYMS  "[kernel.kallsyms]"
0020 #define DSO__NAME_KCORE     "[kernel.kcore]"
0021 
0022 enum dso_binary_type {
0023     DSO_BINARY_TYPE__KALLSYMS = 0,
0024     DSO_BINARY_TYPE__GUEST_KALLSYMS,
0025     DSO_BINARY_TYPE__VMLINUX,
0026     DSO_BINARY_TYPE__GUEST_VMLINUX,
0027     DSO_BINARY_TYPE__JAVA_JIT,
0028     DSO_BINARY_TYPE__DEBUGLINK,
0029     DSO_BINARY_TYPE__BUILD_ID_CACHE,
0030     DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO,
0031     DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
0032     DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
0033     DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO,
0034     DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
0035     DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
0036     DSO_BINARY_TYPE__GUEST_KMODULE,
0037     DSO_BINARY_TYPE__GUEST_KMODULE_COMP,
0038     DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
0039     DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP,
0040     DSO_BINARY_TYPE__KCORE,
0041     DSO_BINARY_TYPE__GUEST_KCORE,
0042     DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
0043     DSO_BINARY_TYPE__BPF_PROG_INFO,
0044     DSO_BINARY_TYPE__BPF_IMAGE,
0045     DSO_BINARY_TYPE__OOL,
0046     DSO_BINARY_TYPE__NOT_FOUND,
0047 };
0048 
0049 enum dso_space_type {
0050     DSO_SPACE__USER = 0,
0051     DSO_SPACE__KERNEL,
0052     DSO_SPACE__KERNEL_GUEST
0053 };
0054 
0055 enum dso_swap_type {
0056     DSO_SWAP__UNSET,
0057     DSO_SWAP__NO,
0058     DSO_SWAP__YES,
0059 };
0060 
0061 enum dso_data_status {
0062     DSO_DATA_STATUS_ERROR   = -1,
0063     DSO_DATA_STATUS_UNKNOWN = 0,
0064     DSO_DATA_STATUS_OK  = 1,
0065 };
0066 
0067 enum dso_data_status_seen {
0068     DSO_DATA_STATUS_SEEN_ITRACE,
0069 };
0070 
0071 enum dso_type {
0072     DSO__TYPE_UNKNOWN,
0073     DSO__TYPE_64BIT,
0074     DSO__TYPE_32BIT,
0075     DSO__TYPE_X32BIT,
0076 };
0077 
0078 enum dso_load_errno {
0079     DSO_LOAD_ERRNO__SUCCESS     = 0,
0080 
0081     /*
0082      * Choose an arbitrary negative big number not to clash with standard
0083      * errno since SUS requires the errno has distinct positive values.
0084      * See 'Issue 6' in the link below.
0085      *
0086      * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html
0087      */
0088     __DSO_LOAD_ERRNO__START     = -10000,
0089 
0090     DSO_LOAD_ERRNO__INTERNAL_ERROR  = __DSO_LOAD_ERRNO__START,
0091 
0092     /* for symsrc__init() */
0093     DSO_LOAD_ERRNO__INVALID_ELF,
0094     DSO_LOAD_ERRNO__CANNOT_READ_BUILDID,
0095     DSO_LOAD_ERRNO__MISMATCHING_BUILDID,
0096 
0097     /* for decompress_kmodule */
0098     DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE,
0099 
0100     __DSO_LOAD_ERRNO__END,
0101 };
0102 
0103 #define DSO__SWAP(dso, type, val)           \
0104 ({                          \
0105     type ____r = val;               \
0106     BUG_ON(dso->needs_swap == DSO_SWAP__UNSET); \
0107     if (dso->needs_swap == DSO_SWAP__YES) {     \
0108         switch (sizeof(____r)) {        \
0109         case 2:                 \
0110             ____r = bswap_16(val);      \
0111             break;              \
0112         case 4:                 \
0113             ____r = bswap_32(val);      \
0114             break;              \
0115         case 8:                 \
0116             ____r = bswap_64(val);      \
0117             break;              \
0118         default:                \
0119             BUG_ON(1);          \
0120         }                   \
0121     }                       \
0122     ____r;                      \
0123 })
0124 
0125 #define DSO__DATA_CACHE_SIZE 4096
0126 #define DSO__DATA_CACHE_MASK ~(DSO__DATA_CACHE_SIZE - 1)
0127 
0128 /*
0129  * Data about backing storage DSO, comes from PERF_RECORD_MMAP2 meta events
0130  */
0131 struct dso_id {
0132     u32 maj;
0133     u32 min;
0134     u64 ino;
0135     u64 ino_generation;
0136 };
0137 
0138 struct dso_cache {
0139     struct rb_node  rb_node;
0140     u64 offset;
0141     u64 size;
0142     char data[];
0143 };
0144 
0145 struct auxtrace_cache;
0146 
0147 struct dso {
0148     pthread_mutex_t  lock;
0149     struct list_head node;
0150     struct rb_node   rb_node;   /* rbtree node sorted by long name */
0151     struct rb_root   *root;     /* root of rbtree that rb_node is in */
0152     struct rb_root_cached symbols;
0153     struct rb_root_cached symbol_names;
0154     struct rb_root_cached inlined_nodes;
0155     struct rb_root_cached srclines;
0156     struct {
0157         u64     addr;
0158         struct symbol   *symbol;
0159     } last_find_result;
0160     void         *a2l;
0161     char         *symsrc_filename;
0162     unsigned int     a2l_fails;
0163     enum dso_space_type kernel;
0164     enum dso_swap_type  needs_swap;
0165     enum dso_binary_type    symtab_type;
0166     enum dso_binary_type    binary_type;
0167     enum dso_load_errno load_errno;
0168     u8       adjust_symbols:1;
0169     u8       has_build_id:1;
0170     u8       header_build_id:1;
0171     u8       has_srcline:1;
0172     u8       hit:1;
0173     u8       annotate_warned:1;
0174     u8       auxtrace_warned:1;
0175     u8       short_name_allocated:1;
0176     u8       long_name_allocated:1;
0177     u8       is_64_bit:1;
0178     bool         sorted_by_name;
0179     bool         loaded;
0180     u8       rel;
0181     struct build_id  bid;
0182     u64      text_offset;
0183     const char   *short_name;
0184     const char   *long_name;
0185     u16      long_name_len;
0186     u16      short_name_len;
0187     void        *dwfl;          /* DWARF debug info */
0188     struct auxtrace_cache *auxtrace_cache;
0189     int      comp;
0190 
0191     /* dso data file */
0192     struct {
0193         struct rb_root   cache;
0194         int      fd;
0195         int      status;
0196         u32      status_seen;
0197         u64      file_size;
0198         struct list_head open_entry;
0199         u64      elf_base_addr;
0200         u64      debug_frame_offset;
0201         u64      eh_frame_hdr_addr;
0202         u64      eh_frame_hdr_offset;
0203     } data;
0204     /* bpf prog information */
0205     struct {
0206         u32     id;
0207         u32     sub_id;
0208         struct perf_env *env;
0209     } bpf_prog;
0210 
0211     union { /* Tool specific area */
0212         void     *priv;
0213         u64  db_id;
0214     };
0215     struct nsinfo   *nsinfo;
0216     struct dso_id    id;
0217     refcount_t   refcnt;
0218     char         name[];
0219 };
0220 
0221 /* dso__for_each_symbol - iterate over the symbols of given type
0222  *
0223  * @dso: the 'struct dso *' in which symbols are iterated
0224  * @pos: the 'struct symbol *' to use as a loop cursor
0225  * @n: the 'struct rb_node *' to use as a temporary storage
0226  */
0227 #define dso__for_each_symbol(dso, pos, n)   \
0228     symbols__for_each_entry(&(dso)->symbols, pos, n)
0229 
0230 #define dsos__for_each_with_build_id(pos, head) \
0231     list_for_each_entry(pos, head, node)    \
0232         if (!pos->has_build_id)     \
0233             continue;       \
0234         else
0235 
0236 static inline void dso__set_loaded(struct dso *dso)
0237 {
0238     dso->loaded = true;
0239 }
0240 
0241 struct dso *dso__new_id(const char *name, struct dso_id *id);
0242 struct dso *dso__new(const char *name);
0243 void dso__delete(struct dso *dso);
0244 
0245 int dso__cmp_id(struct dso *a, struct dso *b);
0246 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated);
0247 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated);
0248 
0249 int dso__name_len(const struct dso *dso);
0250 
0251 struct dso *dso__get(struct dso *dso);
0252 void dso__put(struct dso *dso);
0253 
0254 static inline void __dso__zput(struct dso **dso)
0255 {
0256     dso__put(*dso);
0257     *dso = NULL;
0258 }
0259 
0260 #define dso__zput(dso) __dso__zput(&dso)
0261 
0262 bool dso__loaded(const struct dso *dso);
0263 
0264 static inline bool dso__has_symbols(const struct dso *dso)
0265 {
0266     return !RB_EMPTY_ROOT(&dso->symbols.rb_root);
0267 }
0268 
0269 bool dso__sorted_by_name(const struct dso *dso);
0270 void dso__set_sorted_by_name(struct dso *dso);
0271 void dso__sort_by_name(struct dso *dso);
0272 
0273 void dso__set_build_id(struct dso *dso, struct build_id *bid);
0274 bool dso__build_id_equal(const struct dso *dso, struct build_id *bid);
0275 void dso__read_running_kernel_build_id(struct dso *dso,
0276                        struct machine *machine);
0277 int dso__kernel_module_get_build_id(struct dso *dso, const char *root_dir);
0278 
0279 char dso__symtab_origin(const struct dso *dso);
0280 int dso__read_binary_type_filename(const struct dso *dso, enum dso_binary_type type,
0281                    char *root_dir, char *filename, size_t size);
0282 bool is_kernel_module(const char *pathname, int cpumode);
0283 bool dso__needs_decompress(struct dso *dso);
0284 int dso__decompress_kmodule_fd(struct dso *dso, const char *name);
0285 int dso__decompress_kmodule_path(struct dso *dso, const char *name,
0286                  char *pathname, size_t len);
0287 int filename__decompress(const char *name, char *pathname,
0288              size_t len, int comp, int *err);
0289 
0290 #define KMOD_DECOMP_NAME  "/tmp/perf-kmod-XXXXXX"
0291 #define KMOD_DECOMP_LEN   sizeof(KMOD_DECOMP_NAME)
0292 
0293 struct kmod_path {
0294     char *name;
0295     int   comp;
0296     bool  kmod;
0297 };
0298 
0299 int __kmod_path__parse(struct kmod_path *m, const char *path,
0300              bool alloc_name);
0301 
0302 #define kmod_path__parse(__m, __p)      __kmod_path__parse(__m, __p, false)
0303 #define kmod_path__parse_name(__m, __p) __kmod_path__parse(__m, __p, true)
0304 
0305 void dso__set_module_info(struct dso *dso, struct kmod_path *m,
0306               struct machine *machine);
0307 
0308 /*
0309  * The dso__data_* external interface provides following functions:
0310  *   dso__data_get_fd
0311  *   dso__data_put_fd
0312  *   dso__data_close
0313  *   dso__data_size
0314  *   dso__data_read_offset
0315  *   dso__data_read_addr
0316  *   dso__data_write_cache_offs
0317  *   dso__data_write_cache_addr
0318  *
0319  * Please refer to the dso.c object code for each function and
0320  * arguments documentation. Following text tries to explain the
0321  * dso file descriptor caching.
0322  *
0323  * The dso__data* interface allows caching of opened file descriptors
0324  * to speed up the dso data accesses. The idea is to leave the file
0325  * descriptor opened ideally for the whole life of the dso object.
0326  *
0327  * The current usage of the dso__data_* interface is as follows:
0328  *
0329  * Get DSO's fd:
0330  *   int fd = dso__data_get_fd(dso, machine);
0331  *   if (fd >= 0) {
0332  *       USE 'fd' SOMEHOW
0333  *       dso__data_put_fd(dso);
0334  *   }
0335  *
0336  * Read DSO's data:
0337  *   n = dso__data_read_offset(dso_0, &machine, 0, buf, BUFSIZE);
0338  *   n = dso__data_read_addr(dso_0, &machine, 0, buf, BUFSIZE);
0339  *
0340  * Eventually close DSO's fd:
0341  *   dso__data_close(dso);
0342  *
0343  * It is not necessary to close the DSO object data file. Each time new
0344  * DSO data file is opened, the limit (RLIMIT_NOFILE/2) is checked. Once
0345  * it is crossed, the oldest opened DSO object is closed.
0346  *
0347  * The dso__delete function calls close_dso function to ensure the
0348  * data file descriptor gets closed/unmapped before the dso object
0349  * is freed.
0350  *
0351  * TODO
0352 */
0353 int dso__data_get_fd(struct dso *dso, struct machine *machine);
0354 void dso__data_put_fd(struct dso *dso);
0355 void dso__data_close(struct dso *dso);
0356 
0357 int dso__data_file_size(struct dso *dso, struct machine *machine);
0358 off_t dso__data_size(struct dso *dso, struct machine *machine);
0359 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
0360                   u64 offset, u8 *data, ssize_t size);
0361 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
0362                 struct machine *machine, u64 addr,
0363                 u8 *data, ssize_t size);
0364 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by);
0365 ssize_t dso__data_write_cache_offs(struct dso *dso, struct machine *machine,
0366                    u64 offset, const u8 *data, ssize_t size);
0367 ssize_t dso__data_write_cache_addr(struct dso *dso, struct map *map,
0368                    struct machine *machine, u64 addr,
0369                    const u8 *data, ssize_t size);
0370 
0371 struct map *dso__new_map(const char *name);
0372 struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
0373                     const char *short_name, int dso_type);
0374 
0375 void dso__reset_find_symbol_cache(struct dso *dso);
0376 
0377 size_t dso__fprintf_symbols_by_name(struct dso *dso, FILE *fp);
0378 size_t dso__fprintf(struct dso *dso, FILE *fp);
0379 
0380 static inline bool dso__is_vmlinux(struct dso *dso)
0381 {
0382     return dso->binary_type == DSO_BINARY_TYPE__VMLINUX ||
0383            dso->binary_type == DSO_BINARY_TYPE__GUEST_VMLINUX;
0384 }
0385 
0386 static inline bool dso__is_kcore(struct dso *dso)
0387 {
0388     return dso->binary_type == DSO_BINARY_TYPE__KCORE ||
0389            dso->binary_type == DSO_BINARY_TYPE__GUEST_KCORE;
0390 }
0391 
0392 static inline bool dso__is_kallsyms(struct dso *dso)
0393 {
0394     return dso->kernel && dso->long_name[0] != '/';
0395 }
0396 
0397 void dso__free_a2l(struct dso *dso);
0398 
0399 enum dso_type dso__type(struct dso *dso, struct machine *machine);
0400 
0401 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen);
0402 
0403 void reset_fd_limit(void);
0404 
0405 #endif /* __PERF_DSO */