简体   繁体   English

在C文件中设置GDB断点

[英]Set GDB breakpoint in C file

Is it possible to add GDB breakpoints in a C files, before compilation? 在编译之前,是否可以在C文件中添加GDB断点? Currently, I have to specify breakpoints after compilation through GDB. 目前,我必须在通过GDB编译后指定断点。

I'm looking for something similar to JavaScript's debugger; 我正在寻找类似JavaScript的debugger; statement. 声明。

Not as such, but you can insert some helpful function calls: 不是这样,但你可以插入一些有用的函数调用:

One option is to create a function: 一种选择是创建一个函数:

void break_here ()
{
  /* do nothing */
}

then call it wherever you want, but be careful it doesn't get inlined (put it in a different file, or add a "noinline" attribute). 然后在任何你想要的地方调用它,但要小心它不会被内联(将它放在不同的文件中,或添加“noinline”属性)。

Then, in GDB, just set a breakpoint on break_here , and you're done. 然后,在GDB中,只需在break_here上设置一个断点,就完成了。 If you find it tedious to set that breakpoint every time, you can create a file named .gdbinit in your home directory or in the current working directory that contains the breakpoint command. 如果您发现每次设置该断点都很繁琐,可以在主目录或包含断点命令的当前工作目录中创建名为.gdbinit的文件。

Another option that works on Linux is to use a signal: 在Linux上运行的另一个选项是使用信号:

raise (SIGUSR1);

You could use any signal you like, but it's better to use one that doesn't kill your program (although you can configure GDB to not pass them onto your program, if you choose). 你可以使用你喜欢的任何信号,但最好使用一个不会杀死你的程序的信号(尽管你可以配置GDB不将它们传递给你的程序,如果你选择的话)。

I'm not aware of a way to directly set a breakpoint, but GDB will catch interrupts, so that is one possible solution. 我不知道直接设置断点的方法,但是GDB会捕获中断,所以这是一种可能的解决方案。

Something like this should work. 这样的事情应该有效。 You'll need to include signal.h: 你需要包含signal.h:

raise(SIGABRT);

on linux, which I'm assuming you're on since you're using gdb you could make a script to do it with comments in the code 在Linux上,我假设你正在使用gdb,你可以创建一个脚本来完成代码中的注释

#!/bin/bash
#debug: Script to run programs in gdb with comments to specify break 
points
echo "file ./a.out" > run
grep -nrIH "/\*GDB\*/" |
    sed "s/\(^[^:]\+:[^:]\+\):.*$/\1/g" |
    awk '{print "b" " " $1 }'|
    grep -v $(echo $0| sed "s/.*\///g") >> run
gdb --init-command ./run -ex=r
exit 0

then wherever you need a breakpoint, just add /*GDB*/ , the script will use grep to find the line numbers and files with those comments, and set a break point on each line of each file it finds them on, then start gdb 然后只要你需要一个断点,只需添加/*GDB*/ ,脚本将使用grep查找带有这些注释的行号和文件,并在它找到的每个文件的每一行上设置一个断点,然后启动gdb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM