Skip to content

Commit eca8183

Browse files
Michael Lentineolsajiri
authored andcommitted
perf tools: Add automatic remapping of Android libraries
This patch automatically adjusts the path of MMAP records associated with Android system libraries. The Android system is organized with system libraries found in /system/lib and user libraries in /data/app-lib. On the host system (not running Android), system libraries can be found in the downloaded NDK directory under ${NDK_ROOT}/platforms/${APP_PLATFORM}/arch-${ARCH}/usr/lib and the user libraries are installed under libs/${APP_ABI} within the apk build directory. This patch makes running the reporting tools possible on the host system using the libraries from the NDK. Signed-off-by: Michael Lentine <mlentine@google.com> Reviewed-by: Stephane Eranian <eranian@google.com> Link: http://lkml.kernel.org/r/1400579330-5043-3-git-send-email-eranian@google.com [ fixed 'space required before the open parenthesis' checkpatch.pl errors ] Signed-off-by: Jiri Olsa <jolsa@kernel.org>
1 parent 21cfc5e commit eca8183

1 file changed

Lines changed: 94 additions & 1 deletion

File tree

‎tools/perf/util/map.c‎

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,93 @@ static inline int is_no_dso_memory(const char *filename)
3232
!strcmp(filename, "[heap]");
3333
}
3434

35+
static inline int is_android_lib(const char *filename)
36+
{
37+
return !strncmp(filename, "/data/app-lib", 13) ||
38+
!strncmp(filename, "/system/lib", 11);
39+
}
40+
41+
static inline bool replace_android_lib(const char *filename, char *newfilename)
42+
{
43+
const char *libname;
44+
char *app_abi;
45+
size_t app_abi_length, new_length;
46+
size_t lib_length = 0;
47+
48+
libname = strrchr(filename, '/');
49+
if (libname)
50+
lib_length = strlen(libname);
51+
52+
app_abi = getenv("APP_ABI");
53+
if (!app_abi)
54+
return false;
55+
56+
app_abi_length = strlen(app_abi);
57+
58+
if (!strncmp(filename, "/data/app-lib", 13)) {
59+
char *apk_path;
60+
61+
if (!app_abi_length)
62+
return false;
63+
64+
new_length = 7 + app_abi_length + lib_length;
65+
66+
apk_path = getenv("APK_PATH");
67+
if (apk_path) {
68+
new_length += strlen(apk_path) + 1;
69+
if (new_length > PATH_MAX)
70+
return false;
71+
snprintf(newfilename, new_length,
72+
"%s/libs/%s/%s", apk_path, app_abi, libname);
73+
} else {
74+
if (new_length > PATH_MAX)
75+
return false;
76+
snprintf(newfilename, new_length,
77+
"libs/%s/%s", app_abi, libname);
78+
}
79+
return true;
80+
}
81+
82+
if (!strncmp(filename, "/system/lib/", 11)) {
83+
char *ndk, *app;
84+
const char *arch;
85+
size_t ndk_length;
86+
size_t app_length;
87+
88+
ndk = getenv("NDK_ROOT");
89+
app = getenv("APP_PLATFORM");
90+
91+
if (!(ndk && app))
92+
return false;
93+
94+
ndk_length = strlen(ndk);
95+
app_length = strlen(app);
96+
97+
if (!(ndk_length && app_length && app_abi_length))
98+
return false;
99+
100+
arch = !strncmp(app_abi, "arm", 3) ? "arm" :
101+
!strncmp(app_abi, "mips", 4) ? "mips" :
102+
!strncmp(app_abi, "x86", 3) ? "x86" : NULL;
103+
104+
if (!arch)
105+
return false;
106+
107+
new_length = 27 + ndk_length +
108+
app_length + lib_length
109+
+ strlen(arch);
110+
111+
if (new_length > PATH_MAX)
112+
return false;
113+
snprintf(newfilename, new_length,
114+
"%s/platforms/%s/arch-%s/usr/lib/%s",
115+
ndk, app, arch, libname);
116+
117+
return true;
118+
}
119+
return false;
120+
}
121+
35122
void map__init(struct map *map, enum map_type type,
36123
u64 start, u64 end, u64 pgoff, struct dso *dso)
37124
{
@@ -59,8 +146,9 @@ struct map *map__new(struct list_head *dsos__list, u64 start, u64 len,
59146
if (map != NULL) {
60147
char newfilename[PATH_MAX];
61148
struct dso *dso;
62-
int anon, no_dso, vdso;
149+
int anon, no_dso, vdso, android;
63150

151+
android = is_android_lib(filename);
64152
anon = is_anon_memory(filename);
65153
vdso = is_vdso_map(filename);
66154
no_dso = is_no_dso_memory(filename);
@@ -75,6 +163,11 @@ struct map *map__new(struct list_head *dsos__list, u64 start, u64 len,
75163
filename = newfilename;
76164
}
77165

166+
if (android) {
167+
if (replace_android_lib(filename, newfilename))
168+
filename = newfilename;
169+
}
170+
78171
if (vdso) {
79172
pgoff = 0;
80173
dso = vdso__dso_findnew(dsos__list);

0 commit comments

Comments
 (0)