alibi@alibi-virtual-machine:~/桌面$ ./shell 段错误 (核心已转储) alibi@alibi-virtual-machine:~/桌面$ gdb ./shell GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.2) 9.2 Copyright (C) 2020 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-linux-gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: <http://www.gnu.org/software/gdb/documentation/>. For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from ./shell... (No debugging symbols found in ./shell) gdb-peda$ run Starting program: /home/alibi/桌面/shell Program received signal SIGSEGV, Segmentation fault. [----------------------------------registers-----------------------------------] EAX: 0xfffffffe EBX: 0xffffd23c --> 0x0 ECX: 0xffffd234 ("/bin//sh") EDX: 0xb ('\x0b') ESI: 0x0 EDI: 0x0 EBP: 0x0 ESP: 0xffffd234 ("/bin//sh") EIP: 0x8049017 --> 0x0 EFLAGS: 0x10246 (carry PARITY adjust ZERO sign trap INTERRUPT direction overflow) [-------------------------------------code-------------------------------------] 0x8049011 <_start+17>: mov al,0xb 0x8049013 <_start+19>: mov edx,eax 0x8049015 <_start+21>: int 0x80 => 0x8049017: add BYTE PTR [eax],al 0x8049019: add BYTE PTR [eax],al 0x804901b: add BYTE PTR [eax],al 0x804901d: add BYTE PTR [eax],al 0x804901f: add BYTE PTR [eax],al [------------------------------------stack-------------------------------------] 0000| 0xffffd234 ("/bin//sh") 0004| 0xffffd238 ("//sh") 0008| 0xffffd23c --> 0x0 0012| 0xffffd240 --> 0x1 0016| 0xffffd244 --> 0xffffd3fe ("/home/alibi/桌面/shell") 0020| 0xf
时间: 2025-05-10 11:35:06 浏览: 21
### 调试段错误(SIGSEGV)并分析崩溃原因
当 Linux 程序运行时出现段错误(Segmentation Fault,简称 SIGSEGV),通常表示程序尝试访问未分配给它的内存区域。为了有效定位和解决问题,可以按照以下方式操作:
#### 1. 启用 Core Dump 文件生成
如果程序崩溃后没有生成 core dump 文件,则无法通过 GDB 加载核心转储来进一步分析问题。可以通过设置 `ulimit` 参数启用 core dump 的生成。
```bash
ulimit -c unlimited
```
即使启用了此参数,某些情况下仍可能失败[^3]。此时需确认 `/etc/security/limits.conf` 和系统级配置文件中的相关设置是否允许生成 core 文件。
#### 2. 编译带调试符号的目标程序
目标程序应编译时加入 `-g` 或者更高版本的调试选项以便于 GDB 使用完整的符号表信息。例如:
```bash
gcc -o my_program my_program.c -g
qmake CONFIG+=debug QMAKE_CXXFLAGS+=-g && make
```
对于 Qt 应用程序,确保在项目构建过程中加入了调试标志[^1]。
#### 3. 利用 GDB 定位崩溃位置
加载可执行文件到 GDB 中,并附加断点或者直接运行直到发生异常为止:
```bash
gdb ./my_program
run
bt full
frame <number>
list
print variable_name
```
其中:
- `bt full`: 显示调用栈帧及其局部变量值。
- `frame <number>`: 查看特定堆栈层次的信息。
- `list`: 展示当前源码上下文。
- `print variable_name`: 打印指定变量的内容用于验证逻辑错误或非法指针解引用等问题。
如果没有可用的符号表数据,GDB 将仅提供汇编级别的跟踪记录而非高级别的 C/C++ 函数名及行号等细节。
#### 4. 配合其他工具辅助排查
除了传统的 GDB 方法外,还可以借助 Valgrind 工具检测潜在的内存管理漏洞如越界读写、野指针释放后再使用等情况:
```bash
valgrind --leak-check=full ./my_program
```
另外也可以考虑采用 AddressSanitizer (ASan),它是一种快速的内存错误探测器,能够实时报告各种类型的 bug 并给出更清晰的位置指示:
```cpp
// 修改 Makefile 添加如下链接器标记
LDFLAGS += -fsanitize=address -fno-omit-frame-pointer
CFLAGS += -fsanitize=address -fno-omit-frame-pointer
```
重新编译应用程序再重复之前的测试流程即可获得增强版诊断消息。
---
### 总结
针对 x86_64 架构下的 Ubuntu 20.04 系统上发生的段错误现象,推荐采取上述措施逐步缩小范围直至找到根本诱因。务必保证开发环境支持详尽的日志输出以及精确至每一指令周期的行为重现能力。
阅读全文
相关推荐














