gdb附加上去
[root@localhost ~]# gdb -p 5673
GNU gdb (GDB) Red Hat Enterprise Linux (7.0.1-23.el5)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Attaching to process 5673
查看进程信息
(gdb) info proc
process 5673
cmdline = '/XXX/XXXYYY'
cwd = '/XXX'
exe = '/XXX/XXXYYY'
第一种添加符号表文件方法
以模块libXXX.so为例
计算libXXX.so 代码段地址
查看libXXX.so在进程5673中的首地址
有r-xp标志行的第一个地址,即2aaaab5fa000
[root@localhost YYY]# cat /proc/5673/maps | grep libXXX.so
2aaaab5fa000-2aaaab768000 r-xp 00000000 fd:00 23134337 /XXX/libXXX.so
2aaaab768000-2aaaab967000 ---p 0016e000 fd:00 23134337 /XXX/libXXX.so
2aaaab967000-2aaaab96d000 rw-p 0016d000 fd:00 23134337 /XXX/libXXX.so
查看libXXX.so中代码段偏移地址
即00000000000440c0
[root@localhost ]# readelf -S libXXX.so | grep .text
[10] .text PROGBITS 00000000000440c0 000440c0
计算实际代码段地址,1的结果+2的结果
即hex(0x2aaaab5fa000 + 0x00000000000440c0) = 0x2aaaab63e0c0
[root@localhost ]# python
Python 2.7.15 (default, Dec 26 2018, 00:31:25)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> hex(0x2aaaab5fa000 + 0x00000000000440c0)
'0x2aaaab63e0c0'
还可以在gdb里边输入info shared,可以直接看到so模块的加载地址
添加符号
(gdb) add-symbol-file /XXX/libXXX.so.symbol 0x2aaaab63e0c0
add symbol table from file "/XXX/libXXX.so.symbol" at
.text_addr = 0x2aaaab63e0c0
(y or n) y
Reading symbols from /XXX/libXXX.so.symbol...done.
下断点
(gdb) b XXX::YYY
Breakpoint 1 at 0x2aaaab65a90e: file XXX.cpp, line 777.
(gdb) c
Continuing.
[Switching to Thread 0x523f3940 (LWP 5903)]
Breakpoint 1,
查看并修改源文件路径
(gdb) info source
Current source file is XXX.cpp
Compilation directory is /var/lib/YYY/libXXX
Source language is c++.
Compiled with DWARF 2 debugging format.
Does not include preprocessor macro info.
(gdb) set substitute-path /var/lib/ /home/work/
查看代码
(gdb) l
772 return response;
773 }
774
(gdb) bt