Android源码树中C代码的编译
侯 Sir说:“源码之下,了无秘密。” 但有些秘密还是要搞起来了看得更真切,仅从静态代码的体位很难体会到运动时的妙处。因此环境搭好了,下一步就是调试。gdbserver搭配gdb的调试环境走得很顺利,可是mmm出的C代码总是没有debugging symbol。在调研原因的过程中倒是熟悉了一些工具的用法,值得记录。
首先,关于mmm的代码,它是写在build/envsetup.sh中的函数,内部是对make的简单封装:指定要编译的模块,该函数会进入Android源码根目录,将指定模块的.mk文件传给make进行编译、链接。网上有对它的详细分析。
期初,我创建了external/helloworld,并在其下放了helloworld.c和Android.mk,执行
$ mmm external/helloworld
$ make snod
把生成的helloworld可执行文件打包到emulator的/system/bin里。当dbg提示(no debugging symbols found),我的第一反应是应该修改Android.mk文件,添加-g编译参数:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := helloworld
LOCAL_CFLAGS += -g -O0
LOCAL_SRC_FILES := $(call all-subdir-c-files)
include $(BUILD_EXECUTABLE)
可是没有任何效果,gdb还是那么提示。我怀疑是gdb的版本不匹配,我是用的是Mac平台,gdb是这么用的:
$ ./prebuilts/gcc/darwin-x86/arm/arm-linux-androideabi-4.9/bin/arm-linux-androideabi-gdb
(gdb) file out/target/product/generic/system/bin/helloworld
Reading symbols from out/target/product/generic/system/bin/helloworld...(no debugging symbols found)...done.
怎么验证helloworld中是否存在调试符号表呢?能干这事的工具还挺多,我用了三个:
- objdump工具,它可以用来反汇编binary文件,加参数-t显示symbol table:
$ ./prebuilts/gcc/darwin-x86/x86/x86_64-linux-android-4.9/bin/x86_64-linux-android-objdump -t out/target/product/generic/system/bin/helloworld
- nm工具,它可以列出目标文件中的各种符号:
$ ./prebuilts/gcc/darwin-x86/arm/arm-linux-androideabi-4.9/bin/arm-linux-androideabi-nm out/target/product/generic/system/bin/helloworld
- readelf工具,它可以解析elf格式的文件:
$ ./prebuilts/gcc/darwin-x86/arm/arm-linux-androideabi-4.9/bin/arm-linux-androideabi-readelf -a out/target/product/generic/system/bin/helloworld
Linux下的工具真是好强大,而且在prebuilts/gcc下面都有对应的版本。不过这些强大的工具一致表明刚刚make出来的binary确实没有调试信息!
我又怀疑到自己的编译环境,难道是我的cc工具有问题?在.mk文件下添加如下输出,看$(CC)是啥:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
$(warning $(CC))
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := helloworld
LOCAL_CFLAGS += -O2 -fno-inline
LOCAL_SRC_FILES := $(call all-subdir-c-files)
include $(BUILD_EXECUTABLE)
输出的结果就是cc,再使用cc -g helloworld.c看生成的a.out文件是有调试信息的!
网上不少文章都提到不要在LOCAL_CFLAGS中加入优化/调试级别的参数:
Try not to change the optimization/debugging level in your Android.mk, this can be handled automatically for you by specifying the appropriate information in your Application.mk, and will let the NDK generate useful data files used during debugging.
这让我更加疑惑,这种纯C文件没有Application.mk怎么办呢?
晚上跟宋大侠一起review过程的时候,大侠关注到mmm最后有这么一段文字:

