aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/util
diff options
context:
space:
mode:
Diffstat (limited to 'tools/perf/util')
-rw-r--r--tools/perf/util/annotate.c14
-rw-r--r--tools/perf/util/cpumap.c2
-rw-r--r--tools/perf/util/dso.c4
-rw-r--r--tools/perf/util/dso.h10
-rw-r--r--tools/perf/util/event.c188
-rw-r--r--tools/perf/util/event.h5
-rw-r--r--tools/perf/util/evsel.c60
-rw-r--r--tools/perf/util/evsel.h18
-rw-r--r--tools/perf/util/fs.c119
-rw-r--r--tools/perf/util/fs.h7
-rw-r--r--tools/perf/util/hist.c13
-rw-r--r--tools/perf/util/hist.h29
-rw-r--r--tools/perf/util/include/linux/hash.h5
-rw-r--r--tools/perf/util/include/linux/kernel.h6
-rw-r--r--tools/perf/util/include/linux/list.h1
-rw-r--r--tools/perf/util/include/linux/magic.h16
-rw-r--r--tools/perf/util/include/linux/prefetch.h6
-rw-r--r--tools/perf/util/machine.c79
-rw-r--r--tools/perf/util/machine.h13
-rw-r--r--tools/perf/util/map.h10
-rw-r--r--tools/perf/util/parse-options.c37
-rw-r--r--tools/perf/util/parse-options.h8
-rw-r--r--tools/perf/util/perf_regs.c19
-rw-r--r--tools/perf/util/perf_regs.h13
-rw-r--r--tools/perf/util/pmu.c2
-rw-r--r--tools/perf/util/probe-event.c863
-rw-r--r--tools/perf/util/probe-event.h12
-rw-r--r--tools/perf/util/probe-finder.c198
-rw-r--r--tools/perf/util/probe-finder.h5
-rw-r--r--tools/perf/util/python-ext-sources2
-rw-r--r--tools/perf/util/record.c2
-rw-r--r--tools/perf/util/session.c7
-rw-r--r--tools/perf/util/symbol-elf.c8
-rw-r--r--tools/perf/util/symbol.c63
-rw-r--r--tools/perf/util/symbol.h14
-rw-r--r--tools/perf/util/thread.c21
-rw-r--r--tools/perf/util/thread.h11
-rw-r--r--tools/perf/util/trace-event-parse.c1
-rw-r--r--tools/perf/util/unwind-libdw.c210
-rw-r--r--tools/perf/util/unwind-libdw.h21
-rw-r--r--tools/perf/util/unwind-libunwind.c (renamed from tools/perf/util/unwind.c)50
-rw-r--r--tools/perf/util/unwind.h11
-rw-r--r--tools/perf/util/util.c2
43 files changed, 1235 insertions, 950 deletions
diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c
index 3aa555ff9d89..809b4c50beae 100644
--- a/tools/perf/util/annotate.c
+++ b/tools/perf/util/annotate.c
@@ -1236,6 +1236,7 @@ int symbol__annotate_printf(struct symbol *sym, struct map *map,
1236 struct dso *dso = map->dso; 1236 struct dso *dso = map->dso;
1237 char *filename; 1237 char *filename;
1238 const char *d_filename; 1238 const char *d_filename;
1239 const char *evsel_name = perf_evsel__name(evsel);
1239 struct annotation *notes = symbol__annotation(sym); 1240 struct annotation *notes = symbol__annotation(sym);
1240 struct disasm_line *pos, *queue = NULL; 1241 struct disasm_line *pos, *queue = NULL;
1241 u64 start = map__rip_2objdump(map, sym->start); 1242 u64 start = map__rip_2objdump(map, sym->start);
@@ -1243,7 +1244,7 @@ int symbol__annotate_printf(struct symbol *sym, struct map *map,
1243 int more = 0; 1244 int more = 0;
1244 u64 len; 1245 u64 len;
1245 int width = 8; 1246 int width = 8;
1246 int namelen; 1247 int namelen, evsel_name_len, graph_dotted_len;
1247 1248
1248 filename = strdup(dso->long_name); 1249 filename = strdup(dso->long_name);
1249 if (!filename) 1250 if (!filename)
@@ -1256,14 +1257,17 @@ int symbol__annotate_printf(struct symbol *sym, struct map *map,
1256 1257
1257 len = symbol__size(sym); 1258 len = symbol__size(sym);
1258 namelen = strlen(d_filename); 1259 namelen = strlen(d_filename);
1260 evsel_name_len = strlen(evsel_name);
1259 1261
1260 if (perf_evsel__is_group_event(evsel)) 1262 if (perf_evsel__is_group_event(evsel))
1261 width *= evsel->nr_members; 1263 width *= evsel->nr_members;
1262 1264
1263 printf(" %-*.*s| Source code & Disassembly of %s\n", 1265 printf(" %-*.*s| Source code & Disassembly of %s for %s\n",
1264 width, width, "Percent", d_filename); 1266 width, width, "Percent", d_filename, evsel_name);
1265 printf("-%-*.*s-------------------------------------\n", 1267
1266 width+namelen, width+namelen, graph_dotted_line); 1268 graph_dotted_len = width + namelen + evsel_name_len;
1269 printf("-%-*.*s-----------------------------------------\n",
1270 graph_dotted_len, graph_dotted_len, graph_dotted_line);
1267 1271
1268 if (verbose) 1272 if (verbose)
1269 symbol__annotate_hits(sym, evsel); 1273 symbol__annotate_hits(sym, evsel);
diff --git a/tools/perf/util/cpumap.c b/tools/perf/util/cpumap.c
index a9b48c42e81e..7fe4994eeb63 100644
--- a/tools/perf/util/cpumap.c
+++ b/tools/perf/util/cpumap.c
@@ -1,5 +1,5 @@
1#include "util.h" 1#include "util.h"
2#include "fs.h" 2#include <api/fs/fs.h>
3#include "../perf.h" 3#include "../perf.h"
4#include "cpumap.h" 4#include "cpumap.h"
5#include <assert.h> 5#include <assert.h>
diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
index 4045d086d9d9..64453d63b971 100644
--- a/tools/perf/util/dso.c
+++ b/tools/perf/util/dso.c
@@ -45,8 +45,8 @@ int dso__read_binary_type_filename(const struct dso *dso,
45 debuglink--; 45 debuglink--;
46 if (*debuglink == '/') 46 if (*debuglink == '/')
47 debuglink++; 47 debuglink++;
48 filename__read_debuglink(dso->long_name, debuglink, 48 ret = filename__read_debuglink(dso->long_name, debuglink,
49 size - (debuglink - filename)); 49 size - (debuglink - filename));
50 } 50 }
51 break; 51 break;
52 case DSO_BINARY_TYPE__BUILD_ID_CACHE: 52 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h
index cd7d6f078cdd..ab06f1c03655 100644
--- a/tools/perf/util/dso.h
+++ b/tools/perf/util/dso.h
@@ -102,6 +102,16 @@ struct dso {
102 char name[0]; 102 char name[0];
103}; 103};
104 104
105/* dso__for_each_symbol - iterate over the symbols of given type
106 *
107 * @dso: the 'struct dso *' in which symbols itereated
108 * @pos: the 'struct symbol *' to use as a loop cursor
109 * @n: the 'struct rb_node *' to use as a temporary storage
110 * @type: the 'enum map_type' type of symbols
111 */
112#define dso__for_each_symbol(dso, pos, n, type) \
113 symbols__for_each_entry(&(dso)->symbols[(type)], pos, n)
114
105static inline void dso__set_loaded(struct dso *dso, enum map_type type) 115static inline void dso__set_loaded(struct dso *dso, enum map_type type)
106{ 116{
107 dso->loaded |= (1 << type); 117 dso->loaded |= (1 << type);
diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c
index b0f3ca850e9e..9d12aa6dd485 100644
--- a/tools/perf/util/event.c
+++ b/tools/perf/util/event.c
@@ -1,6 +1,7 @@
1#include <linux/types.h> 1#include <linux/types.h>
2#include "event.h" 2#include "event.h"
3#include "debug.h" 3#include "debug.h"
4#include "hist.h"
4#include "machine.h" 5#include "machine.h"
5#include "sort.h" 6#include "sort.h"
6#include "string.h" 7#include "string.h"
@@ -94,14 +95,10 @@ static pid_t perf_event__get_comm_tgid(pid_t pid, char *comm, size_t len)
94 95
95static pid_t perf_event__synthesize_comm(struct perf_tool *tool, 96static pid_t perf_event__synthesize_comm(struct perf_tool *tool,
96 union perf_event *event, pid_t pid, 97 union perf_event *event, pid_t pid,
97 int full,
98 perf_event__handler_t process, 98 perf_event__handler_t process,
99 struct machine *machine) 99 struct machine *machine)
100{ 100{
101 char filename[PATH_MAX];
102 size_t size; 101 size_t size;
103 DIR *tasks;
104 struct dirent dirent, *next;
105 pid_t tgid; 102 pid_t tgid;
106 103
107 memset(&event->comm, 0, sizeof(event->comm)); 104 memset(&event->comm, 0, sizeof(event->comm));
@@ -124,55 +121,35 @@ static pid_t perf_event__synthesize_comm(struct perf_tool *tool,
124 event->comm.header.size = (sizeof(event->comm) - 121 event->comm.header.size = (sizeof(event->comm) -
125 (sizeof(event->comm.comm) - size) + 122 (sizeof(event->comm.comm) - size) +
126 machine->id_hdr_size); 123 machine->id_hdr_size);
127 if (!full) { 124 event->comm.tid = pid;
128 event->comm.tid = pid;
129 125
130 if (process(tool, event, &synth_sample, machine) != 0) 126 if (process(tool, event, &synth_sample, machine) != 0)
131 return -1; 127 return -1;
132
133 goto out;
134 }
135
136 if (machine__is_default_guest(machine))
137 return 0;
138
139 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
140 machine->root_dir, pid);
141
142 tasks = opendir(filename);
143 if (tasks == NULL) {
144 pr_debug("couldn't open %s\n", filename);
145 return 0;
146 }
147 128
148 while (!readdir_r(tasks, &dirent, &next) && next) { 129out:
149 char *end; 130 return tgid;
150 pid = strtol(dirent.d_name, &end, 10); 131}
151 if (*end)
152 continue;
153 132
154 /* already have tgid; jut want to update the comm */ 133static int perf_event__synthesize_fork(struct perf_tool *tool,
155 (void) perf_event__get_comm_tgid(pid, event->comm.comm, 134 union perf_event *event, pid_t pid,
156 sizeof(event->comm.comm)); 135 pid_t tgid, perf_event__handler_t process,
136 struct machine *machine)
137{
138 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
157 139
158 size = strlen(event->comm.comm) + 1; 140 /* this is really a clone event but we use fork to synthesize it */
159 size = PERF_ALIGN(size, sizeof(u64)); 141 event->fork.ppid = tgid;
160 memset(event->comm.comm + size, 0, machine->id_hdr_size); 142 event->fork.ptid = tgid;
161 event->comm.header.size = (sizeof(event->comm) - 143 event->fork.pid = tgid;
162 (sizeof(event->comm.comm) - size) + 144 event->fork.tid = pid;
163 machine->id_hdr_size); 145 event->fork.header.type = PERF_RECORD_FORK;
164 146
165 event->comm.tid = pid; 147 event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
166 148
167 if (process(tool, event, &synth_sample, machine) != 0) { 149 if (process(tool, event, &synth_sample, machine) != 0)
168 tgid = -1; 150 return -1;
169 break;
170 }
171 }
172 151
173 closedir(tasks); 152 return 0;
174out:
175 return tgid;
176} 153}
177 154
178int perf_event__synthesize_mmap_events(struct perf_tool *tool, 155int perf_event__synthesize_mmap_events(struct perf_tool *tool,
@@ -324,17 +301,71 @@ int perf_event__synthesize_modules(struct perf_tool *tool,
324 301
325static int __event__synthesize_thread(union perf_event *comm_event, 302static int __event__synthesize_thread(union perf_event *comm_event,
326 union perf_event *mmap_event, 303 union perf_event *mmap_event,
304 union perf_event *fork_event,
327 pid_t pid, int full, 305 pid_t pid, int full,
328 perf_event__handler_t process, 306 perf_event__handler_t process,
329 struct perf_tool *tool, 307 struct perf_tool *tool,
330 struct machine *machine, bool mmap_data) 308 struct machine *machine, bool mmap_data)
331{ 309{
332 pid_t tgid = perf_event__synthesize_comm(tool, comm_event, pid, full, 310 char filename[PATH_MAX];
311 DIR *tasks;
312 struct dirent dirent, *next;
313 pid_t tgid;
314
315 /* special case: only send one comm event using passed in pid */
316 if (!full) {
317 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
318 process, machine);
319
320 if (tgid == -1)
321 return -1;
322
323 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
324 process, machine, mmap_data);
325 }
326
327 if (machine__is_default_guest(machine))
328 return 0;
329
330 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
331 machine->root_dir, pid);
332
333 tasks = opendir(filename);
334 if (tasks == NULL) {
335 pr_debug("couldn't open %s\n", filename);
336 return 0;
337 }
338
339 while (!readdir_r(tasks, &dirent, &next) && next) {
340 char *end;
341 int rc = 0;
342 pid_t _pid;
343
344 _pid = strtol(dirent.d_name, &end, 10);
345 if (*end)
346 continue;
347
348 tgid = perf_event__synthesize_comm(tool, comm_event, _pid,
349 process, machine);
350 if (tgid == -1)
351 return -1;
352
353 if (_pid == pid) {
354 /* process the parent's maps too */
355 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
356 process, machine, mmap_data);
357 } else {
358 /* only fork the tid's map, to save time */
359 rc = perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
333 process, machine); 360 process, machine);
334 if (tgid == -1) 361 }
335 return -1; 362
336 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid, 363 if (rc)
337 process, machine, mmap_data); 364 return rc;
365 }
366
367 closedir(tasks);
368 return 0;
338} 369}
339 370
340int perf_event__synthesize_thread_map(struct perf_tool *tool, 371int perf_event__synthesize_thread_map(struct perf_tool *tool,
@@ -343,7 +374,7 @@ int perf_event__synthesize_thread_map(struct perf_tool *tool,
343 struct machine *machine, 374 struct machine *machine,
344 bool mmap_data) 375 bool mmap_data)
345{ 376{
346 union perf_event *comm_event, *mmap_event; 377 union perf_event *comm_event, *mmap_event, *fork_event;
347 int err = -1, thread, j; 378 int err = -1, thread, j;
348 379
349 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size); 380 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
@@ -354,9 +385,14 @@ int perf_event__synthesize_thread_map(struct perf_tool *tool,
354 if (mmap_event == NULL) 385 if (mmap_event == NULL)
355 goto out_free_comm; 386 goto out_free_comm;
356 387
388 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
389 if (fork_event == NULL)
390 goto out_free_mmap;
391
357 err = 0; 392 err = 0;
358 for (thread = 0; thread < threads->nr; ++thread) { 393 for (thread = 0; thread < threads->nr; ++thread) {
359 if (__event__synthesize_thread(comm_event, mmap_event, 394 if (__event__synthesize_thread(comm_event, mmap_event,
395 fork_event,
360 threads->map[thread], 0, 396 threads->map[thread], 0,
361 process, tool, machine, 397 process, tool, machine,
362 mmap_data)) { 398 mmap_data)) {
@@ -382,6 +418,7 @@ int perf_event__synthesize_thread_map(struct perf_tool *tool,
382 /* if not, generate events for it */ 418 /* if not, generate events for it */
383 if (need_leader && 419 if (need_leader &&
384 __event__synthesize_thread(comm_event, mmap_event, 420 __event__synthesize_thread(comm_event, mmap_event,
421 fork_event,
385 comm_event->comm.pid, 0, 422 comm_event->comm.pid, 0,
386 process, tool, machine, 423 process, tool, machine,
387 mmap_data)) { 424 mmap_data)) {
@@ -390,6 +427,8 @@ int perf_event__synthesize_thread_map(struct perf_tool *tool,
390 } 427 }
391 } 428 }
392 } 429 }
430 free(fork_event);
431out_free_mmap:
393 free(mmap_event); 432 free(mmap_event);
394out_free_comm: 433out_free_comm:
395 free(comm_event); 434 free(comm_event);
@@ -404,9 +443,12 @@ int perf_event__synthesize_threads(struct perf_tool *tool,
404 DIR *proc; 443 DIR *proc;
405 char proc_path[PATH_MAX]; 444 char proc_path[PATH_MAX];
406 struct dirent dirent, *next; 445 struct dirent dirent, *next;
407 union perf_event *comm_event, *mmap_event; 446 union perf_event *comm_event, *mmap_event, *fork_event;
408 int err = -1; 447 int err = -1;
409 448
449 if (machine__is_default_guest(machine))
450 return 0;
451
410 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size); 452 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
411 if (comm_event == NULL) 453 if (comm_event == NULL)
412 goto out; 454 goto out;
@@ -415,14 +457,15 @@ int perf_event__synthesize_threads(struct perf_tool *tool,
415 if (mmap_event == NULL) 457 if (mmap_event == NULL)
416 goto out_free_comm; 458 goto out_free_comm;
417 459
418 if (machine__is_default_guest(machine)) 460 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
419 return 0; 461 if (fork_event == NULL)
462 goto out_free_mmap;
420 463
421 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir); 464 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
422 proc = opendir(proc_path); 465 proc = opendir(proc_path);
423 466
424 if (proc == NULL) 467 if (proc == NULL)
425 goto out_free_mmap; 468 goto out_free_fork;
426 469
427 while (!readdir_r(proc, &dirent, &next) && next) { 470 while (!readdir_r(proc, &dirent, &next) && next) {
428 char *end; 471 char *end;
@@ -434,12 +477,14 @@ int perf_event__synthesize_threads(struct perf_tool *tool,
434 * We may race with exiting thread, so don't stop just because 477 * We may race with exiting thread, so don't stop just because
435 * one thread couldn't be synthesized. 478 * one thread couldn't be synthesized.
436 */ 479 */
437 __event__synthesize_thread(comm_event, mmap_event, pid, 1, 480 __event__synthesize_thread(comm_event, mmap_event, fork_event, pid,
438 process, tool, machine, mmap_data); 481 1, process, tool, machine, mmap_data);
439 } 482 }
440 483
441 err = 0; 484 err = 0;
442 closedir(proc); 485 closedir(proc);
486out_free_fork:
487 free(fork_event);
443out_free_mmap: 488out_free_mmap:
444 free(mmap_event); 489 free(mmap_event);
445out_free_comm: 490out_free_comm:
@@ -661,7 +706,7 @@ void thread__find_addr_map(struct thread *thread,
661 al->thread = thread; 706 al->thread = thread;
662 al->addr = addr; 707 al->addr = addr;
663 al->cpumode = cpumode; 708 al->cpumode = cpumode;
664 al->filtered = false; 709 al->filtered = 0;
665 710
666 if (machine == NULL) { 711 if (machine == NULL) {
667 al->map = NULL; 712 al->map = NULL;
@@ -687,11 +732,11 @@ void thread__find_addr_map(struct thread *thread,
687 if ((cpumode == PERF_RECORD_MISC_GUEST_USER || 732 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
688 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) && 733 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
689 !perf_guest) 734 !perf_guest)
690 al->filtered = true; 735 al->filtered |= (1 << HIST_FILTER__GUEST);
691 if ((cpumode == PERF_RECORD_MISC_USER || 736 if ((cpumode == PERF_RECORD_MISC_USER ||
692 cpumode == PERF_RECORD_MISC_KERNEL) && 737 cpumode == PERF_RECORD_MISC_KERNEL) &&
693 !perf_host) 738 !perf_host)
694 al->filtered = true; 739 al->filtered |= (1 << HIST_FILTER__HOST);
695 740
696 return; 741 return;
697 } 742 }
@@ -748,9 +793,6 @@ int perf_event__preprocess_sample(const union perf_event *event,
748 if (thread == NULL) 793 if (thread == NULL)
749 return -1; 794 return -1;
750 795
751 if (thread__is_filtered(thread))
752 goto out_filtered;
753
754 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid); 796 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
755 /* 797 /*
756 * Have we already created the kernel maps for this machine? 798 * Have we already created the kernel maps for this machine?
@@ -768,6 +810,10 @@ int perf_event__preprocess_sample(const union perf_event *event,
768 dump_printf(" ...... dso: %s\n", 810 dump_printf(" ...... dso: %s\n",
769 al->map ? al->map->dso->long_name : 811 al->map ? al->map->dso->long_name :
770 al->level == 'H' ? "[hypervisor]" : "<not found>"); 812 al->level == 'H' ? "[hypervisor]" : "<not found>");
813
814 if (thread__is_filtered(thread))
815 al->filtered |= (1 << HIST_FILTER__THREAD);
816
771 al->sym = NULL; 817 al->sym = NULL;
772 al->cpu = sample->cpu; 818 al->cpu = sample->cpu;
773 819
@@ -779,8 +825,9 @@ int perf_event__preprocess_sample(const union perf_event *event,
779 dso->short_name) || 825 dso->short_name) ||
780 (dso->short_name != dso->long_name && 826 (dso->short_name != dso->long_name &&
781 strlist__has_entry(symbol_conf.dso_list, 827 strlist__has_entry(symbol_conf.dso_list,
782 dso->long_name))))) 828 dso->long_name))))) {
783 goto out_filtered; 829 al->filtered |= (1 << HIST_FILTER__DSO);
830 }
784 831
785 al->sym = map__find_symbol(al->map, al->addr, 832 al->sym = map__find_symbol(al->map, al->addr,
786 machine->symbol_filter); 833 machine->symbol_filter);
@@ -788,12 +835,9 @@ int perf_event__preprocess_sample(const union perf_event *event,
788 835
789 if (symbol_conf.sym_list && 836 if (symbol_conf.sym_list &&
790 (!al->sym || !strlist__has_entry(symbol_conf.sym_list, 837 (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
791 al->sym->name))) 838 al->sym->name))) {
792 goto out_filtered; 839 al->filtered |= (1 << HIST_FILTER__SYMBOL);
793 840 }
794 return 0;
795 841
796out_filtered:
797 al->filtered = true;
798 return 0; 842 return 0;
799} 843}
diff --git a/tools/perf/util/event.h b/tools/perf/util/event.h
index 851fa06f4a42..38457d447a13 100644
--- a/tools/perf/util/event.h
+++ b/tools/perf/util/event.h
@@ -85,6 +85,7 @@ struct sample_event {
85 85
86struct regs_dump { 86struct regs_dump {
87 u64 abi; 87 u64 abi;
88 u64 mask;
88 u64 *regs; 89 u64 *regs;
89}; 90};
90 91
@@ -259,9 +260,9 @@ int perf_event__preprocess_sample(const union perf_event *event,
259const char *perf_event__name(unsigned int id); 260const char *perf_event__name(unsigned int id);
260 261
261size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type, 262size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
262 u64 sample_regs_user, u64 read_format); 263 u64 read_format);
263int perf_event__synthesize_sample(union perf_event *event, u64 type, 264int perf_event__synthesize_sample(union perf_event *event, u64 type,
264 u64 sample_regs_user, u64 read_format, 265 u64 read_format,
265 const struct perf_sample *sample, 266 const struct perf_sample *sample,
266 bool swapped); 267 bool swapped);
267 268
diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index 55407c594b87..5c28d82b76c4 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -500,6 +500,34 @@ int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
500 return ret; 500 return ret;
501} 501}
502 502
503static void
504perf_evsel__config_callgraph(struct perf_evsel *evsel,
505 struct record_opts *opts)
506{
507 bool function = perf_evsel__is_function_event(evsel);
508 struct perf_event_attr *attr = &evsel->attr;
509
510 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
511
512 if (opts->call_graph == CALLCHAIN_DWARF) {
513 if (!function) {
514 perf_evsel__set_sample_bit(evsel, REGS_USER);
515 perf_evsel__set_sample_bit(evsel, STACK_USER);
516 attr->sample_regs_user = PERF_REGS_MASK;
517 attr->sample_stack_user = opts->stack_dump_size;
518 attr->exclude_callchain_user = 1;
519 } else {
520 pr_info("Cannot use DWARF unwind for function trace event,"
521 " falling back to framepointers.\n");
522 }
523 }
524
525 if (function) {
526 pr_info("Disabling user space callchains for function trace event.\n");
527 attr->exclude_callchain_user = 1;
528 }
529}
530
503/* 531/*
504 * The enable_on_exec/disabled value strategy: 532 * The enable_on_exec/disabled value strategy:
505 * 533 *
@@ -595,17 +623,8 @@ void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts)
595 attr->mmap_data = track; 623 attr->mmap_data = track;
596 } 624 }
597 625
598 if (opts->call_graph) { 626 if (opts->call_graph_enabled)
599 perf_evsel__set_sample_bit(evsel, CALLCHAIN); 627 perf_evsel__config_callgraph(evsel, opts);
600
601 if (opts->call_graph == CALLCHAIN_DWARF) {
602 perf_evsel__set_sample_bit(evsel, REGS_USER);
603 perf_evsel__set_sample_bit(evsel, STACK_USER);
604 attr->sample_regs_user = PERF_REGS_MASK;
605 attr->sample_stack_user = opts->stack_dump_size;
606 attr->exclude_callchain_user = 1;
607 }
608 }
609 628
610 if (target__has_cpu(&opts->target)) 629 if (target__has_cpu(&opts->target))
611 perf_evsel__set_sample_bit(evsel, CPU); 630 perf_evsel__set_sample_bit(evsel, CPU);
@@ -1004,7 +1023,7 @@ retry_sample_id:
1004 1023
1005 group_fd = get_group_fd(evsel, cpu, thread); 1024 group_fd = get_group_fd(evsel, cpu, thread);
1006retry_open: 1025retry_open:
1007 pr_debug2("perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n", 1026 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
1008 pid, cpus->map[cpu], group_fd, flags); 1027 pid, cpus->map[cpu], group_fd, flags);
1009 1028
1010 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr, 1029 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
@@ -1013,7 +1032,7 @@ retry_open:
1013 group_fd, flags); 1032 group_fd, flags);
1014 if (FD(evsel, cpu, thread) < 0) { 1033 if (FD(evsel, cpu, thread) < 0) {
1015 err = -errno; 1034 err = -errno;
1016 pr_debug2("perf_event_open failed, error %d\n", 1035 pr_debug2("sys_perf_event_open failed, error %d\n",
1017 err); 1036 err);
1018 goto try_fallback; 1037 goto try_fallback;
1019 } 1038 }
@@ -1220,7 +1239,7 @@ int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
1220 memset(data, 0, sizeof(*data)); 1239 memset(data, 0, sizeof(*data));
1221 data->cpu = data->pid = data->tid = -1; 1240 data->cpu = data->pid = data->tid = -1;
1222 data->stream_id = data->id = data->time = -1ULL; 1241 data->stream_id = data->id = data->time = -1ULL;
1223 data->period = 1; 1242 data->period = evsel->attr.sample_period;
1224 data->weight = 0; 1243 data->weight = 0;
1225 1244
1226 if (event->header.type != PERF_RECORD_SAMPLE) { 1245 if (event->header.type != PERF_RECORD_SAMPLE) {
@@ -1396,10 +1415,11 @@ int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
1396 array++; 1415 array++;
1397 1416
1398 if (data->user_regs.abi) { 1417 if (data->user_regs.abi) {
1399 u64 regs_user = evsel->attr.sample_regs_user; 1418 u64 mask = evsel->attr.sample_regs_user;
1400 1419
1401 sz = hweight_long(regs_user) * sizeof(u64); 1420 sz = hweight_long(mask) * sizeof(u64);
1402 OVERFLOW_CHECK(array, sz, max_size); 1421 OVERFLOW_CHECK(array, sz, max_size);
1422 data->user_regs.mask = mask;
1403 data->user_regs.regs = (u64 *)array; 1423 data->user_regs.regs = (u64 *)array;
1404 array = (void *)array + sz; 1424 array = (void *)array + sz;
1405 } 1425 }
@@ -1451,7 +1471,7 @@ int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
1451} 1471}
1452 1472
1453size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type, 1473size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
1454 u64 sample_regs_user, u64 read_format) 1474 u64 read_format)
1455{ 1475{
1456 size_t sz, result = sizeof(struct sample_event); 1476 size_t sz, result = sizeof(struct sample_event);
1457 1477
@@ -1517,7 +1537,7 @@ size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
1517 if (type & PERF_SAMPLE_REGS_USER) { 1537 if (type & PERF_SAMPLE_REGS_USER) {
1518 if (sample->user_regs.abi) { 1538 if (sample->user_regs.abi) {
1519 result += sizeof(u64); 1539 result += sizeof(u64);
1520 sz = hweight_long(sample_regs_user) * sizeof(u64); 1540 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
1521 result += sz; 1541 result += sz;
1522 } else { 1542 } else {
1523 result += sizeof(u64); 1543 result += sizeof(u64);
@@ -1546,7 +1566,7 @@ size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
1546} 1566}
1547 1567
1548int perf_event__synthesize_sample(union perf_event *event, u64 type, 1568int perf_event__synthesize_sample(union perf_event *event, u64 type,
1549 u64 sample_regs_user, u64 read_format, 1569 u64 read_format,
1550 const struct perf_sample *sample, 1570 const struct perf_sample *sample,
1551 bool swapped) 1571 bool swapped)
1552{ 1572{
@@ -1687,7 +1707,7 @@ int perf_event__synthesize_sample(union perf_event *event, u64 type,
1687 if (type & PERF_SAMPLE_REGS_USER) { 1707 if (type & PERF_SAMPLE_REGS_USER) {
1688 if (sample->user_regs.abi) { 1708 if (sample->user_regs.abi) {
1689 *array++ = sample->user_regs.abi; 1709 *array++ = sample->user_regs.abi;
1690 sz = hweight_long(sample_regs_user) * sizeof(u64); 1710 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
1691 memcpy(array, sample->user_regs.regs, sz); 1711 memcpy(array, sample->user_regs.regs, sz);
1692 array = (void *)array + sz; 1712 array = (void *)array + sz;
1693 } else { 1713 } else {
diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
index f1b325665aae..0c9926cfb292 100644
--- a/tools/perf/util/evsel.h
+++ b/tools/perf/util/evsel.h
@@ -315,6 +315,24 @@ static inline bool perf_evsel__is_group_event(struct perf_evsel *evsel)
315 return perf_evsel__is_group_leader(evsel) && evsel->nr_members > 1; 315 return perf_evsel__is_group_leader(evsel) && evsel->nr_members > 1;
316} 316}
317 317
318/**
319 * perf_evsel__is_function_event - Return whether given evsel is a function
320 * trace event
321 *
322 * @evsel - evsel selector to be tested
323 *
324 * Return %true if event is function trace event
325 */
326static inline bool perf_evsel__is_function_event(struct perf_evsel *evsel)
327{
328#define FUNCTION_EVENT "ftrace:function"
329
330 return evsel->name &&
331 !strncmp(FUNCTION_EVENT, evsel->name, sizeof(FUNCTION_EVENT));
332
333#undef FUNCTION_EVENT
334}
335
318struct perf_attr_details { 336struct perf_attr_details {
319 bool freq; 337 bool freq;
320 bool verbose; 338 bool verbose;
diff --git a/tools/perf/util/fs.c b/tools/perf/util/fs.c
deleted file mode 100644
index f5be1f26e724..000000000000
--- a/tools/perf/util/fs.c
+++ /dev/null
@@ -1,119 +0,0 @@
1
2/* TODO merge/factor into tools/lib/lk/debugfs.c */
3
4#include "util.h"
5#include "util/fs.h"
6
7static const char * const sysfs__fs_known_mountpoints[] = {
8 "/sys",
9 0,
10};
11
12static const char * const procfs__known_mountpoints[] = {
13 "/proc",
14 0,
15};
16
17struct fs {
18 const char *name;
19 const char * const *mounts;
20 char path[PATH_MAX + 1];
21 bool found;
22 long magic;
23};
24
25enum {
26 FS__SYSFS = 0,
27 FS__PROCFS = 1,
28};
29
30static struct fs fs__entries[] = {
31 [FS__SYSFS] = {
32 .name = "sysfs",
33 .mounts = sysfs__fs_known_mountpoints,
34 .magic = SYSFS_MAGIC,
35 },
36 [FS__PROCFS] = {
37 .name = "proc",
38 .mounts = procfs__known_mountpoints,
39 .magic = PROC_SUPER_MAGIC,
40 },
41};
42
43static bool fs__read_mounts(struct fs *fs)
44{
45 bool found = false;
46 char type[100];
47 FILE *fp;
48
49 fp = fopen("/proc/mounts", "r");
50 if (fp == NULL)
51 return NULL;
52
53 while (!found &&
54 fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
55 fs->path, type) == 2) {
56
57 if (strcmp(type, fs->name) == 0)
58 found = true;
59 }
60
61 fclose(fp);
62 return fs->found = found;
63}
64
65static int fs__valid_mount(const char *fs, long magic)
66{
67 struct statfs st_fs;
68
69 if (statfs(fs, &st_fs) < 0)
70 return -ENOENT;
71 else if (st_fs.f_type != magic)
72 return -ENOENT;
73
74 return 0;
75}
76
77static bool fs__check_mounts(struct fs *fs)
78{
79 const char * const *ptr;
80
81 ptr = fs->mounts;
82 while (*ptr) {
83 if (fs__valid_mount(*ptr, fs->magic) == 0) {
84 fs->found = true;
85 strcpy(fs->path, *ptr);
86 return true;
87 }
88 ptr++;
89 }
90
91 return false;
92}
93
94static const char *fs__get_mountpoint(struct fs *fs)
95{
96 if (fs__check_mounts(fs))
97 return fs->path;
98
99 return fs__read_mounts(fs) ? fs->path : NULL;
100}
101
102static const char *fs__mountpoint(int idx)
103{
104 struct fs *fs = &fs__entries[idx];
105
106 if (fs->found)
107 return (const char *)fs->path;
108
109 return fs__get_mountpoint(fs);
110}
111
112#define FS__MOUNTPOINT(name, idx) \
113const char *name##__mountpoint(void) \
114{ \
115 return fs__mountpoint(idx); \
116}
117
118FS__MOUNTPOINT(sysfs, FS__SYSFS);
119FS__MOUNTPOINT(procfs, FS__PROCFS);
diff --git a/tools/perf/util/fs.h b/tools/perf/util/fs.h
deleted file mode 100644
index 5e09ce1bab0e..000000000000
--- a/tools/perf/util/fs.h
+++ /dev/null
@@ -1,7 +0,0 @@
1#ifndef __PERF_FS
2#define __PERF_FS
3
4const char *sysfs__mountpoint(void);
5const char *procfs__mountpoint(void);
6
7#endif /* __PERF_FS */
diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c
index e4e6249b87d4..f38590d7561b 100644
--- a/tools/perf/util/hist.c
+++ b/tools/perf/util/hist.c
@@ -13,13 +13,6 @@ static bool hists__filter_entry_by_thread(struct hists *hists,
13static bool hists__filter_entry_by_symbol(struct hists *hists, 13static bool hists__filter_entry_by_symbol(struct hists *hists,
14 struct hist_entry *he); 14 struct hist_entry *he);
15 15
16enum hist_filter {
17 HIST_FILTER__DSO,
18 HIST_FILTER__THREAD,
19 HIST_FILTER__PARENT,
20 HIST_FILTER__SYMBOL,
21};
22
23struct callchain_param callchain_param = { 16struct callchain_param callchain_param = {
24 .mode = CHAIN_GRAPH_REL, 17 .mode = CHAIN_GRAPH_REL,
25 .min_percent = 0.5, 18 .min_percent = 0.5,
@@ -290,7 +283,7 @@ static struct hist_entry *hist_entry__new(struct hist_entry *template)
290 if (he->branch_info) { 283 if (he->branch_info) {
291 /* 284 /*
292 * This branch info is (a part of) allocated from 285 * This branch info is (a part of) allocated from
293 * machine__resolve_bstack() and will be freed after 286 * sample__resolve_bstack() and will be freed after
294 * adding new entries. So we need to save a copy. 287 * adding new entries. So we need to save a copy.
295 */ 288 */
296 he->branch_info = malloc(sizeof(*he->branch_info)); 289 he->branch_info = malloc(sizeof(*he->branch_info));
@@ -369,7 +362,7 @@ static struct hist_entry *add_hist_entry(struct hists *hists,
369 he_stat__add_period(&he->stat, period, weight); 362 he_stat__add_period(&he->stat, period, weight);
370 363
371 /* 364 /*
372 * This mem info was allocated from machine__resolve_mem 365 * This mem info was allocated from sample__resolve_mem
373 * and will not be used anymore. 366 * and will not be used anymore.
374 */ 367 */
375 zfree(&entry->mem_info); 368 zfree(&entry->mem_info);
@@ -429,7 +422,7 @@ struct hist_entry *__hists__add_entry(struct hists *hists,
429 .weight = weight, 422 .weight = weight,
430 }, 423 },
431 .parent = sym_parent, 424 .parent = sym_parent,
432 .filtered = symbol__parent_filter(sym_parent), 425 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
433 .hists = hists, 426 .hists = hists,
434 .branch_info = bi, 427 .branch_info = bi,
435 .mem_info = mi, 428 .mem_info = mi,
diff --git a/tools/perf/util/hist.h b/tools/perf/util/hist.h
index a59743fa3ef7..1f1f513dfe7f 100644
--- a/tools/perf/util/hist.h
+++ b/tools/perf/util/hist.h
@@ -14,6 +14,15 @@ struct hist_entry;
14struct addr_location; 14struct addr_location;
15struct symbol; 15struct symbol;
16 16
17enum hist_filter {
18 HIST_FILTER__DSO,
19 HIST_FILTER__THREAD,
20 HIST_FILTER__PARENT,
21 HIST_FILTER__SYMBOL,
22 HIST_FILTER__GUEST,
23 HIST_FILTER__HOST,
24};
25
17/* 26/*
18 * The kernel collects the number of events it couldn't send in a stretch and 27 * The kernel collects the number of events it couldn't send in a stretch and
19 * when possible sends this number in a PERF_RECORD_LOST event. The number of 28 * when possible sends this number in a PERF_RECORD_LOST event. The number of
@@ -132,8 +141,10 @@ struct perf_hpp {
132}; 141};
133 142
134struct perf_hpp_fmt { 143struct perf_hpp_fmt {
135 int (*header)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp); 144 int (*header)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
136 int (*width)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp); 145 struct perf_evsel *evsel);
146 int (*width)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
147 struct perf_evsel *evsel);
137 int (*color)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 148 int (*color)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
138 struct hist_entry *he); 149 struct hist_entry *he);
139 int (*entry)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 150 int (*entry)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
@@ -166,6 +177,20 @@ void perf_hpp__init(void);
166void perf_hpp__column_register(struct perf_hpp_fmt *format); 177void perf_hpp__column_register(struct perf_hpp_fmt *format);
167void perf_hpp__column_enable(unsigned col); 178void perf_hpp__column_enable(unsigned col);
168 179
180typedef u64 (*hpp_field_fn)(struct hist_entry *he);
181typedef int (*hpp_callback_fn)(struct perf_hpp *hpp, bool front);
182typedef int (*hpp_snprint_fn)(struct perf_hpp *hpp, const char *fmt, ...);
183
184int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he,
185 hpp_field_fn get_field, hpp_callback_fn callback,
186 const char *fmt, hpp_snprint_fn print_fn, bool fmt_percent);
187
188static inline void advance_hpp(struct perf_hpp *hpp, int inc)
189{
190 hpp->buf += inc;
191 hpp->size -= inc;
192}
193
169static inline size_t perf_hpp__use_color(void) 194static inline size_t perf_hpp__use_color(void)
170{ 195{
171 return !symbol_conf.field_sep; 196 return !symbol_conf.field_sep;
diff --git a/tools/perf/util/include/linux/hash.h b/tools/perf/util/include/linux/hash.h
deleted file mode 100644
index 201f57397997..000000000000
--- a/tools/perf/util/include/linux/hash.h
+++ /dev/null
@@ -1,5 +0,0 @@
1#include "../../../../include/linux/hash.h"
2
3#ifndef PERF_HASH_H
4#define PERF_HASH_H
5#endif
diff --git a/tools/perf/util/include/linux/kernel.h b/tools/perf/util/include/linux/kernel.h
index d8c927c868ee..9844c31b7c2b 100644
--- a/tools/perf/util/include/linux/kernel.h
+++ b/tools/perf/util/include/linux/kernel.h
@@ -94,12 +94,6 @@ static inline int scnprintf(char * buf, size_t size, const char * fmt, ...)
94 return (i >= ssize) ? (ssize - 1) : i; 94 return (i >= ssize) ? (ssize - 1) : i;
95} 95}
96 96
97static inline unsigned long
98simple_strtoul(const char *nptr, char **endptr, int base)
99{
100 return strtoul(nptr, endptr, base);
101}
102
103int eprintf(int level, 97int eprintf(int level,
104 const char *fmt, ...) __attribute__((format(printf, 2, 3))); 98 const char *fmt, ...) __attribute__((format(printf, 2, 3)));
105 99
diff --git a/tools/perf/util/include/linux/list.h b/tools/perf/util/include/linux/list.h
index 1d928a0ce997..bfe0a2afd0d2 100644
--- a/tools/perf/util/include/linux/list.h
+++ b/tools/perf/util/include/linux/list.h
@@ -1,5 +1,4 @@
1#include <linux/kernel.h> 1#include <linux/kernel.h>
2#include <linux/prefetch.h>
3 2
4#include "../../../../include/linux/list.h" 3#include "../../../../include/linux/list.h"
5 4
diff --git a/tools/perf/util/include/linux/magic.h b/tools/perf/util/include/linux/magic.h
deleted file mode 100644
index 07d63cf3e0f6..000000000000
--- a/tools/perf/util/include/linux/magic.h
+++ /dev/null
@@ -1,16 +0,0 @@
1#ifndef _PERF_LINUX_MAGIC_H_
2#define _PERF_LINUX_MAGIC_H_
3
4#ifndef DEBUGFS_MAGIC
5#define DEBUGFS_MAGIC 0x64626720
6#endif
7
8#ifndef SYSFS_MAGIC
9#define SYSFS_MAGIC 0x62656572
10#endif
11
12#ifndef PROC_SUPER_MAGIC
13#define PROC_SUPER_MAGIC 0x9fa0
14#endif
15
16#endif
diff --git a/tools/perf/util/include/linux/prefetch.h b/tools/perf/util/include/linux/prefetch.h
deleted file mode 100644
index 7841e485d8c3..000000000000
--- a/tools/perf/util/include/linux/prefetch.h
+++ /dev/null
@@ -1,6 +0,0 @@
1#ifndef PERF_LINUX_PREFETCH_H
2#define PERF_LINUX_PREFETCH_H
3
4static inline void prefetch(void *a __attribute__((unused))) { }
5
6#endif
diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c
index c872991e0f65..a53cd0b8c151 100644
--- a/tools/perf/util/machine.c
+++ b/tools/perf/util/machine.c
@@ -327,9 +327,10 @@ struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
327 return __machine__findnew_thread(machine, pid, tid, true); 327 return __machine__findnew_thread(machine, pid, tid, true);
328} 328}
329 329
330struct thread *machine__find_thread(struct machine *machine, pid_t tid) 330struct thread *machine__find_thread(struct machine *machine, pid_t pid,
331 pid_t tid)
331{ 332{
332 return __machine__findnew_thread(machine, 0, tid, false); 333 return __machine__findnew_thread(machine, pid, tid, false);
333} 334}
334 335
335int machine__process_comm_event(struct machine *machine, union perf_event *event, 336int machine__process_comm_event(struct machine *machine, union perf_event *event,
@@ -1026,7 +1027,7 @@ int machine__process_mmap2_event(struct machine *machine,
1026 } 1027 }
1027 1028
1028 thread = machine__findnew_thread(machine, event->mmap2.pid, 1029 thread = machine__findnew_thread(machine, event->mmap2.pid,
1029 event->mmap2.pid); 1030 event->mmap2.tid);
1030 if (thread == NULL) 1031 if (thread == NULL)
1031 goto out_problem; 1032 goto out_problem;
1032 1033
@@ -1074,7 +1075,7 @@ int machine__process_mmap_event(struct machine *machine, union perf_event *event
1074 } 1075 }
1075 1076
1076 thread = machine__findnew_thread(machine, event->mmap.pid, 1077 thread = machine__findnew_thread(machine, event->mmap.pid,
1077 event->mmap.pid); 1078 event->mmap.tid);
1078 if (thread == NULL) 1079 if (thread == NULL)
1079 goto out_problem; 1080 goto out_problem;
1080 1081
@@ -1114,7 +1115,9 @@ static void machine__remove_thread(struct machine *machine, struct thread *th)
1114int machine__process_fork_event(struct machine *machine, union perf_event *event, 1115int machine__process_fork_event(struct machine *machine, union perf_event *event,
1115 struct perf_sample *sample) 1116 struct perf_sample *sample)
1116{ 1117{
1117 struct thread *thread = machine__find_thread(machine, event->fork.tid); 1118 struct thread *thread = machine__find_thread(machine,
1119 event->fork.pid,
1120 event->fork.tid);
1118 struct thread *parent = machine__findnew_thread(machine, 1121 struct thread *parent = machine__findnew_thread(machine,
1119 event->fork.ppid, 1122 event->fork.ppid,
1120 event->fork.ptid); 1123 event->fork.ptid);
@@ -1140,7 +1143,9 @@ int machine__process_fork_event(struct machine *machine, union perf_event *event
1140int machine__process_exit_event(struct machine *machine, union perf_event *event, 1143int machine__process_exit_event(struct machine *machine, union perf_event *event,
1141 struct perf_sample *sample __maybe_unused) 1144 struct perf_sample *sample __maybe_unused)
1142{ 1145{
1143 struct thread *thread = machine__find_thread(machine, event->fork.tid); 1146 struct thread *thread = machine__find_thread(machine,
1147 event->fork.pid,
1148 event->fork.tid);
1144 1149
1145 if (dump_trace) 1150 if (dump_trace)
1146 perf_event__fprintf_task(event, stdout); 1151 perf_event__fprintf_task(event, stdout);
@@ -1184,39 +1189,22 @@ static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
1184 return 0; 1189 return 0;
1185} 1190}
1186 1191
1187static const u8 cpumodes[] = {
1188 PERF_RECORD_MISC_USER,
1189 PERF_RECORD_MISC_KERNEL,
1190 PERF_RECORD_MISC_GUEST_USER,
1191 PERF_RECORD_MISC_GUEST_KERNEL
1192};
1193#define NCPUMODES (sizeof(cpumodes)/sizeof(u8))
1194
1195static void ip__resolve_ams(struct machine *machine, struct thread *thread, 1192static void ip__resolve_ams(struct machine *machine, struct thread *thread,
1196 struct addr_map_symbol *ams, 1193 struct addr_map_symbol *ams,
1197 u64 ip) 1194 u64 ip)
1198{ 1195{
1199 struct addr_location al; 1196 struct addr_location al;
1200 size_t i;
1201 u8 m;
1202 1197
1203 memset(&al, 0, sizeof(al)); 1198 memset(&al, 0, sizeof(al));
1199 /*
1200 * We cannot use the header.misc hint to determine whether a
1201 * branch stack address is user, kernel, guest, hypervisor.
1202 * Branches may straddle the kernel/user/hypervisor boundaries.
1203 * Thus, we have to try consecutively until we find a match
1204 * or else, the symbol is unknown
1205 */
1206 thread__find_cpumode_addr_location(thread, machine, MAP__FUNCTION, ip, &al);
1204 1207
1205 for (i = 0; i < NCPUMODES; i++) {
1206 m = cpumodes[i];
1207 /*
1208 * We cannot use the header.misc hint to determine whether a
1209 * branch stack address is user, kernel, guest, hypervisor.
1210 * Branches may straddle the kernel/user/hypervisor boundaries.
1211 * Thus, we have to try consecutively until we find a match
1212 * or else, the symbol is unknown
1213 */
1214 thread__find_addr_location(thread, machine, m, MAP__FUNCTION,
1215 ip, &al);
1216 if (al.sym)
1217 goto found;
1218 }
1219found:
1220 ams->addr = ip; 1208 ams->addr = ip;
1221 ams->al_addr = al.addr; 1209 ams->al_addr = al.addr;
1222 ams->sym = al.sym; 1210 ams->sym = al.sym;
@@ -1238,37 +1226,35 @@ static void ip__resolve_data(struct machine *machine, struct thread *thread,
1238 ams->map = al.map; 1226 ams->map = al.map;
1239} 1227}
1240 1228
1241struct mem_info *machine__resolve_mem(struct machine *machine, 1229struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1242 struct thread *thr, 1230 struct addr_location *al)
1243 struct perf_sample *sample,
1244 u8 cpumode)
1245{ 1231{
1246 struct mem_info *mi = zalloc(sizeof(*mi)); 1232 struct mem_info *mi = zalloc(sizeof(*mi));
1247 1233
1248 if (!mi) 1234 if (!mi)
1249 return NULL; 1235 return NULL;
1250 1236
1251 ip__resolve_ams(machine, thr, &mi->iaddr, sample->ip); 1237 ip__resolve_ams(al->machine, al->thread, &mi->iaddr, sample->ip);
1252 ip__resolve_data(machine, thr, cpumode, &mi->daddr, sample->addr); 1238 ip__resolve_data(al->machine, al->thread, al->cpumode,
1239 &mi->daddr, sample->addr);
1253 mi->data_src.val = sample->data_src; 1240 mi->data_src.val = sample->data_src;
1254 1241
1255 return mi; 1242 return mi;
1256} 1243}
1257 1244
1258struct branch_info *machine__resolve_bstack(struct machine *machine, 1245struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
1259 struct thread *thr, 1246 struct addr_location *al)
1260 struct branch_stack *bs)
1261{ 1247{
1262 struct branch_info *bi;
1263 unsigned int i; 1248 unsigned int i;
1249 const struct branch_stack *bs = sample->branch_stack;
1250 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
1264 1251
1265 bi = calloc(bs->nr, sizeof(struct branch_info));
1266 if (!bi) 1252 if (!bi)
1267 return NULL; 1253 return NULL;
1268 1254
1269 for (i = 0; i < bs->nr; i++) { 1255 for (i = 0; i < bs->nr; i++) {
1270 ip__resolve_ams(machine, thr, &bi[i].to, bs->entries[i].to); 1256 ip__resolve_ams(al->machine, al->thread, &bi[i].to, bs->entries[i].to);
1271 ip__resolve_ams(machine, thr, &bi[i].from, bs->entries[i].from); 1257 ip__resolve_ams(al->machine, al->thread, &bi[i].from, bs->entries[i].from);
1272 bi[i].flags = bs->entries[i].flags; 1258 bi[i].flags = bs->entries[i].flags;
1273 } 1259 }
1274 return bi; 1260 return bi;
@@ -1326,7 +1312,7 @@ static int machine__resolve_callchain_sample(struct machine *machine,
1326 continue; 1312 continue;
1327 } 1313 }
1328 1314
1329 al.filtered = false; 1315 al.filtered = 0;
1330 thread__find_addr_location(thread, machine, cpumode, 1316 thread__find_addr_location(thread, machine, cpumode,
1331 MAP__FUNCTION, ip, &al); 1317 MAP__FUNCTION, ip, &al);
1332 if (al.sym != NULL) { 1318 if (al.sym != NULL) {
@@ -1385,8 +1371,7 @@ int machine__resolve_callchain(struct machine *machine,
1385 return 0; 1371 return 0;
1386 1372
1387 return unwind__get_entries(unwind_entry, &callchain_cursor, machine, 1373 return unwind__get_entries(unwind_entry, &callchain_cursor, machine,
1388 thread, evsel->attr.sample_regs_user, 1374 thread, sample, max_stack);
1389 sample, max_stack);
1390 1375
1391} 1376}
1392 1377
diff --git a/tools/perf/util/machine.h b/tools/perf/util/machine.h
index f77e91e483dc..c8c74a119398 100644
--- a/tools/perf/util/machine.h
+++ b/tools/perf/util/machine.h
@@ -41,7 +41,8 @@ struct map *machine__kernel_map(struct machine *machine, enum map_type type)
41 return machine->vmlinux_maps[type]; 41 return machine->vmlinux_maps[type];
42} 42}
43 43
44struct thread *machine__find_thread(struct machine *machine, pid_t tid); 44struct thread *machine__find_thread(struct machine *machine, pid_t pid,
45 pid_t tid);
45 46
46int machine__process_comm_event(struct machine *machine, union perf_event *event, 47int machine__process_comm_event(struct machine *machine, union perf_event *event,
47 struct perf_sample *sample); 48 struct perf_sample *sample);
@@ -91,12 +92,10 @@ void machine__delete_dead_threads(struct machine *machine);
91void machine__delete_threads(struct machine *machine); 92void machine__delete_threads(struct machine *machine);
92void machine__delete(struct machine *machine); 93void machine__delete(struct machine *machine);
93 94
94struct branch_info *machine__resolve_bstack(struct machine *machine, 95struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
95 struct thread *thread, 96 struct addr_location *al);
96 struct branch_stack *bs); 97struct mem_info *sample__resolve_mem(struct perf_sample *sample,
97struct mem_info *machine__resolve_mem(struct machine *machine, 98 struct addr_location *al);
98 struct thread *thread,
99 struct perf_sample *sample, u8 cpumode);
100int machine__resolve_callchain(struct machine *machine, 99int machine__resolve_callchain(struct machine *machine,
101 struct perf_evsel *evsel, 100 struct perf_evsel *evsel,
102 struct thread *thread, 101 struct thread *thread,
diff --git a/tools/perf/util/map.h b/tools/perf/util/map.h
index 257e513205ce..f00f058afb3b 100644
--- a/tools/perf/util/map.h
+++ b/tools/perf/util/map.h
@@ -90,6 +90,16 @@ u64 map__objdump_2mem(struct map *map, u64 ip);
90 90
91struct symbol; 91struct symbol;
92 92
93/* map__for_each_symbol - iterate over the symbols in the given map
94 *
95 * @map: the 'struct map *' in which symbols itereated
96 * @pos: the 'struct symbol *' to use as a loop cursor
97 * @n: the 'struct rb_node *' to use as a temporary storage
98 * Note: caller must ensure map->dso is not NULL (map is loaded).
99 */
100#define map__for_each_symbol(map, pos, n) \
101 dso__for_each_symbol(map->dso, pos, n, map->type)
102
93typedef int (*symbol_filter_t)(struct map *map, struct symbol *sym); 103typedef int (*symbol_filter_t)(struct map *map, struct symbol *sym);
94 104
95void map__init(struct map *map, enum map_type type, 105void map__init(struct map *map, enum map_type type,
diff --git a/tools/perf/util/parse-options.c b/tools/perf/util/parse-options.c
index d22e3f8017dc..bf48092983c6 100644
--- a/tools/perf/util/parse-options.c
+++ b/tools/perf/util/parse-options.c
@@ -407,7 +407,9 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
407 if (internal_help && !strcmp(arg + 2, "help")) 407 if (internal_help && !strcmp(arg + 2, "help"))
408 return usage_with_options_internal(usagestr, options, 0); 408 return usage_with_options_internal(usagestr, options, 0);
409 if (!strcmp(arg + 2, "list-opts")) 409 if (!strcmp(arg + 2, "list-opts"))
410 return PARSE_OPT_LIST; 410 return PARSE_OPT_LIST_OPTS;
411 if (!strcmp(arg + 2, "list-cmds"))
412 return PARSE_OPT_LIST_SUBCMDS;
411 switch (parse_long_opt(ctx, arg + 2, options)) { 413 switch (parse_long_opt(ctx, arg + 2, options)) {
412 case -1: 414 case -1:
413 return parse_options_usage(usagestr, options, arg + 2, 0); 415 return parse_options_usage(usagestr, options, arg + 2, 0);
@@ -433,25 +435,45 @@ int parse_options_end(struct parse_opt_ctx_t *ctx)
433 return ctx->cpidx + ctx->argc; 435 return ctx->cpidx + ctx->argc;
434} 436}
435 437
436int parse_options(int argc, const char **argv, const struct option *options, 438int parse_options_subcommand(int argc, const char **argv, const struct option *options,
437 const char * const usagestr[], int flags) 439 const char *const subcommands[], const char *usagestr[], int flags)
438{ 440{
439 struct parse_opt_ctx_t ctx; 441 struct parse_opt_ctx_t ctx;
440 442
441 perf_header__set_cmdline(argc, argv); 443 perf_header__set_cmdline(argc, argv);
442 444
445 /* build usage string if it's not provided */
446 if (subcommands && !usagestr[0]) {
447 struct strbuf buf = STRBUF_INIT;
448
449 strbuf_addf(&buf, "perf %s [<options>] {", argv[0]);
450 for (int i = 0; subcommands[i]; i++) {
451 if (i)
452 strbuf_addstr(&buf, "|");
453 strbuf_addstr(&buf, subcommands[i]);
454 }
455 strbuf_addstr(&buf, "}");
456
457 usagestr[0] = strdup(buf.buf);
458 strbuf_release(&buf);
459 }
460
443 parse_options_start(&ctx, argc, argv, flags); 461 parse_options_start(&ctx, argc, argv, flags);
444 switch (parse_options_step(&ctx, options, usagestr)) { 462 switch (parse_options_step(&ctx, options, usagestr)) {
445 case PARSE_OPT_HELP: 463 case PARSE_OPT_HELP:
446 exit(129); 464 exit(129);
447 case PARSE_OPT_DONE: 465 case PARSE_OPT_DONE:
448 break; 466 break;
449 case PARSE_OPT_LIST: 467 case PARSE_OPT_LIST_OPTS:
450 while (options->type != OPTION_END) { 468 while (options->type != OPTION_END) {
451 printf("--%s ", options->long_name); 469 printf("--%s ", options->long_name);
452 options++; 470 options++;
453 } 471 }
454 exit(130); 472 exit(130);
473 case PARSE_OPT_LIST_SUBCMDS:
474 for (int i = 0; subcommands[i]; i++)
475 printf("%s ", subcommands[i]);
476 exit(130);
455 default: /* PARSE_OPT_UNKNOWN */ 477 default: /* PARSE_OPT_UNKNOWN */
456 if (ctx.argv[0][1] == '-') { 478 if (ctx.argv[0][1] == '-') {
457 error("unknown option `%s'", ctx.argv[0] + 2); 479 error("unknown option `%s'", ctx.argv[0] + 2);
@@ -464,6 +486,13 @@ int parse_options(int argc, const char **argv, const struct option *options,
464 return parse_options_end(&ctx); 486 return parse_options_end(&ctx);
465} 487}
466 488
489int parse_options(int argc, const char **argv, const struct option *options,
490 const char * const usagestr[], int flags)
491{
492 return parse_options_subcommand(argc, argv, options, NULL,
493 (const char **) usagestr, flags);
494}
495
467#define USAGE_OPTS_WIDTH 24 496#define USAGE_OPTS_WIDTH 24
468#define USAGE_GAP 2 497#define USAGE_GAP 2
469 498
diff --git a/tools/perf/util/parse-options.h b/tools/perf/util/parse-options.h
index cbf0149cf221..d8dac8ac5f37 100644
--- a/tools/perf/util/parse-options.h
+++ b/tools/perf/util/parse-options.h
@@ -140,6 +140,11 @@ extern int parse_options(int argc, const char **argv,
140 const struct option *options, 140 const struct option *options,
141 const char * const usagestr[], int flags); 141 const char * const usagestr[], int flags);
142 142
143extern int parse_options_subcommand(int argc, const char **argv,
144 const struct option *options,
145 const char *const subcommands[],
146 const char *usagestr[], int flags);
147
143extern NORETURN void usage_with_options(const char * const *usagestr, 148extern NORETURN void usage_with_options(const char * const *usagestr,
144 const struct option *options); 149 const struct option *options);
145 150
@@ -148,7 +153,8 @@ extern NORETURN void usage_with_options(const char * const *usagestr,
148enum { 153enum {
149 PARSE_OPT_HELP = -1, 154 PARSE_OPT_HELP = -1,
150 PARSE_OPT_DONE, 155 PARSE_OPT_DONE,
151 PARSE_OPT_LIST, 156 PARSE_OPT_LIST_OPTS,
157 PARSE_OPT_LIST_SUBCMDS,
152 PARSE_OPT_UNKNOWN, 158 PARSE_OPT_UNKNOWN,
153}; 159};
154 160
diff --git a/tools/perf/util/perf_regs.c b/tools/perf/util/perf_regs.c
new file mode 100644
index 000000000000..a3539ef30b15
--- /dev/null
+++ b/tools/perf/util/perf_regs.c
@@ -0,0 +1,19 @@
1#include <errno.h>
2#include "perf_regs.h"
3
4int perf_reg_value(u64 *valp, struct regs_dump *regs, int id)
5{
6 int i, idx = 0;
7 u64 mask = regs->mask;
8
9 if (!(mask & (1 << id)))
10 return -EINVAL;
11
12 for (i = 0; i < id; i++) {
13 if (mask & (1 << i))
14 idx++;
15 }
16
17 *valp = regs->regs[idx];
18 return 0;
19}
diff --git a/tools/perf/util/perf_regs.h b/tools/perf/util/perf_regs.h
index a3d42cd74919..d6e8b6a8d7f3 100644
--- a/tools/perf/util/perf_regs.h
+++ b/tools/perf/util/perf_regs.h
@@ -1,8 +1,14 @@
1#ifndef __PERF_REGS_H 1#ifndef __PERF_REGS_H
2#define __PERF_REGS_H 2#define __PERF_REGS_H
3 3
4#include "types.h"
5#include "event.h"
6
4#ifdef HAVE_PERF_REGS_SUPPORT 7#ifdef HAVE_PERF_REGS_SUPPORT
5#include <perf_regs.h> 8#include <perf_regs.h>
9
10int perf_reg_value(u64 *valp, struct regs_dump *regs, int id);
11
6#else 12#else
7#define PERF_REGS_MASK 0 13#define PERF_REGS_MASK 0
8 14
@@ -10,5 +16,12 @@ static inline const char *perf_reg_name(int id __maybe_unused)
10{ 16{
11 return NULL; 17 return NULL;
12} 18}
19
20static inline int perf_reg_value(u64 *valp __maybe_unused,
21 struct regs_dump *regs __maybe_unused,
22 int id __maybe_unused)
23{
24 return 0;
25}
13#endif /* HAVE_PERF_REGS_SUPPORT */ 26#endif /* HAVE_PERF_REGS_SUPPORT */
14#endif /* __PERF_REGS_H */ 27#endif /* __PERF_REGS_H */
diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
index b752ecb40d86..00a7dcb2f55c 100644
--- a/tools/perf/util/pmu.c
+++ b/tools/perf/util/pmu.c
@@ -3,7 +3,7 @@
3#include <unistd.h> 3#include <unistd.h>
4#include <stdio.h> 4#include <stdio.h>
5#include <dirent.h> 5#include <dirent.h>
6#include "fs.h" 6#include <api/fs/fs.h>
7#include <locale.h> 7#include <locale.h>
8#include "util.h" 8#include "util.h"
9#include "pmu.h" 9#include "pmu.h"
diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c
index d8b048c20cde..0d1542f33d87 100644
--- a/tools/perf/util/probe-event.c
+++ b/tools/perf/util/probe-event.c
@@ -70,34 +70,32 @@ static int e_snprintf(char *str, size_t size, const char *format, ...)
70} 70}
71 71
72static char *synthesize_perf_probe_point(struct perf_probe_point *pp); 72static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
73static int convert_name_to_addr(struct perf_probe_event *pev,
74 const char *exec);
75static void clear_probe_trace_event(struct probe_trace_event *tev); 73static void clear_probe_trace_event(struct probe_trace_event *tev);
76static struct machine machine; 74static struct machine *host_machine;
77 75
78/* Initialize symbol maps and path of vmlinux/modules */ 76/* Initialize symbol maps and path of vmlinux/modules */
79static int init_vmlinux(void) 77static int init_symbol_maps(bool user_only)
80{ 78{
81 int ret; 79 int ret;
82 80
83 symbol_conf.sort_by_name = true; 81 symbol_conf.sort_by_name = true;
84 if (symbol_conf.vmlinux_name == NULL)
85 symbol_conf.try_vmlinux_path = true;
86 else
87 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
88 ret = symbol__init(); 82 ret = symbol__init();
89 if (ret < 0) { 83 if (ret < 0) {
90 pr_debug("Failed to init symbol map.\n"); 84 pr_debug("Failed to init symbol map.\n");
91 goto out; 85 goto out;
92 } 86 }
93 87
94 ret = machine__init(&machine, "", HOST_KERNEL_ID); 88 if (host_machine || user_only) /* already initialized */
95 if (ret < 0) 89 return 0;
96 goto out;
97 90
98 if (machine__create_kernel_maps(&machine) < 0) { 91 if (symbol_conf.vmlinux_name)
99 pr_debug("machine__create_kernel_maps() failed.\n"); 92 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
100 goto out; 93
94 host_machine = machine__new_host();
95 if (!host_machine) {
96 pr_debug("machine__new_host() failed.\n");
97 symbol__exit();
98 ret = -1;
101 } 99 }
102out: 100out:
103 if (ret < 0) 101 if (ret < 0)
@@ -105,21 +103,66 @@ out:
105 return ret; 103 return ret;
106} 104}
107 105
106static void exit_symbol_maps(void)
107{
108 if (host_machine) {
109 machine__delete(host_machine);
110 host_machine = NULL;
111 }
112 symbol__exit();
113}
114
108static struct symbol *__find_kernel_function_by_name(const char *name, 115static struct symbol *__find_kernel_function_by_name(const char *name,
109 struct map **mapp) 116 struct map **mapp)
110{ 117{
111 return machine__find_kernel_function_by_name(&machine, name, mapp, 118 return machine__find_kernel_function_by_name(host_machine, name, mapp,
112 NULL); 119 NULL);
113} 120}
114 121
122static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
123{
124 return machine__find_kernel_function(host_machine, addr, mapp, NULL);
125}
126
127static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
128{
129 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
130 struct kmap *kmap;
131
132 if (map__load(host_machine->vmlinux_maps[MAP__FUNCTION], NULL) < 0)
133 return NULL;
134
135 kmap = map__kmap(host_machine->vmlinux_maps[MAP__FUNCTION]);
136 return kmap->ref_reloc_sym;
137}
138
139static u64 kernel_get_symbol_address_by_name(const char *name, bool reloc)
140{
141 struct ref_reloc_sym *reloc_sym;
142 struct symbol *sym;
143 struct map *map;
144
145 /* ref_reloc_sym is just a label. Need a special fix*/
146 reloc_sym = kernel_get_ref_reloc_sym();
147 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
148 return (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
149 else {
150 sym = __find_kernel_function_by_name(name, &map);
151 if (sym)
152 return map->unmap_ip(map, sym->start) -
153 (reloc) ? 0 : map->reloc;
154 }
155 return 0;
156}
157
115static struct map *kernel_get_module_map(const char *module) 158static struct map *kernel_get_module_map(const char *module)
116{ 159{
117 struct rb_node *nd; 160 struct rb_node *nd;
118 struct map_groups *grp = &machine.kmaps; 161 struct map_groups *grp = &host_machine->kmaps;
119 162
120 /* A file path -- this is an offline module */ 163 /* A file path -- this is an offline module */
121 if (module && strchr(module, '/')) 164 if (module && strchr(module, '/'))
122 return machine__new_module(&machine, 0, module); 165 return machine__new_module(host_machine, 0, module);
123 166
124 if (!module) 167 if (!module)
125 module = "kernel"; 168 module = "kernel";
@@ -141,7 +184,7 @@ static struct dso *kernel_get_module_dso(const char *module)
141 const char *vmlinux_name; 184 const char *vmlinux_name;
142 185
143 if (module) { 186 if (module) {
144 list_for_each_entry(dso, &machine.kernel_dsos, node) { 187 list_for_each_entry(dso, &host_machine->kernel_dsos, node) {
145 if (strncmp(dso->short_name + 1, module, 188 if (strncmp(dso->short_name + 1, module,
146 dso->short_name_len - 2) == 0) 189 dso->short_name_len - 2) == 0)
147 goto found; 190 goto found;
@@ -150,7 +193,7 @@ static struct dso *kernel_get_module_dso(const char *module)
150 return NULL; 193 return NULL;
151 } 194 }
152 195
153 map = machine.vmlinux_maps[MAP__FUNCTION]; 196 map = host_machine->vmlinux_maps[MAP__FUNCTION];
154 dso = map->dso; 197 dso = map->dso;
155 198
156 vmlinux_name = symbol_conf.vmlinux_name; 199 vmlinux_name = symbol_conf.vmlinux_name;
@@ -173,20 +216,6 @@ const char *kernel_get_module_path(const char *module)
173 return (dso) ? dso->long_name : NULL; 216 return (dso) ? dso->long_name : NULL;
174} 217}
175 218
176static int init_user_exec(void)
177{
178 int ret = 0;
179
180 symbol_conf.try_vmlinux_path = false;
181 symbol_conf.sort_by_name = true;
182 ret = symbol__init();
183
184 if (ret < 0)
185 pr_debug("Failed to init symbol map.\n");
186
187 return ret;
188}
189
190static int convert_exec_to_group(const char *exec, char **result) 219static int convert_exec_to_group(const char *exec, char **result)
191{ 220{
192 char *ptr1, *ptr2, *exec_copy; 221 char *ptr1, *ptr2, *exec_copy;
@@ -218,32 +247,23 @@ out:
218 return ret; 247 return ret;
219} 248}
220 249
221static int convert_to_perf_probe_point(struct probe_trace_point *tp, 250static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
222 struct perf_probe_point *pp)
223{ 251{
224 pp->function = strdup(tp->symbol); 252 int i;
225
226 if (pp->function == NULL)
227 return -ENOMEM;
228
229 pp->offset = tp->offset;
230 pp->retprobe = tp->retprobe;
231 253
232 return 0; 254 for (i = 0; i < ntevs; i++)
255 clear_probe_trace_event(tevs + i);
233} 256}
234 257
235#ifdef HAVE_DWARF_SUPPORT 258#ifdef HAVE_DWARF_SUPPORT
259
236/* Open new debuginfo of given module */ 260/* Open new debuginfo of given module */
237static struct debuginfo *open_debuginfo(const char *module) 261static struct debuginfo *open_debuginfo(const char *module)
238{ 262{
239 const char *path; 263 const char *path = module;
240 264
241 /* A file path -- this is an offline module */ 265 if (!module || !strchr(module, '/')) {
242 if (module && strchr(module, '/'))
243 path = module;
244 else {
245 path = kernel_get_module_path(module); 266 path = kernel_get_module_path(module);
246
247 if (!path) { 267 if (!path) {
248 pr_err("Failed to find path of %s module.\n", 268 pr_err("Failed to find path of %s module.\n",
249 module ?: "kernel"); 269 module ?: "kernel");
@@ -253,46 +273,6 @@ static struct debuginfo *open_debuginfo(const char *module)
253 return debuginfo__new(path); 273 return debuginfo__new(path);
254} 274}
255 275
256/*
257 * Convert trace point to probe point with debuginfo
258 * Currently only handles kprobes.
259 */
260static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
261 struct perf_probe_point *pp)
262{
263 struct symbol *sym;
264 struct map *map;
265 u64 addr;
266 int ret = -ENOENT;
267 struct debuginfo *dinfo;
268
269 sym = __find_kernel_function_by_name(tp->symbol, &map);
270 if (sym) {
271 addr = map->unmap_ip(map, sym->start + tp->offset);
272 pr_debug("try to find %s+%ld@%" PRIx64 "\n", tp->symbol,
273 tp->offset, addr);
274
275 dinfo = debuginfo__new_online_kernel(addr);
276 if (dinfo) {
277 ret = debuginfo__find_probe_point(dinfo,
278 (unsigned long)addr, pp);
279 debuginfo__delete(dinfo);
280 } else {
281 pr_debug("Failed to open debuginfo at 0x%" PRIx64 "\n",
282 addr);
283 ret = -ENOENT;
284 }
285 }
286 if (ret <= 0) {
287 pr_debug("Failed to find corresponding probes from "
288 "debuginfo. Use kprobe event information.\n");
289 return convert_to_perf_probe_point(tp, pp);
290 }
291 pp->retprobe = tp->retprobe;
292
293 return 0;
294}
295
296static int get_text_start_address(const char *exec, unsigned long *address) 276static int get_text_start_address(const char *exec, unsigned long *address)
297{ 277{
298 Elf *elf; 278 Elf *elf;
@@ -321,12 +301,62 @@ out:
321 return ret; 301 return ret;
322} 302}
323 303
304/*
305 * Convert trace point to probe point with debuginfo
306 */
307static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
308 struct perf_probe_point *pp,
309 bool is_kprobe)
310{
311 struct debuginfo *dinfo = NULL;
312 unsigned long stext = 0;
313 u64 addr = tp->address;
314 int ret = -ENOENT;
315
316 /* convert the address to dwarf address */
317 if (!is_kprobe) {
318 if (!addr) {
319 ret = -EINVAL;
320 goto error;
321 }
322 ret = get_text_start_address(tp->module, &stext);
323 if (ret < 0)
324 goto error;
325 addr += stext;
326 } else {
327 addr = kernel_get_symbol_address_by_name(tp->symbol, false);
328 if (addr == 0)
329 goto error;
330 addr += tp->offset;
331 }
332
333 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
334 tp->module ? : "kernel");
335
336 dinfo = open_debuginfo(tp->module);
337 if (dinfo) {
338 ret = debuginfo__find_probe_point(dinfo,
339 (unsigned long)addr, pp);
340 debuginfo__delete(dinfo);
341 } else {
342 pr_debug("Failed to open debuginfo at 0x%" PRIx64 "\n", addr);
343 ret = -ENOENT;
344 }
345
346 if (ret > 0) {
347 pp->retprobe = tp->retprobe;
348 return 0;
349 }
350error:
351 pr_debug("Failed to find corresponding probes from debuginfo.\n");
352 return ret ? : -ENOENT;
353}
354
324static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs, 355static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
325 int ntevs, const char *exec) 356 int ntevs, const char *exec)
326{ 357{
327 int i, ret = 0; 358 int i, ret = 0;
328 unsigned long offset, stext = 0; 359 unsigned long stext = 0;
329 char buf[32];
330 360
331 if (!exec) 361 if (!exec)
332 return 0; 362 return 0;
@@ -337,15 +367,9 @@ static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
337 367
338 for (i = 0; i < ntevs && ret >= 0; i++) { 368 for (i = 0; i < ntevs && ret >= 0; i++) {
339 /* point.address is the addres of point.symbol + point.offset */ 369 /* point.address is the addres of point.symbol + point.offset */
340 offset = tevs[i].point.address - stext; 370 tevs[i].point.address -= stext;
341 tevs[i].point.offset = 0;
342 zfree(&tevs[i].point.symbol);
343 ret = e_snprintf(buf, 32, "0x%lx", offset);
344 if (ret < 0)
345 break;
346 tevs[i].point.module = strdup(exec); 371 tevs[i].point.module = strdup(exec);
347 tevs[i].point.symbol = strdup(buf); 372 if (!tevs[i].point.module) {
348 if (!tevs[i].point.symbol || !tevs[i].point.module) {
349 ret = -ENOMEM; 373 ret = -ENOMEM;
350 break; 374 break;
351 } 375 }
@@ -388,12 +412,40 @@ static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
388 return ret; 412 return ret;
389} 413}
390 414
391static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs) 415/* Post processing the probe events */
416static int post_process_probe_trace_events(struct probe_trace_event *tevs,
417 int ntevs, const char *module,
418 bool uprobe)
392{ 419{
420 struct ref_reloc_sym *reloc_sym;
421 char *tmp;
393 int i; 422 int i;
394 423
395 for (i = 0; i < ntevs; i++) 424 if (uprobe)
396 clear_probe_trace_event(tevs + i); 425 return add_exec_to_probe_trace_events(tevs, ntevs, module);
426
427 /* Note that currently ref_reloc_sym based probe is not for drivers */
428 if (module)
429 return add_module_to_probe_trace_events(tevs, ntevs, module);
430
431 reloc_sym = kernel_get_ref_reloc_sym();
432 if (!reloc_sym) {
433 pr_warning("Relocated base symbol is not found!\n");
434 return -EINVAL;
435 }
436
437 for (i = 0; i < ntevs; i++) {
438 if (tevs[i].point.address) {
439 tmp = strdup(reloc_sym->name);
440 if (!tmp)
441 return -ENOMEM;
442 free(tevs[i].point.symbol);
443 tevs[i].point.symbol = tmp;
444 tevs[i].point.offset = tevs[i].point.address -
445 reloc_sym->unrelocated_addr;
446 }
447 }
448 return 0;
397} 449}
398 450
399/* Try to find perf_probe_event with debuginfo */ 451/* Try to find perf_probe_event with debuginfo */
@@ -416,21 +468,16 @@ static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
416 return 0; 468 return 0;
417 } 469 }
418 470
471 pr_debug("Try to find probe point from debuginfo.\n");
419 /* Searching trace events corresponding to a probe event */ 472 /* Searching trace events corresponding to a probe event */
420 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs); 473 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs);
421 474
422 debuginfo__delete(dinfo); 475 debuginfo__delete(dinfo);
423 476
424 if (ntevs > 0) { /* Succeeded to find trace events */ 477 if (ntevs > 0) { /* Succeeded to find trace events */
425 pr_debug("find %d probe_trace_events.\n", ntevs); 478 pr_debug("Found %d probe_trace_events.\n", ntevs);
426 if (target) { 479 ret = post_process_probe_trace_events(*tevs, ntevs,
427 if (pev->uprobes) 480 target, pev->uprobes);
428 ret = add_exec_to_probe_trace_events(*tevs,
429 ntevs, target);
430 else
431 ret = add_module_to_probe_trace_events(*tevs,
432 ntevs, target);
433 }
434 if (ret < 0) { 481 if (ret < 0) {
435 clear_probe_trace_events(*tevs, ntevs); 482 clear_probe_trace_events(*tevs, ntevs);
436 zfree(tevs); 483 zfree(tevs);
@@ -563,20 +610,16 @@ static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
563 * Show line-range always requires debuginfo to find source file and 610 * Show line-range always requires debuginfo to find source file and
564 * line number. 611 * line number.
565 */ 612 */
566int show_line_range(struct line_range *lr, const char *module) 613static int __show_line_range(struct line_range *lr, const char *module)
567{ 614{
568 int l = 1; 615 int l = 1;
569 struct line_node *ln; 616 struct int_node *ln;
570 struct debuginfo *dinfo; 617 struct debuginfo *dinfo;
571 FILE *fp; 618 FILE *fp;
572 int ret; 619 int ret;
573 char *tmp; 620 char *tmp;
574 621
575 /* Search a line range */ 622 /* Search a line range */
576 ret = init_vmlinux();
577 if (ret < 0)
578 return ret;
579
580 dinfo = open_debuginfo(module); 623 dinfo = open_debuginfo(module);
581 if (!dinfo) { 624 if (!dinfo) {
582 pr_warning("Failed to open debuginfo file.\n"); 625 pr_warning("Failed to open debuginfo file.\n");
@@ -623,8 +666,8 @@ int show_line_range(struct line_range *lr, const char *module)
623 goto end; 666 goto end;
624 } 667 }
625 668
626 list_for_each_entry(ln, &lr->line_list, list) { 669 intlist__for_each(ln, lr->line_list) {
627 for (; ln->line > l; l++) { 670 for (; ln->i > l; l++) {
628 ret = show_one_line(fp, l - lr->offset); 671 ret = show_one_line(fp, l - lr->offset);
629 if (ret < 0) 672 if (ret < 0)
630 goto end; 673 goto end;
@@ -646,6 +689,19 @@ end:
646 return ret; 689 return ret;
647} 690}
648 691
692int show_line_range(struct line_range *lr, const char *module)
693{
694 int ret;
695
696 ret = init_symbol_maps(false);
697 if (ret < 0)
698 return ret;
699 ret = __show_line_range(lr, module);
700 exit_symbol_maps();
701
702 return ret;
703}
704
649static int show_available_vars_at(struct debuginfo *dinfo, 705static int show_available_vars_at(struct debuginfo *dinfo,
650 struct perf_probe_event *pev, 706 struct perf_probe_event *pev,
651 int max_vls, struct strfilter *_filter, 707 int max_vls, struct strfilter *_filter,
@@ -707,14 +763,15 @@ int show_available_vars(struct perf_probe_event *pevs, int npevs,
707 int i, ret = 0; 763 int i, ret = 0;
708 struct debuginfo *dinfo; 764 struct debuginfo *dinfo;
709 765
710 ret = init_vmlinux(); 766 ret = init_symbol_maps(false);
711 if (ret < 0) 767 if (ret < 0)
712 return ret; 768 return ret;
713 769
714 dinfo = open_debuginfo(module); 770 dinfo = open_debuginfo(module);
715 if (!dinfo) { 771 if (!dinfo) {
716 pr_warning("Failed to open debuginfo file.\n"); 772 pr_warning("Failed to open debuginfo file.\n");
717 return -ENOENT; 773 ret = -ENOENT;
774 goto out;
718 } 775 }
719 776
720 setup_pager(); 777 setup_pager();
@@ -724,23 +781,19 @@ int show_available_vars(struct perf_probe_event *pevs, int npevs,
724 externs); 781 externs);
725 782
726 debuginfo__delete(dinfo); 783 debuginfo__delete(dinfo);
784out:
785 exit_symbol_maps();
727 return ret; 786 return ret;
728} 787}
729 788
730#else /* !HAVE_DWARF_SUPPORT */ 789#else /* !HAVE_DWARF_SUPPORT */
731 790
732static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp, 791static int
733 struct perf_probe_point *pp) 792find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
793 struct perf_probe_point *pp __maybe_unused,
794 bool is_kprobe __maybe_unused)
734{ 795{
735 struct symbol *sym; 796 return -ENOSYS;
736
737 sym = __find_kernel_function_by_name(tp->symbol, NULL);
738 if (!sym) {
739 pr_err("Failed to find symbol %s in kernel.\n", tp->symbol);
740 return -ENOENT;
741 }
742
743 return convert_to_perf_probe_point(tp, pp);
744} 797}
745 798
746static int try_to_find_probe_trace_events(struct perf_probe_event *pev, 799static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
@@ -776,24 +829,22 @@ int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
776 829
777void line_range__clear(struct line_range *lr) 830void line_range__clear(struct line_range *lr)
778{ 831{
779 struct line_node *ln;
780
781 free(lr->function); 832 free(lr->function);
782 free(lr->file); 833 free(lr->file);
783 free(lr->path); 834 free(lr->path);
784 free(lr->comp_dir); 835 free(lr->comp_dir);
785 while (!list_empty(&lr->line_list)) { 836 intlist__delete(lr->line_list);
786 ln = list_first_entry(&lr->line_list, struct line_node, list);
787 list_del(&ln->list);
788 free(ln);
789 }
790 memset(lr, 0, sizeof(*lr)); 837 memset(lr, 0, sizeof(*lr));
791} 838}
792 839
793void line_range__init(struct line_range *lr) 840int line_range__init(struct line_range *lr)
794{ 841{
795 memset(lr, 0, sizeof(*lr)); 842 memset(lr, 0, sizeof(*lr));
796 INIT_LIST_HEAD(&lr->line_list); 843 lr->line_list = intlist__new(NULL);
844 if (!lr->line_list)
845 return -ENOMEM;
846 else
847 return 0;
797} 848}
798 849
799static int parse_line_num(char **ptr, int *val, const char *what) 850static int parse_line_num(char **ptr, int *val, const char *what)
@@ -1267,16 +1318,21 @@ static int parse_probe_trace_command(const char *cmd,
1267 } else 1318 } else
1268 p = argv[1]; 1319 p = argv[1];
1269 fmt1_str = strtok_r(p, "+", &fmt); 1320 fmt1_str = strtok_r(p, "+", &fmt);
1270 tp->symbol = strdup(fmt1_str); 1321 if (fmt1_str[0] == '0') /* only the address started with 0x */
1271 if (tp->symbol == NULL) { 1322 tp->address = strtoul(fmt1_str, NULL, 0);
1272 ret = -ENOMEM; 1323 else {
1273 goto out; 1324 /* Only the symbol-based probe has offset */
1325 tp->symbol = strdup(fmt1_str);
1326 if (tp->symbol == NULL) {
1327 ret = -ENOMEM;
1328 goto out;
1329 }
1330 fmt2_str = strtok_r(NULL, "", &fmt);
1331 if (fmt2_str == NULL)
1332 tp->offset = 0;
1333 else
1334 tp->offset = strtoul(fmt2_str, NULL, 10);
1274 } 1335 }
1275 fmt2_str = strtok_r(NULL, "", &fmt);
1276 if (fmt2_str == NULL)
1277 tp->offset = 0;
1278 else
1279 tp->offset = strtoul(fmt2_str, NULL, 10);
1280 1336
1281 tev->nargs = argc - 2; 1337 tev->nargs = argc - 2;
1282 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs); 1338 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
@@ -1518,20 +1574,27 @@ char *synthesize_probe_trace_command(struct probe_trace_event *tev)
1518 if (buf == NULL) 1574 if (buf == NULL)
1519 return NULL; 1575 return NULL;
1520 1576
1577 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1578 tev->group, tev->event);
1579 if (len <= 0)
1580 goto error;
1581
1582 /* Uprobes must have tp->address and tp->module */
1583 if (tev->uprobes && (!tp->address || !tp->module))
1584 goto error;
1585
1586 /* Use the tp->address for uprobes */
1521 if (tev->uprobes) 1587 if (tev->uprobes)
1522 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s:%s", 1588 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1523 tp->retprobe ? 'r' : 'p', 1589 tp->module, tp->address);
1524 tev->group, tev->event,
1525 tp->module, tp->symbol);
1526 else 1590 else
1527 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s%s%s+%lu", 1591 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
1528 tp->retprobe ? 'r' : 'p',
1529 tev->group, tev->event,
1530 tp->module ?: "", tp->module ? ":" : "", 1592 tp->module ?: "", tp->module ? ":" : "",
1531 tp->symbol, tp->offset); 1593 tp->symbol, tp->offset);
1532 1594
1533 if (len <= 0) 1595 if (ret <= 0)
1534 goto error; 1596 goto error;
1597 len += ret;
1535 1598
1536 for (i = 0; i < tev->nargs; i++) { 1599 for (i = 0; i < tev->nargs; i++) {
1537 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len, 1600 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
@@ -1547,6 +1610,79 @@ error:
1547 return NULL; 1610 return NULL;
1548} 1611}
1549 1612
1613static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1614 struct perf_probe_point *pp,
1615 bool is_kprobe)
1616{
1617 struct symbol *sym = NULL;
1618 struct map *map;
1619 u64 addr;
1620 int ret = -ENOENT;
1621
1622 if (!is_kprobe) {
1623 map = dso__new_map(tp->module);
1624 if (!map)
1625 goto out;
1626 addr = tp->address;
1627 sym = map__find_symbol(map, addr, NULL);
1628 } else {
1629 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1630 if (addr) {
1631 addr += tp->offset;
1632 sym = __find_kernel_function(addr, &map);
1633 }
1634 }
1635 if (!sym)
1636 goto out;
1637
1638 pp->retprobe = tp->retprobe;
1639 pp->offset = addr - map->unmap_ip(map, sym->start);
1640 pp->function = strdup(sym->name);
1641 ret = pp->function ? 0 : -ENOMEM;
1642
1643out:
1644 if (map && !is_kprobe) {
1645 dso__delete(map->dso);
1646 map__delete(map);
1647 }
1648
1649 return ret;
1650}
1651
1652static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1653 struct perf_probe_point *pp,
1654 bool is_kprobe)
1655{
1656 char buf[128];
1657 int ret;
1658
1659 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1660 if (!ret)
1661 return 0;
1662 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1663 if (!ret)
1664 return 0;
1665
1666 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1667
1668 if (tp->symbol) {
1669 pp->function = strdup(tp->symbol);
1670 pp->offset = tp->offset;
1671 } else if (!tp->module && !is_kprobe) {
1672 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1673 if (ret < 0)
1674 return ret;
1675 pp->function = strdup(buf);
1676 pp->offset = 0;
1677 }
1678 if (pp->function == NULL)
1679 return -ENOMEM;
1680
1681 pp->retprobe = tp->retprobe;
1682
1683 return 0;
1684}
1685
1550static int convert_to_perf_probe_event(struct probe_trace_event *tev, 1686static int convert_to_perf_probe_event(struct probe_trace_event *tev,
1551 struct perf_probe_event *pev, bool is_kprobe) 1687 struct perf_probe_event *pev, bool is_kprobe)
1552{ 1688{
@@ -1560,11 +1696,7 @@ static int convert_to_perf_probe_event(struct probe_trace_event *tev,
1560 return -ENOMEM; 1696 return -ENOMEM;
1561 1697
1562 /* Convert trace_point to probe_point */ 1698 /* Convert trace_point to probe_point */
1563 if (is_kprobe) 1699 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
1564 ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
1565 else
1566 ret = convert_to_perf_probe_point(&tev->point, &pev->point);
1567
1568 if (ret < 0) 1700 if (ret < 0)
1569 return ret; 1701 return ret;
1570 1702
@@ -1731,7 +1863,8 @@ static struct strlist *get_probe_trace_command_rawlist(int fd)
1731} 1863}
1732 1864
1733/* Show an event */ 1865/* Show an event */
1734static int show_perf_probe_event(struct perf_probe_event *pev) 1866static int show_perf_probe_event(struct perf_probe_event *pev,
1867 const char *module)
1735{ 1868{
1736 int i, ret; 1869 int i, ret;
1737 char buf[128]; 1870 char buf[128];
@@ -1747,6 +1880,8 @@ static int show_perf_probe_event(struct perf_probe_event *pev)
1747 return ret; 1880 return ret;
1748 1881
1749 printf(" %-20s (on %s", buf, place); 1882 printf(" %-20s (on %s", buf, place);
1883 if (module)
1884 printf(" in %s", module);
1750 1885
1751 if (pev->nargs > 0) { 1886 if (pev->nargs > 0) {
1752 printf(" with"); 1887 printf(" with");
@@ -1784,7 +1919,8 @@ static int __show_perf_probe_events(int fd, bool is_kprobe)
1784 ret = convert_to_perf_probe_event(&tev, &pev, 1919 ret = convert_to_perf_probe_event(&tev, &pev,
1785 is_kprobe); 1920 is_kprobe);
1786 if (ret >= 0) 1921 if (ret >= 0)
1787 ret = show_perf_probe_event(&pev); 1922 ret = show_perf_probe_event(&pev,
1923 tev.point.module);
1788 } 1924 }
1789 clear_perf_probe_event(&pev); 1925 clear_perf_probe_event(&pev);
1790 clear_probe_trace_event(&tev); 1926 clear_probe_trace_event(&tev);
@@ -1807,7 +1943,7 @@ int show_perf_probe_events(void)
1807 if (fd < 0) 1943 if (fd < 0)
1808 return fd; 1944 return fd;
1809 1945
1810 ret = init_vmlinux(); 1946 ret = init_symbol_maps(false);
1811 if (ret < 0) 1947 if (ret < 0)
1812 return ret; 1948 return ret;
1813 1949
@@ -1820,6 +1956,7 @@ int show_perf_probe_events(void)
1820 close(fd); 1956 close(fd);
1821 } 1957 }
1822 1958
1959 exit_symbol_maps();
1823 return ret; 1960 return ret;
1824} 1961}
1825 1962
@@ -1982,7 +2119,7 @@ static int __add_probe_trace_events(struct perf_probe_event *pev,
1982 group = pev->group; 2119 group = pev->group;
1983 pev->event = tev->event; 2120 pev->event = tev->event;
1984 pev->group = tev->group; 2121 pev->group = tev->group;
1985 show_perf_probe_event(pev); 2122 show_perf_probe_event(pev, tev->point.module);
1986 /* Trick here - restore current event/group */ 2123 /* Trick here - restore current event/group */
1987 pev->event = (char *)event; 2124 pev->event = (char *)event;
1988 pev->group = (char *)group; 2125 pev->group = (char *)group;
@@ -2008,113 +2145,175 @@ static int __add_probe_trace_events(struct perf_probe_event *pev,
2008 return ret; 2145 return ret;
2009} 2146}
2010 2147
2011static int convert_to_probe_trace_events(struct perf_probe_event *pev, 2148static char *looking_function_name;
2012 struct probe_trace_event **tevs, 2149static int num_matched_functions;
2013 int max_tevs, const char *target) 2150
2151static int probe_function_filter(struct map *map __maybe_unused,
2152 struct symbol *sym)
2014{ 2153{
2154 if ((sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL) &&
2155 strcmp(looking_function_name, sym->name) == 0) {
2156 num_matched_functions++;
2157 return 0;
2158 }
2159 return 1;
2160}
2161
2162#define strdup_or_goto(str, label) \
2163 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2164
2165/*
2166 * Find probe function addresses from map.
2167 * Return an error or the number of found probe_trace_event
2168 */
2169static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2170 struct probe_trace_event **tevs,
2171 int max_tevs, const char *target)
2172{
2173 struct map *map = NULL;
2174 struct kmap *kmap = NULL;
2175 struct ref_reloc_sym *reloc_sym = NULL;
2015 struct symbol *sym; 2176 struct symbol *sym;
2016 int ret, i; 2177 struct rb_node *nd;
2017 struct probe_trace_event *tev; 2178 struct probe_trace_event *tev;
2179 struct perf_probe_point *pp = &pev->point;
2180 struct probe_trace_point *tp;
2181 int ret, i;
2018 2182
2019 if (pev->uprobes && !pev->group) { 2183 /* Init maps of given executable or kernel */
2020 /* Replace group name if not given */ 2184 if (pev->uprobes)
2021 ret = convert_exec_to_group(target, &pev->group); 2185 map = dso__new_map(target);
2022 if (ret != 0) { 2186 else
2023 pr_warning("Failed to make a group name.\n"); 2187 map = kernel_get_module_map(target);
2024 return ret; 2188 if (!map) {
2025 } 2189 ret = -EINVAL;
2190 goto out;
2026 } 2191 }
2027 2192
2028 /* Convert perf_probe_event with debuginfo */ 2193 /*
2029 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target); 2194 * Load matched symbols: Since the different local symbols may have
2030 if (ret != 0) 2195 * same name but different addresses, this lists all the symbols.
2031 return ret; /* Found in debuginfo or got an error */ 2196 */
2032 2197 num_matched_functions = 0;
2033 if (pev->uprobes) { 2198 looking_function_name = pp->function;
2034 ret = convert_name_to_addr(pev, target); 2199 ret = map__load(map, probe_function_filter);
2035 if (ret < 0) 2200 if (ret || num_matched_functions == 0) {
2036 return ret; 2201 pr_err("Failed to find symbol %s in %s\n", pp->function,
2202 target ? : "kernel");
2203 ret = -ENOENT;
2204 goto out;
2205 } else if (num_matched_functions > max_tevs) {
2206 pr_err("Too many functions matched in %s\n",
2207 target ? : "kernel");
2208 ret = -E2BIG;
2209 goto out;
2037 } 2210 }
2038 2211
2039 /* Allocate trace event buffer */ 2212 if (!pev->uprobes) {
2040 tev = *tevs = zalloc(sizeof(struct probe_trace_event)); 2213 kmap = map__kmap(map);
2041 if (tev == NULL) 2214 reloc_sym = kmap->ref_reloc_sym;
2042 return -ENOMEM; 2215 if (!reloc_sym) {
2216 pr_warning("Relocated base symbol is not found!\n");
2217 ret = -EINVAL;
2218 goto out;
2219 }
2220 }
2043 2221
2044 /* Copy parameters */ 2222 /* Setup result trace-probe-events */
2045 tev->point.symbol = strdup(pev->point.function); 2223 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2046 if (tev->point.symbol == NULL) { 2224 if (!*tevs) {
2047 ret = -ENOMEM; 2225 ret = -ENOMEM;
2048 goto error; 2226 goto out;
2049 } 2227 }
2050 2228
2051 if (target) { 2229 ret = 0;
2052 tev->point.module = strdup(target); 2230 map__for_each_symbol(map, sym, nd) {
2053 if (tev->point.module == NULL) { 2231 tev = (*tevs) + ret;
2054 ret = -ENOMEM; 2232 tp = &tev->point;
2055 goto error; 2233 if (ret == num_matched_functions) {
2234 pr_warning("Too many symbols are listed. Skip it.\n");
2235 break;
2056 } 2236 }
2057 } 2237 ret++;
2058
2059 tev->point.offset = pev->point.offset;
2060 tev->point.retprobe = pev->point.retprobe;
2061 tev->nargs = pev->nargs;
2062 tev->uprobes = pev->uprobes;
2063 2238
2064 if (tev->nargs) { 2239 if (pp->offset > sym->end - sym->start) {
2065 tev->args = zalloc(sizeof(struct probe_trace_arg) 2240 pr_warning("Offset %ld is bigger than the size of %s\n",
2066 * tev->nargs); 2241 pp->offset, sym->name);
2067 if (tev->args == NULL) { 2242 ret = -ENOENT;
2068 ret = -ENOMEM; 2243 goto err_out;
2069 goto error; 2244 }
2245 /* Add one probe point */
2246 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2247 if (reloc_sym) {
2248 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2249 tp->offset = tp->address - reloc_sym->addr;
2250 } else {
2251 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2252 tp->offset = pp->offset;
2253 }
2254 tp->retprobe = pp->retprobe;
2255 if (target)
2256 tev->point.module = strdup_or_goto(target, nomem_out);
2257 tev->uprobes = pev->uprobes;
2258 tev->nargs = pev->nargs;
2259 if (tev->nargs) {
2260 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2261 tev->nargs);
2262 if (tev->args == NULL)
2263 goto nomem_out;
2070 } 2264 }
2071 for (i = 0; i < tev->nargs; i++) { 2265 for (i = 0; i < tev->nargs; i++) {
2072 if (pev->args[i].name) { 2266 if (pev->args[i].name)
2073 tev->args[i].name = strdup(pev->args[i].name); 2267 tev->args[i].name =
2074 if (tev->args[i].name == NULL) { 2268 strdup_or_goto(pev->args[i].name,
2075 ret = -ENOMEM; 2269 nomem_out);
2076 goto error; 2270
2077 } 2271 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2078 } 2272 nomem_out);
2079 tev->args[i].value = strdup(pev->args[i].var); 2273 if (pev->args[i].type)
2080 if (tev->args[i].value == NULL) { 2274 tev->args[i].type =
2081 ret = -ENOMEM; 2275 strdup_or_goto(pev->args[i].type,
2082 goto error; 2276 nomem_out);
2083 }
2084 if (pev->args[i].type) {
2085 tev->args[i].type = strdup(pev->args[i].type);
2086 if (tev->args[i].type == NULL) {
2087 ret = -ENOMEM;
2088 goto error;
2089 }
2090 }
2091 } 2277 }
2092 } 2278 }
2093 2279
2094 if (pev->uprobes) 2280out:
2095 return 1; 2281 if (map && pev->uprobes) {
2282 /* Only when using uprobe(exec) map needs to be released */
2283 dso__delete(map->dso);
2284 map__delete(map);
2285 }
2286 return ret;
2096 2287
2097 /* Currently just checking function name from symbol map */ 2288nomem_out:
2098 sym = __find_kernel_function_by_name(tev->point.symbol, NULL); 2289 ret = -ENOMEM;
2099 if (!sym) { 2290err_out:
2100 pr_warning("Kernel symbol \'%s\' not found.\n", 2291 clear_probe_trace_events(*tevs, num_matched_functions);
2101 tev->point.symbol); 2292 zfree(tevs);
2102 ret = -ENOENT; 2293 goto out;
2103 goto error; 2294}
2104 } else if (tev->point.offset > sym->end - sym->start) { 2295
2105 pr_warning("Offset specified is greater than size of %s\n", 2296static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2106 tev->point.symbol); 2297 struct probe_trace_event **tevs,
2107 ret = -ENOENT; 2298 int max_tevs, const char *target)
2108 goto error; 2299{
2300 int ret;
2109 2301
2302 if (pev->uprobes && !pev->group) {
2303 /* Replace group name if not given */
2304 ret = convert_exec_to_group(target, &pev->group);
2305 if (ret != 0) {
2306 pr_warning("Failed to make a group name.\n");
2307 return ret;
2308 }
2110 } 2309 }
2111 2310
2112 return 1; 2311 /* Convert perf_probe_event with debuginfo */
2113error: 2312 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
2114 clear_probe_trace_event(tev); 2313 if (ret != 0)
2115 free(tev); 2314 return ret; /* Found in debuginfo or got an error */
2116 *tevs = NULL; 2315
2117 return ret; 2316 return find_probe_trace_events_from_map(pev, tevs, max_tevs, target);
2118} 2317}
2119 2318
2120struct __event_package { 2319struct __event_package {
@@ -2135,12 +2334,7 @@ int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
2135 if (pkgs == NULL) 2334 if (pkgs == NULL)
2136 return -ENOMEM; 2335 return -ENOMEM;
2137 2336
2138 if (!pevs->uprobes) 2337 ret = init_symbol_maps(pevs->uprobes);
2139 /* Init vmlinux path */
2140 ret = init_vmlinux();
2141 else
2142 ret = init_user_exec();
2143
2144 if (ret < 0) { 2338 if (ret < 0) {
2145 free(pkgs); 2339 free(pkgs);
2146 return ret; 2340 return ret;
@@ -2174,6 +2368,7 @@ end:
2174 zfree(&pkgs[i].tevs); 2368 zfree(&pkgs[i].tevs);
2175 } 2369 }
2176 free(pkgs); 2370 free(pkgs);
2371 exit_symbol_maps();
2177 2372
2178 return ret; 2373 return ret;
2179} 2374}
@@ -2323,159 +2518,51 @@ static struct strfilter *available_func_filter;
2323static int filter_available_functions(struct map *map __maybe_unused, 2518static int filter_available_functions(struct map *map __maybe_unused,
2324 struct symbol *sym) 2519 struct symbol *sym)
2325{ 2520{
2326 if (sym->binding == STB_GLOBAL && 2521 if ((sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL) &&
2327 strfilter__compare(available_func_filter, sym->name)) 2522 strfilter__compare(available_func_filter, sym->name))
2328 return 0; 2523 return 0;
2329 return 1; 2524 return 1;
2330} 2525}
2331 2526
2332static int __show_available_funcs(struct map *map) 2527int show_available_funcs(const char *target, struct strfilter *_filter,
2333{ 2528 bool user)
2334 if (map__load(map, filter_available_functions)) {
2335 pr_err("Failed to load map.\n");
2336 return -EINVAL;
2337 }
2338 if (!dso__sorted_by_name(map->dso, map->type))
2339 dso__sort_by_name(map->dso, map->type);
2340
2341 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2342 return 0;
2343}
2344
2345static int available_kernel_funcs(const char *module)
2346{ 2529{
2347 struct map *map; 2530 struct map *map;
2348 int ret; 2531 int ret;
2349 2532
2350 ret = init_vmlinux(); 2533 ret = init_symbol_maps(user);
2351 if (ret < 0) 2534 if (ret < 0)
2352 return ret; 2535 return ret;
2353 2536
2354 map = kernel_get_module_map(module); 2537 /* Get a symbol map */
2538 if (user)
2539 map = dso__new_map(target);
2540 else
2541 map = kernel_get_module_map(target);
2355 if (!map) { 2542 if (!map) {
2356 pr_err("Failed to find %s map.\n", (module) ? : "kernel"); 2543 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
2357 return -EINVAL; 2544 return -EINVAL;
2358 } 2545 }
2359 return __show_available_funcs(map);
2360}
2361
2362static int available_user_funcs(const char *target)
2363{
2364 struct map *map;
2365 int ret;
2366
2367 ret = init_user_exec();
2368 if (ret < 0)
2369 return ret;
2370
2371 map = dso__new_map(target);
2372 ret = __show_available_funcs(map);
2373 dso__delete(map->dso);
2374 map__delete(map);
2375 return ret;
2376}
2377 2546
2378int show_available_funcs(const char *target, struct strfilter *_filter, 2547 /* Load symbols with given filter */
2379 bool user)
2380{
2381 setup_pager();
2382 available_func_filter = _filter; 2548 available_func_filter = _filter;
2383
2384 if (!user)
2385 return available_kernel_funcs(target);
2386
2387 return available_user_funcs(target);
2388}
2389
2390/*
2391 * uprobe_events only accepts address:
2392 * Convert function and any offset to address
2393 */
2394static int convert_name_to_addr(struct perf_probe_event *pev, const char *exec)
2395{
2396 struct perf_probe_point *pp = &pev->point;
2397 struct symbol *sym;
2398 struct map *map = NULL;
2399 char *function = NULL;
2400 int ret = -EINVAL;
2401 unsigned long long vaddr = 0;
2402
2403 if (!pp->function) {
2404 pr_warning("No function specified for uprobes");
2405 goto out;
2406 }
2407
2408 function = strdup(pp->function);
2409 if (!function) {
2410 pr_warning("Failed to allocate memory by strdup.\n");
2411 ret = -ENOMEM;
2412 goto out;
2413 }
2414
2415 map = dso__new_map(exec);
2416 if (!map) {
2417 pr_warning("Cannot find appropriate DSO for %s.\n", exec);
2418 goto out;
2419 }
2420 available_func_filter = strfilter__new(function, NULL);
2421 if (map__load(map, filter_available_functions)) { 2549 if (map__load(map, filter_available_functions)) {
2422 pr_err("Failed to load map.\n"); 2550 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2423 goto out; 2551 goto end;
2424 }
2425
2426 sym = map__find_symbol_by_name(map, function, NULL);
2427 if (!sym) {
2428 pr_warning("Cannot find %s in DSO %s\n", function, exec);
2429 goto out;
2430 }
2431
2432 if (map->start > sym->start)
2433 vaddr = map->start;
2434 vaddr += sym->start + pp->offset + map->pgoff;
2435 pp->offset = 0;
2436
2437 if (!pev->event) {
2438 pev->event = function;
2439 function = NULL;
2440 }
2441 if (!pev->group) {
2442 char *ptr1, *ptr2, *exec_copy;
2443
2444 pev->group = zalloc(sizeof(char *) * 64);
2445 exec_copy = strdup(exec);
2446 if (!exec_copy) {
2447 ret = -ENOMEM;
2448 pr_warning("Failed to copy exec string.\n");
2449 goto out;
2450 }
2451
2452 ptr1 = strdup(basename(exec_copy));
2453 if (ptr1) {
2454 ptr2 = strpbrk(ptr1, "-._");
2455 if (ptr2)
2456 *ptr2 = '\0';
2457 e_snprintf(pev->group, 64, "%s_%s", PERFPROBE_GROUP,
2458 ptr1);
2459 free(ptr1);
2460 }
2461 free(exec_copy);
2462 }
2463 free(pp->function);
2464 pp->function = zalloc(sizeof(char *) * MAX_PROBE_ARGS);
2465 if (!pp->function) {
2466 ret = -ENOMEM;
2467 pr_warning("Failed to allocate memory by zalloc.\n");
2468 goto out;
2469 } 2552 }
2470 e_snprintf(pp->function, MAX_PROBE_ARGS, "0x%llx", vaddr); 2553 if (!dso__sorted_by_name(map->dso, map->type))
2471 ret = 0; 2554 dso__sort_by_name(map->dso, map->type);
2472 2555
2473out: 2556 /* Show all (filtered) symbols */
2474 if (map) { 2557 setup_pager();
2558 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2559end:
2560 if (user) {
2475 dso__delete(map->dso); 2561 dso__delete(map->dso);
2476 map__delete(map); 2562 map__delete(map);
2477 } 2563 }
2478 if (function) 2564 exit_symbol_maps();
2479 free(function); 2565
2480 return ret; 2566 return ret;
2481} 2567}
2568
diff --git a/tools/perf/util/probe-event.h b/tools/perf/util/probe-event.h
index fcaf7273e85a..776c9347a3b6 100644
--- a/tools/perf/util/probe-event.h
+++ b/tools/perf/util/probe-event.h
@@ -2,6 +2,7 @@
2#define _PROBE_EVENT_H 2#define _PROBE_EVENT_H
3 3
4#include <stdbool.h> 4#include <stdbool.h>
5#include "intlist.h"
5#include "strlist.h" 6#include "strlist.h"
6#include "strfilter.h" 7#include "strfilter.h"
7 8
@@ -76,13 +77,6 @@ struct perf_probe_event {
76 struct perf_probe_arg *args; /* Arguments */ 77 struct perf_probe_arg *args; /* Arguments */
77}; 78};
78 79
79
80/* Line number container */
81struct line_node {
82 struct list_head list;
83 int line;
84};
85
86/* Line range */ 80/* Line range */
87struct line_range { 81struct line_range {
88 char *file; /* File name */ 82 char *file; /* File name */
@@ -92,7 +86,7 @@ struct line_range {
92 int offset; /* Start line offset */ 86 int offset; /* Start line offset */
93 char *path; /* Real path name */ 87 char *path; /* Real path name */
94 char *comp_dir; /* Compile directory */ 88 char *comp_dir; /* Compile directory */
95 struct list_head line_list; /* Visible lines */ 89 struct intlist *line_list; /* Visible lines */
96}; 90};
97 91
98/* List of variables */ 92/* List of variables */
@@ -124,7 +118,7 @@ extern int parse_line_range_desc(const char *cmd, struct line_range *lr);
124extern void line_range__clear(struct line_range *lr); 118extern void line_range__clear(struct line_range *lr);
125 119
126/* Initialize line range */ 120/* Initialize line range */
127extern void line_range__init(struct line_range *lr); 121extern int line_range__init(struct line_range *lr);
128 122
129/* Internal use: Return kernel/module path */ 123/* Internal use: Return kernel/module path */
130extern const char *kernel_get_module_path(const char *module); 124extern const char *kernel_get_module_path(const char *module);
diff --git a/tools/perf/util/probe-finder.c b/tools/perf/util/probe-finder.c
index 061edb162b5b..df0238654698 100644
--- a/tools/perf/util/probe-finder.c
+++ b/tools/perf/util/probe-finder.c
@@ -34,7 +34,9 @@
34 34
35#include <linux/bitops.h> 35#include <linux/bitops.h>
36#include "event.h" 36#include "event.h"
37#include "dso.h"
37#include "debug.h" 38#include "debug.h"
39#include "intlist.h"
38#include "util.h" 40#include "util.h"
39#include "symbol.h" 41#include "symbol.h"
40#include "probe-finder.h" 42#include "probe-finder.h"
@@ -42,65 +44,6 @@
42/* Kprobe tracer basic type is up to u64 */ 44/* Kprobe tracer basic type is up to u64 */
43#define MAX_BASIC_TYPE_BITS 64 45#define MAX_BASIC_TYPE_BITS 64
44 46
45/* Line number list operations */
46
47/* Add a line to line number list */
48static int line_list__add_line(struct list_head *head, int line)
49{
50 struct line_node *ln;
51 struct list_head *p;
52
53 /* Reverse search, because new line will be the last one */
54 list_for_each_entry_reverse(ln, head, list) {
55 if (ln->line < line) {
56 p = &ln->list;
57 goto found;
58 } else if (ln->line == line) /* Already exist */
59 return 1;
60 }
61 /* List is empty, or the smallest entry */
62 p = head;
63found:
64 pr_debug("line list: add a line %u\n", line);
65 ln = zalloc(sizeof(struct line_node));
66 if (ln == NULL)
67 return -ENOMEM;
68 ln->line = line;
69 INIT_LIST_HEAD(&ln->list);
70 list_add(&ln->list, p);
71 return 0;
72}
73
74/* Check if the line in line number list */
75static int line_list__has_line(struct list_head *head, int line)
76{
77 struct line_node *ln;
78
79 /* Reverse search, because new line will be the last one */
80 list_for_each_entry(ln, head, list)
81 if (ln->line == line)
82 return 1;
83
84 return 0;
85}
86
87/* Init line number list */
88static void line_list__init(struct list_head *head)
89{
90 INIT_LIST_HEAD(head);
91}
92
93/* Free line number list */
94static void line_list__free(struct list_head *head)
95{
96 struct line_node *ln;
97 while (!list_empty(head)) {
98 ln = list_first_entry(head, struct line_node, list);
99 list_del(&ln->list);
100 free(ln);
101 }
102}
103
104/* Dwarf FL wrappers */ 47/* Dwarf FL wrappers */
105static char *debuginfo_path; /* Currently dummy */ 48static char *debuginfo_path; /* Currently dummy */
106 49
@@ -147,80 +90,7 @@ error:
147 return -ENOENT; 90 return -ENOENT;
148} 91}
149 92
150#if _ELFUTILS_PREREQ(0, 148) 93static struct debuginfo *__debuginfo__new(const char *path)
151/* This method is buggy if elfutils is older than 0.148 */
152static int __linux_kernel_find_elf(Dwfl_Module *mod,
153 void **userdata,
154 const char *module_name,
155 Dwarf_Addr base,
156 char **file_name, Elf **elfp)
157{
158 int fd;
159 const char *path = kernel_get_module_path(module_name);
160
161 pr_debug2("Use file %s for %s\n", path, module_name);
162 if (path) {
163 fd = open(path, O_RDONLY);
164 if (fd >= 0) {
165 *file_name = strdup(path);
166 return fd;
167 }
168 }
169 /* If failed, try to call standard method */
170 return dwfl_linux_kernel_find_elf(mod, userdata, module_name, base,
171 file_name, elfp);
172}
173
174static const Dwfl_Callbacks kernel_callbacks = {
175 .find_debuginfo = dwfl_standard_find_debuginfo,
176 .debuginfo_path = &debuginfo_path,
177
178 .find_elf = __linux_kernel_find_elf,
179 .section_address = dwfl_linux_kernel_module_section_address,
180};
181
182/* Get a Dwarf from live kernel image */
183static int debuginfo__init_online_kernel_dwarf(struct debuginfo *dbg,
184 Dwarf_Addr addr)
185{
186 dbg->dwfl = dwfl_begin(&kernel_callbacks);
187 if (!dbg->dwfl)
188 return -EINVAL;
189
190 /* Load the kernel dwarves: Don't care the result here */
191 dwfl_linux_kernel_report_kernel(dbg->dwfl);
192 dwfl_linux_kernel_report_modules(dbg->dwfl);
193
194 dbg->dbg = dwfl_addrdwarf(dbg->dwfl, addr, &dbg->bias);
195 /* Here, check whether we could get a real dwarf */
196 if (!dbg->dbg) {
197 pr_debug("Failed to find kernel dwarf at %lx\n",
198 (unsigned long)addr);
199 dwfl_end(dbg->dwfl);
200 memset(dbg, 0, sizeof(*dbg));
201 return -ENOENT;
202 }
203
204 return 0;
205}
206#else
207/* With older elfutils, this just support kernel module... */
208static int debuginfo__init_online_kernel_dwarf(struct debuginfo *dbg,
209 Dwarf_Addr addr __maybe_unused)
210{
211 const char *path = kernel_get_module_path("kernel");
212
213 if (!path) {
214 pr_err("Failed to find vmlinux path\n");
215 return -ENOENT;
216 }
217
218 pr_debug2("Use file %s for debuginfo\n", path);
219 return debuginfo__init_offline_dwarf(dbg, path);
220}
221#endif
222
223struct debuginfo *debuginfo__new(const char *path)
224{ 94{
225 struct debuginfo *dbg = zalloc(sizeof(*dbg)); 95 struct debuginfo *dbg = zalloc(sizeof(*dbg));
226 if (!dbg) 96 if (!dbg)
@@ -228,21 +98,44 @@ struct debuginfo *debuginfo__new(const char *path)
228 98
229 if (debuginfo__init_offline_dwarf(dbg, path) < 0) 99 if (debuginfo__init_offline_dwarf(dbg, path) < 0)
230 zfree(&dbg); 100 zfree(&dbg);
231 101 if (dbg)
102 pr_debug("Open Debuginfo file: %s\n", path);
232 return dbg; 103 return dbg;
233} 104}
234 105
235struct debuginfo *debuginfo__new_online_kernel(unsigned long addr) 106enum dso_binary_type distro_dwarf_types[] = {
236{ 107 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
237 struct debuginfo *dbg = zalloc(sizeof(*dbg)); 108 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
109 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
110 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
111 DSO_BINARY_TYPE__NOT_FOUND,
112};
238 113
239 if (!dbg) 114struct debuginfo *debuginfo__new(const char *path)
240 return NULL; 115{
116 enum dso_binary_type *type;
117 char buf[PATH_MAX], nil = '\0';
118 struct dso *dso;
119 struct debuginfo *dinfo = NULL;
120
121 /* Try to open distro debuginfo files */
122 dso = dso__new(path);
123 if (!dso)
124 goto out;
241 125
242 if (debuginfo__init_online_kernel_dwarf(dbg, (Dwarf_Addr)addr) < 0) 126 for (type = distro_dwarf_types;
243 zfree(&dbg); 127 !dinfo && *type != DSO_BINARY_TYPE__NOT_FOUND;
128 type++) {
129 if (dso__read_binary_type_filename(dso, *type, &nil,
130 buf, PATH_MAX) < 0)
131 continue;
132 dinfo = __debuginfo__new(buf);
133 }
134 dso__delete(dso);
244 135
245 return dbg; 136out:
137 /* if failed to open all distro debuginfo, open given binary */
138 return dinfo ? : __debuginfo__new(path);
246} 139}
247 140
248void debuginfo__delete(struct debuginfo *dbg) 141void debuginfo__delete(struct debuginfo *dbg)
@@ -880,7 +773,7 @@ static int find_probe_point_by_line(struct probe_finder *pf)
880} 773}
881 774
882/* Find lines which match lazy pattern */ 775/* Find lines which match lazy pattern */
883static int find_lazy_match_lines(struct list_head *head, 776static int find_lazy_match_lines(struct intlist *list,
884 const char *fname, const char *pat) 777 const char *fname, const char *pat)
885{ 778{
886 FILE *fp; 779 FILE *fp;
@@ -901,7 +794,7 @@ static int find_lazy_match_lines(struct list_head *head,
901 line[len - 1] = '\0'; 794 line[len - 1] = '\0';
902 795
903 if (strlazymatch(line, pat)) { 796 if (strlazymatch(line, pat)) {
904 line_list__add_line(head, linenum); 797 intlist__add(list, linenum);
905 count++; 798 count++;
906 } 799 }
907 linenum++; 800 linenum++;
@@ -924,7 +817,7 @@ static int probe_point_lazy_walker(const char *fname, int lineno,
924 Dwarf_Die *sc_die, die_mem; 817 Dwarf_Die *sc_die, die_mem;
925 int ret; 818 int ret;
926 819
927 if (!line_list__has_line(&pf->lcache, lineno) || 820 if (!intlist__has_entry(pf->lcache, lineno) ||
928 strtailcmp(fname, pf->fname) != 0) 821 strtailcmp(fname, pf->fname) != 0)
929 return 0; 822 return 0;
930 823
@@ -952,9 +845,9 @@ static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
952{ 845{
953 int ret = 0; 846 int ret = 0;
954 847
955 if (list_empty(&pf->lcache)) { 848 if (intlist__empty(pf->lcache)) {
956 /* Matching lazy line pattern */ 849 /* Matching lazy line pattern */
957 ret = find_lazy_match_lines(&pf->lcache, pf->fname, 850 ret = find_lazy_match_lines(pf->lcache, pf->fname,
958 pf->pev->point.lazy_line); 851 pf->pev->point.lazy_line);
959 if (ret <= 0) 852 if (ret <= 0)
960 return ret; 853 return ret;
@@ -1096,7 +989,9 @@ static int debuginfo__find_probes(struct debuginfo *dbg,
1096#endif 989#endif
1097 990
1098 off = 0; 991 off = 0;
1099 line_list__init(&pf->lcache); 992 pf->lcache = intlist__new(NULL);
993 if (!pf->lcache)
994 return -ENOMEM;
1100 995
1101 /* Fastpath: lookup by function name from .debug_pubnames section */ 996 /* Fastpath: lookup by function name from .debug_pubnames section */
1102 if (pp->function) { 997 if (pp->function) {
@@ -1149,7 +1044,8 @@ static int debuginfo__find_probes(struct debuginfo *dbg,
1149 } 1044 }
1150 1045
1151found: 1046found:
1152 line_list__free(&pf->lcache); 1047 intlist__delete(pf->lcache);
1048 pf->lcache = NULL;
1153 1049
1154 return ret; 1050 return ret;
1155} 1051}
@@ -1537,7 +1433,7 @@ static int line_range_add_line(const char *src, unsigned int lineno,
1537 if (lr->path == NULL) 1433 if (lr->path == NULL)
1538 return -ENOMEM; 1434 return -ENOMEM;
1539 } 1435 }
1540 return line_list__add_line(&lr->line_list, lineno); 1436 return intlist__add(lr->line_list, lineno);
1541} 1437}
1542 1438
1543static int line_range_walk_cb(const char *fname, int lineno, 1439static int line_range_walk_cb(const char *fname, int lineno,
@@ -1565,7 +1461,7 @@ static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
1565 1461
1566 /* Update status */ 1462 /* Update status */
1567 if (ret >= 0) 1463 if (ret >= 0)
1568 if (!list_empty(&lf->lr->line_list)) 1464 if (!intlist__empty(lf->lr->line_list))
1569 ret = lf->found = 1; 1465 ret = lf->found = 1;
1570 else 1466 else
1571 ret = 0; /* Lines are not found */ 1467 ret = 0; /* Lines are not found */
diff --git a/tools/perf/util/probe-finder.h b/tools/perf/util/probe-finder.h
index ffc33cdd25cc..92590b2c7e1c 100644
--- a/tools/perf/util/probe-finder.h
+++ b/tools/perf/util/probe-finder.h
@@ -3,6 +3,7 @@
3 3
4#include <stdbool.h> 4#include <stdbool.h>
5#include "util.h" 5#include "util.h"
6#include "intlist.h"
6#include "probe-event.h" 7#include "probe-event.h"
7 8
8#define MAX_PROBE_BUFFER 1024 9#define MAX_PROBE_BUFFER 1024
@@ -29,8 +30,8 @@ struct debuginfo {
29 Dwarf_Addr bias; 30 Dwarf_Addr bias;
30}; 31};
31 32
33/* This also tries to open distro debuginfo */
32extern struct debuginfo *debuginfo__new(const char *path); 34extern struct debuginfo *debuginfo__new(const char *path);
33extern struct debuginfo *debuginfo__new_online_kernel(unsigned long addr);
34extern void debuginfo__delete(struct debuginfo *dbg); 35extern void debuginfo__delete(struct debuginfo *dbg);
35 36
36/* Find probe_trace_events specified by perf_probe_event from debuginfo */ 37/* Find probe_trace_events specified by perf_probe_event from debuginfo */
@@ -66,7 +67,7 @@ struct probe_finder {
66 const char *fname; /* Real file name */ 67 const char *fname; /* Real file name */
67 Dwarf_Die cu_die; /* Current CU */ 68 Dwarf_Die cu_die; /* Current CU */
68 Dwarf_Die sp_die; 69 Dwarf_Die sp_die;
69 struct list_head lcache; /* Line cache for lazy match */ 70 struct intlist *lcache; /* Line cache for lazy match */
70 71
71 /* For variable searching */ 72 /* For variable searching */
72#if _ELFUTILS_PREREQ(0, 142) 73#if _ELFUTILS_PREREQ(0, 142)
diff --git a/tools/perf/util/python-ext-sources b/tools/perf/util/python-ext-sources
index 595bfc73d2ed..16a475a7d492 100644
--- a/tools/perf/util/python-ext-sources
+++ b/tools/perf/util/python-ext-sources
@@ -17,6 +17,6 @@ util/xyarray.c
17util/cgroup.c 17util/cgroup.c
18util/rblist.c 18util/rblist.c
19util/strlist.c 19util/strlist.c
20util/fs.c 20../lib/api/fs/fs.c
21util/trace-event.c 21util/trace-event.c
22../../lib/rbtree.c 22../../lib/rbtree.c
diff --git a/tools/perf/util/record.c b/tools/perf/util/record.c
index 373762501dad..049e0a09ccd3 100644
--- a/tools/perf/util/record.c
+++ b/tools/perf/util/record.c
@@ -2,7 +2,7 @@
2#include "evsel.h" 2#include "evsel.h"
3#include "cpumap.h" 3#include "cpumap.h"
4#include "parse-events.h" 4#include "parse-events.h"
5#include "fs.h" 5#include <api/fs/fs.h>
6#include "util.h" 6#include "util.h"
7 7
8typedef void (*setup_probe_fn_t)(struct perf_evsel *evsel); 8typedef void (*setup_probe_fn_t)(struct perf_evsel *evsel);
diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index 5da6ce74c676..55960f22233c 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -702,11 +702,12 @@ static void regs_dump__printf(u64 mask, u64 *regs)
702 } 702 }
703} 703}
704 704
705static void regs_user__printf(struct perf_sample *sample, u64 mask) 705static void regs_user__printf(struct perf_sample *sample)
706{ 706{
707 struct regs_dump *user_regs = &sample->user_regs; 707 struct regs_dump *user_regs = &sample->user_regs;
708 708
709 if (user_regs->regs) { 709 if (user_regs->regs) {
710 u64 mask = user_regs->mask;
710 printf("... user regs: mask 0x%" PRIx64 "\n", mask); 711 printf("... user regs: mask 0x%" PRIx64 "\n", mask);
711 regs_dump__printf(mask, user_regs->regs); 712 regs_dump__printf(mask, user_regs->regs);
712 } 713 }
@@ -793,7 +794,7 @@ static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
793 if (!dump_trace) 794 if (!dump_trace)
794 return; 795 return;
795 796
796 printf("(IP, %d): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n", 797 printf("(IP, 0x%x): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
797 event->header.misc, sample->pid, sample->tid, sample->ip, 798 event->header.misc, sample->pid, sample->tid, sample->ip,
798 sample->period, sample->addr); 799 sample->period, sample->addr);
799 800
@@ -806,7 +807,7 @@ static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
806 branch_stack__printf(sample); 807 branch_stack__printf(sample);
807 808
808 if (sample_type & PERF_SAMPLE_REGS_USER) 809 if (sample_type & PERF_SAMPLE_REGS_USER)
809 regs_user__printf(sample, evsel->attr.sample_regs_user); 810 regs_user__printf(sample);
810 811
811 if (sample_type & PERF_SAMPLE_STACK_USER) 812 if (sample_type & PERF_SAMPLE_STACK_USER)
812 stack_user__printf(&sample->user_stack); 813 stack_user__printf(&sample->user_stack);
diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
index 3e9f336740fa..3b7dbf51d4a9 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -151,15 +151,15 @@ Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
151 151
152 gelf_getshdr(sec, shp); 152 gelf_getshdr(sec, shp);
153 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name); 153 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
154 if (!strcmp(name, str)) { 154 if (str && !strcmp(name, str)) {
155 if (idx) 155 if (idx)
156 *idx = cnt; 156 *idx = cnt;
157 break; 157 return sec;
158 } 158 }
159 ++cnt; 159 ++cnt;
160 } 160 }
161 161
162 return sec; 162 return NULL;
163} 163}
164 164
165#define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \ 165#define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
@@ -506,6 +506,8 @@ int filename__read_debuglink(const char *filename, char *debuglink,
506 /* the start of this section is a zero-terminated string */ 506 /* the start of this section is a zero-terminated string */
507 strncpy(debuglink, data->d_buf, size); 507 strncpy(debuglink, data->d_buf, size);
508 508
509 err = 0;
510
509out_elf_end: 511out_elf_end:
510 elf_end(elf); 512 elf_end(elf);
511out_close: 513out_close:
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index e89afc097d8a..95e249779931 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -410,7 +410,7 @@ struct symbol *dso__find_symbol(struct dso *dso,
410 return symbols__find(&dso->symbols[type], addr); 410 return symbols__find(&dso->symbols[type], addr);
411} 411}
412 412
413struct symbol *dso__first_symbol(struct dso *dso, enum map_type type) 413static struct symbol *dso__first_symbol(struct dso *dso, enum map_type type)
414{ 414{
415 return symbols__first(&dso->symbols[type]); 415 return symbols__first(&dso->symbols[type]);
416} 416}
@@ -1251,6 +1251,46 @@ out_failure:
1251 return -1; 1251 return -1;
1252} 1252}
1253 1253
1254static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod,
1255 enum dso_binary_type type)
1256{
1257 switch (type) {
1258 case DSO_BINARY_TYPE__JAVA_JIT:
1259 case DSO_BINARY_TYPE__DEBUGLINK:
1260 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1261 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1262 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1263 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1264 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
1265 return !kmod && dso->kernel == DSO_TYPE_USER;
1266
1267 case DSO_BINARY_TYPE__KALLSYMS:
1268 case DSO_BINARY_TYPE__VMLINUX:
1269 case DSO_BINARY_TYPE__KCORE:
1270 return dso->kernel == DSO_TYPE_KERNEL;
1271
1272 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1273 case DSO_BINARY_TYPE__GUEST_VMLINUX:
1274 case DSO_BINARY_TYPE__GUEST_KCORE:
1275 return dso->kernel == DSO_TYPE_GUEST_KERNEL;
1276
1277 case DSO_BINARY_TYPE__GUEST_KMODULE:
1278 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
1279 /*
1280 * kernel modules know their symtab type - it's set when
1281 * creating a module dso in machine__new_module().
1282 */
1283 return kmod && dso->symtab_type == type;
1284
1285 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
1286 return true;
1287
1288 case DSO_BINARY_TYPE__NOT_FOUND:
1289 default:
1290 return false;
1291 }
1292}
1293
1254int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter) 1294int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
1255{ 1295{
1256 char *name; 1296 char *name;
@@ -1261,6 +1301,7 @@ int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
1261 int ss_pos = 0; 1301 int ss_pos = 0;
1262 struct symsrc ss_[2]; 1302 struct symsrc ss_[2];
1263 struct symsrc *syms_ss = NULL, *runtime_ss = NULL; 1303 struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
1304 bool kmod;
1264 1305
1265 dso__set_loaded(dso, map->type); 1306 dso__set_loaded(dso, map->type);
1266 1307
@@ -1301,7 +1342,11 @@ int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
1301 if (!name) 1342 if (!name)
1302 return -1; 1343 return -1;
1303 1344
1304 /* Iterate over candidate debug images. 1345 kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1346 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1347
1348 /*
1349 * Iterate over candidate debug images.
1305 * Keep track of "interesting" ones (those which have a symtab, dynsym, 1350 * Keep track of "interesting" ones (those which have a symtab, dynsym,
1306 * and/or opd section) for processing. 1351 * and/or opd section) for processing.
1307 */ 1352 */
@@ -1311,6 +1356,9 @@ int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
1311 1356
1312 enum dso_binary_type symtab_type = binary_type_symtab[i]; 1357 enum dso_binary_type symtab_type = binary_type_symtab[i];
1313 1358
1359 if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type))
1360 continue;
1361
1314 if (dso__read_binary_type_filename(dso, symtab_type, 1362 if (dso__read_binary_type_filename(dso, symtab_type,
1315 root_dir, name, PATH_MAX)) 1363 root_dir, name, PATH_MAX))
1316 continue; 1364 continue;
@@ -1353,15 +1401,10 @@ int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
1353 if (!runtime_ss && syms_ss) 1401 if (!runtime_ss && syms_ss)
1354 runtime_ss = syms_ss; 1402 runtime_ss = syms_ss;
1355 1403
1356 if (syms_ss) { 1404 if (syms_ss)
1357 int km; 1405 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, kmod);
1358 1406 else
1359 km = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1360 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1361 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, km);
1362 } else {
1363 ret = -1; 1407 ret = -1;
1364 }
1365 1408
1366 if (ret > 0) { 1409 if (ret > 0) {
1367 int nr_plt; 1410 int nr_plt;
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index fffe2888a1c7..501e4e722e8e 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -79,6 +79,17 @@ struct symbol {
79void symbol__delete(struct symbol *sym); 79void symbol__delete(struct symbol *sym);
80void symbols__delete(struct rb_root *symbols); 80void symbols__delete(struct rb_root *symbols);
81 81
82/* symbols__for_each_entry - iterate over symbols (rb_root)
83 *
84 * @symbols: the rb_root of symbols
85 * @pos: the 'struct symbol *' to use as a loop cursor
86 * @nd: the 'struct rb_node *' to use as a temporary storage
87 */
88#define symbols__for_each_entry(symbols, pos, nd) \
89 for (nd = rb_first(symbols); \
90 nd && (pos = rb_entry(nd, struct symbol, rb_node)); \
91 nd = rb_next(nd))
92
82static inline size_t symbol__size(const struct symbol *sym) 93static inline size_t symbol__size(const struct symbol *sym)
83{ 94{
84 return sym->end - sym->start + 1; 95 return sym->end - sym->start + 1;
@@ -175,7 +186,7 @@ struct addr_location {
175 struct symbol *sym; 186 struct symbol *sym;
176 u64 addr; 187 u64 addr;
177 char level; 188 char level;
178 bool filtered; 189 u8 filtered;
179 u8 cpumode; 190 u8 cpumode;
180 s32 cpu; 191 s32 cpu;
181}; 192};
@@ -223,7 +234,6 @@ struct symbol *dso__find_symbol(struct dso *dso, enum map_type type,
223 u64 addr); 234 u64 addr);
224struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type, 235struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
225 const char *name); 236 const char *name);
226struct symbol *dso__first_symbol(struct dso *dso, enum map_type type);
227 237
228int filename__read_build_id(const char *filename, void *bf, size_t size); 238int filename__read_build_id(const char *filename, void *bf, size_t size);
229int sysfs__read_build_id(const char *filename, void *bf, size_t size); 239int sysfs__read_build_id(const char *filename, void *bf, size_t size);
diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
index 0358882c8910..3ce0498bdae6 100644
--- a/tools/perf/util/thread.c
+++ b/tools/perf/util/thread.c
@@ -142,3 +142,24 @@ int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
142 142
143 return 0; 143 return 0;
144} 144}
145
146void thread__find_cpumode_addr_location(struct thread *thread,
147 struct machine *machine,
148 enum map_type type, u64 addr,
149 struct addr_location *al)
150{
151 size_t i;
152 const u8 const cpumodes[] = {
153 PERF_RECORD_MISC_USER,
154 PERF_RECORD_MISC_KERNEL,
155 PERF_RECORD_MISC_GUEST_USER,
156 PERF_RECORD_MISC_GUEST_KERNEL
157 };
158
159 for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
160 thread__find_addr_location(thread, machine, cpumodes[i], type,
161 addr, al);
162 if (al->map)
163 break;
164 }
165}
diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h
index 5b856bf942e1..9b29f085aede 100644
--- a/tools/perf/util/thread.h
+++ b/tools/perf/util/thread.h
@@ -44,12 +44,6 @@ void thread__insert_map(struct thread *thread, struct map *map);
44int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp); 44int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp);
45size_t thread__fprintf(struct thread *thread, FILE *fp); 45size_t thread__fprintf(struct thread *thread, FILE *fp);
46 46
47static inline struct map *thread__find_map(struct thread *thread,
48 enum map_type type, u64 addr)
49{
50 return thread ? map_groups__find(&thread->mg, type, addr) : NULL;
51}
52
53void thread__find_addr_map(struct thread *thread, struct machine *machine, 47void thread__find_addr_map(struct thread *thread, struct machine *machine,
54 u8 cpumode, enum map_type type, u64 addr, 48 u8 cpumode, enum map_type type, u64 addr,
55 struct addr_location *al); 49 struct addr_location *al);
@@ -58,6 +52,11 @@ void thread__find_addr_location(struct thread *thread, struct machine *machine,
58 u8 cpumode, enum map_type type, u64 addr, 52 u8 cpumode, enum map_type type, u64 addr,
59 struct addr_location *al); 53 struct addr_location *al);
60 54
55void thread__find_cpumode_addr_location(struct thread *thread,
56 struct machine *machine,
57 enum map_type type, u64 addr,
58 struct addr_location *al);
59
61static inline void *thread__priv(struct thread *thread) 60static inline void *thread__priv(struct thread *thread)
62{ 61{
63 return thread->priv; 62 return thread->priv;
diff --git a/tools/perf/util/trace-event-parse.c b/tools/perf/util/trace-event-parse.c
index e0d6d07f6848..c36636fd825b 100644
--- a/tools/perf/util/trace-event-parse.c
+++ b/tools/perf/util/trace-event-parse.c
@@ -126,6 +126,7 @@ void event_format__print(struct event_format *event,
126 trace_seq_init(&s); 126 trace_seq_init(&s);
127 pevent_event_info(&s, event, &record); 127 pevent_event_info(&s, event, &record);
128 trace_seq_do_printf(&s); 128 trace_seq_do_printf(&s);
129 trace_seq_destroy(&s);
129} 130}
130 131
131void parse_proc_kallsyms(struct pevent *pevent, 132void parse_proc_kallsyms(struct pevent *pevent,
diff --git a/tools/perf/util/unwind-libdw.c b/tools/perf/util/unwind-libdw.c
new file mode 100644
index 000000000000..67db73ec3dab
--- /dev/null
+++ b/tools/perf/util/unwind-libdw.c
@@ -0,0 +1,210 @@
1#include <linux/compiler.h>
2#include <elfutils/libdw.h>
3#include <elfutils/libdwfl.h>
4#include <inttypes.h>
5#include <errno.h>
6#include "unwind.h"
7#include "unwind-libdw.h"
8#include "machine.h"
9#include "thread.h"
10#include "types.h"
11#include "event.h"
12#include "perf_regs.h"
13
14static char *debuginfo_path;
15
16static const Dwfl_Callbacks offline_callbacks = {
17 .find_debuginfo = dwfl_standard_find_debuginfo,
18 .debuginfo_path = &debuginfo_path,
19 .section_address = dwfl_offline_section_address,
20};
21
22static int __report_module(struct addr_location *al, u64 ip,
23 struct unwind_info *ui)
24{
25 Dwfl_Module *mod;
26 struct dso *dso = NULL;
27
28 thread__find_addr_location(ui->thread, ui->machine,
29 PERF_RECORD_MISC_USER,
30 MAP__FUNCTION, ip, al);
31
32 if (al->map)
33 dso = al->map->dso;
34
35 if (!dso)
36 return 0;
37
38 mod = dwfl_addrmodule(ui->dwfl, ip);
39 if (!mod)
40 mod = dwfl_report_elf(ui->dwfl, dso->short_name,
41 dso->long_name, -1, al->map->start,
42 false);
43
44 return mod && dwfl_addrmodule(ui->dwfl, ip) == mod ? 0 : -1;
45}
46
47static int report_module(u64 ip, struct unwind_info *ui)
48{
49 struct addr_location al;
50
51 return __report_module(&al, ip, ui);
52}
53
54static int entry(u64 ip, struct unwind_info *ui)
55
56{
57 struct unwind_entry e;
58 struct addr_location al;
59
60 if (__report_module(&al, ip, ui))
61 return -1;
62
63 e.ip = ip;
64 e.map = al.map;
65 e.sym = al.sym;
66
67 pr_debug("unwind: %s:ip = 0x%" PRIx64 " (0x%" PRIx64 ")\n",
68 al.sym ? al.sym->name : "''",
69 ip,
70 al.map ? al.map->map_ip(al.map, ip) : (u64) 0);
71
72 return ui->cb(&e, ui->arg);
73}
74
75static pid_t next_thread(Dwfl *dwfl, void *arg, void **thread_argp)
76{
77 /* We want only single thread to be processed. */
78 if (*thread_argp != NULL)
79 return 0;
80
81 *thread_argp = arg;
82 return dwfl_pid(dwfl);
83}
84
85static int access_dso_mem(struct unwind_info *ui, Dwarf_Addr addr,
86 Dwarf_Word *data)
87{
88 struct addr_location al;
89 ssize_t size;
90
91 thread__find_addr_map(ui->thread, ui->machine, PERF_RECORD_MISC_USER,
92 MAP__FUNCTION, addr, &al);
93 if (!al.map) {
94 pr_debug("unwind: no map for %lx\n", (unsigned long)addr);
95 return -1;
96 }
97
98 if (!al.map->dso)
99 return -1;
100
101 size = dso__data_read_addr(al.map->dso, al.map, ui->machine,
102 addr, (u8 *) data, sizeof(*data));
103
104 return !(size == sizeof(*data));
105}
106
107static bool memory_read(Dwfl *dwfl __maybe_unused, Dwarf_Addr addr, Dwarf_Word *result,
108 void *arg)
109{
110 struct unwind_info *ui = arg;
111 struct stack_dump *stack = &ui->sample->user_stack;
112 u64 start, end;
113 int offset;
114 int ret;
115
116 ret = perf_reg_value(&start, &ui->sample->user_regs, PERF_REG_SP);
117 if (ret)
118 return false;
119
120 end = start + stack->size;
121
122 /* Check overflow. */
123 if (addr + sizeof(Dwarf_Word) < addr)
124 return false;
125
126 if (addr < start || addr + sizeof(Dwarf_Word) > end) {
127 ret = access_dso_mem(ui, addr, result);
128 if (ret) {
129 pr_debug("unwind: access_mem 0x%" PRIx64 " not inside range"
130 " 0x%" PRIx64 "-0x%" PRIx64 "\n",
131 addr, start, end);
132 return false;
133 }
134 return true;
135 }
136
137 offset = addr - start;
138 *result = *(Dwarf_Word *)&stack->data[offset];
139 pr_debug("unwind: access_mem addr 0x%" PRIx64 ", val %lx, offset %d\n",
140 addr, (unsigned long)*result, offset);
141 return true;
142}
143
144static const Dwfl_Thread_Callbacks callbacks = {
145 .next_thread = next_thread,
146 .memory_read = memory_read,
147 .set_initial_registers = libdw__arch_set_initial_registers,
148};
149
150static int
151frame_callback(Dwfl_Frame *state, void *arg)
152{
153 struct unwind_info *ui = arg;
154 Dwarf_Addr pc;
155
156 if (!dwfl_frame_pc(state, &pc, NULL)) {
157 pr_err("%s", dwfl_errmsg(-1));
158 return DWARF_CB_ABORT;
159 }
160
161 return entry(pc, ui) || !(--ui->max_stack) ?
162 DWARF_CB_ABORT : DWARF_CB_OK;
163}
164
165int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
166 struct machine *machine, struct thread *thread,
167 struct perf_sample *data,
168 int max_stack)
169{
170 struct unwind_info ui = {
171 .sample = data,
172 .thread = thread,
173 .machine = machine,
174 .cb = cb,
175 .arg = arg,
176 .max_stack = max_stack,
177 };
178 Dwarf_Word ip;
179 int err = -EINVAL;
180
181 if (!data->user_regs.regs)
182 return -EINVAL;
183
184 ui.dwfl = dwfl_begin(&offline_callbacks);
185 if (!ui.dwfl)
186 goto out;
187
188 err = perf_reg_value(&ip, &data->user_regs, PERF_REG_IP);
189 if (err)
190 goto out;
191
192 err = report_module(ip, &ui);
193 if (err)
194 goto out;
195
196 if (!dwfl_attach_state(ui.dwfl, EM_NONE, thread->tid, &callbacks, &ui))
197 goto out;
198
199 err = dwfl_getthread_frames(ui.dwfl, thread->tid, frame_callback, &ui);
200
201 if (err && !ui.max_stack)
202 err = 0;
203
204 out:
205 if (err)
206 pr_debug("unwind: failed with '%s'\n", dwfl_errmsg(-1));
207
208 dwfl_end(ui.dwfl);
209 return 0;
210}
diff --git a/tools/perf/util/unwind-libdw.h b/tools/perf/util/unwind-libdw.h
new file mode 100644
index 000000000000..417a1426f3ad
--- /dev/null
+++ b/tools/perf/util/unwind-libdw.h
@@ -0,0 +1,21 @@
1#ifndef __PERF_UNWIND_LIBDW_H
2#define __PERF_UNWIND_LIBDW_H
3
4#include <elfutils/libdwfl.h>
5#include "event.h"
6#include "thread.h"
7#include "unwind.h"
8
9bool libdw__arch_set_initial_registers(Dwfl_Thread *thread, void *arg);
10
11struct unwind_info {
12 Dwfl *dwfl;
13 struct perf_sample *sample;
14 struct machine *machine;
15 struct thread *thread;
16 unwind_entry_cb_t cb;
17 void *arg;
18 int max_stack;
19};
20
21#endif /* __PERF_UNWIND_LIBDW_H */
diff --git a/tools/perf/util/unwind.c b/tools/perf/util/unwind-libunwind.c
index 742f23bf35ff..bd5768d74f01 100644
--- a/tools/perf/util/unwind.c
+++ b/tools/perf/util/unwind-libunwind.c
@@ -86,7 +86,6 @@ struct unwind_info {
86 struct perf_sample *sample; 86 struct perf_sample *sample;
87 struct machine *machine; 87 struct machine *machine;
88 struct thread *thread; 88 struct thread *thread;
89 u64 sample_uregs;
90}; 89};
91 90
92#define dw_read(ptr, type, end) ({ \ 91#define dw_read(ptr, type, end) ({ \
@@ -391,30 +390,13 @@ static int access_dso_mem(struct unwind_info *ui, unw_word_t addr,
391 return !(size == sizeof(*data)); 390 return !(size == sizeof(*data));
392} 391}
393 392
394static int reg_value(unw_word_t *valp, struct regs_dump *regs, int id,
395 u64 sample_regs)
396{
397 int i, idx = 0;
398
399 if (!(sample_regs & (1 << id)))
400 return -EINVAL;
401
402 for (i = 0; i < id; i++) {
403 if (sample_regs & (1 << i))
404 idx++;
405 }
406
407 *valp = regs->regs[idx];
408 return 0;
409}
410
411static int access_mem(unw_addr_space_t __maybe_unused as, 393static int access_mem(unw_addr_space_t __maybe_unused as,
412 unw_word_t addr, unw_word_t *valp, 394 unw_word_t addr, unw_word_t *valp,
413 int __write, void *arg) 395 int __write, void *arg)
414{ 396{
415 struct unwind_info *ui = arg; 397 struct unwind_info *ui = arg;
416 struct stack_dump *stack = &ui->sample->user_stack; 398 struct stack_dump *stack = &ui->sample->user_stack;
417 unw_word_t start, end; 399 u64 start, end;
418 int offset; 400 int offset;
419 int ret; 401 int ret;
420 402
@@ -424,8 +406,7 @@ static int access_mem(unw_addr_space_t __maybe_unused as,
424 return 0; 406 return 0;
425 } 407 }
426 408
427 ret = reg_value(&start, &ui->sample->user_regs, PERF_REG_SP, 409 ret = perf_reg_value(&start, &ui->sample->user_regs, PERF_REG_SP);
428 ui->sample_uregs);
429 if (ret) 410 if (ret)
430 return ret; 411 return ret;
431 412
@@ -438,8 +419,9 @@ static int access_mem(unw_addr_space_t __maybe_unused as,
438 if (addr < start || addr + sizeof(unw_word_t) >= end) { 419 if (addr < start || addr + sizeof(unw_word_t) >= end) {
439 ret = access_dso_mem(ui, addr, valp); 420 ret = access_dso_mem(ui, addr, valp);
440 if (ret) { 421 if (ret) {
441 pr_debug("unwind: access_mem %p not inside range %p-%p\n", 422 pr_debug("unwind: access_mem %p not inside range"
442 (void *)addr, (void *)start, (void *)end); 423 " 0x%" PRIx64 "-0x%" PRIx64 "\n",
424 (void *) addr, start, end);
443 *valp = 0; 425 *valp = 0;
444 return ret; 426 return ret;
445 } 427 }
@@ -448,8 +430,8 @@ static int access_mem(unw_addr_space_t __maybe_unused as,
448 430
449 offset = addr - start; 431 offset = addr - start;
450 *valp = *(unw_word_t *)&stack->data[offset]; 432 *valp = *(unw_word_t *)&stack->data[offset];
451 pr_debug("unwind: access_mem addr %p, val %lx, offset %d\n", 433 pr_debug("unwind: access_mem addr %p val %lx, offset %d\n",
452 (void *)addr, (unsigned long)*valp, offset); 434 (void *) addr, (unsigned long)*valp, offset);
453 return 0; 435 return 0;
454} 436}
455 437
@@ -459,6 +441,7 @@ static int access_reg(unw_addr_space_t __maybe_unused as,
459{ 441{
460 struct unwind_info *ui = arg; 442 struct unwind_info *ui = arg;
461 int id, ret; 443 int id, ret;
444 u64 val;
462 445
463 /* Don't support write, I suspect we don't need it. */ 446 /* Don't support write, I suspect we don't need it. */
464 if (__write) { 447 if (__write) {
@@ -471,16 +454,17 @@ static int access_reg(unw_addr_space_t __maybe_unused as,
471 return 0; 454 return 0;
472 } 455 }
473 456
474 id = unwind__arch_reg_id(regnum); 457 id = libunwind__arch_reg_id(regnum);
475 if (id < 0) 458 if (id < 0)
476 return -EINVAL; 459 return -EINVAL;
477 460
478 ret = reg_value(valp, &ui->sample->user_regs, id, ui->sample_uregs); 461 ret = perf_reg_value(&val, &ui->sample->user_regs, id);
479 if (ret) { 462 if (ret) {
480 pr_err("unwind: can't read reg %d\n", regnum); 463 pr_err("unwind: can't read reg %d\n", regnum);
481 return ret; 464 return ret;
482 } 465 }
483 466
467 *valp = (unw_word_t) val;
484 pr_debug("unwind: reg %d, val %lx\n", regnum, (unsigned long)*valp); 468 pr_debug("unwind: reg %d, val %lx\n", regnum, (unsigned long)*valp);
485 return 0; 469 return 0;
486} 470}
@@ -563,7 +547,7 @@ static int get_entries(struct unwind_info *ui, unwind_entry_cb_t cb,
563 unw_word_t ip; 547 unw_word_t ip;
564 548
565 unw_get_reg(&c, UNW_REG_IP, &ip); 549 unw_get_reg(&c, UNW_REG_IP, &ip);
566 ret = entry(ip, ui->thread, ui->machine, cb, arg); 550 ret = ip ? entry(ip, ui->thread, ui->machine, cb, arg) : 0;
567 } 551 }
568 552
569 unw_destroy_addr_space(addr_space); 553 unw_destroy_addr_space(addr_space);
@@ -572,13 +556,11 @@ static int get_entries(struct unwind_info *ui, unwind_entry_cb_t cb,
572 556
573int unwind__get_entries(unwind_entry_cb_t cb, void *arg, 557int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
574 struct machine *machine, struct thread *thread, 558 struct machine *machine, struct thread *thread,
575 u64 sample_uregs, struct perf_sample *data, 559 struct perf_sample *data, int max_stack)
576 int max_stack)
577{ 560{
578 unw_word_t ip; 561 u64 ip;
579 struct unwind_info ui = { 562 struct unwind_info ui = {
580 .sample = data, 563 .sample = data,
581 .sample_uregs = sample_uregs,
582 .thread = thread, 564 .thread = thread,
583 .machine = machine, 565 .machine = machine,
584 }; 566 };
@@ -587,7 +569,7 @@ int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
587 if (!data->user_regs.regs) 569 if (!data->user_regs.regs)
588 return -EINVAL; 570 return -EINVAL;
589 571
590 ret = reg_value(&ip, &data->user_regs, PERF_REG_IP, sample_uregs); 572 ret = perf_reg_value(&ip, &data->user_regs, PERF_REG_IP);
591 if (ret) 573 if (ret)
592 return ret; 574 return ret;
593 575
@@ -595,5 +577,5 @@ int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
595 if (ret) 577 if (ret)
596 return -ENOMEM; 578 return -ENOMEM;
597 579
598 return get_entries(&ui, cb, arg, max_stack); 580 return --max_stack > 0 ? get_entries(&ui, cb, arg, max_stack) : 0;
599} 581}
diff --git a/tools/perf/util/unwind.h b/tools/perf/util/unwind.h
index d5966f49e22c..b031316f221a 100644
--- a/tools/perf/util/unwind.h
+++ b/tools/perf/util/unwind.h
@@ -13,24 +13,25 @@ struct unwind_entry {
13 13
14typedef int (*unwind_entry_cb_t)(struct unwind_entry *entry, void *arg); 14typedef int (*unwind_entry_cb_t)(struct unwind_entry *entry, void *arg);
15 15
16#ifdef HAVE_LIBUNWIND_SUPPORT 16#ifdef HAVE_DWARF_UNWIND_SUPPORT
17int unwind__get_entries(unwind_entry_cb_t cb, void *arg, 17int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
18 struct machine *machine, 18 struct machine *machine,
19 struct thread *thread, 19 struct thread *thread,
20 u64 sample_uregs,
21 struct perf_sample *data, int max_stack); 20 struct perf_sample *data, int max_stack);
22int unwind__arch_reg_id(int regnum); 21/* libunwind specific */
22#ifdef HAVE_LIBUNWIND_SUPPORT
23int libunwind__arch_reg_id(int regnum);
24#endif
23#else 25#else
24static inline int 26static inline int
25unwind__get_entries(unwind_entry_cb_t cb __maybe_unused, 27unwind__get_entries(unwind_entry_cb_t cb __maybe_unused,
26 void *arg __maybe_unused, 28 void *arg __maybe_unused,
27 struct machine *machine __maybe_unused, 29 struct machine *machine __maybe_unused,
28 struct thread *thread __maybe_unused, 30 struct thread *thread __maybe_unused,
29 u64 sample_uregs __maybe_unused,
30 struct perf_sample *data __maybe_unused, 31 struct perf_sample *data __maybe_unused,
31 int max_stack __maybe_unused) 32 int max_stack __maybe_unused)
32{ 33{
33 return 0; 34 return 0;
34} 35}
35#endif /* HAVE_LIBUNWIND_SUPPORT */ 36#endif /* HAVE_DWARF_UNWIND_SUPPORT */
36#endif /* __UNWIND_H */ 37#endif /* __UNWIND_H */
diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
index 42ad667bb317..9f66549562bd 100644
--- a/tools/perf/util/util.c
+++ b/tools/perf/util/util.c
@@ -1,6 +1,6 @@
1#include "../perf.h" 1#include "../perf.h"
2#include "util.h" 2#include "util.h"
3#include "fs.h" 3#include <api/fs/fs.h>
4#include <sys/mman.h> 4#include <sys/mman.h>
5#ifdef HAVE_BACKTRACE_SUPPORT 5#ifdef HAVE_BACKTRACE_SUPPORT
6#include <execinfo.h> 6#include <execinfo.h>