是否有更清洁(最好只有gdb)的方法来执行以下操作(在代码中出现特定模式的每一行设置断点),
grep ~/work/proj some_var_or_pattern -Irni| cut –f1,2 –d":" | sed –e 's/^/b /' > ~/moo.txt
并使用
加载结果(gdb) source ~/moo.txt
答案 0 :(得分:2)
不幸的是,没有用于在匹配特定模式的所有行上设置断点的命令。但是有一些内置命令用于在与正则表达式匹配的所有函数上设置断点:
rbreak regex
和rbreak file:regex
(short tutorial)。
答案 1 :(得分:1)
gdb本身并不这样做,但您可以将grep和break命令生成代码放入shell脚本中,并使用单个命令从gdb调用它。
将其放在$ PATH中的文件中,例如greptobp
:
#!/bin/sh
# usage: greptobp pattern dir outputfile
pattern="$1"
dir="$2"
outputfile="$3"
if [ "${dir#/}" = "$dir" ]
then
# if dir isn't absolute, make it absolute, as requested by OP
dir="$PWD/$dir"
fi
grep -HIirn "$pattern" "$dir" |
awk -F: '{ print "break \"" $1 "\":" $2; }' > "$outputfile"
并将其添加到gdb:
(gdb) define patbreak
Type commands for definition of "patbreak".
End with a line saying just "end".
>shell greptobp $arg0 $arg1 /tmp/gdbtmp
>source /tmp/gdbtmp
>shell rm /tmp/gdbtmp
>end
(gdb) document patbreak
Type documentation for "patbreak".
End with a line saying just "end".
>Set breakpoints at lines matching specified pattern in a specified directory.
>Usage: patbreak pattern dir
>end