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