我实验的版本是android 4.0.3r1。
通过repo init和repo sync下载好源码之后,
敲命令:source ./build/envsetup.sh
然后:lunch
You're building on Linux Lunch menu... pick a combo: 1. full-eng 2. full_x86-eng 3. vbox_x86-eng 4. full_stingray-userdebug 5. full_wingray-userdebug 6. full_crespo4g-userdebug 7. full_crespo-userdebug 8. full_maguro-userdebug 9. full_toro-userdebug 10. full_tuna-userdebug 11. full_panda-eng
输入2,回车。
然后,输入命令:make dalvikvm core ext dexopt framework android.policy services -j16
最后一个参数是为了加快编译速度,根据自己的机器配置来设置。
编译完成之后,如果cd到out/target/product/generic_x86/system/bin
然后,ll
会看到有如下两个文件:
wuhe.jyh@ubuntu:~/4.0.3r1/out/target/product/generic_x86/system/bin$ ll total 32 drwxr-xr-x 2 wuhe.jyh wuhe.jyh 4096 2013-06-03 15:29 ./ drwxr-xr-x 5 wuhe.jyh wuhe.jyh 4096 2013-06-03 15:29 ../ -rwxr-xr-x 1 wuhe.jyh wuhe.jyh 8311 2013-06-03 15:29 dalvikvm* -rwxr-xr-x 1 wuhe.jyh wuhe.jyh 12204 2013-06-03 15:29 dexopt*
此时,输入dalvikvm回车:
E/dalvikvm(24398): ERROR: must specify non-'.' bootclasspath W/dalvikvm(24398): CreateJavaVM failed: dvmClassStartup failed Dalvik VM init failed (check log file)
如果cd到~/4.0.3r1,然后执行:
./out/target/product/generic_x86/system/bin/dalvikvm
则:
wuhe.jyh@ubuntu:~/4.0.3r1$ ./out/target/product/generic_x86/system/bin/dalvikvm -bash: ./out/target/product/generic_x86/system/bin/dalvikvm: No such file or directory
启动dalvik,还是需要一些环境设置,用脚本比较方便。在源码根目录,创建文件rund.sh,内容如下(https://gist.github.com/guohai/5048153):
#!/bin/sh # base directory, at top of source tree; replace with absolute path base=`pwd` # configure root dir of interesting stuff root=$base/out/host/linux-x86 export ANDROID_ROOT=$root # configure bootclasspath bootpath=$base/out/target/product/generic_x86/system/framework export BOOTCLASSPATH=$bootpath/core.jar:$bootpath/ext.jar:$bootpath/framework.jar:$bootpath/android.policy.jar:$bootpath/services.jar export LD_LIBRARY_PATH=$bootpath/lib:$LD_LIBRARY_PATH # this is where we create the dalvik-cache directory; make sure it exists export ANDROID_DATA=/tmp/dalvik_$USER mkdir -p $ANDROID_DATA/dalvik-cache exec $root/bin/dalvikvm -Xdexopt:none $@
然后chmod a+x rund.sh
然后,我们创建一个Java文件:
class Foo { public static void main(String[] args) { System.out.println("Hello, Foo!!"); } }
保存为Foo.java到源码根目录。
然后,shell里边执行:
javac Foo.java
dx --dex --output=Foo.dex Foo.class
然后,我们来执行dalvik虚拟机:
./rund.sh -cp Foo.dex Foo回车
结果如下:
I/dalvikvm(24743): DexOpt: mismatch dep name: '/home/wuhe.jyh/4.0.3r1/out/target/product/generic_x86/system/framework/core.odex' vs. '/system/framework/core.odex' E/dalvikvm(24743): /home/wuhe.jyh/4.0.3r1/out/target/product/generic_x86/system/framework/ext.jar odex has stale dependencies I/dalvikvm(24743): Zip is good, but no classes.dex inside, and no valid .odex file in the same directory I/dalvikvm(24743): DexOpt: mismatch dep name: '/home/wuhe.jyh/4.0.3r1/out/target/product/generic_x86/system/framework/core.odex' vs. '/system/framework/core.odex' E/dalvikvm(24743): /home/wuhe.jyh/4.0.3r1/out/target/product/generic_x86/system/framework/framework.jar odex has stale dependencies I/dalvikvm(24743): Zip is good, but no classes.dex inside, and no valid .odex file in the same directory I/dalvikvm(24743): DexOpt: mismatch dep name: '/home/wuhe.jyh/4.0.3r1/out/target/product/generic_x86/system/framework/core.odex' vs. '/system/framework/core.odex' E/dalvikvm(24743): /home/wuhe.jyh/4.0.3r1/out/target/product/generic_x86/system/framework/android.policy.jar odex has stale dependencies I/dalvikvm(24743): Zip is good, but no classes.dex inside, and no valid .odex file in the same directory I/dalvikvm(24743): DexOpt: mismatch dep name: '/home/wuhe.jyh/4.0.3r1/out/target/product/generic_x86/system/framework/core.odex' vs. '/system/framework/core.odex' E/dalvikvm(24743): /home/wuhe.jyh/4.0.3r1/out/target/product/generic_x86/system/framework/services.jar odex has stale dependencies I/dalvikvm(24743): Zip is good, but no classes.dex inside, and no valid .odex file in the same directory Hello, Foo!!
如果要用gdb调试,只要把rund.sh最后一句修改成:
exec gdb $root/bin/dalvikvm
然后,
./rund.sh
然后在gdb调试下输入运行参数:
setargs-cphello.jarhello
设置断点
bmain
以下就可以单步调试dalvik了。
以下是我的运行结果:
wuhe.jyh@ubuntu:~/4.0.3r1$ ./rund.sh GNU gdb (Ubuntu/Linaro 7.2-1ubuntu11) 7.2 Copyright (C) 2010 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-linux-gnu". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>... Reading symbols from /home/wuhe.jyh/4.0.3r1/out/host/linux-x86/bin/dalvikvm...done. (gdb) set args -cp Foo.dex Foo (gdb) b main Breakpoint 1 at 0x804877a: file dalvik/dalvikvm/Main.cpp, line 152. (gdb) r Starting program: /home/wuhe.jyh/4.0.3r1/out/host/linux-x86/bin/dalvikvm -cp Foo.dex Foo [Thread debugging using libthread_db enabled] Breakpoint 1, main (argc=4, argv=0xffffcdf4) at dalvik/dalvikvm/Main.cpp:152 152 setvbuf(stdout, NULL, _IONBF, 0); (gdb) c Continuing. I/dalvikvm(24808): DexOpt: mismatch dep name: '/home/wuhe.jyh/4.0.3r1/out/target/product/generic_x86/system/framework/core.odex' vs. '/system/framework/core.odex' E/dalvikvm(24808): /home/wuhe.jyh/4.0.3r1/out/target/product/generic_x86/system/framework/ext.jar odex has stale dependencies I/dalvikvm(24808): Zip is good, but no classes.dex inside, and no valid .odex file in the same directory I/dalvikvm(24808): DexOpt: mismatch dep name: '/home/wuhe.jyh/4.0.3r1/out/target/product/generic_x86/system/framework/core.odex' vs. '/system/framework/core.odex' E/dalvikvm(24808): /home/wuhe.jyh/4.0.3r1/out/target/product/generic_x86/system/framework/framework.jar odex has stale dependencies I/dalvikvm(24808): Zip is good, but no classes.dex inside, and no valid .odex file in the same directory I/dalvikvm(24808): DexOpt: mismatch dep name: '/home/wuhe.jyh/4.0.3r1/out/target/product/generic_x86/system/framework/core.odex' vs. '/system/framework/core.odex' E/dalvikvm(24808): /home/wuhe.jyh/4.0.3r1/out/target/product/generic_x86/system/framework/android.policy.jar odex has stale dependencies I/dalvikvm(24808): Zip is good, but no classes.dex inside, and no valid .odex file in the same directory I/dalvikvm(24808): DexOpt: mismatch dep name: '/home/wuhe.jyh/4.0.3r1/out/target/product/generic_x86/system/framework/core.odex' vs. '/system/framework/core.odex' E/dalvikvm(24808): /home/wuhe.jyh/4.0.3r1/out/target/product/generic_x86/system/framework/services.jar odex has stale dependencies I/dalvikvm(24808): Zip is good, but no classes.dex inside, and no valid .odex file in the same directory [New Thread 0xf4ef3b70 (LWP 24811)] [New Thread 0xf46eeb70 (LWP 24812)] [New Thread 0xf3ee9b70 (LWP 24813)] [New Thread 0xf36e8b70 (LWP 24814)] [New Thread 0xf2ee3b70 (LWP 24815)] Hello, Foo!! [Thread 0xf36e8b70 (LWP 24814) exited] [Thread 0xf2ee3b70 (LWP 24815) exited] [Thread 0xf3ee9b70 (LWP 24813) exited] [Thread 0xf46eeb70 (LWP 24812) exited] [Thread 0xf4ef3b70 (LWP 24811) exited] Program exited normally.
gdb调试,可以用ctrl x+a切换到简易的图形界面。就像vc下的调试,可以对应到代码行。