运行时添加符号表文件方法

本文介绍了如何在GDB中附加到进程并动态添加符号表文件,以跟踪和调试动态链接库libXXX.so。通过查看进程映射、计算代码段地址,并使用`add-symbol-file`命令添加符号表,实现对特定函数的断点设置和源代码查看。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值