c - 通过system()执行外部程序时的GDB断点

标签 c gdb

我让函数 system() 调用一个已编译的单独脚本。但我希望能够在该特定文件中的函数中设置断点。

所以:

文件A:

system("./fileB");

文件 B:

void main() {
 /* etc */
}

我希望能够在调用系统命令后在 main 处设置断点。

如有任何帮助,我们将不胜感激!

最佳答案

较新版本的 GDB ( 7.1+ ) 可以 debug multiple programs at once并且确实可以支持这一点:

运行程序.c

#include <stdlib.h>

int main()
{
    system("./program-i-want-to-debug");
    return 0;
}

program-i-want-to-debug.c

#include <stdio.h>

int main()
{
    printf("Hello, World\n");
    return 0;
}

运行程序.gdb

set detach-on-fork off
set target-async on
set pagination off
set non-stop on

add-inferior -exec program-i-want-to-debug
break program-i-want-to-debug.c:5
file run-program
run

inferior 3
backtrace

示例 session

$ gdb -q -x run-program.gdb
Added inferior 2
Breakpoint 1 at 0x400441: file program-i-want-to-debug.c, line 5.
[New process 20297]
process 20297 is executing new program: /usr/bin/bash
process 20297 is executing new program: /home/scottt/Dropbox/stackoverflow/program-i-want-to-debug
Reading symbols from /home/scottt/Dropbox/stackoverflow/program-i-want-to-debug...done.

Breakpoint 1, main () at program-i-want-to-debug.c:5
5       printf("Hello, World\n");
[Switching to inferior 3 [process 20297] (/home/scottt/Dropbox/stackoverflow/program-i-want-to-debug)]
[Switching to thread 2 (process 20297)] 
#0  main () at program-i-want-to-debug.c:5
5       printf("Hello, World\n");
#0  main () at program-i-want-to-debug.c:5

显然,您希望使用调试信息 (gcc -g) 编译程序。

关于c - 通过system()执行外部程序时的GDB断点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16755363/

相关文章:

c - 为 pthread_cond_broadcast 调用同步线程

c++ - 能不能用g++编译代码用Solaris Studio的Performance Analyser做性能分析?

c - 解密 x86 汇编函数

c - 我不明白为什么“在 c 中发生段错误”

c - 在运行时检测堆栈区域不与 RAM 区域重叠的方法

gdb - 连接 GDB 便利变量

c - 如何使用gdb调试越界?

无法访问 .bss 部分中的内存,但 gdb 'info files' 显示地址在范围内

c - 为什么 gcc 反汇编程序为局部变量分配额外的空间?

linux - GDB 没有在 SIGSEGV 上中断