Back to home page

LXR

 
 

    


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