aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKrister Johansen <kjlx@templeofstupid.com>2017-07-05 21:48:08 -0400
committerArnaldo Carvalho de Melo <acme@redhat.com>2017-07-18 22:14:09 -0400
commit843ff37bb59edbe51d64e77ba1b3245a15a4dd9f (patch)
tree7c1e50cb4b60af9776364ec65118c9a0db0cbcdf
parent86bcdb5a43997bb02ba25a76482c7bfc652ba45b (diff)
perf symbols: Find symbols in different mount namespace
Teach perf how to resolve symbols from binaries that are in a different mount namespace from the tool. This allows perf to generate meaningful stack traces even if the binary resides in a different mount namespace from the tool. Signed-off-by: Krister Johansen <kjlx@templeofstupid.com> Tested-by: Brendan Gregg <brendan.d.gregg@gmail.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas-Mich Richter <tmricht@linux.vnet.ibm.com> Link: http://lkml.kernel.org/r/1499305693-1599-2-git-send-email-kjlx@templeofstupid.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
-rw-r--r--tools/perf/util/dso.c1
-rw-r--r--tools/perf/util/dso.h2
-rw-r--r--tools/perf/util/map.c2
-rw-r--r--tools/perf/util/namespaces.c127
-rw-r--r--tools/perf/util/namespaces.h33
-rw-r--r--tools/perf/util/symbol.c11
-rw-r--r--tools/perf/util/thread.c3
-rw-r--r--tools/perf/util/thread.h1
8 files changed, 180 insertions, 0 deletions
diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
index 4e7ab611377a..beda40ed63b0 100644
--- a/tools/perf/util/dso.c
+++ b/tools/perf/util/dso.c
@@ -1236,6 +1236,7 @@ void dso__delete(struct dso *dso)
1236 dso_cache__free(dso); 1236 dso_cache__free(dso);
1237 dso__free_a2l(dso); 1237 dso__free_a2l(dso);
1238 zfree(&dso->symsrc_filename); 1238 zfree(&dso->symsrc_filename);
1239 nsinfo__zput(dso->nsinfo);
1239 pthread_mutex_destroy(&dso->lock); 1240 pthread_mutex_destroy(&dso->lock);
1240 free(dso); 1241 free(dso);
1241} 1242}
diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h
index bd061ba7b47c..78ec637fc68b 100644
--- a/tools/perf/util/dso.h
+++ b/tools/perf/util/dso.h
@@ -10,6 +10,7 @@
10#include <linux/types.h> 10#include <linux/types.h>
11#include <linux/bitops.h> 11#include <linux/bitops.h>
12#include "map.h" 12#include "map.h"
13#include "namespaces.h"
13#include "build-id.h" 14#include "build-id.h"
14 15
15enum dso_binary_type { 16enum dso_binary_type {
@@ -187,6 +188,7 @@ struct dso {
187 void *priv; 188 void *priv;
188 u64 db_id; 189 u64 db_id;
189 }; 190 };
191 struct nsinfo *nsinfo;
190 refcount_t refcnt; 192 refcount_t refcnt;
191 char name[0]; 193 char name[0];
192}; 194};
diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c
index 2179b2deb730..5dc60ca5a294 100644
--- a/tools/perf/util/map.c
+++ b/tools/perf/util/map.c
@@ -16,6 +16,7 @@
16#include "machine.h" 16#include "machine.h"
17#include <linux/string.h> 17#include <linux/string.h>
18#include "srcline.h" 18#include "srcline.h"
19#include "namespaces.h"
19#include "unwind.h" 20#include "unwind.h"
20 21
21static void __maps__insert(struct maps *maps, struct map *map); 22static void __maps__insert(struct maps *maps, struct map *map);
@@ -200,6 +201,7 @@ struct map *map__new(struct machine *machine, u64 start, u64 len,
200 if (type != MAP__FUNCTION) 201 if (type != MAP__FUNCTION)
201 dso__set_loaded(dso, map->type); 202 dso__set_loaded(dso, map->type);
202 } 203 }
204 dso->nsinfo = nsinfo__get(thread->nsinfo);
203 dso__put(dso); 205 dso__put(dso);
204 } 206 }
205 return map; 207 return map;
diff --git a/tools/perf/util/namespaces.c b/tools/perf/util/namespaces.c
index 67dcbcc73c7d..bcc6bb19cf10 100644
--- a/tools/perf/util/namespaces.c
+++ b/tools/perf/util/namespaces.c
@@ -9,9 +9,13 @@
9#include "namespaces.h" 9#include "namespaces.h"
10#include "util.h" 10#include "util.h"
11#include "event.h" 11#include "event.h"
12#include <sys/types.h>
13#include <sys/stat.h>
14#include <sched.h>
12#include <stdlib.h> 15#include <stdlib.h>
13#include <stdio.h> 16#include <stdio.h>
14#include <string.h> 17#include <string.h>
18#include <unistd.h>
15 19
16struct namespaces *namespaces__new(struct namespaces_event *event) 20struct namespaces *namespaces__new(struct namespaces_event *event)
17{ 21{
@@ -35,3 +39,126 @@ void namespaces__free(struct namespaces *namespaces)
35{ 39{
36 free(namespaces); 40 free(namespaces);
37} 41}
42
43void nsinfo__init(struct nsinfo *nsi)
44{
45 char oldns[PATH_MAX];
46 char *newns = NULL;
47 struct stat old_stat;
48 struct stat new_stat;
49
50 if (snprintf(oldns, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
51 return;
52
53 if (asprintf(&newns, "/proc/%d/ns/mnt", nsi->pid) == -1)
54 return;
55
56 if (stat(oldns, &old_stat) < 0)
57 goto out;
58
59 if (stat(newns, &new_stat) < 0)
60 goto out;
61
62 /* Check if the mount namespaces differ, if so then indicate that we
63 * want to switch as part of looking up dso/map data.
64 */
65 if (old_stat.st_ino != new_stat.st_ino) {
66 nsi->need_setns = true;
67 nsi->mntns_path = newns;
68 newns = NULL;
69 }
70
71out:
72 free(newns);
73}
74
75struct nsinfo *nsinfo__new(pid_t pid)
76{
77 struct nsinfo *nsi = calloc(1, sizeof(*nsi));
78
79 if (nsi != NULL) {
80 nsi->pid = pid;
81 nsi->need_setns = false;
82 nsinfo__init(nsi);
83 refcount_set(&nsi->refcnt, 1);
84 }
85
86 return nsi;
87}
88
89void nsinfo__delete(struct nsinfo *nsi)
90{
91 zfree(&nsi->mntns_path);
92 free(nsi);
93}
94
95struct nsinfo *nsinfo__get(struct nsinfo *nsi)
96{
97 if (nsi)
98 refcount_inc(&nsi->refcnt);
99 return nsi;
100}
101
102void nsinfo__put(struct nsinfo *nsi)
103{
104 if (nsi && refcount_dec_and_test(&nsi->refcnt))
105 nsinfo__delete(nsi);
106}
107
108void nsinfo__mountns_enter(struct nsinfo *nsi, struct nscookie *nc)
109{
110 char curpath[PATH_MAX];
111 int oldns = -1;
112 int newns = -1;
113
114 if (nc == NULL)
115 return;
116
117 nc->oldns = -1;
118 nc->newns = -1;
119
120 if (!nsi || !nsi->need_setns)
121 return;
122
123 if (snprintf(curpath, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
124 return;
125
126 oldns = open(curpath, O_RDONLY);
127 if (oldns < 0)
128 return;
129
130 newns = open(nsi->mntns_path, O_RDONLY);
131 if (newns < 0)
132 goto errout;
133
134 if (setns(newns, CLONE_NEWNS) < 0)
135 goto errout;
136
137 nc->oldns = oldns;
138 nc->newns = newns;
139 return;
140
141errout:
142 if (oldns > -1)
143 close(oldns);
144 if (newns > -1)
145 close(newns);
146}
147
148void nsinfo__mountns_exit(struct nscookie *nc)
149{
150 if (nc == NULL || nc->oldns == -1 || nc->newns == -1)
151 return;
152
153 setns(nc->oldns, CLONE_NEWNS);
154
155 if (nc->oldns > -1) {
156 close(nc->oldns);
157 nc->oldns = -1;
158 }
159
160 if (nc->newns > -1) {
161 close(nc->newns);
162 nc->newns = -1;
163 }
164}
diff --git a/tools/perf/util/namespaces.h b/tools/perf/util/namespaces.h
index 468f1e9a1484..b20f6ead7b2d 100644
--- a/tools/perf/util/namespaces.h
+++ b/tools/perf/util/namespaces.h
@@ -11,6 +11,7 @@
11 11
12#include "../perf.h" 12#include "../perf.h"
13#include <linux/list.h> 13#include <linux/list.h>
14#include <linux/refcount.h>
14 15
15struct namespaces_event; 16struct namespaces_event;
16 17
@@ -23,4 +24,36 @@ struct namespaces {
23struct namespaces *namespaces__new(struct namespaces_event *event); 24struct namespaces *namespaces__new(struct namespaces_event *event);
24void namespaces__free(struct namespaces *namespaces); 25void namespaces__free(struct namespaces *namespaces);
25 26
27struct nsinfo {
28 pid_t pid;
29 bool need_setns;
30 char *mntns_path;
31 refcount_t refcnt;
32};
33
34struct nscookie {
35 int oldns;
36 int newns;
37};
38
39void nsinfo__init(struct nsinfo *nsi);
40struct nsinfo *nsinfo__new(pid_t pid);
41void nsinfo__delete(struct nsinfo *nsi);
42
43struct nsinfo *nsinfo__get(struct nsinfo *nsi);
44void nsinfo__put(struct nsinfo *nsi);
45
46void nsinfo__mountns_enter(struct nsinfo *nsi, struct nscookie *nc);
47void nsinfo__mountns_exit(struct nscookie *nc);
48
49static inline void __nsinfo__zput(struct nsinfo **nsip)
50{
51 if (nsip) {
52 nsinfo__put(*nsip);
53 *nsip = NULL;
54 }
55}
56
57#define nsinfo__zput(nsi) __nsinfo__zput(&nsi)
58
26#endif /* __PERF_NAMESPACES_H */ 59#endif /* __PERF_NAMESPACES_H */
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index e7a98dbd2aed..60a9eaa372ef 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -18,6 +18,8 @@
18#include "symbol.h" 18#include "symbol.h"
19#include "strlist.h" 19#include "strlist.h"
20#include "intlist.h" 20#include "intlist.h"
21#include "namespaces.h"
22#include "vdso.h"
21#include "header.h" 23#include "header.h"
22#include "path.h" 24#include "path.h"
23#include "sane_ctype.h" 25#include "sane_ctype.h"
@@ -1436,9 +1438,17 @@ int dso__load(struct dso *dso, struct map *map)
1436 struct symsrc *syms_ss = NULL, *runtime_ss = NULL; 1438 struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
1437 bool kmod; 1439 bool kmod;
1438 unsigned char build_id[BUILD_ID_SIZE]; 1440 unsigned char build_id[BUILD_ID_SIZE];
1441 struct nscookie nsc;
1439 1442
1443 nsinfo__mountns_enter(dso->nsinfo, &nsc);
1440 pthread_mutex_lock(&dso->lock); 1444 pthread_mutex_lock(&dso->lock);
1441 1445
1446 /* The vdso files always live in the host container, so don't go looking
1447 * for them in the container's mount namespace.
1448 */
1449 if (dso__is_vdso(dso))
1450 nsinfo__mountns_exit(&nsc);
1451
1442 /* check again under the dso->lock */ 1452 /* check again under the dso->lock */
1443 if (dso__loaded(dso, map->type)) { 1453 if (dso__loaded(dso, map->type)) {
1444 ret = 1; 1454 ret = 1;
@@ -1584,6 +1594,7 @@ out_free:
1584out: 1594out:
1585 dso__set_loaded(dso, map->type); 1595 dso__set_loaded(dso, map->type);
1586 pthread_mutex_unlock(&dso->lock); 1596 pthread_mutex_unlock(&dso->lock);
1597 nsinfo__mountns_exit(&nsc);
1587 1598
1588 return ret; 1599 return ret;
1589} 1600}
diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
index 378c418ca0c1..aee9a42102ba 100644
--- a/tools/perf/util/thread.c
+++ b/tools/perf/util/thread.c
@@ -59,6 +59,8 @@ struct thread *thread__new(pid_t pid, pid_t tid)
59 list_add(&comm->list, &thread->comm_list); 59 list_add(&comm->list, &thread->comm_list);
60 refcount_set(&thread->refcnt, 1); 60 refcount_set(&thread->refcnt, 1);
61 RB_CLEAR_NODE(&thread->rb_node); 61 RB_CLEAR_NODE(&thread->rb_node);
62 /* Thread holds first ref to nsdata. */
63 thread->nsinfo = nsinfo__new(pid);
62 } 64 }
63 65
64 return thread; 66 return thread;
@@ -91,6 +93,7 @@ void thread__delete(struct thread *thread)
91 comm__free(comm); 93 comm__free(comm);
92 } 94 }
93 unwind__finish_access(thread); 95 unwind__finish_access(thread);
96 nsinfo__zput(thread->nsinfo);
94 97
95 free(thread); 98 free(thread);
96} 99}
diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h
index 4eb849e9098f..cb1a5dd5c2b9 100644
--- a/tools/perf/util/thread.h
+++ b/tools/perf/util/thread.h
@@ -34,6 +34,7 @@ struct thread {
34 34
35 void *priv; 35 void *priv;
36 struct thread_stack *ts; 36 struct thread_stack *ts;
37 struct nsinfo *nsinfo;
37#ifdef HAVE_LIBUNWIND_SUPPORT 38#ifdef HAVE_LIBUNWIND_SUPPORT
38 void *addr_space; 39 void *addr_space;
39 struct unwind_libunwind_ops *unwind_libunwind_ops; 40 struct unwind_libunwind_ops *unwind_libunwind_ops;