是否可以检查调用进程从另一个共享库(.so)加载哪些共享库?我知道有这样的命令行工具,但是可以在C ++代码中执行这样的检查吗?
我需要以某种方式获取Android应用程序加载的本机共享库列表,但似乎无法从Java代码中获取。
答案 0 :(得分:2)
您可以使用/proc/<pid>/maps
文件或/proc/self/maps
作为调用进程:以。。'结尾的行用于链接的共享库。这是一个黑客,但应该工作。请注意,可以多次映射一个库,因此您需要跳过重复。
好消息:你可以用java做到这一点。下面的代码片段将当前进程的共享库打印到logcat。
try {
Set<String> libs = new HashSet<String>();
String mapsFile = "/proc/self/maps";
BufferedReader reader = new BufferedReader(new FileReader(mapsFile));
String line;
while ((line = reader.readLine()) != null) {
if (line.endsWith(".so")) {
int n = line.lastIndexOf(" ");
libs.add(line.substring(n + 1));
}
}
Log.d("Ldd", libs.size() + " libraries:");
for (String lib : libs) {
Log.d("Ldd", lib);
}
} catch (FileNotFoundException e) {
// Do some error handling...
} catch (IOException e) {
// Do some error handling...
}
我的设备上的输出是:
D/Ldd (11286): 55 libraries:
D/Ldd (11286): /system/lib/libc.so
D/Ldd (11286): /system/lib/libdbus.so
D/Ldd (11286): /system/lib/librpc.so
D/Ldd (11286): /system/lib/libEGL.so
D/Ldd (11286): /system/lib/libstagefright_color_conversion.so
D/Ldd (11286): /system/lib/libmedia.so
D/Ldd (11286): /system/lib/libemoji.so
D/Ldd (11286): /system/lib/libcrypto.so
D/Ldd (11286): /system/lib/libstagefright_avc_common.so
D/Ldd (11286): /system/lib/libnativehelper.so
D/Ldd (11286): /system/lib/libskiagl.so
D/Ldd (11286): /system/lib/libopencore_player.so
D/Ldd (11286): /system/lib/libjpeg.so
D/Ldd (11286): /system/lib/libsurfaceflinger_client.so
D/Ldd (11286): /system/lib/libstagefright.so
D/Ldd (11286): /system/lib/libdrm1.so
D/Ldd (11286): /system/lib/libdvm.so
D/Ldd (11286): /system/lib/libwebcore.so
D/Ldd (11286): /system/lib/libGLESv1_CM.so
D/Ldd (11286): /system/lib/libhardware.so
D/Ldd (11286): /system/lib/libexif.so
D/Ldd (11286): /system/lib/libgps.so
D/Ldd (11286): /system/lib/liblog.so
D/Ldd (11286): /system/lib/libexpat.so
D/Ldd (11286): /system/lib/libopencore_common.so
D/Ldd (11286): /system/lib/libbluedroid.so
D/Ldd (11286): /system/lib/libm.so
D/Ldd (11286): /system/lib/libicui18n.so
D/Ldd (11286): /system/lib/libomx_amrenc_sharedlibrary.so
D/Ldd (11286): /system/lib/libwpa_client.so
D/Ldd (11286): /system/lib/libstdc++.so
D/Ldd (11286): /system/lib/libandroid_runtime.so
D/Ldd (11286): /system/lib/libz.so
D/Ldd (11286): /system/lib/libETC1.so
D/Ldd (11286): /system/lib/libsonivox.so
D/Ldd (11286): /system/lib/libstlport.so
D/Ldd (11286): /system/lib/libutils.so
D/Ldd (11286): /system/lib/libicudata.so
D/Ldd (11286): /system/lib/libsqlite.so
D/Ldd (11286): /system/lib/libhardware_legacy.so
D/Ldd (11286): /system/lib/libpixelflinger.so
D/Ldd (11286): /system/lib/libvorbisidec.so
D/Ldd (11286): /system/lib/libstagefright_amrnb_common.so
D/Ldd (11286): /system/lib/libcutils.so
D/Ldd (11286): /system/lib/libui.so
D/Ldd (11286): /system/lib/libmedia_jni.so
D/Ldd (11286): /system/lib/libomx_sharedlibrary.so
D/Ldd (11286): /system/lib/libcamera_client.so
D/Ldd (11286): /system/lib/libskia.so
D/Ldd (11286): /system/lib/libopencore_net_support.so
D/Ldd (11286): /system/lib/libnetutils.so
D/Ldd (11286): /system/lib/libbinder.so
D/Ldd (11286): /system/lib/libssl.so
D/Ldd (11286): /system/lib/libicuuc.so
D/Ldd (11286): /system/lib/libGLESv2.so
如果必要,可以通过android.os.Process.myPid()
获得pid。
答案 1 :(得分:0)
WRT java,启动一个进程,在有问题的.so上调用ldd。从stdout捕获输出并处理输出:
Process process = Runtime.getRuntime().exec("ldd a.so");
StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream());
outputGobbler.start();
process.waitFor();
String result = outputGobbler.buffer;
有关课程定义,请参阅StreamGobbler上的开创性帖子。我添加了一个String buffer
变量,并在上一个链接的类定义中将此行System.out.println(type + ">" + line);
替换为此buffer = buffer + line+"\n";
。显然,我还将第二个参数放到了构造函数中。
class StreamGobbler extends Thread
{
InputStream is;
String buffer;
StreamGobbler(InputStream is)
{
this.is = is;
this.buffer = "";
}
public void run()
{
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
buffer += line+"\n";
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}