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