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

时间:2013-05-26 01:16:43

标签: c gdb

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

所以:

档案A:

system("./fileB");

档案B:

void main() {
 /* etc */
}

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

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:3)

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

生成-program.c

#include <stdlib.h>

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

<强>程序-I-想做某些debug.c

#include <stdio.h>

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

生成-program.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

示例会话

$ 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 )编译程序。

答案 1 :(得分:0)

也许我没有理解你的观点。似乎在文件A上启动gdb调试并在“FileB:main of line”上设置断点可以解决您的问题。