设置gdb搜索目录



在用GDB进行 debug的时候,可以用  directory 设置搜索目录,用 show direcories 显示设置的搜索目录:

  1. (gdb) show directories   
  2. Source directories searched: $cdir:$cwd  
  3. (gdb) directory  /home  
  4. Source directories searched: /home:$cdir:$cwd  
  5. (gdb) show directories   
  6. Source directories searched: /home:$cdir:$cwd  
  7. (gdb)   
(gdb) show directories 
Source directories searched: $cdir:$cwd
(gdb) directory  /home
Source directories searched: /home:$cdir:$cwd
(gdb) show directories 
Source directories searched: /home:$cdir:$cwd
(gdb) 

但是,有个局限性,就是这个搜索目录不是递归的,它只对设置的目录下的文件起作用,对于这个目录下的其它目录里的文件不起作用。所以这个功能不是很好用。


一种比较好的方法是借助 gdb的-d选项:

  1. -directory=directory  
  2. -d directory  
  3.     Add directory to the path to search for source files.  
       -directory=directory
       -d directory
           Add directory to the path to search for source files.
先使用命令把需要搜索的目录设置好,传给gdb.

具体方法是:

  1. gdb `find srcdir -type d -printf '-d %p '` prog  
gdb `find srcdir -type d -printf '-d %p '` prog
有的版本的 find不支持  -printf选项,此时,可以用下面的方法:

  1. gdb `find . -type d | xargs printf " -d %s"`  prog  
gdb `find . -type d | xargs printf " -d %s"`  prog

