Content
0. 引子
1. 第1个例子
(1) at提示前半部分代表什么?
(2) at提示后半部分代表什么?
(3) 如果要阅读gcc的源代码,那么(2)中的文件在哪里?
2. 第2个例子
(1) 在gcc源代码中该函数在哪里?
(2) 为什么没有单步进入(step in)_Rb_tree_insert_and_rebalance函数?
(3) 该函数的实现在什么地方?即被编译进了哪个库?能否看到其信息?
(4) 如何单步调试关于红黑树的操作,例如左旋、右旋、平衡等(tree.cc中的函数)?
(4.1) 利用disassemble命令找到_Rb_tree_insert_and_rebalance的符号。
(4.2) 利用disassemble命令查看该函数的起始地址
(4.3) 在该函数中设置断点
3. 小结
(1) 本文使用的命令
(2) STL源代码位置
0. 引子
在Linux平台上开发C/C++程序,就免不了要使用GDB,当然你也可以使用DDD(Data Display Debugger),关于DDD可以参考Appendix和官方网站,非本文重点,不做讨论。本文就使用GDB调试的过程中碰到的几个问题进行简单介绍。
1. 第1个例子
我们先看一个使用GDB调试的例子。
(gdb)
operator new (__p=0x8246018)
at /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/new:94
94 inline void* operator new(std::size_t, void* __p) throw() { return __p; }
(gdb)
0x08049059 in __gnu_cxx::new_allocator ::construct (this=0xbfca1b9f, __p=0x8246018,
__val=@0xbfca1cec)
at /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/ext/new_allocator.h:104
104 { ::new(__p) _Tp(__val); }
(gdb)
~allocator (this=0xbfca1b9f)
at /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/allocator.h:105
105 ~allocator() throw() { }
(gdb)
本文讨论的问题如下。
(1) at提示前半部分代表什么?
表示当前调用的代码所在的库的路径,即某个symbol存在于该路径下的某个库中。该例子指出当前调用的函数均存在于/usr/lib/gcc/i386-redhat-linux/4.1.2目录下的库文件中。例如,
# cd /usr/lib/gcc/i386-redhat-linux/4.1.2
# ls
crtbegin.o finclude libgcj.so libgfortran.so libstdc++.so
crtbeginS.o include libgcj.spec libgij.so libsupc++.a
crtbeginT.o libgcc.a libgcj-tools.so libgomp.a SYSCALLS.c.X
crtend.o libgcc_eh.a libgcov.a libgomp.so
crtendS.o libgcc_s.so libgfortran.a libgomp.spec
crtfastmath.o libgcj_bc.so libgfortranbegin.a libstdc++.a
# objdump -x libsupc++.a | grep new
new_handler.o: file format elf32-i386
rw-r--r-- 102/102 3564 Oct 21 23:42 2007 new_handler.o
8 .text._ZSt15set_new_handlerPFvvE 0000001f 00000000 00000000 00000060 2**4
13 .bss.__new_handler 00000004 00000000 00000000 00000120 2**2
00000000 l d .text._ZSt15set_new_handlerPFvvE 00000000 .text._ZSt15set_new_handlerPFvvE
00000000 l d .bss.__new_handler 00000000 .bss.__new_handler
00000000 g F .text._ZSt15set_new_handlerPFvvE 0000001f _ZSt15set_new_handlerPFvvE
00000000 g O .bss.__new_handler 00000004 __new_handler
RELOCATION RECORDS FOR [.text._ZSt15set_new_handlerPFvvE]:
00000014 R_386_GOT32 __new_handler
00000024 R_386_PC32 .text._ZSt15set_new_handlerPFvvE
new_op.o: file format elf32-i386
rw-r--r-- 102/102 2432 Oct 21 23:42 2007 new_op.o
00000000 *UND* 00000000 __new_handler
0000002d R_386_GOT32 __new_handler
new_opnt.o: file format elf32-i386
rw-r--r-- 102/102 2360 Oct 21 23:42 2007 new_opnt.o
00000000 *UND* 00000000 __new_handler
00000030 R_386_GOT32 __new_handler
new_opv.o: file format elf32-i386
rw-r--r-- 102/102 2100 Oct 21 23:42 2007 new_opv.o
new_opvnt.o: file format elf32-i386
rw-r--r-- 102/102 1608 Oct 21 23:42 2007 new_opvnt.o
14 .text.__cxa_vec_new2 000000c5 00000000 00000000 00000630 2**4
15 .text.__cxa_vec_new 00000053 00000000 00000000 00000700 2**4
16 .text.__cxa_vec_new3 000000cc 00000000 00000000 00000760 2**4
00000000 l d .text.__cxa_vec_new2 00000000 .text.__cxa_vec_new2
00000000 l d .text.__cxa_vec_new 00000000 .text.__cxa_vec_new
00000000 l d .text.__cxa_vec_new3 00000000 .text.__cxa_vec_new3
00000000 g F .text.__cxa_vec_new2 000000c5 __cxa_vec_new2
00000000 g F .text.__cxa_vec_new 00000053 __cxa_vec_new
00000000 g F .text.__cxa_vec_new3 000000cc __cxa_vec_new3
RELOCATION RECORDS FOR [.text.__cxa_vec_new2]:
RELOCATION RECORDS FOR [.text.__cxa_vec_new]:
00000049 R_386_PLT32 __cxa_vec_new2
RELOCATION RECORDS FOR [.text.__cxa_vec_new3]:
0000014c R_386_PC32 .text.__cxa_vec_new2
00000174 R_386_PC32 .text.__cxa_vec_new
00000194 R_386_PC32 .text.__cxa_vec_new3
# nm libsupc++.a | grep new
nm: eh_arm.o: no symbols
new_handler.o:
00000000 T _ZSt15set_new_handlerPFvvE
00000000 B __new_handler
new_op.o:
U __new_handler
new_opnt.o:
U __new_handler
new_opv.o:
new_opvnt.o: