当自己开发了一个so文件,如何调试呢?
对于如何编写一个so,请参考文章:http://blog.csdn.net/zz7zz7zz/article/details/41448987
对于如何进行gdb调试,请参考文章:http://blog.csdn.net/zz7zz7zz/article/details/41654457
我们的源文件分别如下:
filea.c
#include <stdio.h> void fun1() { printf("i am from filea fun1 \n"); printf("i am from filea fun11 \n"); printf("i am from filea fun12 \n"); }
fileb.c
#include <stdio.h> void fun2() { printf("i am from fileb fun2 \n"); printf("i am from fileb fun21 \n"); printf("i am from fileb fun22 \n"); }
CLoadSo.h
#ifndef _CLOADSO_H #define _CLOADSO_H #ifdef _cplusplus extern "C" { #endif void fun1(); void fun2(); #ifdef _cplusplus } #endif #endif
CLoadSo.c
#include <stdio.h> #include <dlfcn.h> #include "CLoadSo.h" int main(int argc,char **argv) { void *soHandle; int (*fun)(); char *errorMsg; soHandle=dlopen("first.so",RTLD_LAZY); errorMsg=dlerror(); printf("A1---------loadSo is %s \n",soHandle ? "success" : "failed"); if(errorMsg) { printf("A2--------loadSo error , Msg is: %s \n",errorMsg); return -1; } fun=dlsym(soHandle,"fun1"); errorMsg=dlerror(); printf("B1---------fun1 , fun1 is %s \n",fun ? "success" : "Null"); if(fun) { fun(); } if(errorMsg) { printf("B2---------fun1 error , Msg is: %s \n",errorMsg); } fun=dlsym(soHandle,"fun2"); errorMsg=dlerror(); printf("B3---------fun2 , fun2 is %s \n",fun ? "success" : "Null"); if(fun) { fun(); } if(errorMsg) { printf("B4---------fun2 error , Msg is: %s \n",errorMsg); } fun=dlsym(soHandle,"fun3"); errorMsg=dlerror(); printf("B5---------fun3 , fun3 is %s \n",fun ? "success": "Null"); if(fun) { fun(); } if(!errorMsg) { printf("B6---------fun3 error , Msg is: %s \n",errorMsg); } dlclose(soHandle); printf("C---------close LoadSo \n"); return 0; }
用命令 gcc -Wall -fpic -g -shared filea.c fileb.c -o first.so 生成first.so文件
用命令 gcc -g -Wall CLoadSo.c -o CLoadSo -ldl生成可执行文件CLoadSo
gdb步骤如下(请注意观察注释语句):
结果:成功进入到了so的fun1中
大概步骤:
1.启动gdb调试:gdb CLoadSo
2.打断点 gdb) : break 28
3.执行 gdb) : run
4.将动态库的符号读入gdb,为了你能找到变量和函数名 gdb)sharedlibrary first.so
5.s进入函数,l列出源代码,n单步执行,直到结束.
代码地址:http://download.csdn.net/detail/zz7zz7zz/8311533