<think>我们正在讨论GDB的符号搜索路径配置。用户之前的问题是关于调试信息为何放在/usr/lib/debug下就能被加载,现在转向如何配置GDB的符号搜索路径。根据引用内容,用户提供了几个关于GDB使用的引用,但我们需要聚焦于符号搜索路径的配置。在GDB中,调试符号的加载涉及多个路径配置,包括:1.调试信息目录(debug-file-directory):用于查找分离的调试符号文件(如/usr/lib/debug)2.源代码搜索路径(sourcepath):用于查找源代码文件3.其他可能影响符号加载的设置(如sysroot,solib-search-path等)重点:用户询问的是“symbol searchpath”,但符号搜索通常涉及两个方面:调试符号文件(如.debug文件)和源代码文件。根据上下文,用户更可能关心调试符号文件的搜索路径(因为上一个问题讨论的是调试信息在/usr/lib/debug下的加载)。因此,我们主要介绍GDB中调试符号文件的搜索路径配置,同时简要提及源代码搜索路径。步骤:1.介绍GDB如何查找调试符号文件(包括分离的调试符号)2.介绍相关命令:setdebug-file-directory, showdebug-file-directory3.介绍如何设置多个搜索路径(注意:debug-file-directory只能设置一个目录,但可以通过软链接等方式扩展)4.介绍其他可能影响符号加载的设置(如setsysroot,set solib-absolute-prefix, setsolib-search-path)5.介绍源代码搜索路径(set substitute-path,directory等)注意:引用[3]提到了源文件搜索目录(directory命令),所以我们也需要覆盖。根据引用[1],用户可能对GDB脚本感兴趣,但当前问题不直接相关,可以忽略。根据引用[2]和引用[4],用户提供了GDB语言环境和测试程序,但当前问题不直接相关,可以忽略。因此,回答结构如下:一、调试符号文件搜索路径配置1.默认路径:/usr/lib/debug2.设置全局调试符号目录:set debug-file-directory /path/to/debug3.构建ID(Build ID)机制下的路径查找规则二、共享库搜索路径配置(影响符号加载)1.set sysroot:设置目标库的前缀(用于远程调试或不同环境)2.set solib-search-path:设置共享库搜索路径(多个路径用冒号分隔)三、源代码搜索路径配置1.directory命令:添加源代码搜索路径(引用[3])2. setsubstitute-path:替换源代码路径(当编译路径与当前路径不同时)四、验证符号加载1. infosharedlibrary:查看加载的共享库及其符号是否加载2. infofunctions,info variables等检查符号五、示例配置步骤六、相关问题注意:使用中文,并按照要求处理数学公式(但本问题可能不涉及数学公式,所以可忽略),在适当位置添加引用标记(引用[1][2][3][4]中与当前问题相关的部分)。</think>### GDB 符号搜索路径配置详解 在调试过程中,GDB 通过多层搜索路径机制定位调试符号和源代码,确保调试信息能被正确加载。以下是完整的配置体系和实践方法: --- #### 一、**核心符号搜索路径配置** 1. **调试符号目录(Debug File Directory)** 控制分离调试符号(如 `.debug` 文件)的搜索路径: ```gdb (gdb) set debug-file-directory /custom/debug/path:/usr/lib/debug (gdb) show debug-file-directory ``` - 默认路径:`/usr/lib/debug`(遵循 FHS 标准[^2]) - 支持多路径(用 `:` 分隔),优先级从左到右 2. **共享库搜索路径(Solib Search Path)** 定位动态链接库的符号: ```gdb (gdb) set solib-search-path /lib:/usr/lib:/custom/libs (gdb) show solib-search-path ``` - 影响 `info sharedlibrary` 列出的库文件加载 3. **系统根目录(Sysroot)** 跨平台调试时重定向系统路径: ```gdb (gdb) set sysroot /remote/system/root/ ``` --- #### 二、**源代码搜索路径配置** 1. **源代码目录指令** 添加源代码搜索位置(引用[3]): ```gdb (gdb) directory /new/source/path # 添加路径到搜索列表头部 (gdb) directory # 重置为默认路径 ($cdir:$cwd) ``` - `$cdir`:编译时目录 - `$cwd`:当前工作目录 2. **路径替换规则** 解决编译路径与本地路径不一致问题: ```gdb (gdb) set substitute-path /original/build/path /local/source/path ``` 3. **查看源代码信息**(引用[2]): ```gdb (gdb) info source # 输出示例: # Current source file is test.c # Source language is c # Compilation directory = /build/path ``` --- #### 三、**验证符号加载状态** 1. **检查符号加载**: ```gdb (gdb) info functions foo # 验证函数符号 (gdb) p foo # 测试符号解析 ``` 2. **诊断符号搜索失败**: ```gdb (gdb) set debug symtab 1 # 开启符号加载调试 (gdb) file /path/to/program ``` 3. **查看模块信息**: ```gdb (gdb) info sharedlibrary # 列出所有加载库的符号状态 ``` --- #### 四、**配置持久化方法** 1. **全局配置**(`~/.gdbinit`): ```bash set debug-file-directory /usr/lib/debug:/custom/debug set solib-search-path /lib/x86_64-linux-gnu ``` 2. **脚本启动**(引用[1]): ```bash gdb -x /path/to/config.gdb ./program ``` `config.gdb` 内容: ``` set debug-file-directory /debug/path directory /source/path ``` --- #### 五、**典型调试场景示例** 以引用[4]的程序为例: ```bash # 编译添加调试信息 g++ -g command.cpp -o program # 自定义调试环境 echo "set debug-file-directory /custom/debug" > config.gdb echo "directory /home/user/sources" >> config.gdb # 启动调试 gdb -x config.gdb ./program # GDB内验证 (gdb) b foo # 应成功断点 (gdb) info source # 显示正确源路径[^2] ``` ```mermaid graph LR A[启动GDB] --> B[加载 -x 脚本] B --> C[设置 debug-file-directory] C --> D[加载程序符号] D --> E[设置目录 directory] E --> F[解析源代码路径] F --> G[断点设置/调试] ``` --- ### 关键故障排查点 | 问题现象 | 检查命令 | 解决方案 | |------------------------|------------------------------|---------------------------| | 缺失函数符号 | `info functions <name>` | 添加 `solib-search-path` | | 源代码路径不匹配 | `info source` | 使用 `set substitute-path`| | 调试符号未加载 | `set debug symtab 1` | 检查 `debug-file-directory`| | 共享库未加载 | `info sharedlibrary` | 设置 `sysroot` 路径 | --- ### 相关问题 1. 如何让 GDB 自动加载自定义目录下的调试符号? 2. 当程序使用静态链接库时,符号搜索路径配置有何不同? 3. 如何验证 GDB 实际加载的调试符号文件路径? 4. `set sysroot` 和 `set solib-search-path` 的区别是什么? 5. 调试核心转储(core dump)时符号路径如何配置? [^1]: GDB 脚本通过 `-x` 参数加载,实现自定义配置 [^2]: `info source` 命令显示源代码语言和编译路径信息 [^3]: `directory` 命令管理源代码搜索路径,支持 `$cdir` 和 `$cwd` [^4]: 测试程序用于验证符号加载功能
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值