C++(Qt)软件调试---binutils工具集详解

1. 概述

2. binutils包含的工具

3. 主要工具详细说明

4. 其它工具详细说明

将地址转换为文件名和行号

addr2line -e myprogram 0x400526

显示地址、函数名和源码位置

addr2line -a -f -e myprogram 0x400526

只显示文件名(不包含路径)和行号

addr2line -s -e myprogram 0x400526

ar [选项] 归档文件名 [成员文件...]

创建一个包含多个目标文件的静态库

ar rcs libmylib.a file1.o file2.o file3.o

列出库中包含的所有文件

ar t libmylib.a

从库中提取特定文件

ar x libmylib.a file1.o

向已存在的库中添加新文件

ar r libmylib.a newfile.o

c++filt [options] [mangled_names...]

echo "_ZNSsD1Ev" | c++filt # 通过管道解码

nm executable | c++filt # 解码nm输出中的符号

dlltool [options] [--input-def def-file] [--output-lib lib-file] [--dllname dll-name]

dlltool --def mydll.def --dllname mydll.dll --output-lib libmydll.a

elfedit [options] elf-file

修改 ELF 文件的机器类型

elfedit --input-mach i386 --output-mach x86_64 program.elf

修改 ELF 文件的 OSABI

elfedit --output-osabi linux program.elf

启用 x86 特性

elfedit --enable-x86-feature feature_name program.elf

4.6 gprof

1. 编译程序时添加 -pg 选项

gcc -pg -o myprogram myprogram.c

2. 运行程序生成 gmon.out

./myprogram

3. 使用 gprof 分析性能

gprof myprogram gmon.out > analysis.txt

只显示扁平性能分析,不显示调用图

gprof -p myprogram gmon.out

设置最小调用次数阈值

gprof -m 10 myprogram gmon.out

排除特定函数

gprof -e init_function myprogram gmon.out

显示所有函数包括未调用的

gprof -z myprogram gmon.out

生成带注释的源代码

gprof -S myprogram gmon.out

4.7 gprofng

收集指定程序的性能数据

gprofng collect your_program

收集正在运行的进程数据

gprofng collect -p PID

收集系统范围的性能数据

gprofng collect -s

分析收集到的数据

gprofng display experiment_name

生成火焰图

gprofng display -flamegraph experiment_name

生成调用树

gprofng display -calltree experiment_name

4.8 nlmconv

nlmconv [options] input-file

nlmconv -T nlm32-i386 input.o output.nlm

4.9 nm

nm [options] object-file...

nm executable # 显示所有符号

nm -D shared_library.so # 显示动态符号表

nm -g executable # 只显示全局符号

4.10 objcopy

objcopy [options] infile outfile

objcopy -O binary executable bootsect.bin # 转换为原始二进制格式

objcopy --strip-debug executable stripped # 去除调试信息

objcopy --only-section=.text input output # 只保留.text段

4.11 objdump

objdump [options] object-file...

反汇编可执行文件

objdump -d program

显示文件头信息

objdump -f program

显示所有节头信息

objdump -h program

混合显示源码和反汇编

objdump -S program

只反汇编特定函数

objdump --disassemble=main program

4.12 ranlib

ranlib archive-file

ar rcs libname.a *.o # ar的s选项会自动调用ranlib

4.13 readelf

readelf [options] elf-file

显示文件头信息

readelf -h a.out

显示所有节区信息

readelf -S a.out

显示符号表

readelf -s a.out

显示动态段信息

readelf -d a.out

以十六进制显示 .text 节内容

readelf -x .text a.out

显示所有调试信息

readelf -w a.out

4.14 size

size [options] object-file...

基本用法,显示默认格式

size a.out

使用 System V 格式显示详细节区信息

size -A a.out

以十六进制显示数值

size -x a.out

显示多个文件并计算总计(Berkeley 格式)

size -t *.o

显示 COMMON 符号大小

size --common test.o

指定特定目标格式

size --target=elf32-i386 program.bin

4.15 strings

strings [options] file...

提取文件中的所有字符串(至少4个字符)

strings myfile

提取至少8个字符长度的字符串

strings -n 8 myfile

显示字符串在文件中的位置(十六进制)

strings -t x myfile

只扫描数据段并显示文件名

strings -d -f myfile

指定字符编码(16位小端)

strings -e l myfile

4.16 strip

strip [options] file...

基本使用:移除所有符号信息

strip program

保留调试信息

strip --only-keep-debug program.debug

移除调试信息但保留符号表

strip --strip-debug program

保留特定符号

strip -K main -K important_func program

移除指定节区

strip -R .comment -R .note program

4.17 windmc

windmc [options] msgfile.mc

基本编译

windmc messages.mc

指定头文件输出目录

windmc -h include messages.mc

指定资源文件输出目录

windmc -r resources messages.mc

使用UTF16输入和详细模式

windmc -u -v messages.mc

4.18 windres

windres [options] input-file output-file

windres resource.rc resource.o # 编译资源文件

windres -i input.rc -o output.o # 指定输入输出文件

https://sourceware.org/binutils/

https://www.gnu.org/software/binutils/

https://sourceware.org/pub/binutils/releases/

相关推荐
yuyanjingtao9 小时前
CCF-GESP 等级考试 2025年9月认证C++一级真题解析
c++·青少年编程·gesp·csp-j/s
xzk201210029 小时前
洛谷 P1438 无聊的数列 题解
c++·算法·树状数组
赶飞机偏偏下雨9 小时前
【Java笔记】消息队列
java·开发语言·笔记
00后程序员张9 小时前
C++ string 类使用攻略
开发语言·c++
OKkankan9 小时前
list的使用和模拟实现
数据结构·c++·算法·list
融化的雪9 小时前
reactflow整理节点,尾节点位置的大坑
开发语言·前端·javascript
豐儀麟阁贵9 小时前
2.6 代码注释与编码规
java·开发语言
egoist202310 小时前
[linux仓库]信号保存[进程信号·肆]
linux·开发语言·信号集·信号保存·sigpending
你不是我我10 小时前
【Java 开发日记】什么是线程池?它的工作原理?
java·开发语言