A. 如何快速查找C语言代码中的宏的值
很多的系统,代码量超大,在我们阅读其代码的时候,往往macro能决定代码执行的具体分支。利用find grep查找起来会特别慢,而且不一定能找到正确的值。如果系统是可编译的,可以利用编译器的预处理功能很快知道宏的具体值。
方法如下:
在CFLAGS里面添加 -E 选项。
举例说明:
在android 系统里面,
1. 在Andriod.mk里面添加-E 参数。
LOCAL_CFLAGS += -E
2.重新编译,显示其过程
mm showcommands
3.编译过程会打印出类似下面的结果。
prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/arm-eabi-gcc -I hardware/ttd/marvell/generic/bmm-lib/lib -I out/target/proct/OMS_TTD/obj/SHARED_LIBRARIES/libbmm_intermediates -I system/core/include -I hardware/libhardware/include -I hardware/libhardware_legacy/include -I hardware/ril/include -I dalvik/libnativehelper/include -I frameworks/base/include -I frameworks/base/opengl/include -I external/skia/include -I out/target/proct/OMS_TTD/obj/include -I bionic/libc/arch-arm/include -I bionic/libc/include -I bionic/libstdc++/include -I bionic/libc/kernel/common -I bionic/libc/kernel/arch-arm -I bionic/libm/include -I bionic/libm/include/arch/arm -I bionic/libthread_db/include -c -fno-exceptions -Wno-multichar -msoft-float -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-short-enums -march=armv5te -mtune=xscale -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -include system/core/include/arch/linux-arm/AndroidConfig.h -I system/core/include/arch/linux-arm/ -mthumb-interwork -DANDROID -fmessage-length=0 -W -Wall -Wno-unused -Winit-self -Wpointer-arith -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -DNDEBUG -g -Wstrict-aliasing=2 -finline-functions -fno-inline-functions-called-once -fgcse-after-reload -frerun-cse-after-loop -frename-registers -DNDEBUG -UDEBUG -DLOG_DISABLEDEBUG=1 -mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -E -MD -o out/target/proct/OMS_TTD/obj/SHARED_LIBRARIES/libbmm_intermediates/bmm_lib.o hardware/ttd/marvell/generic/bmm-lib/lib/bmm_lib.c
其中-o 是生成的文件。
打开这个文件,就是我们预编译的结果,可以很清晰地看到各个宏的具体值。
B. ubuntu 驱动编译无法通过 求神来解决
复制代码
1 #ifndef __KERNEL__
2 # define __KERNEL__
3 #endif
4 #ifndef MODULE
5 # define MODULE
6 #endif
7
8 // 下面的是主要的内容
9 #include <linux/kernel.h>
10 #include <linux/mole.h>
11 #include <linux/init.h>
12
13 MODULE_LICENSE("GPL");
14
15 static int year=2012;
16
17 int hello_init()
18 {
19 printk(KERN_WARNING "Hello kernel, it's %d!\n",year);
20 return 0;
21 }
22
23
24 void hello_exit()
25 {
26 printk("Bye, kernel!\n");
27 }
28
29 // 下面两个为关键的模块函数
30 mole_init(hello_init);
31 mole_exit(hello_exit);
复制代码
如果上面的代码看起来不太熟悉,那么需要查看以下相关的书籍,比如《Linux设备驱动程序,第三版》,也就是大名鼎鼎的LDD;
2、老式驱动模块编译方法:
直接写出make规则到makefile文件中,引用内核体系的头文件路径,举例如下:
复制代码
1 # The path of kernel source code
2 INCLUDEDIR = /media/GoldenResources/linux/linux-2.6.30/include
3
4 # Compiler
5 CC = gcc
6
7 # Options
8 CFLAGS = -D__KERNEL__ -DMODULE -O -Wall -I$(INCLUDEDIR)
9
10 # Target
11 OBJS = hello.o
12
13 all: $(OBJS)
14
15 $(OBJS): hello.c
16 $(CC) $(CFLAGS) -c $<
17
18 install:
19 insmod $(OBJS)
20
21 uninstall:
22 rmmod hello
23
24 .PHONY: clean
25 clean:
26 rm -f *.o
复制代码
这里有我是用的一个linux内核源代码路径:/media/GoldenResources/linux/linux-2.6.30/include ,注意设置到正确的源码路径。
尝试这编译:
复制代码
$make
gcc -D__KERNEL__ -DMODULE -O -Wall -I/media/GoldenResources/linux/linux-2.6.30/include -c hello.c
In file included from /media/GoldenResources/linux/linux-2.6.30/include/linux/kernel.h:11:0,
from hello.c:8:
/media/GoldenResources/linux/linux-2.6.30/include/linux/linkage.h:5:25: fatal error: asm/linkage.h: No such file or directory
compilation terminated.
make: *** [hello.o] Error 1
复制代码
C. 如何编译libcurl
1. Android
1.1配置
将curl源文件加到在Android源代码external/curl/中。创建shell脚本configure.sh,并把它放到external/curl/中。
脚本内容如下:
#!/bin/sh SDK_ROOT=/home/nudtzxm/android_sdk export PATH="$SDK_ROOT/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin:$PATH" ./configure --host=arm-linux CC=arm-eabi-gcc \ CPPFLAGS="-I $SDK_ROOT/system/core/include -I $SDK_ROOT/hardware/libhardware/include -I $SDK_ROOT/hardware/libhardware_legacy/include -I $SDK_ROOT/hardware/ril/include -I $SDK_ROOT/dalvik/libnativehelper/include -I $SDK_ROOT/frameworks/base/include -I $SDK_ROOT/frameworks/base/opengl/include -I $SDK_ROOT/external/skia/include -I $SDK_ROOT/out/target/proct/generic/obj/include -I $SDK_ROOT/bionic/libc/arch-arm/include -I $SDK_ROOT/bionic/libc/include -I $SDK_ROOT/bionic/libstdc++/include -I $SDK_ROOT/bionic/libc/kernel/common -I $SDK_ROOT/bionic/libc/kernel/arch-arm -I $SDK_ROOT/bionic/libm/include -I $SDK_ROOT/bionic/libm/include/arch/arm -I $SDK_ROOT/bionic/libthread_db/include -I $SDK_ROOT/bionic/libc/kernel/common -I $SDK_ROOT/bionic/libc/kernel/arch-arm -I $SDK_ROOT/system/core/libcutils -I $SDK_ROOT/out/target/proct/generic/obj/STATIC_LIBRARIES/libcutils_intermediates -I $SDK_ROOT/out/target/proct/generic/obj/STATIC_LIBRARIES/libwebcore_intermediates -I $SDK_ROOT/system/core/include/arch/linux-arm/ -include $SDK_ROOT/system/core/include/arch/linux-arm/AndroidConfig.h" \ CFLAGS="-nostdlib -fno-exceptions -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-short-enums -Wno-multichar -msoft-float -march=armv5te -mtune=xscale -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -mthumb-interwork -DANDROID -fmessage-length=0 -W -Wall -Wno-unused -Winit-self -Wpointer-arith -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -DSK_RELEASE -DNDEBUG -g -Wstrict-aliasing=2 -finline-functions -fno-inline-functions-called-once -fgcse-after-reload -frerun-cse-after-loop -frename-registers -DNDEBUG -UDEBUG -mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64" \ LIBS="-L$SDK_ROOT/out/target/proct/generic/obj/lib -L$SDK_ROOT/out/target/proct/generic/obj/STATIC_LIBRARIES/libcutils_intermediates/libcutils.a -lc -lm -ldl $SDK_ROOT/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/../lib/gcc/arm-eabi/4.4.0/interwork/libgcc.a"
进入到curl的根目录,运行这个脚本:
1.2 Make
进入curl的根目录,运行make curl命令:
1.3 参数配置
您可能会看到参数配置是复杂的。如何设置呢?你应该阅读curl/ Android.mk的注释。
2 iOS
我改变了参数的配置,所以配置命令是:
./configure –disable-shared –host=arm-apple-darwin10
3.视窗
打开的vc6curl.dsw,并选择“DLL Release”来编译。您会在CURL_ROOT\lib\DLL-Release
发现libcurl.dll和libcurl_imp.lib。
4.关于头文件
curl/include的头文件,只在成功配置后使用,头文件在不同的平台上是不同的。
D. 如何建立android的C/C++交叉编译环境
因此,构建android上C/C++的交叉编译环境也就成为了一个很大的需求。特别是对于已经取得root权限的机器,如果能直接运行按需编译的二进制文件,那么将可以做很多有意义和有趣的事情。 很不幸,Google没有直接给出如何建立这个交叉编译环境,但是我们可以借助Google提供的强大的NDK (Native Development Tools)来达到这一目的。NDK的本来目标是编译得到.so动态链接库文件,然后通过JNI提供给上层的Java调用,从而实现C/C++程序的简易迁移。而编译.so和编译成二进制可执行文件的过程是完全一样的,这就给了我们可以发挥的空间。 有两种方式获取交叉编译所需的工具链:git下prebuilt这个project或者直接去下载NDK,我这里arm-eabi的版本是最新的4.4.0。1 git clone git://android.git.kernel.org/platform/prebuilt.git 然后创建一个helloworld.c文件。1 2 3 4 5 6 //// root@delleon:~/android/myapp# cat helloworld.c#include int main(){printf("HelloWorld!n");return0;} 接下来创建Makefile文件。注意修改其中的NDK_DIR和SDKTOOL为自己的目录,修改APP为自己的待编译程序主文件名。另外注意自己的arm-eabi的版本,若有变化则也需要修改。1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 #### root@delleon:~/android/myapp# cat Makefile APP=helloworld NDK_DIR := ~/android/android-ndk-r4 NDK_HOST := linux-x86 SDKTOOL := ~/android/android-sdk-linux_86/tools TOOLCHAIN_PREFIX :=$(NDK_DIR)/build/prebuilt/$(NDK_HOST)/arm-eabi-4.4.0/bin/arm-eabi- CC :=$(TOOLCHAIN_PREFIX)gcc CPP :=$(TOOLCHAIN_PREFIX)g++ LD :=$(CC) COMMON_FLAGS :=-mandroid -ffunction-sections -fdata-sections -Os -g --sysroot=$(NDK_DIR)/build/platforms/android-5/arch-arm -fPIC -fvisibility=hidden -D__NEW__ CFLAGS :=$(COMMON_FLAGS) CFLAGS +=-D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -DANDROID -DSK_RELEASE -DNDEBUG CFLAGS +=-UDEBUG -march=armv5te -mtune=xscale -msoft-float -mthumb-interwork -fpic -ffunction-sections -funwind-tables -fstack-protector -fmessage-length=0-Bdynamic CPPFLAGS :=$(COMMON_FLAGS)-fno-rtti -fno-exceptions -fvisibility-inlines-hidden LDFLAGS +=--sysroot=$(NDK_DIR)/build/platforms/android-5/arch-arm LDFLAGS +=-Bdynamic -Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,noreloc LDFLAGS +=-L$(NDK_DIR)/build/prebuilt/$(NDK_HOST)/arm-eabi-4.4.0/lib/gcc/arm-eabi/4.4.0 LDFLAGS +=-L$(NDK_DIR)/build/prebuilt/$(NDK_HOST)/arm-eabi-4.4.0/lib/gcc LDFLAGS +=-L$(NDK_DIR)/build/prebuilt/$(NDK_HOST)/arm-eabi-4.4.0/arm-eabi/lib LDFLAGS +=-nostdlib -lc -llog -lgcc --no-undefined -z $(NDK_DIR)/build/platforms/android-5/arch-arm/usr/lib/crtbegin_dynamic.o $(NDK_DIR)/build/platforms/android-5/arch-arm/usr/lib/crtend_android.o OBJS +=$(APP).o all:$(APP) $(APP):$(OBJS)$(LD)$(LDFLAGS)-o $@$^ %.o:%.c $(CC)-c $(CFLAGS)$
E. 在linux apache php环境下,如何配置php的openssl模块各位高手快显显神通吧
回答不了你。只是说说我的经验,看看有没有帮助。openssl编译有些麻烦。最好用别人编译好的,直接安装。不管是centos, ubuntu, 还是freebsd都可以找到别人编译好的openssl库。安装上就可以了。然后是apache2, 也可找支持openssl的版本,直接安装。自己编译也可以,不过相当的麻烦。这样其实已经支持SSL了。
php不需要支持openssl,apache+php,只要apache支持了。打开43端口,在虚拟主机里设置43的虚拟主机就可以支持ssl。在虚拟主机里配置上php的连接方式。
tls是openssl中加密协议的一种。应该至少还有另外的两种。
F. 如何利用GCC编译选项检测栈溢出
gcc的一个编译选项:-fstack-protector,以下是关于这个选项的描述: -fstack-protector 启用该选项后编译器会产生额外的代码来检测缓冲区溢出,例如栈溢出攻击。这是通过在有缺陷的函数中添加一个保护变量来实现的。这包括会调用到alloca的函数
G. 如何利用gcc编译选项检测函数参数个数
gcc的一个编译选项:-fstack-protector,以下是关于这个选项的描述: -fstack-protector 启用该选项后编译器会产生额外的代码来检测缓冲区溢出,例如栈溢出攻击。这是通过在有缺陷的函数中添加一个保护变量来实现的。这包括会调用到alloca的函数.
H. 如何使用自己的makefile编译android ndk项目
android ndk提供了一套自己的makefile管理方式,要将源码项目移植到android平台,需要按照android的makefile规则编写makefile,还要按android的规则部署源码目录,对一个有自己的makefile管理方法的大型项目来说,只是做一下makefile迁移工作就是一件很麻烦的事。
其实android ndk上的编译说到底也就是交叉编译,只要配置好交叉编译工具链,使用原有的makefile也是可以编译出在android运行的c、c++程序的。
以android-ndk-r4-crystax的ndk版本为例:
编译器路径 android-ndk-r4-crystax/build/prebuilt/linux-x86/arm-eabi-4.4.0/bin
名称前缀 arm-eabi-
头文件目录 android-ndk-r4-crystax/build/platforms/android-3/arch-arm/usr/include
库文件目录 android-ndk-r4-crystax/build/platforms/android-3/arch-arm/usr/lib
你可以试一下上面的配置,如果编译链接都没有问题,可以adb push到android设备上运行看看,什么结果?
有点崩溃,根本运行不起来,你也许想试试看android自带的ndk例子,确实是能够运行的,问题在哪儿呢?
只是正确配置了编译器、头文件、库文件还不够,还需要配置编译、链接的参数,android例子中编译链接的参数是什么呢?你也许想深究一下android的makefile,可是不久你会发现那是更崩溃的事情,里面用了很多的make脚本函数。其实android的makefile是可以把执行的详细命令输出来的,只要make的时候加上V=1即可。可以看到确实带了很多参数
编译参数:
-fpic
-mthumb-interwork
-ffunction-sections
-funwind-tables
-fstack-protector
-fno-short-enums
-Wno-psabi
-march=armv5te
-mtune=xscale
-msoft-float
-mthumb
-fomit-frame-pointer
-fno-strict-aliasing
-finline-limit=64
-Wa,--noexecstack
-D__ARM_ARCH_5__
-D__ARM_ARCH_5T__
-D__ARM_ARCH_5E__
-D__ARM_ARCH_5TE__
-DANDROID
链接参数:
-nostdlib
-Bdynamic
-Wl,-dynamic-linker,/system/bin/linker
-Wl,--gc-sections
-Wl,-z,noreloc
-Wl,--no-undefined
-Wl,-z,noexecstack
-L$(PLATFORM_LIBRARY_DIRECTORYS)
crtbegin_static.o
crtend_android.o
这其中链接参数中的-Wl,-dynamic-linker,/system/bin/linker、crtbegin_static.o、crtend_android.o是最关键的,android使用了自己的进程加载器,并且自定义了c运行时的启动结束。难怪先前编译的进程启动不了。
I. [gdb]函数堆栈乱掉的解决办法 [转]
程序core掉,要去debug,但是函数堆栈乱掉了,很恶心.....经过Google/wiki一番,找到两种解决办法.
x86ManualBacktrace
This tutorial will show you how to manually rebuild a backtrace with GDB on x86 using the stack frame pointer and current instruction pointer.
Consider the following gdb backtrace:
It's pretty clear that this is corrupted, evidence the following field:
Get the register information for the process:
On x86:
Stack Frame Layout for x86:
Color Key:blue
Using the current stack frame address from %ebp mp the stack above it:
Using this stack mp we know that the stack frame address in the %ebp register is the stack frame for the current instruction in the %eip register (unless this was a leaf function which didn't stack a frame but that's irrelevant for this discussion). Using the %ebp and %eip registers as a starting point we can build the first line in our backtrace rebuild:
When program control branches to a new function a stack frame is stacked and the callee function's last address is stored at the new frame's address %ebp + 4 (i.e. the last memory address in the callee's stack frame which is +4 from the current stack frame at %ebp). In order to get the caller's stack frame and instruction pointer just look at %ebp and %ebp+4:
In the stack find the memory at the stack frame in %ebp and the one at %ebp+4 (which will hold the callee's instruction pointer).
So using the address at 0xbf9ef358 and 0xbf9ef35c which is 0xbf9ef388 and 0x00d94cf7 continue to build our list:
Continue by looking at the next stack frame address 0xbf9ef388:
Keep doing this until we have a full back trace. You'll know you've reached the bottom of the stack when the previous stack frame pointer is 0x00000000.
Here's a complete manually rebuilt stack frame:
amd64下面,无非就是寄存器变成rbp,字长增加了一倍.当然这边选择了手动寻找函数返回地址,然后info symbol打印出函数名,其实还可以通过gdb格式化来直接打印函数名:
gdb>x/128agrbp内的内容
所以手动还原的办法就变得很简单:
gdb>info reg rbp*x86换成info reg ebp
gdb>x/128ag rbp内的内容 *x86换成 x/128aw ebp的内容
这样就能看到函数栈.如果你想解析参数是啥,也是可以的,只是比较麻烦,苦力活儿....想解析参数,就要知道栈的布局,可以参考这篇文章: http://blog.csdn.net/liigo/archive/2006/12/23/1456938.aspx
昨天和 海洋 一块研究了下函数调用栈,顺便写两句。不足或错误之处请包涵!
理解调用栈最重要的两点是:栈的结构,EBP寄存器的作用。
首先要认识到这样两个事实:
1、一个函数调用动作可分解为:零到多个PUSH指令(用于参数入栈),一个CALL指令。CALL指令内部其实还暗含了一个将返回地址(即CALL指令下一条指令的地址)压栈的动作。
2、几乎所有本地编译器都会在每个函数体之前插入类似如下指令:PUSH EBP; MOV EBP ESP;
即,在程序执行到一个函数的真正函数体时,已经有以下数据顺序入栈:参数,返回地址,EBP。由此得到类似如下的栈结构(参数入栈顺序跟调用方式有关,这里以 C语言 默认的CDECL为例):
“PUSH EBP”“MOV EBP ESP”这两条指令实在大有深意:首先将EBP入栈,然后将栈顶指针ESP赋值给EBP。“MOV EBP ESP”这条指令表面上看是用ESP把EBP原来的值覆盖了,其实不然——因为给EBP赋值之前,原EBP值已经被压栈(位于栈顶),而新的EBP又恰恰指向栈顶。
此时EBP寄存器就已经处于一个非常重要的地位,该寄存器中存储着栈中的一个地址(原EBP入栈后的栈顶),从该地址为基准,向上(栈底方向)能获取返回地址、参数值,向下(栈顶方向)能获取函数局部变量值,而该地址处又存储着上一层函数调用时的EBP值!
一般而言,ss:[ebp+4]处为返回地址,ss:[ebp+8]处为第一个参数值(最后一个入栈的参数值,此处假设其占用4字节内存),ss:[ebp-4]处为第一个局部变量,ss:[ebp]处为上一层EBP值。
由于EBP中的地址处总是“上一层函数调用时的EBP值”,而在每一层函数调用中,都能通过当时的EBP值“向上(栈底方向)能获取返回地址、参数值,向下(栈顶方向)能获取函数局部变量值”。如此形成递归,直至到达栈底。这就是函数调用栈。
编译器对EBP的使用实在太精妙了。
从当前EBP出发,逐层向上找到所有的EBP是非常容易的:
这个办法比较简单,很容易实践,但是有一个前提,如果栈的内容被冲刷干净了,你连毛都看不到(事实就是这样).所以你需要开始栈保护...至少你还能找到栈顶的函数...
gcc有参数: -fstack-protector 和 -fstack-protector-all,强烈建议开启....
**********************************************************************/
用打印方法调试
在客户项目那里混了半年,发现Top的客户确实是比我们牛逼。先说说调试的方法。
客户那边不依赖于GDB调试,因为他们可能觉得GDB依赖于系统 实现,不利于移植吧,所以客户的程序完全是依赖于打印调试的。这点很佩服他们的软件规划能力和项目管理,实现能力。说老实话,如果换了一家中国公司,每人 一个调试方法,要follow 一个rule是很不容易的。
完善的调试菜单。调试菜单并不难实现,只是一个打印和字符接受的函数。在其中控制是开放某些打印信息。
在每个模块中加上仔细规划打印输出,根据需求分成不同的基本。最好情况是在最高打印级别中可以可以发现所有的问题。打印级别可以很方便的动态控制。
函数调用LOG
如果能定位发生问题的模块,可以在该模块的在每个函数的调用入口加上打印一个函数名字+Enter,在返回处加上一个函数名字+Exit。对于每个模块用一个打印开关控制是否打印Trace信息。在调试菜单中控制这个打印开关。
如果懒得加打印语句,可以利用gcc 的-finstrument-functions 选项来快速的加入调试信息。-finstrumnet-function会是的编译器在函数调用的开始和退出处调用
可以利用这两个函数来跟踪函数调用的过程。
在 实现这两个函数时要加入 attribute ((no_instrument_function));以避免编译器再调用这两个函数的时候也调用__cyg_profile_func_enter 和 __cyg_profile_func_exit 而造成循环调用。
可以用dladdr()来获得this_fn的文件和函数名。code如下:
// 由于dladdr是GNU扩展,不是dl的标准函数,因此在这句话必须加在文件的开始处
关于打印堆栈。可以用
该方法需要编译器支持。
但是需要在编译的时候加上-rdynamic 否则只能输出在内存中的绝对地址。
在没有-rdynamic的时候,关于如何找到动态库的运行时地址还需要研究。
可以在系统运行的时候发送SIGSEGV给应用程序,产生当前进程的Coremp来获取动态库中函数的运行是地址。
用GDB获取backtrace的方法(在有-g选项的时候可以看到,不需要-rdynamic):
list <*address>
在没有-g的时候,又该如何呢?
J. 如何利用GCC编译选项检测栈溢出
gcc的一个编译选项:-fstack-protector,以下是关于这个选项的描述:
-fstack-protector
启用该选项后编译器会产生额外的代码来检测缓冲区溢出,例如栈溢出攻击。这是通过在有缺陷的函数中添加一个保护变量来实现的。这包括会调用到alloca的函数,以及具有超过8个字节缓冲区的函数。当执行到这样的函数时,保护变量会得到初始化,而函数退出时会检测保护变量。如果检测失败,会输出一个错误信息并退出程序。
!注意:在Ubuntu 6.10以及之后的版本中,如果编译时没有指定-fno-fstack-protector, -nostdlib或者-ffreestanding选项的话,那么这个选项对于C,C++,ObjC,ObjC++语言默认是启用的。