当前位置:  开发笔记 > 编程语言 > 正文

在gdb中如何在任何具有特定单词的行上设置断点?

如何解决《在gdb中如何在任何具有特定单词的行上设置断点?》经验,为你挑选了1个好方法。

我想在我的程序在gdb下执行时,
在执行任何代码行之前检查行是否有特定的单词示例宏名称或变量名称或任何特定单词,如果有,则停止执行.
如果可以使用正则表达式完成更好.

我知道有一个命令rbreak或者rb在匹配函数名称上设置断点,这不是我想要的.

那有可能吗?



1> amdixon..:

在gdb之外,您可以先搜索源并列出断点,然后使用-x标志调用gdb来设置这些断点.

计划

grep在用户定义模式的源上(例如擦除,删除)

根据上面的行号生成break语句,将这些语句写入文件

使用-x标志调用gdb并使用break语句指向生成的文件

男人gdb

   ...
   -x file
       Execute GDB commands from file file.
   ...

write_breakpoints.sh

#!/bin/bash

sourcename="$1";
patterns="$2";
outbreaks="$3";

grep -En "$2" "$1" | \
  cut -d: -f1 | \
  sed "s/^\(.\+\)$/break $1:\1/g" 1>>"$3";

example.c

#include  

void delete()
{
  printf("!! data is being deleted..\n");
}

void erase()
{
  printf("!! data is being erased..\n");
}

int main(void)
{
  printf("this line is safe..\n");
  erase();
  delete();
  return 0;
}

用法

$ gcc -g example.c -o example
$ ./write_breakpoints.sh example.c "(delete|erase)" "breakpoints.txt"

$ gdb -x breakpoints.txt example
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
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:
.
Find the GDB manual and other documentation resources online at:
.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from example...done.
Breakpoint 1 at 0x400531: file example.c, line 3.
Breakpoint 2 at 0x400531: file example.c, line 5.
Breakpoint 3 at 0x400541: file example.c, line 8.
Breakpoint 4 at 0x400541: file example.c, line 10.
Breakpoint 5 at 0x40055b: file example.c, line 16.
Breakpoint 6 at 0x400565: file example.c, line 17.
(gdb) run
Starting program: /path/to/example 
this line is safe..

Breakpoint 5, main () at example.c:16
16    erase();
(gdb) c
Continuing.

Breakpoint 3, erase () at example.c:10
10    printf("!! data is being erased..\n");
(gdb) c
Continuing.
!! data is being erased..

Breakpoint 6, main () at example.c:17
17    delete();
(gdb) c
Continuing.

Breakpoint 1, delete () at example.c:5
5     printf("!! data is being deleted..\n");
(gdb) c
Continuing.
!! data is being deleted..
[Inferior 1 (process 26130) exited normally]
(gdb) quit

笔记

可以使用多个源文件调用write_breakpoints(因为它附加了break语句)

也可以使用forward-search和b 在gdb中执行此操作

推荐阅读
贴进你的心聆听你的世界
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有