Linux Debugging 3 - Compile and Debug
来源:互联网 发布:宁芙 淘宝 编辑:程序博客网 时间:2024/05/21 14:58
1. Build的四个阶段
1) 预处理:gcc -E (cpp)
选项:-D -U -I -M -g3 (output macro info for debugger)
2) 编译: gcc -S (cc1)
优化选项:-O0, -O1, -O2, -O3, -Os
一个关于优化级别选项的对比例子,
不优化编译
>gcc -g2 memset.c
查看产生的汇编代码,
>objdump -dS a.out
优化编译
>gcc -g2 -O3 memset.c
建议不要在测试阶段打开优化。
gdb信息选项:
-g == -g2
-g3 include symbols and extra.
下面显示了不同的级别产生的代码的大小。
在产生的ELF(Executable & Library Format, magic is /x7FELF, man 5 elf)文件中,debug信息占到若干个section,
使用objdump -W可以查看具体的debug段信息。
3) 汇编: gcc -c (as)
可以使用-m(achine)选项指定产生目标平台的代码。
4) 链接: ld
insertion of libraries(static) or reference(dynamic).
选项:
-l -L -shared -static
在release阶段,可以strip掉debug信息, “-d”选项只删除掉调试符号信息。
2. Parse ELF tools
1) objdump
常用选项: -d(反汇编代码段),-g -W (显示调试信息), -S(同-d,显示源文件),-s(以十六进制格式显示所有段) ,-t(显示符号表),-x(显示所有头部信息)
2) readelf
3) nm: 显示符号表,要求不能被strip过
4) ldd: resolve shared library dependencies
举例说明,
动态库的load可以分为装载时和运行时(dl_open),下面是运行时装载动态库的例子。
3. gdb
3种使用模式:
1) start a binary in the debugger, gdb a.out
2) running process attachment, gdb a.out PID
3) post-mortem, core file analysis, gdb a.out core
常用命令总结:
break, bt(backtrack of stack call of current thread), disass, i r, stepi, info threads, r, c, p, x(dump memory from address)
关于core文件
1) ulimit -c -s -t
注意编译器在编译程序时,本身也是需要栈空间的。可以使用函数getrlimit, setrlimit来get/set resource limits。
2) core文件的格式
man 5 core to reference the naming rule of core dump files.
注意:如果发生了core,但是gdb backtrace找不到在哪个函数core了,可以先ldd这个程序,看依赖于哪些动态链接库,然后nm动态链接库并且搜索core的地址的前几位,基本就可确定在哪个函数core了。一般的text段以0x8***开始。
对于正在运行的程序,可以通过检查maps文件来确定so的地址范围, 例如
- Linux Debugging 3 - Compile and Debug
- build, debug, compile and run
- GCC compile debug: print include files and compile stage info.
- Linux Kernel Testing and Debugging
- strip and linux lib compile
- Debug Tutorial Part 1: Beginning Debugging Using CDB and NTSD
- Linux Debugging 7 - Stack and Heap
- Debugging with Xdebug and Sublime Text 3
- 翻译 Secrets of the JavaScript Ninja - 2.测试和debug(2.Testing and debugging)
- Debug Android and Linux suspend and resume
- Debug Android and Linux suspend and resume
- Debug Android and Linux suspend and resume
- Android and Linux suspend and resume debug
- Top 25 Best Linux Performance Monitoring and Debugging Tools
- Installing VMware Tools on Kali Linux and Some Debugging Basics
- Debugging the linux kernel using kgdb and VirtualBox
- How to compile and debug VLC player source code in eclipse IDE on Ubuntu 10.04 (lucid)?
- How to compile and debug VLC player source code in eclipse IDE on Ubuntu 10.04 (lucid)
- Java刷新局部组件
- VS2005 用 ADO 连接ACCESS数据库
- 基于Android源码开发的来电接通振动功能实现
- HDU 1159 最长公共子序列问题 动态规划
- 把 VC 工程的编译配置写在一个文件中,并影响 VC 的编译
- Linux Debugging 3 - Compile and Debug
- 开了!
- sed 学习
- 为迅雷7添加流媒体嗅觉器的方法。
- iocomp控件的应用
- 文件或文件夹无法显示或隐藏的解决办法
- 在服务器有IDE光驱的情况下使用光驱安装RAID驱动
- 网络共享打印机的方法。
- 正则表达式里那该死的点号