Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 1 | #include <linux/kernel.h> |
| 2 | |
| 3 | #include <unistd.h> |
| 4 | #include <sys/types.h> |
| 5 | |
| 6 | #include "session.h" |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 7 | #include "sort.h" |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 8 | #include "util.h" |
| 9 | |
| 10 | static int perf_session__open(struct perf_session *self, bool force) |
| 11 | { |
| 12 | struct stat input_stat; |
| 13 | |
| 14 | self->fd = open(self->filename, O_RDONLY); |
| 15 | if (self->fd < 0) { |
| 16 | pr_err("failed to open file: %s", self->filename); |
| 17 | if (!strcmp(self->filename, "perf.data")) |
| 18 | pr_err(" (try 'perf record' first)"); |
| 19 | pr_err("\n"); |
| 20 | return -errno; |
| 21 | } |
| 22 | |
| 23 | if (fstat(self->fd, &input_stat) < 0) |
| 24 | goto out_close; |
| 25 | |
| 26 | if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) { |
| 27 | pr_err("file %s not owned by current user or root\n", |
| 28 | self->filename); |
| 29 | goto out_close; |
| 30 | } |
| 31 | |
| 32 | if (!input_stat.st_size) { |
| 33 | pr_info("zero-sized file (%s), nothing to do!\n", |
| 34 | self->filename); |
| 35 | goto out_close; |
| 36 | } |
| 37 | |
| 38 | if (perf_header__read(&self->header, self->fd) < 0) { |
| 39 | pr_err("incompatible file format"); |
| 40 | goto out_close; |
| 41 | } |
| 42 | |
| 43 | self->size = input_stat.st_size; |
| 44 | return 0; |
| 45 | |
| 46 | out_close: |
| 47 | close(self->fd); |
| 48 | self->fd = -1; |
| 49 | return -1; |
| 50 | } |
| 51 | |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 52 | struct perf_session *perf_session__new(const char *filename, int mode, bool force) |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 53 | { |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 54 | size_t len = filename ? strlen(filename) + 1 : 0; |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 55 | struct perf_session *self = zalloc(sizeof(*self) + len); |
| 56 | |
| 57 | if (self == NULL) |
| 58 | goto out; |
| 59 | |
| 60 | if (perf_header__init(&self->header) < 0) |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 61 | goto out_free; |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 62 | |
| 63 | memcpy(self->filename, filename, len); |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 64 | self->threads = RB_ROOT; |
| 65 | self->last_match = NULL; |
Arnaldo Carvalho de Melo | ec91336 | 2009-12-13 19:50:27 -0200 | [diff] [blame] | 66 | self->mmap_window = 32; |
| 67 | self->cwd = NULL; |
| 68 | self->cwdlen = 0; |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 69 | map_groups__init(&self->kmaps); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 70 | |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 71 | if (perf_session__create_kernel_maps(self) < 0) |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 72 | goto out_delete; |
| 73 | |
| 74 | if (mode == O_RDONLY && perf_session__open(self, force) < 0) |
| 75 | goto out_delete; |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 76 | out: |
| 77 | return self; |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 78 | out_free: |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 79 | free(self); |
| 80 | return NULL; |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 81 | out_delete: |
| 82 | perf_session__delete(self); |
| 83 | return NULL; |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void perf_session__delete(struct perf_session *self) |
| 87 | { |
| 88 | perf_header__exit(&self->header); |
| 89 | close(self->fd); |
Arnaldo Carvalho de Melo | ec91336 | 2009-12-13 19:50:27 -0200 | [diff] [blame] | 90 | free(self->cwd); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 91 | free(self); |
| 92 | } |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 93 | |
| 94 | static bool symbol__match_parent_regex(struct symbol *sym) |
| 95 | { |
| 96 | if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0)) |
| 97 | return 1; |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | struct symbol **perf_session__resolve_callchain(struct perf_session *self, |
| 103 | struct thread *thread, |
| 104 | struct ip_callchain *chain, |
| 105 | struct symbol **parent) |
| 106 | { |
| 107 | u8 cpumode = PERF_RECORD_MISC_USER; |
| 108 | struct symbol **syms = NULL; |
| 109 | unsigned int i; |
| 110 | |
| 111 | if (self->use_callchain) { |
| 112 | syms = calloc(chain->nr, sizeof(*syms)); |
| 113 | if (!syms) { |
| 114 | fprintf(stderr, "Can't allocate memory for symbols\n"); |
| 115 | exit(-1); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | for (i = 0; i < chain->nr; i++) { |
| 120 | u64 ip = chain->ips[i]; |
| 121 | struct addr_location al; |
| 122 | |
| 123 | if (ip >= PERF_CONTEXT_MAX) { |
| 124 | switch (ip) { |
| 125 | case PERF_CONTEXT_HV: |
| 126 | cpumode = PERF_RECORD_MISC_HYPERVISOR; break; |
| 127 | case PERF_CONTEXT_KERNEL: |
| 128 | cpumode = PERF_RECORD_MISC_KERNEL; break; |
| 129 | case PERF_CONTEXT_USER: |
| 130 | cpumode = PERF_RECORD_MISC_USER; break; |
| 131 | default: |
| 132 | break; |
| 133 | } |
| 134 | continue; |
| 135 | } |
| 136 | |
| 137 | thread__find_addr_location(thread, self, cpumode, |
| 138 | MAP__FUNCTION, ip, &al, NULL); |
| 139 | if (al.sym != NULL) { |
| 140 | if (sort__has_parent && !*parent && |
| 141 | symbol__match_parent_regex(al.sym)) |
| 142 | *parent = al.sym; |
| 143 | if (!self->use_callchain) |
| 144 | break; |
| 145 | syms[i] = al.sym; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | return syms; |
| 150 | } |