In gdb, how do I set a breakpoint on any line that has a specific word?

I want that when my program runs under gdb,
Before executing any line of code, check whether the line has a specific word, example macro name or variable name or any specific word, and if it does, stop execution.
It is better if this can be done using regular expression.

I know that there is a command rbreakor rb, but it sets a breakpoint when the function name matches, this is not what I want.

So maybe?

+4
source share
3 answers

gdb , gdb -x, .

  • grep (, , )
  • break, ,
  • gdb -x ​​ break

man 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 <stdio.h> 

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 <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 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

+5

?

.

GDB . , , .

, , - GDB next , , , , . , script, , .

0

@amdixon, , git ( svn, merc ..). .

git ls-files '*.cpp' -z | xargs -n1 -P4 -0 awk -v wd=$(pwd) '/[REGEX]/ {print "break \"" wd "/" FILENAME ":" NR "\""}' >outbreak

[REGEX] - . , :

mv outbreak /pathToExecutable
cd !$
cgdb -x outbreak executable

Explanation

-z, -0and \"- they all deal with spaces / eols along the way. -n1causes awk to parse one file at a time (otherwise the line numbers will be wrong because it will treat it like a giant globe). P4allows awk to start 4 processes at once (optionally for speed). -v wd=$(pwd)It transmits a working directory in awk, as wdand FILENAMEand NRare awk builtins.

0
source

Source: https://habr.com/ru/post/1622168/


All Articles