在 Linux 下,strings 是一个非常实用的小工具,常用于从二进制文件、core dump、日志、内存镜像中提取可读字符串,辅助调试和分析程序行为。下面从「基本用法 → 调试场景 → 实战技巧」系统讲一下。
strings 的作用是:
从文件中提取可打印的字符序列(默认长度 ≥ 4)
strings file
常用于:
strings a.out
你可以看到:
示例输出:
usage: ./demo [-h] [-v]
config path: /etc/demo.conf
failed to open file
✅ 用来快速判断“程序在做什么”
strings a.out | grep -i error
strings a.out | grep -i config
调试时非常有用:
strings core
或:
strings core | grep -i segfault
适合:
strings /lib/x86_64-linux-gnu/libc.so.6 | grep -i version
或:
ldd a.out
strings a.out | grep -i glibc
strings a.out | grep -i main
strings a.out | grep -i assert
如果程序是 -g 编译且未 strip,会看到很多有用信息。
-n 指定最小字符串长度strings -n 6 a.out
默认是 4,调大可以减少噪声。
-a 扫描整个文件(不只数据段)strings -a a.out
✅ 调试时强烈推荐加 -a,否则可能漏掉信息。
-e 指定字符编码strings -e l a.out # 16-bit little endian
strings -e B a.out # 32-bit
用于:
-t x 显示偏移(十六进制)strings -t x a.out
输出示例:
1a3f error: null pointer
✅ 可以配合 objdump / gdb 定位代码位置
strings -t x a.out
objdump -d a.out | less
gdb ./a.out core
(gdb) info proc mappings
(gdb) shell strings -a core | grep panic
nm a.out
readelf -p .rodata a.out
readelf -p 有时比 strings 更精确。
strings core | grep -i "null"
strings core | grep -i "assert"
strings a.out | grep -i ".conf"
strings a.out | grep "/etc"
strings a.out | grep -i version
❌ strings 不能:
✅ 它只是辅助线索工具
strings 是 Linux 调试的“放大镜”,不是“显微镜”。
如果你愿意,可以告诉我:
我可以给你针对你场景的 strings 调试模板。