Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1 | #include "callchain.h" |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 2 | #include "debug.h" |
| 3 | #include "event.h" |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 4 | #include "evsel.h" |
| 5 | #include "hist.h" |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 6 | #include "machine.h" |
| 7 | #include "map.h" |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 8 | #include "sort.h" |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 9 | #include "strlist.h" |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 10 | #include "thread.h" |
Adrian Hunter | d027b64 | 2014-07-23 14:23:00 +0300 | [diff] [blame] | 11 | #include "vdso.h" |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 12 | #include <stdbool.h> |
Arnaldo Carvalho de Melo | c506c96 | 2013-12-11 09:15:00 -0300 | [diff] [blame] | 13 | #include <symbol/kallsyms.h> |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 14 | #include "unwind.h" |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 15 | #include "linux/hash.h" |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 16 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 17 | static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock); |
| 18 | |
Arnaldo Carvalho de Melo | e167f99 | 2014-10-14 15:07:48 -0300 | [diff] [blame] | 19 | static void dsos__init(struct dsos *dsos) |
| 20 | { |
| 21 | INIT_LIST_HEAD(&dsos->head); |
| 22 | dsos->root = RB_ROOT; |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 23 | pthread_rwlock_init(&dsos->lock, NULL); |
Arnaldo Carvalho de Melo | e167f99 | 2014-10-14 15:07:48 -0300 | [diff] [blame] | 24 | } |
| 25 | |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 26 | int machine__init(struct machine *machine, const char *root_dir, pid_t pid) |
| 27 | { |
Arnaldo Carvalho de Melo | 11246c7 | 2014-10-21 17:29:02 -0300 | [diff] [blame] | 28 | map_groups__init(&machine->kmaps, machine); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 29 | RB_CLEAR_NODE(&machine->rb_node); |
Arnaldo Carvalho de Melo | 3d39ac5 | 2015-05-28 13:06:42 -0300 | [diff] [blame] | 30 | dsos__init(&machine->dsos); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 31 | |
| 32 | machine->threads = RB_ROOT; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 33 | pthread_rwlock_init(&machine->threads_lock, NULL); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 34 | INIT_LIST_HEAD(&machine->dead_threads); |
| 35 | machine->last_match = NULL; |
| 36 | |
Adrian Hunter | d027b64 | 2014-07-23 14:23:00 +0300 | [diff] [blame] | 37 | machine->vdso_info = NULL; |
Arnaldo Carvalho de Melo | 4cde998 | 2015-09-09 12:25:00 -0300 | [diff] [blame] | 38 | machine->env = NULL; |
Adrian Hunter | d027b64 | 2014-07-23 14:23:00 +0300 | [diff] [blame] | 39 | |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 40 | machine->pid = pid; |
| 41 | |
Adrian Hunter | 611a5ce | 2013-08-08 14:32:20 +0300 | [diff] [blame] | 42 | machine->symbol_filter = NULL; |
Jiri Olsa | 14bd6d2 | 2014-01-07 13:47:19 +0100 | [diff] [blame] | 43 | machine->id_hdr_size = 0; |
Adrian Hunter | cfe1c41 | 2014-07-31 09:00:45 +0300 | [diff] [blame] | 44 | machine->comm_exec = false; |
Adrian Hunter | fbe2af4 | 2014-08-15 22:08:39 +0300 | [diff] [blame] | 45 | machine->kernel_start = 0; |
Adrian Hunter | 611a5ce | 2013-08-08 14:32:20 +0300 | [diff] [blame] | 46 | |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 47 | machine->root_dir = strdup(root_dir); |
| 48 | if (machine->root_dir == NULL) |
| 49 | return -ENOMEM; |
| 50 | |
| 51 | if (pid != HOST_KERNEL_ID) { |
Adrian Hunter | 1fcb876 | 2014-07-14 13:02:25 +0300 | [diff] [blame] | 52 | struct thread *thread = machine__findnew_thread(machine, -1, |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 53 | pid); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 54 | char comm[64]; |
| 55 | |
| 56 | if (thread == NULL) |
| 57 | return -ENOMEM; |
| 58 | |
| 59 | snprintf(comm, sizeof(comm), "[guest/%d]", pid); |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 60 | thread__set_comm(thread, comm, 0); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 61 | thread__put(thread); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 62 | } |
| 63 | |
Adrian Hunter | b9d266b | 2014-07-22 16:17:25 +0300 | [diff] [blame] | 64 | machine->current_tid = NULL; |
| 65 | |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 66 | return 0; |
| 67 | } |
| 68 | |
David Ahern | 8fb598e | 2013-09-28 13:13:00 -0600 | [diff] [blame] | 69 | struct machine *machine__new_host(void) |
| 70 | { |
| 71 | struct machine *machine = malloc(sizeof(*machine)); |
| 72 | |
| 73 | if (machine != NULL) { |
| 74 | machine__init(machine, "", HOST_KERNEL_ID); |
| 75 | |
| 76 | if (machine__create_kernel_maps(machine) < 0) |
| 77 | goto out_delete; |
| 78 | } |
| 79 | |
| 80 | return machine; |
| 81 | out_delete: |
| 82 | free(machine); |
| 83 | return NULL; |
| 84 | } |
| 85 | |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 86 | static void dsos__purge(struct dsos *dsos) |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 87 | { |
| 88 | struct dso *pos, *n; |
| 89 | |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 90 | pthread_rwlock_wrlock(&dsos->lock); |
| 91 | |
Waiman Long | 8fa7d87 | 2014-09-29 16:07:28 -0400 | [diff] [blame] | 92 | list_for_each_entry_safe(pos, n, &dsos->head, node) { |
Waiman Long | 4598a0a | 2014-09-30 13:36:15 -0400 | [diff] [blame] | 93 | RB_CLEAR_NODE(&pos->rb_node); |
Adrian Hunter | e266a75 | 2015-11-13 11:48:30 +0200 | [diff] [blame] | 94 | pos->root = NULL; |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 95 | list_del_init(&pos->node); |
| 96 | dso__put(pos); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 97 | } |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 98 | |
| 99 | pthread_rwlock_unlock(&dsos->lock); |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 100 | } |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 101 | |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 102 | static void dsos__exit(struct dsos *dsos) |
| 103 | { |
| 104 | dsos__purge(dsos); |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 105 | pthread_rwlock_destroy(&dsos->lock); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 106 | } |
| 107 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 108 | void machine__delete_threads(struct machine *machine) |
| 109 | { |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 110 | struct rb_node *nd; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 111 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 112 | pthread_rwlock_wrlock(&machine->threads_lock); |
| 113 | nd = rb_first(&machine->threads); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 114 | while (nd) { |
| 115 | struct thread *t = rb_entry(nd, struct thread, rb_node); |
| 116 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 117 | nd = rb_next(nd); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 118 | __machine__remove_thread(machine, t, false); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 119 | } |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 120 | pthread_rwlock_unlock(&machine->threads_lock); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 121 | } |
| 122 | |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 123 | void machine__exit(struct machine *machine) |
| 124 | { |
Masami Hiramatsu | ebe9729 | 2015-11-18 15:40:24 +0900 | [diff] [blame^] | 125 | machine__destroy_kernel_maps(machine); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 126 | map_groups__exit(&machine->kmaps); |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 127 | dsos__exit(&machine->dsos); |
Arnaldo Carvalho de Melo | 9a4388c | 2015-05-29 11:54:08 -0300 | [diff] [blame] | 128 | machine__exit_vdso(machine); |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 129 | zfree(&machine->root_dir); |
Adrian Hunter | b9d266b | 2014-07-22 16:17:25 +0300 | [diff] [blame] | 130 | zfree(&machine->current_tid); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 131 | pthread_rwlock_destroy(&machine->threads_lock); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void machine__delete(struct machine *machine) |
| 135 | { |
| 136 | machine__exit(machine); |
| 137 | free(machine); |
| 138 | } |
| 139 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 140 | void machines__init(struct machines *machines) |
| 141 | { |
| 142 | machine__init(&machines->host, "", HOST_KERNEL_ID); |
| 143 | machines->guests = RB_ROOT; |
Adrian Hunter | 611a5ce | 2013-08-08 14:32:20 +0300 | [diff] [blame] | 144 | machines->symbol_filter = NULL; |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void machines__exit(struct machines *machines) |
| 148 | { |
| 149 | machine__exit(&machines->host); |
| 150 | /* XXX exit guest */ |
| 151 | } |
| 152 | |
| 153 | struct machine *machines__add(struct machines *machines, pid_t pid, |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 154 | const char *root_dir) |
| 155 | { |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 156 | struct rb_node **p = &machines->guests.rb_node; |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 157 | struct rb_node *parent = NULL; |
| 158 | struct machine *pos, *machine = malloc(sizeof(*machine)); |
| 159 | |
| 160 | if (machine == NULL) |
| 161 | return NULL; |
| 162 | |
| 163 | if (machine__init(machine, root_dir, pid) != 0) { |
| 164 | free(machine); |
| 165 | return NULL; |
| 166 | } |
| 167 | |
Adrian Hunter | 611a5ce | 2013-08-08 14:32:20 +0300 | [diff] [blame] | 168 | machine->symbol_filter = machines->symbol_filter; |
| 169 | |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 170 | while (*p != NULL) { |
| 171 | parent = *p; |
| 172 | pos = rb_entry(parent, struct machine, rb_node); |
| 173 | if (pid < pos->pid) |
| 174 | p = &(*p)->rb_left; |
| 175 | else |
| 176 | p = &(*p)->rb_right; |
| 177 | } |
| 178 | |
| 179 | rb_link_node(&machine->rb_node, parent, p); |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 180 | rb_insert_color(&machine->rb_node, &machines->guests); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 181 | |
| 182 | return machine; |
| 183 | } |
| 184 | |
Adrian Hunter | 611a5ce | 2013-08-08 14:32:20 +0300 | [diff] [blame] | 185 | void machines__set_symbol_filter(struct machines *machines, |
| 186 | symbol_filter_t symbol_filter) |
| 187 | { |
| 188 | struct rb_node *nd; |
| 189 | |
| 190 | machines->symbol_filter = symbol_filter; |
| 191 | machines->host.symbol_filter = symbol_filter; |
| 192 | |
| 193 | for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) { |
| 194 | struct machine *machine = rb_entry(nd, struct machine, rb_node); |
| 195 | |
| 196 | machine->symbol_filter = symbol_filter; |
| 197 | } |
| 198 | } |
| 199 | |
Adrian Hunter | cfe1c41 | 2014-07-31 09:00:45 +0300 | [diff] [blame] | 200 | void machines__set_comm_exec(struct machines *machines, bool comm_exec) |
| 201 | { |
| 202 | struct rb_node *nd; |
| 203 | |
| 204 | machines->host.comm_exec = comm_exec; |
| 205 | |
| 206 | for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) { |
| 207 | struct machine *machine = rb_entry(nd, struct machine, rb_node); |
| 208 | |
| 209 | machine->comm_exec = comm_exec; |
| 210 | } |
| 211 | } |
| 212 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 213 | struct machine *machines__find(struct machines *machines, pid_t pid) |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 214 | { |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 215 | struct rb_node **p = &machines->guests.rb_node; |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 216 | struct rb_node *parent = NULL; |
| 217 | struct machine *machine; |
| 218 | struct machine *default_machine = NULL; |
| 219 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 220 | if (pid == HOST_KERNEL_ID) |
| 221 | return &machines->host; |
| 222 | |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 223 | while (*p != NULL) { |
| 224 | parent = *p; |
| 225 | machine = rb_entry(parent, struct machine, rb_node); |
| 226 | if (pid < machine->pid) |
| 227 | p = &(*p)->rb_left; |
| 228 | else if (pid > machine->pid) |
| 229 | p = &(*p)->rb_right; |
| 230 | else |
| 231 | return machine; |
| 232 | if (!machine->pid) |
| 233 | default_machine = machine; |
| 234 | } |
| 235 | |
| 236 | return default_machine; |
| 237 | } |
| 238 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 239 | struct machine *machines__findnew(struct machines *machines, pid_t pid) |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 240 | { |
| 241 | char path[PATH_MAX]; |
| 242 | const char *root_dir = ""; |
| 243 | struct machine *machine = machines__find(machines, pid); |
| 244 | |
| 245 | if (machine && (machine->pid == pid)) |
| 246 | goto out; |
| 247 | |
| 248 | if ((pid != HOST_KERNEL_ID) && |
| 249 | (pid != DEFAULT_GUEST_KERNEL_ID) && |
| 250 | (symbol_conf.guestmount)) { |
| 251 | sprintf(path, "%s/%d", symbol_conf.guestmount, pid); |
| 252 | if (access(path, R_OK)) { |
| 253 | static struct strlist *seen; |
| 254 | |
| 255 | if (!seen) |
Arnaldo Carvalho de Melo | 4a77e21 | 2015-07-20 12:13:34 -0300 | [diff] [blame] | 256 | seen = strlist__new(NULL, NULL); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 257 | |
| 258 | if (!strlist__has_entry(seen, path)) { |
| 259 | pr_err("Can't access file %s\n", path); |
| 260 | strlist__add(seen, path); |
| 261 | } |
| 262 | machine = NULL; |
| 263 | goto out; |
| 264 | } |
| 265 | root_dir = path; |
| 266 | } |
| 267 | |
| 268 | machine = machines__add(machines, pid, root_dir); |
| 269 | out: |
| 270 | return machine; |
| 271 | } |
| 272 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 273 | void machines__process_guests(struct machines *machines, |
| 274 | machine__process_t process, void *data) |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 275 | { |
| 276 | struct rb_node *nd; |
| 277 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 278 | for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 279 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
| 280 | process(pos, data); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | char *machine__mmap_name(struct machine *machine, char *bf, size_t size) |
| 285 | { |
| 286 | if (machine__is_host(machine)) |
| 287 | snprintf(bf, size, "[%s]", "kernel.kallsyms"); |
| 288 | else if (machine__is_default_guest(machine)) |
| 289 | snprintf(bf, size, "[%s]", "guest.kernel.kallsyms"); |
| 290 | else { |
| 291 | snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms", |
| 292 | machine->pid); |
| 293 | } |
| 294 | |
| 295 | return bf; |
| 296 | } |
| 297 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 298 | void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size) |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 299 | { |
| 300 | struct rb_node *node; |
| 301 | struct machine *machine; |
| 302 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 303 | machines->host.id_hdr_size = id_hdr_size; |
| 304 | |
| 305 | for (node = rb_first(&machines->guests); node; node = rb_next(node)) { |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 306 | machine = rb_entry(node, struct machine, rb_node); |
| 307 | machine->id_hdr_size = id_hdr_size; |
| 308 | } |
| 309 | |
| 310 | return; |
| 311 | } |
| 312 | |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 313 | static void machine__update_thread_pid(struct machine *machine, |
| 314 | struct thread *th, pid_t pid) |
| 315 | { |
| 316 | struct thread *leader; |
| 317 | |
| 318 | if (pid == th->pid_ || pid == -1 || th->pid_ != -1) |
| 319 | return; |
| 320 | |
| 321 | th->pid_ = pid; |
| 322 | |
| 323 | if (th->pid_ == th->tid) |
| 324 | return; |
| 325 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 326 | leader = __machine__findnew_thread(machine, th->pid_, th->pid_); |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 327 | if (!leader) |
| 328 | goto out_err; |
| 329 | |
| 330 | if (!leader->mg) |
Arnaldo Carvalho de Melo | 11246c7 | 2014-10-21 17:29:02 -0300 | [diff] [blame] | 331 | leader->mg = map_groups__new(machine); |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 332 | |
| 333 | if (!leader->mg) |
| 334 | goto out_err; |
| 335 | |
| 336 | if (th->mg == leader->mg) |
| 337 | return; |
| 338 | |
| 339 | if (th->mg) { |
| 340 | /* |
| 341 | * Maps are created from MMAP events which provide the pid and |
| 342 | * tid. Consequently there never should be any maps on a thread |
| 343 | * with an unknown pid. Just print an error if there are. |
| 344 | */ |
| 345 | if (!map_groups__empty(th->mg)) |
| 346 | pr_err("Discarding thread maps for %d:%d\n", |
| 347 | th->pid_, th->tid); |
Arnaldo Carvalho de Melo | 8e160b2 | 2015-05-19 20:07:14 -0300 | [diff] [blame] | 348 | map_groups__put(th->mg); |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | th->mg = map_groups__get(leader->mg); |
| 352 | |
| 353 | return; |
| 354 | |
| 355 | out_err: |
| 356 | pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid); |
| 357 | } |
| 358 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 359 | static struct thread *____machine__findnew_thread(struct machine *machine, |
| 360 | pid_t pid, pid_t tid, |
| 361 | bool create) |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 362 | { |
| 363 | struct rb_node **p = &machine->threads.rb_node; |
| 364 | struct rb_node *parent = NULL; |
| 365 | struct thread *th; |
| 366 | |
| 367 | /* |
Adrian Hunter | 3805123 | 2013-07-04 16:20:31 +0300 | [diff] [blame] | 368 | * Front-end cache - TID lookups come in blocks, |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 369 | * so most of the time we dont have to look up |
| 370 | * the full rbtree: |
| 371 | */ |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 372 | th = machine->last_match; |
Arnaldo Carvalho de Melo | f3b623b | 2015-03-02 22:21:35 -0300 | [diff] [blame] | 373 | if (th != NULL) { |
| 374 | if (th->tid == tid) { |
| 375 | machine__update_thread_pid(machine, th, pid); |
| 376 | return th; |
| 377 | } |
| 378 | |
Arnaldo Carvalho de Melo | 0ceb8f6 | 2015-05-11 18:08:12 -0300 | [diff] [blame] | 379 | machine->last_match = NULL; |
Adrian Hunter | 99d725f | 2013-08-26 16:00:19 +0300 | [diff] [blame] | 380 | } |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 381 | |
| 382 | while (*p != NULL) { |
| 383 | parent = *p; |
| 384 | th = rb_entry(parent, struct thread, rb_node); |
| 385 | |
Adrian Hunter | 3805123 | 2013-07-04 16:20:31 +0300 | [diff] [blame] | 386 | if (th->tid == tid) { |
Arnaldo Carvalho de Melo | 0ceb8f6 | 2015-05-11 18:08:12 -0300 | [diff] [blame] | 387 | machine->last_match = th; |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 388 | machine__update_thread_pid(machine, th, pid); |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 389 | return th; |
| 390 | } |
| 391 | |
Adrian Hunter | 3805123 | 2013-07-04 16:20:31 +0300 | [diff] [blame] | 392 | if (tid < th->tid) |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 393 | p = &(*p)->rb_left; |
| 394 | else |
| 395 | p = &(*p)->rb_right; |
| 396 | } |
| 397 | |
| 398 | if (!create) |
| 399 | return NULL; |
| 400 | |
Adrian Hunter | 99d725f | 2013-08-26 16:00:19 +0300 | [diff] [blame] | 401 | th = thread__new(pid, tid); |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 402 | if (th != NULL) { |
| 403 | rb_link_node(&th->rb_node, parent, p); |
| 404 | rb_insert_color(&th->rb_node, &machine->threads); |
Jiri Olsa | cddcef6 | 2014-04-09 20:54:29 +0200 | [diff] [blame] | 405 | |
| 406 | /* |
| 407 | * We have to initialize map_groups separately |
| 408 | * after rb tree is updated. |
| 409 | * |
| 410 | * The reason is that we call machine__findnew_thread |
| 411 | * within thread__init_map_groups to find the thread |
| 412 | * leader and that would screwed the rb tree. |
| 413 | */ |
Adrian Hunter | 418029b | 2014-07-16 10:19:44 +0300 | [diff] [blame] | 414 | if (thread__init_map_groups(th, machine)) { |
Arnaldo Carvalho de Melo | 0170b14 | 2015-05-25 15:23:05 -0300 | [diff] [blame] | 415 | rb_erase_init(&th->rb_node, &machine->threads); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 416 | RB_CLEAR_NODE(&th->rb_node); |
Adrian Hunter | 418029b | 2014-07-16 10:19:44 +0300 | [diff] [blame] | 417 | thread__delete(th); |
Jiri Olsa | cddcef6 | 2014-04-09 20:54:29 +0200 | [diff] [blame] | 418 | return NULL; |
Adrian Hunter | 418029b | 2014-07-16 10:19:44 +0300 | [diff] [blame] | 419 | } |
Arnaldo Carvalho de Melo | f3b623b | 2015-03-02 22:21:35 -0300 | [diff] [blame] | 420 | /* |
| 421 | * It is now in the rbtree, get a ref |
| 422 | */ |
| 423 | thread__get(th); |
Arnaldo Carvalho de Melo | 0ceb8f6 | 2015-05-11 18:08:12 -0300 | [diff] [blame] | 424 | machine->last_match = th; |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | return th; |
| 428 | } |
| 429 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 430 | struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid) |
| 431 | { |
| 432 | return ____machine__findnew_thread(machine, pid, tid, true); |
| 433 | } |
| 434 | |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 435 | struct thread *machine__findnew_thread(struct machine *machine, pid_t pid, |
| 436 | pid_t tid) |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 437 | { |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 438 | struct thread *th; |
| 439 | |
| 440 | pthread_rwlock_wrlock(&machine->threads_lock); |
| 441 | th = thread__get(__machine__findnew_thread(machine, pid, tid)); |
| 442 | pthread_rwlock_unlock(&machine->threads_lock); |
| 443 | return th; |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 444 | } |
| 445 | |
Jiri Olsa | d75e609 | 2014-03-14 15:00:03 +0100 | [diff] [blame] | 446 | struct thread *machine__find_thread(struct machine *machine, pid_t pid, |
| 447 | pid_t tid) |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 448 | { |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 449 | struct thread *th; |
| 450 | pthread_rwlock_rdlock(&machine->threads_lock); |
| 451 | th = thread__get(____machine__findnew_thread(machine, pid, tid, false)); |
| 452 | pthread_rwlock_unlock(&machine->threads_lock); |
| 453 | return th; |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 454 | } |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 455 | |
Adrian Hunter | cfe1c41 | 2014-07-31 09:00:45 +0300 | [diff] [blame] | 456 | struct comm *machine__thread_exec_comm(struct machine *machine, |
| 457 | struct thread *thread) |
| 458 | { |
| 459 | if (machine->comm_exec) |
| 460 | return thread__exec_comm(thread); |
| 461 | else |
| 462 | return thread__comm(thread); |
| 463 | } |
| 464 | |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 465 | int machine__process_comm_event(struct machine *machine, union perf_event *event, |
| 466 | struct perf_sample *sample) |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 467 | { |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 468 | struct thread *thread = machine__findnew_thread(machine, |
| 469 | event->comm.pid, |
| 470 | event->comm.tid); |
Adrian Hunter | 65de51f | 2014-07-31 09:00:44 +0300 | [diff] [blame] | 471 | bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 472 | int err = 0; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 473 | |
Adrian Hunter | cfe1c41 | 2014-07-31 09:00:45 +0300 | [diff] [blame] | 474 | if (exec) |
| 475 | machine->comm_exec = true; |
| 476 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 477 | if (dump_trace) |
| 478 | perf_event__fprintf_comm(event, stdout); |
| 479 | |
Adrian Hunter | 65de51f | 2014-07-31 09:00:44 +0300 | [diff] [blame] | 480 | if (thread == NULL || |
| 481 | __thread__set_comm(thread, event->comm.comm, sample->time, exec)) { |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 482 | dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n"); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 483 | err = -1; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 484 | } |
| 485 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 486 | thread__put(thread); |
| 487 | |
| 488 | return err; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | int machine__process_lost_event(struct machine *machine __maybe_unused, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 492 | union perf_event *event, struct perf_sample *sample __maybe_unused) |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 493 | { |
| 494 | dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n", |
| 495 | event->lost.id, event->lost.lost); |
| 496 | return 0; |
| 497 | } |
| 498 | |
Kan Liang | c4937a9 | 2015-05-10 15:13:15 -0400 | [diff] [blame] | 499 | int machine__process_lost_samples_event(struct machine *machine __maybe_unused, |
| 500 | union perf_event *event, struct perf_sample *sample) |
| 501 | { |
| 502 | dump_printf(": id:%" PRIu64 ": lost samples :%" PRIu64 "\n", |
| 503 | sample->id, event->lost_samples.lost); |
| 504 | return 0; |
| 505 | } |
| 506 | |
Arnaldo Carvalho de Melo | 9f2de31 | 2015-06-01 12:01:02 -0300 | [diff] [blame] | 507 | static struct dso *machine__findnew_module_dso(struct machine *machine, |
| 508 | struct kmod_path *m, |
| 509 | const char *filename) |
Jiri Olsa | da17ea3 | 2015-02-12 22:10:52 +0100 | [diff] [blame] | 510 | { |
| 511 | struct dso *dso; |
Jiri Olsa | da17ea3 | 2015-02-12 22:10:52 +0100 | [diff] [blame] | 512 | |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 513 | pthread_rwlock_wrlock(&machine->dsos.lock); |
| 514 | |
| 515 | dso = __dsos__find(&machine->dsos, m->name, true); |
Jiri Olsa | da17ea3 | 2015-02-12 22:10:52 +0100 | [diff] [blame] | 516 | if (!dso) { |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 517 | dso = __dsos__addnew(&machine->dsos, m->name); |
Jiri Olsa | da17ea3 | 2015-02-12 22:10:52 +0100 | [diff] [blame] | 518 | if (dso == NULL) |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 519 | goto out_unlock; |
Jiri Olsa | da17ea3 | 2015-02-12 22:10:52 +0100 | [diff] [blame] | 520 | |
| 521 | if (machine__is_host(machine)) |
| 522 | dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE; |
| 523 | else |
| 524 | dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE; |
| 525 | |
| 526 | /* _KMODULE_COMP should be next to _KMODULE */ |
Jiri Olsa | ca33380 | 2015-02-17 17:29:57 +0100 | [diff] [blame] | 527 | if (m->kmod && m->comp) |
Jiri Olsa | da17ea3 | 2015-02-12 22:10:52 +0100 | [diff] [blame] | 528 | dso->symtab_type++; |
Jiri Olsa | ca33380 | 2015-02-17 17:29:57 +0100 | [diff] [blame] | 529 | |
| 530 | dso__set_short_name(dso, strdup(m->name), true); |
| 531 | dso__set_long_name(dso, strdup(filename), true); |
Jiri Olsa | da17ea3 | 2015-02-12 22:10:52 +0100 | [diff] [blame] | 532 | } |
| 533 | |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 534 | dso__get(dso); |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 535 | out_unlock: |
| 536 | pthread_rwlock_unlock(&machine->dsos.lock); |
Jiri Olsa | da17ea3 | 2015-02-12 22:10:52 +0100 | [diff] [blame] | 537 | return dso; |
| 538 | } |
| 539 | |
Adrian Hunter | 4a96f7a | 2015-04-30 17:37:29 +0300 | [diff] [blame] | 540 | int machine__process_aux_event(struct machine *machine __maybe_unused, |
| 541 | union perf_event *event) |
| 542 | { |
| 543 | if (dump_trace) |
| 544 | perf_event__fprintf_aux(event, stdout); |
| 545 | return 0; |
| 546 | } |
| 547 | |
Adrian Hunter | 0ad21f6 | 2015-04-30 17:37:30 +0300 | [diff] [blame] | 548 | int machine__process_itrace_start_event(struct machine *machine __maybe_unused, |
| 549 | union perf_event *event) |
| 550 | { |
| 551 | if (dump_trace) |
| 552 | perf_event__fprintf_itrace_start(event, stdout); |
| 553 | return 0; |
| 554 | } |
| 555 | |
Adrian Hunter | 0286039 | 2015-07-21 12:44:03 +0300 | [diff] [blame] | 556 | int machine__process_switch_event(struct machine *machine __maybe_unused, |
| 557 | union perf_event *event) |
| 558 | { |
| 559 | if (dump_trace) |
| 560 | perf_event__fprintf_switch(event, stdout); |
| 561 | return 0; |
| 562 | } |
| 563 | |
Arnaldo Carvalho de Melo | 9f2de31 | 2015-06-01 12:01:02 -0300 | [diff] [blame] | 564 | struct map *machine__findnew_module_map(struct machine *machine, u64 start, |
| 565 | const char *filename) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 566 | { |
Jiri Olsa | ca33380 | 2015-02-17 17:29:57 +0100 | [diff] [blame] | 567 | struct map *map = NULL; |
| 568 | struct dso *dso; |
| 569 | struct kmod_path m; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 570 | |
Jiri Olsa | ca33380 | 2015-02-17 17:29:57 +0100 | [diff] [blame] | 571 | if (kmod_path__parse_name(&m, filename)) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 572 | return NULL; |
| 573 | |
Jiri Olsa | bc84f46 | 2015-02-17 17:31:18 +0100 | [diff] [blame] | 574 | map = map_groups__find_by_name(&machine->kmaps, MAP__FUNCTION, |
| 575 | m.name); |
| 576 | if (map) |
| 577 | goto out; |
| 578 | |
Arnaldo Carvalho de Melo | 9f2de31 | 2015-06-01 12:01:02 -0300 | [diff] [blame] | 579 | dso = machine__findnew_module_dso(machine, &m, filename); |
Jiri Olsa | ca33380 | 2015-02-17 17:29:57 +0100 | [diff] [blame] | 580 | if (dso == NULL) |
| 581 | goto out; |
| 582 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 583 | map = map__new2(start, dso, MAP__FUNCTION); |
| 584 | if (map == NULL) |
Jiri Olsa | ca33380 | 2015-02-17 17:29:57 +0100 | [diff] [blame] | 585 | goto out; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 586 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 587 | map_groups__insert(&machine->kmaps, map); |
Jiri Olsa | ca33380 | 2015-02-17 17:29:57 +0100 | [diff] [blame] | 588 | |
Masami Hiramatsu | 9afcb420 | 2015-11-18 15:40:20 +0900 | [diff] [blame] | 589 | /* Put the map here because map_groups__insert alread got it */ |
| 590 | map__put(map); |
Jiri Olsa | ca33380 | 2015-02-17 17:29:57 +0100 | [diff] [blame] | 591 | out: |
| 592 | free(m.name); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 593 | return map; |
| 594 | } |
| 595 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 596 | size_t machines__fprintf_dsos(struct machines *machines, FILE *fp) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 597 | { |
| 598 | struct rb_node *nd; |
Arnaldo Carvalho de Melo | 3d39ac5 | 2015-05-28 13:06:42 -0300 | [diff] [blame] | 599 | size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 600 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 601 | for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 602 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
Arnaldo Carvalho de Melo | 3d39ac5 | 2015-05-28 13:06:42 -0300 | [diff] [blame] | 603 | ret += __dsos__fprintf(&pos->dsos.head, fp); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | return ret; |
| 607 | } |
| 608 | |
Waiman Long | 8fa7d87 | 2014-09-29 16:07:28 -0400 | [diff] [blame] | 609 | size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp, |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 610 | bool (skip)(struct dso *dso, int parm), int parm) |
| 611 | { |
Arnaldo Carvalho de Melo | 3d39ac5 | 2015-05-28 13:06:42 -0300 | [diff] [blame] | 612 | return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 613 | } |
| 614 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 615 | size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp, |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 616 | bool (skip)(struct dso *dso, int parm), int parm) |
| 617 | { |
| 618 | struct rb_node *nd; |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 619 | size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 620 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 621 | for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 622 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
| 623 | ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm); |
| 624 | } |
| 625 | return ret; |
| 626 | } |
| 627 | |
| 628 | size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp) |
| 629 | { |
| 630 | int i; |
| 631 | size_t printed = 0; |
Arnaldo Carvalho de Melo | a5e813c | 2015-09-30 11:54:04 -0300 | [diff] [blame] | 632 | struct dso *kdso = machine__kernel_map(machine)->dso; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 633 | |
| 634 | if (kdso->has_build_id) { |
| 635 | char filename[PATH_MAX]; |
| 636 | if (dso__build_id_filename(kdso, filename, sizeof(filename))) |
| 637 | printed += fprintf(fp, "[0] %s\n", filename); |
| 638 | } |
| 639 | |
| 640 | for (i = 0; i < vmlinux_path__nr_entries; ++i) |
| 641 | printed += fprintf(fp, "[%d] %s\n", |
| 642 | i + kdso->has_build_id, vmlinux_path[i]); |
| 643 | |
| 644 | return printed; |
| 645 | } |
| 646 | |
| 647 | size_t machine__fprintf(struct machine *machine, FILE *fp) |
| 648 | { |
| 649 | size_t ret = 0; |
| 650 | struct rb_node *nd; |
| 651 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 652 | pthread_rwlock_rdlock(&machine->threads_lock); |
| 653 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 654 | for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) { |
| 655 | struct thread *pos = rb_entry(nd, struct thread, rb_node); |
| 656 | |
| 657 | ret += thread__fprintf(pos, fp); |
| 658 | } |
| 659 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 660 | pthread_rwlock_unlock(&machine->threads_lock); |
| 661 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 662 | return ret; |
| 663 | } |
| 664 | |
| 665 | static struct dso *machine__get_kernel(struct machine *machine) |
| 666 | { |
| 667 | const char *vmlinux_name = NULL; |
| 668 | struct dso *kernel; |
| 669 | |
| 670 | if (machine__is_host(machine)) { |
| 671 | vmlinux_name = symbol_conf.vmlinux_name; |
| 672 | if (!vmlinux_name) |
| 673 | vmlinux_name = "[kernel.kallsyms]"; |
| 674 | |
Arnaldo Carvalho de Melo | 459ce51 | 2015-05-28 12:40:55 -0300 | [diff] [blame] | 675 | kernel = machine__findnew_kernel(machine, vmlinux_name, |
| 676 | "[kernel]", DSO_TYPE_KERNEL); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 677 | } else { |
| 678 | char bf[PATH_MAX]; |
| 679 | |
| 680 | if (machine__is_default_guest(machine)) |
| 681 | vmlinux_name = symbol_conf.default_guest_vmlinux_name; |
| 682 | if (!vmlinux_name) |
| 683 | vmlinux_name = machine__mmap_name(machine, bf, |
| 684 | sizeof(bf)); |
| 685 | |
Arnaldo Carvalho de Melo | 459ce51 | 2015-05-28 12:40:55 -0300 | [diff] [blame] | 686 | kernel = machine__findnew_kernel(machine, vmlinux_name, |
| 687 | "[guest.kernel]", |
| 688 | DSO_TYPE_GUEST_KERNEL); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | if (kernel != NULL && (!kernel->has_build_id)) |
| 692 | dso__read_running_kernel_build_id(kernel, machine); |
| 693 | |
| 694 | return kernel; |
| 695 | } |
| 696 | |
| 697 | struct process_args { |
| 698 | u64 start; |
| 699 | }; |
| 700 | |
Adrian Hunter | 15a0a87 | 2014-01-29 16:14:38 +0200 | [diff] [blame] | 701 | static void machine__get_kallsyms_filename(struct machine *machine, char *buf, |
| 702 | size_t bufsz) |
| 703 | { |
| 704 | if (machine__is_default_guest(machine)) |
| 705 | scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms); |
| 706 | else |
| 707 | scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir); |
| 708 | } |
| 709 | |
Simon Que | a93f0e5 | 2014-06-16 11:32:09 -0700 | [diff] [blame] | 710 | const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL}; |
| 711 | |
| 712 | /* Figure out the start address of kernel map from /proc/kallsyms. |
| 713 | * Returns the name of the start symbol in *symbol_name. Pass in NULL as |
| 714 | * symbol_name if it's not that important. |
| 715 | */ |
Adrian Hunter | 4b99375 | 2014-08-15 22:08:38 +0300 | [diff] [blame] | 716 | static u64 machine__get_running_kernel_start(struct machine *machine, |
| 717 | const char **symbol_name) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 718 | { |
Adrian Hunter | 15a0a87 | 2014-01-29 16:14:38 +0200 | [diff] [blame] | 719 | char filename[PATH_MAX]; |
Simon Que | a93f0e5 | 2014-06-16 11:32:09 -0700 | [diff] [blame] | 720 | int i; |
| 721 | const char *name; |
| 722 | u64 addr = 0; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 723 | |
Adrian Hunter | 15a0a87 | 2014-01-29 16:14:38 +0200 | [diff] [blame] | 724 | machine__get_kallsyms_filename(machine, filename, PATH_MAX); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 725 | |
| 726 | if (symbol__restricted_filename(filename, "/proc/kallsyms")) |
| 727 | return 0; |
| 728 | |
Simon Que | a93f0e5 | 2014-06-16 11:32:09 -0700 | [diff] [blame] | 729 | for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) { |
| 730 | addr = kallsyms__get_function_start(filename, name); |
| 731 | if (addr) |
| 732 | break; |
| 733 | } |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 734 | |
Simon Que | a93f0e5 | 2014-06-16 11:32:09 -0700 | [diff] [blame] | 735 | if (symbol_name) |
| 736 | *symbol_name = name; |
| 737 | |
| 738 | return addr; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel) |
| 742 | { |
| 743 | enum map_type type; |
Adrian Hunter | 4b99375 | 2014-08-15 22:08:38 +0300 | [diff] [blame] | 744 | u64 start = machine__get_running_kernel_start(machine, NULL); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 745 | |
| 746 | for (type = 0; type < MAP__NR_TYPES; ++type) { |
| 747 | struct kmap *kmap; |
Arnaldo Carvalho de Melo | 77e6597 | 2015-09-30 11:08:58 -0300 | [diff] [blame] | 748 | struct map *map; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 749 | |
| 750 | machine->vmlinux_maps[type] = map__new2(start, kernel, type); |
| 751 | if (machine->vmlinux_maps[type] == NULL) |
| 752 | return -1; |
| 753 | |
| 754 | machine->vmlinux_maps[type]->map_ip = |
| 755 | machine->vmlinux_maps[type]->unmap_ip = |
| 756 | identity__map_ip; |
Arnaldo Carvalho de Melo | a5e813c | 2015-09-30 11:54:04 -0300 | [diff] [blame] | 757 | map = __machine__kernel_map(machine, type); |
Arnaldo Carvalho de Melo | 77e6597 | 2015-09-30 11:08:58 -0300 | [diff] [blame] | 758 | kmap = map__kmap(map); |
Wang Nan | ba92732 | 2015-04-07 08:22:45 +0000 | [diff] [blame] | 759 | if (!kmap) |
| 760 | return -1; |
| 761 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 762 | kmap->kmaps = &machine->kmaps; |
Arnaldo Carvalho de Melo | 77e6597 | 2015-09-30 11:08:58 -0300 | [diff] [blame] | 763 | map_groups__insert(&machine->kmaps, map); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | return 0; |
| 767 | } |
| 768 | |
| 769 | void machine__destroy_kernel_maps(struct machine *machine) |
| 770 | { |
| 771 | enum map_type type; |
| 772 | |
| 773 | for (type = 0; type < MAP__NR_TYPES; ++type) { |
| 774 | struct kmap *kmap; |
Arnaldo Carvalho de Melo | a5e813c | 2015-09-30 11:54:04 -0300 | [diff] [blame] | 775 | struct map *map = __machine__kernel_map(machine, type); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 776 | |
Arnaldo Carvalho de Melo | 77e6597 | 2015-09-30 11:08:58 -0300 | [diff] [blame] | 777 | if (map == NULL) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 778 | continue; |
| 779 | |
Arnaldo Carvalho de Melo | 77e6597 | 2015-09-30 11:08:58 -0300 | [diff] [blame] | 780 | kmap = map__kmap(map); |
| 781 | map_groups__remove(&machine->kmaps, map); |
Wang Nan | ba92732 | 2015-04-07 08:22:45 +0000 | [diff] [blame] | 782 | if (kmap && kmap->ref_reloc_sym) { |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 783 | /* |
| 784 | * ref_reloc_sym is shared among all maps, so free just |
| 785 | * on one of them. |
| 786 | */ |
| 787 | if (type == MAP__FUNCTION) { |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 788 | zfree((char **)&kmap->ref_reloc_sym->name); |
| 789 | zfree(&kmap->ref_reloc_sym); |
| 790 | } else |
| 791 | kmap->ref_reloc_sym = NULL; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 792 | } |
| 793 | |
Masami Hiramatsu | e96e407 | 2015-11-18 15:40:22 +0900 | [diff] [blame] | 794 | map__put(machine->vmlinux_maps[type]); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 795 | machine->vmlinux_maps[type] = NULL; |
| 796 | } |
| 797 | } |
| 798 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 799 | int machines__create_guest_kernel_maps(struct machines *machines) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 800 | { |
| 801 | int ret = 0; |
| 802 | struct dirent **namelist = NULL; |
| 803 | int i, items = 0; |
| 804 | char path[PATH_MAX]; |
| 805 | pid_t pid; |
| 806 | char *endp; |
| 807 | |
| 808 | if (symbol_conf.default_guest_vmlinux_name || |
| 809 | symbol_conf.default_guest_modules || |
| 810 | symbol_conf.default_guest_kallsyms) { |
| 811 | machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID); |
| 812 | } |
| 813 | |
| 814 | if (symbol_conf.guestmount) { |
| 815 | items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL); |
| 816 | if (items <= 0) |
| 817 | return -ENOENT; |
| 818 | for (i = 0; i < items; i++) { |
| 819 | if (!isdigit(namelist[i]->d_name[0])) { |
| 820 | /* Filter out . and .. */ |
| 821 | continue; |
| 822 | } |
| 823 | pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10); |
| 824 | if ((*endp != '\0') || |
| 825 | (endp == namelist[i]->d_name) || |
| 826 | (errno == ERANGE)) { |
| 827 | pr_debug("invalid directory (%s). Skipping.\n", |
| 828 | namelist[i]->d_name); |
| 829 | continue; |
| 830 | } |
| 831 | sprintf(path, "%s/%s/proc/kallsyms", |
| 832 | symbol_conf.guestmount, |
| 833 | namelist[i]->d_name); |
| 834 | ret = access(path, R_OK); |
| 835 | if (ret) { |
| 836 | pr_debug("Can't access file %s\n", path); |
| 837 | goto failure; |
| 838 | } |
| 839 | machines__create_kernel_maps(machines, pid); |
| 840 | } |
| 841 | failure: |
| 842 | free(namelist); |
| 843 | } |
| 844 | |
| 845 | return ret; |
| 846 | } |
| 847 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 848 | void machines__destroy_kernel_maps(struct machines *machines) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 849 | { |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 850 | struct rb_node *next = rb_first(&machines->guests); |
| 851 | |
| 852 | machine__destroy_kernel_maps(&machines->host); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 853 | |
| 854 | while (next) { |
| 855 | struct machine *pos = rb_entry(next, struct machine, rb_node); |
| 856 | |
| 857 | next = rb_next(&pos->rb_node); |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 858 | rb_erase(&pos->rb_node, &machines->guests); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 859 | machine__delete(pos); |
| 860 | } |
| 861 | } |
| 862 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 863 | int machines__create_kernel_maps(struct machines *machines, pid_t pid) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 864 | { |
| 865 | struct machine *machine = machines__findnew(machines, pid); |
| 866 | |
| 867 | if (machine == NULL) |
| 868 | return -1; |
| 869 | |
| 870 | return machine__create_kernel_maps(machine); |
| 871 | } |
| 872 | |
| 873 | int machine__load_kallsyms(struct machine *machine, const char *filename, |
| 874 | enum map_type type, symbol_filter_t filter) |
| 875 | { |
Arnaldo Carvalho de Melo | a5e813c | 2015-09-30 11:54:04 -0300 | [diff] [blame] | 876 | struct map *map = machine__kernel_map(machine); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 877 | int ret = dso__load_kallsyms(map->dso, filename, map, filter); |
| 878 | |
| 879 | if (ret > 0) { |
| 880 | dso__set_loaded(map->dso, type); |
| 881 | /* |
| 882 | * Since /proc/kallsyms will have multiple sessions for the |
| 883 | * kernel, with modules between them, fixup the end of all |
| 884 | * sections. |
| 885 | */ |
| 886 | __map_groups__fixup_end(&machine->kmaps, type); |
| 887 | } |
| 888 | |
| 889 | return ret; |
| 890 | } |
| 891 | |
| 892 | int machine__load_vmlinux_path(struct machine *machine, enum map_type type, |
| 893 | symbol_filter_t filter) |
| 894 | { |
Arnaldo Carvalho de Melo | a5e813c | 2015-09-30 11:54:04 -0300 | [diff] [blame] | 895 | struct map *map = machine__kernel_map(machine); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 896 | int ret = dso__load_vmlinux_path(map->dso, map, filter); |
| 897 | |
Adrian Hunter | 39b12f78 | 2013-08-07 14:38:47 +0300 | [diff] [blame] | 898 | if (ret > 0) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 899 | dso__set_loaded(map->dso, type); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 900 | |
| 901 | return ret; |
| 902 | } |
| 903 | |
| 904 | static void map_groups__fixup_end(struct map_groups *mg) |
| 905 | { |
| 906 | int i; |
| 907 | for (i = 0; i < MAP__NR_TYPES; ++i) |
| 908 | __map_groups__fixup_end(mg, i); |
| 909 | } |
| 910 | |
| 911 | static char *get_kernel_version(const char *root_dir) |
| 912 | { |
| 913 | char version[PATH_MAX]; |
| 914 | FILE *file; |
| 915 | char *name, *tmp; |
| 916 | const char *prefix = "Linux version "; |
| 917 | |
| 918 | sprintf(version, "%s/proc/version", root_dir); |
| 919 | file = fopen(version, "r"); |
| 920 | if (!file) |
| 921 | return NULL; |
| 922 | |
| 923 | version[0] = '\0'; |
| 924 | tmp = fgets(version, sizeof(version), file); |
| 925 | fclose(file); |
| 926 | |
| 927 | name = strstr(version, prefix); |
| 928 | if (!name) |
| 929 | return NULL; |
| 930 | name += strlen(prefix); |
| 931 | tmp = strchr(name, ' '); |
| 932 | if (tmp) |
| 933 | *tmp = '\0'; |
| 934 | |
| 935 | return strdup(name); |
| 936 | } |
| 937 | |
Jiri Olsa | bb58a8a | 2015-02-12 22:20:01 +0100 | [diff] [blame] | 938 | static bool is_kmod_dso(struct dso *dso) |
| 939 | { |
| 940 | return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE || |
| 941 | dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE; |
| 942 | } |
| 943 | |
| 944 | static int map_groups__set_module_path(struct map_groups *mg, const char *path, |
| 945 | struct kmod_path *m) |
| 946 | { |
| 947 | struct map *map; |
| 948 | char *long_name; |
| 949 | |
| 950 | map = map_groups__find_by_name(mg, MAP__FUNCTION, m->name); |
| 951 | if (map == NULL) |
| 952 | return 0; |
| 953 | |
| 954 | long_name = strdup(path); |
| 955 | if (long_name == NULL) |
| 956 | return -ENOMEM; |
| 957 | |
| 958 | dso__set_long_name(map->dso, long_name, true); |
| 959 | dso__kernel_module_get_build_id(map->dso, ""); |
| 960 | |
| 961 | /* |
| 962 | * Full name could reveal us kmod compression, so |
| 963 | * we need to update the symtab_type if needed. |
| 964 | */ |
| 965 | if (m->comp && is_kmod_dso(map->dso)) |
| 966 | map->dso->symtab_type++; |
| 967 | |
| 968 | return 0; |
| 969 | } |
| 970 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 971 | static int map_groups__set_modules_path_dir(struct map_groups *mg, |
Richard Yao | 61d4290 | 2014-04-26 13:17:55 -0400 | [diff] [blame] | 972 | const char *dir_name, int depth) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 973 | { |
| 974 | struct dirent *dent; |
| 975 | DIR *dir = opendir(dir_name); |
| 976 | int ret = 0; |
| 977 | |
| 978 | if (!dir) { |
| 979 | pr_debug("%s: cannot open %s dir\n", __func__, dir_name); |
| 980 | return -1; |
| 981 | } |
| 982 | |
| 983 | while ((dent = readdir(dir)) != NULL) { |
| 984 | char path[PATH_MAX]; |
| 985 | struct stat st; |
| 986 | |
| 987 | /*sshfs might return bad dent->d_type, so we have to stat*/ |
| 988 | snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name); |
| 989 | if (stat(path, &st)) |
| 990 | continue; |
| 991 | |
| 992 | if (S_ISDIR(st.st_mode)) { |
| 993 | if (!strcmp(dent->d_name, ".") || |
| 994 | !strcmp(dent->d_name, "..")) |
| 995 | continue; |
| 996 | |
Richard Yao | 61d4290 | 2014-04-26 13:17:55 -0400 | [diff] [blame] | 997 | /* Do not follow top-level source and build symlinks */ |
| 998 | if (depth == 0) { |
| 999 | if (!strcmp(dent->d_name, "source") || |
| 1000 | !strcmp(dent->d_name, "build")) |
| 1001 | continue; |
| 1002 | } |
| 1003 | |
| 1004 | ret = map_groups__set_modules_path_dir(mg, path, |
| 1005 | depth + 1); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1006 | if (ret < 0) |
| 1007 | goto out; |
| 1008 | } else { |
Jiri Olsa | bb58a8a | 2015-02-12 22:20:01 +0100 | [diff] [blame] | 1009 | struct kmod_path m; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1010 | |
Jiri Olsa | bb58a8a | 2015-02-12 22:20:01 +0100 | [diff] [blame] | 1011 | ret = kmod_path__parse_name(&m, dent->d_name); |
| 1012 | if (ret) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1013 | goto out; |
Jiri Olsa | bb58a8a | 2015-02-12 22:20:01 +0100 | [diff] [blame] | 1014 | |
| 1015 | if (m.kmod) |
| 1016 | ret = map_groups__set_module_path(mg, path, &m); |
| 1017 | |
| 1018 | free(m.name); |
| 1019 | |
| 1020 | if (ret) |
| 1021 | goto out; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | out: |
| 1026 | closedir(dir); |
| 1027 | return ret; |
| 1028 | } |
| 1029 | |
| 1030 | static int machine__set_modules_path(struct machine *machine) |
| 1031 | { |
| 1032 | char *version; |
| 1033 | char modules_path[PATH_MAX]; |
| 1034 | |
| 1035 | version = get_kernel_version(machine->root_dir); |
| 1036 | if (!version) |
| 1037 | return -1; |
| 1038 | |
Richard Yao | 61d4290 | 2014-04-26 13:17:55 -0400 | [diff] [blame] | 1039 | snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s", |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1040 | machine->root_dir, version); |
| 1041 | free(version); |
| 1042 | |
Richard Yao | 61d4290 | 2014-04-26 13:17:55 -0400 | [diff] [blame] | 1043 | return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1044 | } |
| 1045 | |
Adrian Hunter | 316d70d | 2013-10-08 11:45:48 +0300 | [diff] [blame] | 1046 | static int machine__create_module(void *arg, const char *name, u64 start) |
| 1047 | { |
| 1048 | struct machine *machine = arg; |
| 1049 | struct map *map; |
| 1050 | |
Arnaldo Carvalho de Melo | 9f2de31 | 2015-06-01 12:01:02 -0300 | [diff] [blame] | 1051 | map = machine__findnew_module_map(machine, start, name); |
Adrian Hunter | 316d70d | 2013-10-08 11:45:48 +0300 | [diff] [blame] | 1052 | if (map == NULL) |
| 1053 | return -1; |
| 1054 | |
| 1055 | dso__kernel_module_get_build_id(map->dso, machine->root_dir); |
| 1056 | |
| 1057 | return 0; |
| 1058 | } |
| 1059 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1060 | static int machine__create_modules(struct machine *machine) |
| 1061 | { |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1062 | const char *modules; |
| 1063 | char path[PATH_MAX]; |
| 1064 | |
Adrian Hunter | f4be904 | 2013-09-22 13:22:09 +0300 | [diff] [blame] | 1065 | if (machine__is_default_guest(machine)) { |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1066 | modules = symbol_conf.default_guest_modules; |
Adrian Hunter | f4be904 | 2013-09-22 13:22:09 +0300 | [diff] [blame] | 1067 | } else { |
| 1068 | snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1069 | modules = path; |
| 1070 | } |
| 1071 | |
Adrian Hunter | aa7fe3b | 2013-09-22 13:22:09 +0300 | [diff] [blame] | 1072 | if (symbol__restricted_filename(modules, "/proc/modules")) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1073 | return -1; |
| 1074 | |
Adrian Hunter | 316d70d | 2013-10-08 11:45:48 +0300 | [diff] [blame] | 1075 | if (modules__parse(modules, machine, machine__create_module)) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1076 | return -1; |
| 1077 | |
Adrian Hunter | 316d70d | 2013-10-08 11:45:48 +0300 | [diff] [blame] | 1078 | if (!machine__set_modules_path(machine)) |
| 1079 | return 0; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1080 | |
Adrian Hunter | 316d70d | 2013-10-08 11:45:48 +0300 | [diff] [blame] | 1081 | pr_debug("Problems setting modules path maps, continuing anyway...\n"); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1082 | |
Jason Wessel | 8f76fcd | 2013-07-15 15:27:53 -0500 | [diff] [blame] | 1083 | return 0; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | int machine__create_kernel_maps(struct machine *machine) |
| 1087 | { |
| 1088 | struct dso *kernel = machine__get_kernel(machine); |
Adrian Hunter | 5512cf2 | 2014-01-29 16:14:39 +0200 | [diff] [blame] | 1089 | const char *name; |
Adrian Hunter | 4b99375 | 2014-08-15 22:08:38 +0300 | [diff] [blame] | 1090 | u64 addr = machine__get_running_kernel_start(machine, &name); |
Adrian Hunter | 5512cf2 | 2014-01-29 16:14:39 +0200 | [diff] [blame] | 1091 | if (!addr) |
| 1092 | return -1; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1093 | |
| 1094 | if (kernel == NULL || |
| 1095 | __machine__create_kernel_maps(machine, kernel) < 0) |
| 1096 | return -1; |
| 1097 | |
| 1098 | if (symbol_conf.use_modules && machine__create_modules(machine) < 0) { |
| 1099 | if (machine__is_host(machine)) |
| 1100 | pr_debug("Problems creating module maps, " |
| 1101 | "continuing anyway...\n"); |
| 1102 | else |
| 1103 | pr_debug("Problems creating module maps for guest %d, " |
| 1104 | "continuing anyway...\n", machine->pid); |
| 1105 | } |
| 1106 | |
| 1107 | /* |
| 1108 | * Now that we have all the maps created, just set the ->end of them: |
| 1109 | */ |
| 1110 | map_groups__fixup_end(&machine->kmaps); |
Adrian Hunter | 5512cf2 | 2014-01-29 16:14:39 +0200 | [diff] [blame] | 1111 | |
| 1112 | if (maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, name, |
| 1113 | addr)) { |
| 1114 | machine__destroy_kernel_maps(machine); |
| 1115 | return -1; |
| 1116 | } |
| 1117 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1118 | return 0; |
| 1119 | } |
| 1120 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1121 | static void machine__set_kernel_mmap_len(struct machine *machine, |
| 1122 | union perf_event *event) |
| 1123 | { |
Namhyung Kim | 4552cf0 | 2012-11-07 16:27:10 +0900 | [diff] [blame] | 1124 | int i; |
| 1125 | |
| 1126 | for (i = 0; i < MAP__NR_TYPES; i++) { |
| 1127 | machine->vmlinux_maps[i]->start = event->mmap.start; |
| 1128 | machine->vmlinux_maps[i]->end = (event->mmap.start + |
| 1129 | event->mmap.len); |
| 1130 | /* |
| 1131 | * Be a bit paranoid here, some perf.data file came with |
| 1132 | * a zero sized synthesized MMAP event for the kernel. |
| 1133 | */ |
| 1134 | if (machine->vmlinux_maps[i]->end == 0) |
| 1135 | machine->vmlinux_maps[i]->end = ~0ULL; |
| 1136 | } |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1137 | } |
| 1138 | |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1139 | static bool machine__uses_kcore(struct machine *machine) |
| 1140 | { |
| 1141 | struct dso *dso; |
| 1142 | |
Arnaldo Carvalho de Melo | 3d39ac5 | 2015-05-28 13:06:42 -0300 | [diff] [blame] | 1143 | list_for_each_entry(dso, &machine->dsos.head, node) { |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1144 | if (dso__is_kcore(dso)) |
| 1145 | return true; |
| 1146 | } |
| 1147 | |
| 1148 | return false; |
| 1149 | } |
| 1150 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1151 | static int machine__process_kernel_mmap_event(struct machine *machine, |
| 1152 | union perf_event *event) |
| 1153 | { |
| 1154 | struct map *map; |
| 1155 | char kmmap_prefix[PATH_MAX]; |
| 1156 | enum dso_kernel_type kernel_type; |
| 1157 | bool is_kernel_mmap; |
| 1158 | |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1159 | /* If we have maps from kcore then we do not need or want any others */ |
| 1160 | if (machine__uses_kcore(machine)) |
| 1161 | return 0; |
| 1162 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1163 | machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix)); |
| 1164 | if (machine__is_host(machine)) |
| 1165 | kernel_type = DSO_TYPE_KERNEL; |
| 1166 | else |
| 1167 | kernel_type = DSO_TYPE_GUEST_KERNEL; |
| 1168 | |
| 1169 | is_kernel_mmap = memcmp(event->mmap.filename, |
| 1170 | kmmap_prefix, |
| 1171 | strlen(kmmap_prefix) - 1) == 0; |
| 1172 | if (event->mmap.filename[0] == '/' || |
| 1173 | (!is_kernel_mmap && event->mmap.filename[0] == '[')) { |
Arnaldo Carvalho de Melo | 9f2de31 | 2015-06-01 12:01:02 -0300 | [diff] [blame] | 1174 | map = machine__findnew_module_map(machine, event->mmap.start, |
| 1175 | event->mmap.filename); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1176 | if (map == NULL) |
| 1177 | goto out_problem; |
| 1178 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1179 | map->end = map->start + event->mmap.len; |
| 1180 | } else if (is_kernel_mmap) { |
| 1181 | const char *symbol_name = (event->mmap.filename + |
| 1182 | strlen(kmmap_prefix)); |
| 1183 | /* |
| 1184 | * Should be there already, from the build-id table in |
| 1185 | * the header. |
| 1186 | */ |
Namhyung Kim | b837a8b | 2014-11-04 10:14:33 +0900 | [diff] [blame] | 1187 | struct dso *kernel = NULL; |
| 1188 | struct dso *dso; |
| 1189 | |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 1190 | pthread_rwlock_rdlock(&machine->dsos.lock); |
| 1191 | |
Arnaldo Carvalho de Melo | 3d39ac5 | 2015-05-28 13:06:42 -0300 | [diff] [blame] | 1192 | list_for_each_entry(dso, &machine->dsos.head, node) { |
Wang Nan | 1f121b0 | 2015-06-03 08:52:21 +0000 | [diff] [blame] | 1193 | |
| 1194 | /* |
| 1195 | * The cpumode passed to is_kernel_module is not the |
| 1196 | * cpumode of *this* event. If we insist on passing |
| 1197 | * correct cpumode to is_kernel_module, we should |
| 1198 | * record the cpumode when we adding this dso to the |
| 1199 | * linked list. |
| 1200 | * |
| 1201 | * However we don't really need passing correct |
| 1202 | * cpumode. We know the correct cpumode must be kernel |
| 1203 | * mode (if not, we should not link it onto kernel_dsos |
| 1204 | * list). |
| 1205 | * |
| 1206 | * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN. |
| 1207 | * is_kernel_module() treats it as a kernel cpumode. |
| 1208 | */ |
| 1209 | |
| 1210 | if (!dso->kernel || |
| 1211 | is_kernel_module(dso->long_name, |
| 1212 | PERF_RECORD_MISC_CPUMODE_UNKNOWN)) |
Namhyung Kim | b837a8b | 2014-11-04 10:14:33 +0900 | [diff] [blame] | 1213 | continue; |
| 1214 | |
Wang Nan | 1f121b0 | 2015-06-03 08:52:21 +0000 | [diff] [blame] | 1215 | |
Namhyung Kim | b837a8b | 2014-11-04 10:14:33 +0900 | [diff] [blame] | 1216 | kernel = dso; |
| 1217 | break; |
| 1218 | } |
| 1219 | |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 1220 | pthread_rwlock_unlock(&machine->dsos.lock); |
| 1221 | |
Namhyung Kim | b837a8b | 2014-11-04 10:14:33 +0900 | [diff] [blame] | 1222 | if (kernel == NULL) |
Arnaldo Carvalho de Melo | aa7cc2a | 2015-05-29 11:31:12 -0300 | [diff] [blame] | 1223 | kernel = machine__findnew_dso(machine, kmmap_prefix); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1224 | if (kernel == NULL) |
| 1225 | goto out_problem; |
| 1226 | |
| 1227 | kernel->kernel = kernel_type; |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 1228 | if (__machine__create_kernel_maps(machine, kernel) < 0) { |
| 1229 | dso__put(kernel); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1230 | goto out_problem; |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 1231 | } |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1232 | |
Namhyung Kim | 330dfa2 | 2014-11-18 13:30:28 +0900 | [diff] [blame] | 1233 | if (strstr(kernel->long_name, "vmlinux")) |
| 1234 | dso__set_short_name(kernel, "[kernel.vmlinux]", false); |
Namhyung Kim | 96d7805 | 2014-11-04 10:14:34 +0900 | [diff] [blame] | 1235 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1236 | machine__set_kernel_mmap_len(machine, event); |
| 1237 | |
| 1238 | /* |
| 1239 | * Avoid using a zero address (kptr_restrict) for the ref reloc |
| 1240 | * symbol. Effectively having zero here means that at record |
| 1241 | * time /proc/sys/kernel/kptr_restrict was non zero. |
| 1242 | */ |
| 1243 | if (event->mmap.pgoff != 0) { |
| 1244 | maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, |
| 1245 | symbol_name, |
| 1246 | event->mmap.pgoff); |
| 1247 | } |
| 1248 | |
| 1249 | if (machine__is_default_guest(machine)) { |
| 1250 | /* |
| 1251 | * preload dso of guest kernel and modules |
| 1252 | */ |
Arnaldo Carvalho de Melo | a5e813c | 2015-09-30 11:54:04 -0300 | [diff] [blame] | 1253 | dso__load(kernel, machine__kernel_map(machine), NULL); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1254 | } |
| 1255 | } |
| 1256 | return 0; |
| 1257 | out_problem: |
| 1258 | return -1; |
| 1259 | } |
| 1260 | |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1261 | int machine__process_mmap2_event(struct machine *machine, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1262 | union perf_event *event, |
| 1263 | struct perf_sample *sample __maybe_unused) |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1264 | { |
| 1265 | u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
| 1266 | struct thread *thread; |
| 1267 | struct map *map; |
| 1268 | enum map_type type; |
| 1269 | int ret = 0; |
| 1270 | |
| 1271 | if (dump_trace) |
| 1272 | perf_event__fprintf_mmap2(event, stdout); |
| 1273 | |
| 1274 | if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL || |
| 1275 | cpumode == PERF_RECORD_MISC_KERNEL) { |
| 1276 | ret = machine__process_kernel_mmap_event(machine, event); |
| 1277 | if (ret < 0) |
| 1278 | goto out_problem; |
| 1279 | return 0; |
| 1280 | } |
| 1281 | |
| 1282 | thread = machine__findnew_thread(machine, event->mmap2.pid, |
Don Zickus | 11c9abf | 2014-02-26 10:45:27 -0500 | [diff] [blame] | 1283 | event->mmap2.tid); |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1284 | if (thread == NULL) |
| 1285 | goto out_problem; |
| 1286 | |
| 1287 | if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) |
| 1288 | type = MAP__VARIABLE; |
| 1289 | else |
| 1290 | type = MAP__FUNCTION; |
| 1291 | |
Adrian Hunter | 2a03068 | 2014-07-22 16:17:53 +0300 | [diff] [blame] | 1292 | map = map__new(machine, event->mmap2.start, |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1293 | event->mmap2.len, event->mmap2.pgoff, |
| 1294 | event->mmap2.pid, event->mmap2.maj, |
| 1295 | event->mmap2.min, event->mmap2.ino, |
| 1296 | event->mmap2.ino_generation, |
Don Zickus | 7ef8070 | 2014-05-19 15:13:49 -0400 | [diff] [blame] | 1297 | event->mmap2.prot, |
| 1298 | event->mmap2.flags, |
Adrian Hunter | 5835edd | 2014-07-22 16:18:00 +0300 | [diff] [blame] | 1299 | event->mmap2.filename, type, thread); |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1300 | |
| 1301 | if (map == NULL) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1302 | goto out_problem_map; |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1303 | |
| 1304 | thread__insert_map(thread, map); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1305 | thread__put(thread); |
Arnaldo Carvalho de Melo | 84c2caf | 2015-05-25 16:59:56 -0300 | [diff] [blame] | 1306 | map__put(map); |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1307 | return 0; |
| 1308 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1309 | out_problem_map: |
| 1310 | thread__put(thread); |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1311 | out_problem: |
| 1312 | dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n"); |
| 1313 | return 0; |
| 1314 | } |
| 1315 | |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1316 | int machine__process_mmap_event(struct machine *machine, union perf_event *event, |
| 1317 | struct perf_sample *sample __maybe_unused) |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1318 | { |
| 1319 | u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
| 1320 | struct thread *thread; |
| 1321 | struct map *map; |
Stephane Eranian | bad4091 | 2013-01-24 16:10:40 +0100 | [diff] [blame] | 1322 | enum map_type type; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1323 | int ret = 0; |
| 1324 | |
| 1325 | if (dump_trace) |
| 1326 | perf_event__fprintf_mmap(event, stdout); |
| 1327 | |
| 1328 | if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL || |
| 1329 | cpumode == PERF_RECORD_MISC_KERNEL) { |
| 1330 | ret = machine__process_kernel_mmap_event(machine, event); |
| 1331 | if (ret < 0) |
| 1332 | goto out_problem; |
| 1333 | return 0; |
| 1334 | } |
| 1335 | |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 1336 | thread = machine__findnew_thread(machine, event->mmap.pid, |
Don Zickus | 11c9abf | 2014-02-26 10:45:27 -0500 | [diff] [blame] | 1337 | event->mmap.tid); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1338 | if (thread == NULL) |
| 1339 | goto out_problem; |
Stephane Eranian | bad4091 | 2013-01-24 16:10:40 +0100 | [diff] [blame] | 1340 | |
| 1341 | if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) |
| 1342 | type = MAP__VARIABLE; |
| 1343 | else |
| 1344 | type = MAP__FUNCTION; |
| 1345 | |
Adrian Hunter | 2a03068 | 2014-07-22 16:17:53 +0300 | [diff] [blame] | 1346 | map = map__new(machine, event->mmap.start, |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1347 | event->mmap.len, event->mmap.pgoff, |
Don Zickus | 7ef8070 | 2014-05-19 15:13:49 -0400 | [diff] [blame] | 1348 | event->mmap.pid, 0, 0, 0, 0, 0, 0, |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1349 | event->mmap.filename, |
Adrian Hunter | 5835edd | 2014-07-22 16:18:00 +0300 | [diff] [blame] | 1350 | type, thread); |
Stephane Eranian | bad4091 | 2013-01-24 16:10:40 +0100 | [diff] [blame] | 1351 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1352 | if (map == NULL) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1353 | goto out_problem_map; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1354 | |
| 1355 | thread__insert_map(thread, map); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1356 | thread__put(thread); |
Arnaldo Carvalho de Melo | 84c2caf | 2015-05-25 16:59:56 -0300 | [diff] [blame] | 1357 | map__put(map); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1358 | return 0; |
| 1359 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1360 | out_problem_map: |
| 1361 | thread__put(thread); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1362 | out_problem: |
| 1363 | dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n"); |
| 1364 | return 0; |
| 1365 | } |
| 1366 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1367 | static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock) |
David Ahern | 236a3bb | 2013-08-14 08:49:27 -0600 | [diff] [blame] | 1368 | { |
Arnaldo Carvalho de Melo | f3b623b | 2015-03-02 22:21:35 -0300 | [diff] [blame] | 1369 | if (machine->last_match == th) |
Arnaldo Carvalho de Melo | 0ceb8f6 | 2015-05-11 18:08:12 -0300 | [diff] [blame] | 1370 | machine->last_match = NULL; |
Arnaldo Carvalho de Melo | f3b623b | 2015-03-02 22:21:35 -0300 | [diff] [blame] | 1371 | |
Arnaldo Carvalho de Melo | 59a51c1 | 2015-05-15 15:32:55 -0300 | [diff] [blame] | 1372 | BUG_ON(atomic_read(&th->refcnt) == 0); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1373 | if (lock) |
| 1374 | pthread_rwlock_wrlock(&machine->threads_lock); |
Arnaldo Carvalho de Melo | 0170b14 | 2015-05-25 15:23:05 -0300 | [diff] [blame] | 1375 | rb_erase_init(&th->rb_node, &machine->threads); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1376 | RB_CLEAR_NODE(&th->rb_node); |
David Ahern | 236a3bb | 2013-08-14 08:49:27 -0600 | [diff] [blame] | 1377 | /* |
Arnaldo Carvalho de Melo | f3b623b | 2015-03-02 22:21:35 -0300 | [diff] [blame] | 1378 | * Move it first to the dead_threads list, then drop the reference, |
| 1379 | * if this is the last reference, then the thread__delete destructor |
| 1380 | * will be called and we will remove it from the dead_threads list. |
David Ahern | 236a3bb | 2013-08-14 08:49:27 -0600 | [diff] [blame] | 1381 | */ |
| 1382 | list_add_tail(&th->node, &machine->dead_threads); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1383 | if (lock) |
| 1384 | pthread_rwlock_unlock(&machine->threads_lock); |
Arnaldo Carvalho de Melo | f3b623b | 2015-03-02 22:21:35 -0300 | [diff] [blame] | 1385 | thread__put(th); |
David Ahern | 236a3bb | 2013-08-14 08:49:27 -0600 | [diff] [blame] | 1386 | } |
| 1387 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1388 | void machine__remove_thread(struct machine *machine, struct thread *th) |
| 1389 | { |
| 1390 | return __machine__remove_thread(machine, th, true); |
| 1391 | } |
| 1392 | |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1393 | int machine__process_fork_event(struct machine *machine, union perf_event *event, |
| 1394 | struct perf_sample *sample) |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1395 | { |
Jiri Olsa | d75e609 | 2014-03-14 15:00:03 +0100 | [diff] [blame] | 1396 | struct thread *thread = machine__find_thread(machine, |
| 1397 | event->fork.pid, |
| 1398 | event->fork.tid); |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 1399 | struct thread *parent = machine__findnew_thread(machine, |
| 1400 | event->fork.ppid, |
| 1401 | event->fork.ptid); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1402 | int err = 0; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1403 | |
Adrian Hunter | 5cb7334 | 2015-08-19 17:29:20 +0300 | [diff] [blame] | 1404 | if (dump_trace) |
| 1405 | perf_event__fprintf_task(event, stdout); |
| 1406 | |
| 1407 | /* |
| 1408 | * There may be an existing thread that is not actually the parent, |
| 1409 | * either because we are processing events out of order, or because the |
| 1410 | * (fork) event that would have removed the thread was lost. Assume the |
| 1411 | * latter case and continue on as best we can. |
| 1412 | */ |
| 1413 | if (parent->pid_ != (pid_t)event->fork.ppid) { |
| 1414 | dump_printf("removing erroneous parent thread %d/%d\n", |
| 1415 | parent->pid_, parent->tid); |
| 1416 | machine__remove_thread(machine, parent); |
| 1417 | thread__put(parent); |
| 1418 | parent = machine__findnew_thread(machine, event->fork.ppid, |
| 1419 | event->fork.ptid); |
| 1420 | } |
| 1421 | |
David Ahern | 236a3bb | 2013-08-14 08:49:27 -0600 | [diff] [blame] | 1422 | /* if a thread currently exists for the thread id remove it */ |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1423 | if (thread != NULL) { |
David Ahern | 236a3bb | 2013-08-14 08:49:27 -0600 | [diff] [blame] | 1424 | machine__remove_thread(machine, thread); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1425 | thread__put(thread); |
| 1426 | } |
David Ahern | 236a3bb | 2013-08-14 08:49:27 -0600 | [diff] [blame] | 1427 | |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 1428 | thread = machine__findnew_thread(machine, event->fork.pid, |
| 1429 | event->fork.tid); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1430 | |
| 1431 | if (thread == NULL || parent == NULL || |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1432 | thread__fork(thread, parent, sample->time) < 0) { |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1433 | dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n"); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1434 | err = -1; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1435 | } |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1436 | thread__put(thread); |
| 1437 | thread__put(parent); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1438 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1439 | return err; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1440 | } |
| 1441 | |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1442 | int machine__process_exit_event(struct machine *machine, union perf_event *event, |
| 1443 | struct perf_sample *sample __maybe_unused) |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1444 | { |
Jiri Olsa | d75e609 | 2014-03-14 15:00:03 +0100 | [diff] [blame] | 1445 | struct thread *thread = machine__find_thread(machine, |
| 1446 | event->fork.pid, |
| 1447 | event->fork.tid); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1448 | |
| 1449 | if (dump_trace) |
| 1450 | perf_event__fprintf_task(event, stdout); |
| 1451 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1452 | if (thread != NULL) { |
David Ahern | 236a3bb | 2013-08-14 08:49:27 -0600 | [diff] [blame] | 1453 | thread__exited(thread); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1454 | thread__put(thread); |
| 1455 | } |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1456 | |
| 1457 | return 0; |
| 1458 | } |
| 1459 | |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1460 | int machine__process_event(struct machine *machine, union perf_event *event, |
| 1461 | struct perf_sample *sample) |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1462 | { |
| 1463 | int ret; |
| 1464 | |
| 1465 | switch (event->header.type) { |
| 1466 | case PERF_RECORD_COMM: |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1467 | ret = machine__process_comm_event(machine, event, sample); break; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1468 | case PERF_RECORD_MMAP: |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1469 | ret = machine__process_mmap_event(machine, event, sample); break; |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1470 | case PERF_RECORD_MMAP2: |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1471 | ret = machine__process_mmap2_event(machine, event, sample); break; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1472 | case PERF_RECORD_FORK: |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1473 | ret = machine__process_fork_event(machine, event, sample); break; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1474 | case PERF_RECORD_EXIT: |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1475 | ret = machine__process_exit_event(machine, event, sample); break; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1476 | case PERF_RECORD_LOST: |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1477 | ret = machine__process_lost_event(machine, event, sample); break; |
Adrian Hunter | 4a96f7a | 2015-04-30 17:37:29 +0300 | [diff] [blame] | 1478 | case PERF_RECORD_AUX: |
| 1479 | ret = machine__process_aux_event(machine, event); break; |
Adrian Hunter | 0ad21f6 | 2015-04-30 17:37:30 +0300 | [diff] [blame] | 1480 | case PERF_RECORD_ITRACE_START: |
Jiri Olsa | ceb9291 | 2015-06-29 13:27:45 +0200 | [diff] [blame] | 1481 | ret = machine__process_itrace_start_event(machine, event); break; |
Kan Liang | c4937a9 | 2015-05-10 15:13:15 -0400 | [diff] [blame] | 1482 | case PERF_RECORD_LOST_SAMPLES: |
| 1483 | ret = machine__process_lost_samples_event(machine, event, sample); break; |
Adrian Hunter | 0286039 | 2015-07-21 12:44:03 +0300 | [diff] [blame] | 1484 | case PERF_RECORD_SWITCH: |
| 1485 | case PERF_RECORD_SWITCH_CPU_WIDE: |
| 1486 | ret = machine__process_switch_event(machine, event); break; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1487 | default: |
| 1488 | ret = -1; |
| 1489 | break; |
| 1490 | } |
| 1491 | |
| 1492 | return ret; |
| 1493 | } |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1494 | |
Greg Price | b21484f | 2012-12-06 21:48:05 -0800 | [diff] [blame] | 1495 | static bool symbol__match_regex(struct symbol *sym, regex_t *regex) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1496 | { |
Greg Price | b21484f | 2012-12-06 21:48:05 -0800 | [diff] [blame] | 1497 | if (sym->name && !regexec(regex, sym->name, 0, NULL, 0)) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1498 | return 1; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1499 | return 0; |
| 1500 | } |
| 1501 | |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 1502 | static void ip__resolve_ams(struct thread *thread, |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1503 | struct addr_map_symbol *ams, |
| 1504 | u64 ip) |
| 1505 | { |
| 1506 | struct addr_location al; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1507 | |
| 1508 | memset(&al, 0, sizeof(al)); |
Arnaldo Carvalho de Melo | 52a3cb8 | 2014-03-11 16:16:49 -0300 | [diff] [blame] | 1509 | /* |
| 1510 | * We cannot use the header.misc hint to determine whether a |
| 1511 | * branch stack address is user, kernel, guest, hypervisor. |
| 1512 | * Branches may straddle the kernel/user/hypervisor boundaries. |
| 1513 | * Thus, we have to try consecutively until we find a match |
| 1514 | * or else, the symbol is unknown |
| 1515 | */ |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 1516 | thread__find_cpumode_addr_location(thread, MAP__FUNCTION, ip, &al); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1517 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1518 | ams->addr = ip; |
| 1519 | ams->al_addr = al.addr; |
| 1520 | ams->sym = al.sym; |
| 1521 | ams->map = al.map; |
| 1522 | } |
| 1523 | |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 1524 | static void ip__resolve_data(struct thread *thread, |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 1525 | u8 m, struct addr_map_symbol *ams, u64 addr) |
| 1526 | { |
| 1527 | struct addr_location al; |
| 1528 | |
| 1529 | memset(&al, 0, sizeof(al)); |
| 1530 | |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 1531 | thread__find_addr_location(thread, m, MAP__VARIABLE, addr, &al); |
Don Zickus | 06b2afc | 2014-08-20 23:25:11 -0400 | [diff] [blame] | 1532 | if (al.map == NULL) { |
| 1533 | /* |
| 1534 | * some shared data regions have execute bit set which puts |
| 1535 | * their mapping in the MAP__FUNCTION type array. |
| 1536 | * Check there as a fallback option before dropping the sample. |
| 1537 | */ |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 1538 | thread__find_addr_location(thread, m, MAP__FUNCTION, addr, &al); |
Don Zickus | 06b2afc | 2014-08-20 23:25:11 -0400 | [diff] [blame] | 1539 | } |
| 1540 | |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 1541 | ams->addr = addr; |
| 1542 | ams->al_addr = al.addr; |
| 1543 | ams->sym = al.sym; |
| 1544 | ams->map = al.map; |
| 1545 | } |
| 1546 | |
Arnaldo Carvalho de Melo | e80faac | 2014-01-22 13:05:06 -0300 | [diff] [blame] | 1547 | struct mem_info *sample__resolve_mem(struct perf_sample *sample, |
| 1548 | struct addr_location *al) |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 1549 | { |
| 1550 | struct mem_info *mi = zalloc(sizeof(*mi)); |
| 1551 | |
| 1552 | if (!mi) |
| 1553 | return NULL; |
| 1554 | |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 1555 | ip__resolve_ams(al->thread, &mi->iaddr, sample->ip); |
| 1556 | ip__resolve_data(al->thread, al->cpumode, &mi->daddr, sample->addr); |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 1557 | mi->data_src.val = sample->data_src; |
| 1558 | |
| 1559 | return mi; |
| 1560 | } |
| 1561 | |
Andi Kleen | 37592b8 | 2014-11-12 18:05:19 -0800 | [diff] [blame] | 1562 | static int add_callchain_ip(struct thread *thread, |
| 1563 | struct symbol **parent, |
| 1564 | struct addr_location *root_al, |
David Hildenbrand | 73dbcd6 | 2015-03-30 10:11:00 +0200 | [diff] [blame] | 1565 | u8 *cpumode, |
Andi Kleen | 37592b8 | 2014-11-12 18:05:19 -0800 | [diff] [blame] | 1566 | u64 ip) |
| 1567 | { |
| 1568 | struct addr_location al; |
| 1569 | |
| 1570 | al.filtered = 0; |
| 1571 | al.sym = NULL; |
David Hildenbrand | 73dbcd6 | 2015-03-30 10:11:00 +0200 | [diff] [blame] | 1572 | if (!cpumode) { |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 1573 | thread__find_cpumode_addr_location(thread, MAP__FUNCTION, |
| 1574 | ip, &al); |
David Hildenbrand | 73dbcd6 | 2015-03-30 10:11:00 +0200 | [diff] [blame] | 1575 | } else { |
Kan Liang | 2e77784 | 2014-12-02 10:06:53 -0500 | [diff] [blame] | 1576 | if (ip >= PERF_CONTEXT_MAX) { |
| 1577 | switch (ip) { |
| 1578 | case PERF_CONTEXT_HV: |
David Hildenbrand | 73dbcd6 | 2015-03-30 10:11:00 +0200 | [diff] [blame] | 1579 | *cpumode = PERF_RECORD_MISC_HYPERVISOR; |
Kan Liang | 2e77784 | 2014-12-02 10:06:53 -0500 | [diff] [blame] | 1580 | break; |
| 1581 | case PERF_CONTEXT_KERNEL: |
David Hildenbrand | 73dbcd6 | 2015-03-30 10:11:00 +0200 | [diff] [blame] | 1582 | *cpumode = PERF_RECORD_MISC_KERNEL; |
Kan Liang | 2e77784 | 2014-12-02 10:06:53 -0500 | [diff] [blame] | 1583 | break; |
| 1584 | case PERF_CONTEXT_USER: |
David Hildenbrand | 73dbcd6 | 2015-03-30 10:11:00 +0200 | [diff] [blame] | 1585 | *cpumode = PERF_RECORD_MISC_USER; |
Kan Liang | 2e77784 | 2014-12-02 10:06:53 -0500 | [diff] [blame] | 1586 | break; |
| 1587 | default: |
| 1588 | pr_debug("invalid callchain context: " |
| 1589 | "%"PRId64"\n", (s64) ip); |
| 1590 | /* |
| 1591 | * It seems the callchain is corrupted. |
| 1592 | * Discard all. |
| 1593 | */ |
| 1594 | callchain_cursor_reset(&callchain_cursor); |
| 1595 | return 1; |
| 1596 | } |
| 1597 | return 0; |
| 1598 | } |
David Hildenbrand | 73dbcd6 | 2015-03-30 10:11:00 +0200 | [diff] [blame] | 1599 | thread__find_addr_location(thread, *cpumode, MAP__FUNCTION, |
| 1600 | ip, &al); |
Kan Liang | 2e77784 | 2014-12-02 10:06:53 -0500 | [diff] [blame] | 1601 | } |
| 1602 | |
Andi Kleen | 37592b8 | 2014-11-12 18:05:19 -0800 | [diff] [blame] | 1603 | if (al.sym != NULL) { |
| 1604 | if (sort__has_parent && !*parent && |
| 1605 | symbol__match_regex(al.sym, &parent_regex)) |
| 1606 | *parent = al.sym; |
| 1607 | else if (have_ignore_callees && root_al && |
| 1608 | symbol__match_regex(al.sym, &ignore_callees_regex)) { |
| 1609 | /* Treat this symbol as the root, |
| 1610 | forgetting its callees. */ |
| 1611 | *root_al = al; |
| 1612 | callchain_cursor_reset(&callchain_cursor); |
| 1613 | } |
| 1614 | } |
| 1615 | |
Andi Kleen | 5550171 | 2014-11-12 18:05:21 -0800 | [diff] [blame] | 1616 | return callchain_cursor_append(&callchain_cursor, al.addr, al.map, al.sym); |
Andi Kleen | 37592b8 | 2014-11-12 18:05:19 -0800 | [diff] [blame] | 1617 | } |
| 1618 | |
Arnaldo Carvalho de Melo | 644f2df | 2014-01-22 13:15:36 -0300 | [diff] [blame] | 1619 | struct branch_info *sample__resolve_bstack(struct perf_sample *sample, |
| 1620 | struct addr_location *al) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1621 | { |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1622 | unsigned int i; |
Arnaldo Carvalho de Melo | 644f2df | 2014-01-22 13:15:36 -0300 | [diff] [blame] | 1623 | const struct branch_stack *bs = sample->branch_stack; |
| 1624 | struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info)); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1625 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1626 | if (!bi) |
| 1627 | return NULL; |
| 1628 | |
| 1629 | for (i = 0; i < bs->nr; i++) { |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 1630 | ip__resolve_ams(al->thread, &bi[i].to, bs->entries[i].to); |
| 1631 | ip__resolve_ams(al->thread, &bi[i].from, bs->entries[i].from); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1632 | bi[i].flags = bs->entries[i].flags; |
| 1633 | } |
| 1634 | return bi; |
| 1635 | } |
| 1636 | |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 1637 | #define CHASHSZ 127 |
| 1638 | #define CHASHBITS 7 |
| 1639 | #define NO_ENTRY 0xff |
| 1640 | |
| 1641 | #define PERF_MAX_BRANCH_DEPTH 127 |
| 1642 | |
| 1643 | /* Remove loops. */ |
| 1644 | static int remove_loops(struct branch_entry *l, int nr) |
| 1645 | { |
| 1646 | int i, j, off; |
| 1647 | unsigned char chash[CHASHSZ]; |
| 1648 | |
| 1649 | memset(chash, NO_ENTRY, sizeof(chash)); |
| 1650 | |
| 1651 | BUG_ON(PERF_MAX_BRANCH_DEPTH > 255); |
| 1652 | |
| 1653 | for (i = 0; i < nr; i++) { |
| 1654 | int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ; |
| 1655 | |
| 1656 | /* no collision handling for now */ |
| 1657 | if (chash[h] == NO_ENTRY) { |
| 1658 | chash[h] = i; |
| 1659 | } else if (l[chash[h]].from == l[i].from) { |
| 1660 | bool is_loop = true; |
| 1661 | /* check if it is a real loop */ |
| 1662 | off = 0; |
| 1663 | for (j = chash[h]; j < i && i + off < nr; j++, off++) |
| 1664 | if (l[j].from != l[i + off].from) { |
| 1665 | is_loop = false; |
| 1666 | break; |
| 1667 | } |
| 1668 | if (is_loop) { |
| 1669 | memmove(l + i, l + i + off, |
| 1670 | (nr - (i + off)) * sizeof(*l)); |
| 1671 | nr -= off; |
| 1672 | } |
| 1673 | } |
| 1674 | } |
| 1675 | return nr; |
| 1676 | } |
| 1677 | |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 1678 | /* |
| 1679 | * Recolve LBR callstack chain sample |
| 1680 | * Return: |
| 1681 | * 1 on success get LBR callchain information |
| 1682 | * 0 no available LBR callchain information, should try fp |
| 1683 | * negative error code on other errors. |
| 1684 | */ |
| 1685 | static int resolve_lbr_callchain_sample(struct thread *thread, |
| 1686 | struct perf_sample *sample, |
| 1687 | struct symbol **parent, |
| 1688 | struct addr_location *root_al, |
| 1689 | int max_stack) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1690 | { |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 1691 | struct ip_callchain *chain = sample->callchain; |
| 1692 | int chain_nr = min(max_stack, (int)chain->nr); |
David Hildenbrand | 73dbcd6 | 2015-03-30 10:11:00 +0200 | [diff] [blame] | 1693 | u8 cpumode = PERF_RECORD_MISC_USER; |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 1694 | int i, j, err; |
| 1695 | u64 ip; |
| 1696 | |
| 1697 | for (i = 0; i < chain_nr; i++) { |
| 1698 | if (chain->ips[i] == PERF_CONTEXT_USER) |
| 1699 | break; |
| 1700 | } |
| 1701 | |
| 1702 | /* LBR only affects the user callchain */ |
| 1703 | if (i != chain_nr) { |
| 1704 | struct branch_stack *lbr_stack = sample->branch_stack; |
| 1705 | int lbr_nr = lbr_stack->nr; |
| 1706 | /* |
| 1707 | * LBR callstack can only get user call chain. |
| 1708 | * The mix_chain_nr is kernel call chain |
| 1709 | * number plus LBR user call chain number. |
| 1710 | * i is kernel call chain number, |
| 1711 | * 1 is PERF_CONTEXT_USER, |
| 1712 | * lbr_nr + 1 is the user call chain number. |
| 1713 | * For details, please refer to the comments |
| 1714 | * in callchain__printf |
| 1715 | */ |
| 1716 | int mix_chain_nr = i + 1 + lbr_nr + 1; |
| 1717 | |
| 1718 | if (mix_chain_nr > PERF_MAX_STACK_DEPTH + PERF_MAX_BRANCH_DEPTH) { |
| 1719 | pr_warning("corrupted callchain. skipping...\n"); |
| 1720 | return 0; |
| 1721 | } |
| 1722 | |
| 1723 | for (j = 0; j < mix_chain_nr; j++) { |
| 1724 | if (callchain_param.order == ORDER_CALLEE) { |
| 1725 | if (j < i + 1) |
| 1726 | ip = chain->ips[j]; |
| 1727 | else if (j > i + 1) |
| 1728 | ip = lbr_stack->entries[j - i - 2].from; |
| 1729 | else |
| 1730 | ip = lbr_stack->entries[0].to; |
| 1731 | } else { |
| 1732 | if (j < lbr_nr) |
| 1733 | ip = lbr_stack->entries[lbr_nr - j - 1].from; |
| 1734 | else if (j > lbr_nr) |
| 1735 | ip = chain->ips[i + 1 - (j - lbr_nr)]; |
| 1736 | else |
| 1737 | ip = lbr_stack->entries[0].to; |
| 1738 | } |
| 1739 | |
David Hildenbrand | 73dbcd6 | 2015-03-30 10:11:00 +0200 | [diff] [blame] | 1740 | err = add_callchain_ip(thread, parent, root_al, &cpumode, ip); |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 1741 | if (err) |
| 1742 | return (err < 0) ? err : 0; |
| 1743 | } |
| 1744 | return 1; |
| 1745 | } |
| 1746 | |
| 1747 | return 0; |
| 1748 | } |
| 1749 | |
| 1750 | static int thread__resolve_callchain_sample(struct thread *thread, |
| 1751 | struct perf_evsel *evsel, |
| 1752 | struct perf_sample *sample, |
| 1753 | struct symbol **parent, |
| 1754 | struct addr_location *root_al, |
| 1755 | int max_stack) |
| 1756 | { |
| 1757 | struct branch_stack *branch = sample->branch_stack; |
| 1758 | struct ip_callchain *chain = sample->callchain; |
Waiman Long | 91e9561 | 2013-10-18 10:38:48 -0400 | [diff] [blame] | 1759 | int chain_nr = min(max_stack, (int)chain->nr); |
David Hildenbrand | 73dbcd6 | 2015-03-30 10:11:00 +0200 | [diff] [blame] | 1760 | u8 cpumode = PERF_RECORD_MISC_USER; |
Kan Liang | 2e77784 | 2014-12-02 10:06:53 -0500 | [diff] [blame] | 1761 | int i, j, err; |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 1762 | int skip_idx = -1; |
| 1763 | int first_call = 0; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1764 | |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 1765 | callchain_cursor_reset(&callchain_cursor); |
| 1766 | |
| 1767 | if (has_branch_callstack(evsel)) { |
| 1768 | err = resolve_lbr_callchain_sample(thread, sample, parent, |
| 1769 | root_al, max_stack); |
| 1770 | if (err) |
| 1771 | return (err < 0) ? err : 0; |
| 1772 | } |
| 1773 | |
Sukadev Bhattiprolu | a60335b | 2014-06-25 08:49:03 -0700 | [diff] [blame] | 1774 | /* |
| 1775 | * Based on DWARF debug information, some architectures skip |
| 1776 | * a callchain entry saved by the kernel. |
| 1777 | */ |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 1778 | if (chain->nr < PERF_MAX_STACK_DEPTH) |
| 1779 | skip_idx = arch_skip_callchain_idx(thread, chain); |
Sukadev Bhattiprolu | a60335b | 2014-06-25 08:49:03 -0700 | [diff] [blame] | 1780 | |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 1781 | /* |
| 1782 | * Add branches to call stack for easier browsing. This gives |
| 1783 | * more context for a sample than just the callers. |
| 1784 | * |
| 1785 | * This uses individual histograms of paths compared to the |
| 1786 | * aggregated histograms the normal LBR mode uses. |
| 1787 | * |
| 1788 | * Limitations for now: |
| 1789 | * - No extra filters |
| 1790 | * - No annotations (should annotate somehow) |
| 1791 | */ |
| 1792 | |
| 1793 | if (branch && callchain_param.branch_callstack) { |
| 1794 | int nr = min(max_stack, (int)branch->nr); |
| 1795 | struct branch_entry be[nr]; |
| 1796 | |
| 1797 | if (branch->nr > PERF_MAX_BRANCH_DEPTH) { |
| 1798 | pr_warning("corrupted branch chain. skipping...\n"); |
| 1799 | goto check_calls; |
| 1800 | } |
| 1801 | |
| 1802 | for (i = 0; i < nr; i++) { |
| 1803 | if (callchain_param.order == ORDER_CALLEE) { |
| 1804 | be[i] = branch->entries[i]; |
| 1805 | /* |
| 1806 | * Check for overlap into the callchain. |
| 1807 | * The return address is one off compared to |
| 1808 | * the branch entry. To adjust for this |
| 1809 | * assume the calling instruction is not longer |
| 1810 | * than 8 bytes. |
| 1811 | */ |
| 1812 | if (i == skip_idx || |
| 1813 | chain->ips[first_call] >= PERF_CONTEXT_MAX) |
| 1814 | first_call++; |
| 1815 | else if (be[i].from < chain->ips[first_call] && |
| 1816 | be[i].from >= chain->ips[first_call] - 8) |
| 1817 | first_call++; |
| 1818 | } else |
| 1819 | be[i] = branch->entries[branch->nr - i - 1]; |
| 1820 | } |
| 1821 | |
| 1822 | nr = remove_loops(be, nr); |
| 1823 | |
| 1824 | for (i = 0; i < nr; i++) { |
| 1825 | err = add_callchain_ip(thread, parent, root_al, |
David Hildenbrand | 73dbcd6 | 2015-03-30 10:11:00 +0200 | [diff] [blame] | 1826 | NULL, be[i].to); |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 1827 | if (!err) |
| 1828 | err = add_callchain_ip(thread, parent, root_al, |
David Hildenbrand | 73dbcd6 | 2015-03-30 10:11:00 +0200 | [diff] [blame] | 1829 | NULL, be[i].from); |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 1830 | if (err == -EINVAL) |
| 1831 | break; |
| 1832 | if (err) |
| 1833 | return err; |
| 1834 | } |
| 1835 | chain_nr -= nr; |
| 1836 | } |
| 1837 | |
| 1838 | check_calls: |
Adrian Hunter | 0edd453 | 2015-09-25 16:15:48 +0300 | [diff] [blame] | 1839 | if (chain->nr > PERF_MAX_STACK_DEPTH && (int)chain->nr > max_stack) { |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 1840 | pr_warning("corrupted callchain. skipping...\n"); |
| 1841 | return 0; |
| 1842 | } |
| 1843 | |
| 1844 | for (i = first_call; i < chain_nr; i++) { |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1845 | u64 ip; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1846 | |
| 1847 | if (callchain_param.order == ORDER_CALLEE) |
Sukadev Bhattiprolu | a60335b | 2014-06-25 08:49:03 -0700 | [diff] [blame] | 1848 | j = i; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1849 | else |
Sukadev Bhattiprolu | a60335b | 2014-06-25 08:49:03 -0700 | [diff] [blame] | 1850 | j = chain->nr - i - 1; |
| 1851 | |
| 1852 | #ifdef HAVE_SKIP_CALLCHAIN_IDX |
| 1853 | if (j == skip_idx) |
| 1854 | continue; |
| 1855 | #endif |
| 1856 | ip = chain->ips[j]; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1857 | |
David Hildenbrand | 73dbcd6 | 2015-03-30 10:11:00 +0200 | [diff] [blame] | 1858 | err = add_callchain_ip(thread, parent, root_al, &cpumode, ip); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1859 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1860 | if (err) |
Kan Liang | 2e77784 | 2014-12-02 10:06:53 -0500 | [diff] [blame] | 1861 | return (err < 0) ? err : 0; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1862 | } |
| 1863 | |
| 1864 | return 0; |
| 1865 | } |
| 1866 | |
| 1867 | static int unwind_entry(struct unwind_entry *entry, void *arg) |
| 1868 | { |
| 1869 | struct callchain_cursor *cursor = arg; |
| 1870 | return callchain_cursor_append(cursor, entry->ip, |
| 1871 | entry->map, entry->sym); |
| 1872 | } |
| 1873 | |
Arnaldo Carvalho de Melo | cc8b7c2 | 2014-10-23 15:26:17 -0300 | [diff] [blame] | 1874 | int thread__resolve_callchain(struct thread *thread, |
| 1875 | struct perf_evsel *evsel, |
| 1876 | struct perf_sample *sample, |
| 1877 | struct symbol **parent, |
| 1878 | struct addr_location *root_al, |
| 1879 | int max_stack) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1880 | { |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 1881 | int ret = thread__resolve_callchain_sample(thread, evsel, |
| 1882 | sample, parent, |
| 1883 | root_al, max_stack); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1884 | if (ret) |
| 1885 | return ret; |
| 1886 | |
| 1887 | /* Can we do dwarf post unwind? */ |
| 1888 | if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) && |
| 1889 | (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER))) |
| 1890 | return 0; |
| 1891 | |
| 1892 | /* Bail out if nothing was captured. */ |
| 1893 | if ((!sample->user_regs.regs) || |
| 1894 | (!sample->user_stack.size)) |
| 1895 | return 0; |
| 1896 | |
Arnaldo Carvalho de Melo | dd8c17a | 2014-10-23 16:42:19 -0300 | [diff] [blame] | 1897 | return unwind__get_entries(unwind_entry, &callchain_cursor, |
Jiri Olsa | 352ea45 | 2014-01-07 13:47:25 +0100 | [diff] [blame] | 1898 | thread, sample, max_stack); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1899 | |
| 1900 | } |
David Ahern | 35feee1 | 2013-09-28 13:12:58 -0600 | [diff] [blame] | 1901 | |
| 1902 | int machine__for_each_thread(struct machine *machine, |
| 1903 | int (*fn)(struct thread *thread, void *p), |
| 1904 | void *priv) |
| 1905 | { |
| 1906 | struct rb_node *nd; |
| 1907 | struct thread *thread; |
| 1908 | int rc = 0; |
| 1909 | |
| 1910 | for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) { |
| 1911 | thread = rb_entry(nd, struct thread, rb_node); |
| 1912 | rc = fn(thread, priv); |
| 1913 | if (rc != 0) |
| 1914 | return rc; |
| 1915 | } |
| 1916 | |
| 1917 | list_for_each_entry(thread, &machine->dead_threads, node) { |
| 1918 | rc = fn(thread, priv); |
| 1919 | if (rc != 0) |
| 1920 | return rc; |
| 1921 | } |
| 1922 | return rc; |
| 1923 | } |
Arnaldo Carvalho de Melo | 58d925d | 2013-11-11 11:28:02 -0300 | [diff] [blame] | 1924 | |
Adrian Hunter | a5499b3 | 2015-05-29 16:33:30 +0300 | [diff] [blame] | 1925 | int machines__for_each_thread(struct machines *machines, |
| 1926 | int (*fn)(struct thread *thread, void *p), |
| 1927 | void *priv) |
| 1928 | { |
| 1929 | struct rb_node *nd; |
| 1930 | int rc = 0; |
| 1931 | |
| 1932 | rc = machine__for_each_thread(&machines->host, fn, priv); |
| 1933 | if (rc != 0) |
| 1934 | return rc; |
| 1935 | |
| 1936 | for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) { |
| 1937 | struct machine *machine = rb_entry(nd, struct machine, rb_node); |
| 1938 | |
| 1939 | rc = machine__for_each_thread(machine, fn, priv); |
| 1940 | if (rc != 0) |
| 1941 | return rc; |
| 1942 | } |
| 1943 | return rc; |
| 1944 | } |
| 1945 | |
Arnaldo Carvalho de Melo | a33fbd5 | 2013-11-11 11:36:12 -0300 | [diff] [blame] | 1946 | int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool, |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 1947 | struct target *target, struct thread_map *threads, |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 1948 | perf_event__handler_t process, bool data_mmap, |
| 1949 | unsigned int proc_map_timeout) |
Arnaldo Carvalho de Melo | 58d925d | 2013-11-11 11:28:02 -0300 | [diff] [blame] | 1950 | { |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 1951 | if (target__has_task(target)) |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 1952 | return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap, proc_map_timeout); |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 1953 | else if (target__has_cpu(target)) |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 1954 | return perf_event__synthesize_threads(tool, process, machine, data_mmap, proc_map_timeout); |
Arnaldo Carvalho de Melo | 58d925d | 2013-11-11 11:28:02 -0300 | [diff] [blame] | 1955 | /* command specified */ |
| 1956 | return 0; |
| 1957 | } |
Adrian Hunter | b9d266b | 2014-07-22 16:17:25 +0300 | [diff] [blame] | 1958 | |
| 1959 | pid_t machine__get_current_tid(struct machine *machine, int cpu) |
| 1960 | { |
| 1961 | if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid) |
| 1962 | return -1; |
| 1963 | |
| 1964 | return machine->current_tid[cpu]; |
| 1965 | } |
| 1966 | |
| 1967 | int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid, |
| 1968 | pid_t tid) |
| 1969 | { |
| 1970 | struct thread *thread; |
| 1971 | |
| 1972 | if (cpu < 0) |
| 1973 | return -EINVAL; |
| 1974 | |
| 1975 | if (!machine->current_tid) { |
| 1976 | int i; |
| 1977 | |
| 1978 | machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t)); |
| 1979 | if (!machine->current_tid) |
| 1980 | return -ENOMEM; |
| 1981 | for (i = 0; i < MAX_NR_CPUS; i++) |
| 1982 | machine->current_tid[i] = -1; |
| 1983 | } |
| 1984 | |
| 1985 | if (cpu >= MAX_NR_CPUS) { |
| 1986 | pr_err("Requested CPU %d too large. ", cpu); |
| 1987 | pr_err("Consider raising MAX_NR_CPUS\n"); |
| 1988 | return -EINVAL; |
| 1989 | } |
| 1990 | |
| 1991 | machine->current_tid[cpu] = tid; |
| 1992 | |
| 1993 | thread = machine__findnew_thread(machine, pid, tid); |
| 1994 | if (!thread) |
| 1995 | return -ENOMEM; |
| 1996 | |
| 1997 | thread->cpu = cpu; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1998 | thread__put(thread); |
Adrian Hunter | b9d266b | 2014-07-22 16:17:25 +0300 | [diff] [blame] | 1999 | |
| 2000 | return 0; |
| 2001 | } |
Adrian Hunter | fbe2af4 | 2014-08-15 22:08:39 +0300 | [diff] [blame] | 2002 | |
| 2003 | int machine__get_kernel_start(struct machine *machine) |
| 2004 | { |
Arnaldo Carvalho de Melo | a5e813c | 2015-09-30 11:54:04 -0300 | [diff] [blame] | 2005 | struct map *map = machine__kernel_map(machine); |
Adrian Hunter | fbe2af4 | 2014-08-15 22:08:39 +0300 | [diff] [blame] | 2006 | int err = 0; |
| 2007 | |
| 2008 | /* |
| 2009 | * The only addresses above 2^63 are kernel addresses of a 64-bit |
| 2010 | * kernel. Note that addresses are unsigned so that on a 32-bit system |
| 2011 | * all addresses including kernel addresses are less than 2^32. In |
| 2012 | * that case (32-bit system), if the kernel mapping is unknown, all |
| 2013 | * addresses will be assumed to be in user space - see |
| 2014 | * machine__kernel_ip(). |
| 2015 | */ |
| 2016 | machine->kernel_start = 1ULL << 63; |
| 2017 | if (map) { |
| 2018 | err = map__load(map, machine->symbol_filter); |
| 2019 | if (map->start) |
| 2020 | machine->kernel_start = map->start; |
| 2021 | } |
| 2022 | return err; |
| 2023 | } |
Arnaldo Carvalho de Melo | aa7cc2a | 2015-05-29 11:31:12 -0300 | [diff] [blame] | 2024 | |
| 2025 | struct dso *machine__findnew_dso(struct machine *machine, const char *filename) |
| 2026 | { |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 2027 | return dsos__findnew(&machine->dsos, filename); |
Arnaldo Carvalho de Melo | aa7cc2a | 2015-05-29 11:31:12 -0300 | [diff] [blame] | 2028 | } |
Arnaldo Carvalho de Melo | c3168b0 | 2015-07-22 16:14:29 -0300 | [diff] [blame] | 2029 | |
| 2030 | char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp) |
| 2031 | { |
| 2032 | struct machine *machine = vmachine; |
| 2033 | struct map *map; |
| 2034 | struct symbol *sym = map_groups__find_symbol(&machine->kmaps, MAP__FUNCTION, *addrp, &map, NULL); |
| 2035 | |
| 2036 | if (sym == NULL) |
| 2037 | return NULL; |
| 2038 | |
| 2039 | *modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL; |
| 2040 | *addrp = map->unmap_ip(map, sym->start); |
| 2041 | return sym->name; |
| 2042 | } |