原来犯了个弱智错误,mmm说得很清楚:它同时生成了多个版本的helloworld,其中有原始版本,也有被阉割掉调试信息的strip版本,那么用于打包到system.img中的是不是阉割版?
赶快挨个验明正身:
$ ./prebuilts/gcc/darwin-x86/arm/arm-linux-androideabi-4.9/bin/arm-linux-androideabi-nm out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld 00003e90 d _DYNAMIC
00003fc8 d _GLOBAL_OFFSET_TABLE_
00000df8 t _Unwind_Complete
00000eac t _Unwind_DeleteException
00000e74 t _Unwind_GetLanguageSpecificData
00000e90 t _Unwind_GetRegionStart
00000c88 t _Unwind_RaiseException
00000dfc t _Unwind_Resume
000009c0 t _Unwind_VRS_Get
............
果然是这样。所以不建议在LOCAL_CFLAGS中指定优化/调试等级。
Android源码树中C代码的编译的更多相关文章
- 编译android源码官方教程(2)建立编译环境「linux & mac osx」
https://source.android.com/source/initializing.html Establishing a Build Environment IN THIS DOCUMEN ...
- 编译android源码官方教程(4)开始编译
Preparing to Build IN THIS DOCUMENT Obtain proprietary binaries Download proprietary binaries Extrac ...
- android.mk android源码编译
http://www.cnblogs.com/chenbin7/archive/2013/01/05/2846863.html Android.mk简单分析 2013-01-05 22:51 by . ...
- 在Mac mini上编译Android源码
参考文章 1.Android 6.0 源代码编译实践 2.编译Android源码致命错误解决方案 实践过程 1.Mac下安装Ubuntu双系统 (1)Ubuntu版本:Ubuntu 15.10 注:实 ...
- 深入浅出 - Android系统移植与平台开发(五)- 编译Android源码(转)
2.3编译Android源码 Android源码体积非常庞大,由Dalvik虚拟机.Linux内核.编译系统.框架代码.Android定制C库.测试套件.系统应用程序等部分组成,在编译Android源 ...
- Android源码下载和编译过程
这是我在编译android源码时整理记录的编译步骤和错误解决方法,期间参考了一些网上的博客和教程. 第一步: 安装ubuntu12.04,分配一盘空间50G,2G内存.如果分配1G内存编译时将报错.( ...
- 【转】在Ubuntu下编译Android源码并运行Emulator
原文网址:http://www.mcuos.com/thread-4553-1-1.html 建立编译环境 1.在VirtualBox上安装Ubuntu 2.安装JDK $ sudo apt-ge ...
- 【转】Linux(ubuntu14.04)上编译Android4.4源码的环境搭建及编译全过程
原文网址:http://jileniao.net/linux-android-building.html sublime text让我伤心.本来很信任sublime text的自动保存功能,之前使用一 ...
- 一次编译Android源码实验
注意,本文只供参考,是老文章 1.必要的软件环境 sudo apt-get install build-essential sudo apt-get install make sudo apt-get ...
随机推荐
- immutable学习
React 做性能优化时有一个避免重复渲染的大招,就是使用 shouldComponentUpdate(),但它默认返回 true,即始终会执行 render() 方法,然后做 Virtual DOM ...
- (1)java8初体验
很多博客都拿Comparator,我也贴一下吧. java8以前的匿名内部类用来排序. //匿名内部类 @Test public void java8Test() { Person p1 = new ...
- grafana 4.0.2 + openfalcon query 1.4.3
转:http://blog.csdn.net/vbaspdelphi/article/details/53884282 grafana 4.0.2 加上官方app自带的openfalcon插件, 可以 ...
- type命令
用途说明 type命令用来显示指定命令的类型.一个命令的类型可以是如下几种: alias 别名 keyword 关键字,Shell保留字 function 函数,Shell函数 builtin 内建命 ...
- linux uid/euid/suid
Each UNIX process has 3 UIDs associated to it. Superuser/root is UID=0. UID Read UID. It is of the u ...
- spring 框架整合
struts hibernate spring 先贴出框架整合需要的maven <project xmlns="http://maven.apache.org/POM/4.0.0&qu ...
- js正则基础总结和工作中常用验证规则
知识是需要系统的.就像js正则用了那么多次,却还是浑浑噩噩,迫切需要来一次整理,那么来吧! 基本知识 元字符 \d 匹配数字等于[0-9] \w 匹配字母.数字.下划线.中文 \s 匹配任意空白字符 ...
- VC++ 6.0 快捷键
多行注释的快捷键 详细步骤 工具栏上右键-〉Customize-〉“Add-ins and Macro Files”tab页,把SAMPLE前面打上钩-〉“Commands”tab页,Categ ...
- 第四天:servlet的生命周期和一些细节问题
1. servlet的生命周期: a) 流程 i. Web服务器首先会检查是否装载了该servlet的实例对象.如果装载了直接进行第四步. ii. 装载并创建该servlet的实例对象. ii ...
- 关于mybatis和spring复合pom的异常
java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()L ...