python - 广发银行。具有可变数量参数的用户定义命令
问题描述
GDB 版本:GNU gdb (Ubuntu 8.1.1-0ubuntu1) 8.1.1
我无法在gdb中定义具有任意数量参数的命令。
我找到了一些解决方法,例如一个最多支持三个参数的命令:
define test_1
if $argc == 1
python args = "$arg0"
end
if $argc == 2
python args = "$arg0 $arg1"
end
if $argc == 3
python args = "$arg0 $arg1 $arg2"
end
python print(gdb.execute(args, to_string=True))
end
用法
test_1 info breakpoints
我尝试了另一种方法,但它不起作用,因为gdb$arg<n>
在执行 python 之前解析参数,所以"$arg0 $arg1 $arg2"
在args
变量中不会被它们的值替换:
define test_1
python args = ' '.join(f"$arg{i}" for i in range($argc))
python print(gdb.execute(args, to_string=True))
end
问题:如何正确处理?Python不是强制性的,其他解决方案(如纯 gdb 脚本)是允许的。
解决方案
最简单、最可靠的方法是使用 GDB 的 Python 扩展。创建一个子类gdb.Command
将使您可以访问未拆分的参数字符串,作为invoke
. 在您的示例用例中,此字符串可以按原样传递给gdb.execute
.
您也可以使用 将其拆分为参数gdb.string_to_argv
。如果您只想将参数字符串的一部分传递给gdb.execute
,则可以使用string.split
,如下例所示:
class Repeat(gdb.Command):
"""repeat count command - run the given command count times"""
def __init__(self):
super (Repeat, self).__init__ ("repeat", gdb.COMMAND_USER, gdb.COMPLETE_COMMAND)
def invoke(self, argstr, from_tty):
try:
(count, command) = argstr.split(maxsplit = 1)
except ValueError:
raise Exception("Usage: repeat count command")
if not count.isdigit():
raise Exception("missing or garbled repeat count: " + count)
for _ in range(0, int(count)):
gdb.execute(command, from_tty = False)
Repeat()
例子:
(gdb) repeat 3 run $(expr $RANDOM % 20)
16! = 20922789888000
[Inferior 1 (process 259) exited normally]
9! = 362880
[Inferior 1 (process 262) exited normally]
13! = 6227020800
[Inferior 1 (process 265) exited normally]
如果你不能使用 Python,用户定义的命令仍然可以连接它的参数,使用eval
,但它几乎没有那么健壮(见最后的注释。)
(这需要 GDB 9.1 或更高版本;它的类 C 表达式求值器将连接相邻的字符串文字。)
define repeat
if $argc < 2
printf "Usage: repeat count command\n"
else
# first arg goes in $count, rest are concatenated and put in $command
set $count=$arg0
set $i=1
set $command=""
while $i < $argc
eval "set $command = \"%s\" \"$arg%d\"", $command, $i
# add one blank space after every arg except for the last
if $i < $argc - 1
eval "set $command = \"%s\" \" \"", $command
end
set $i++
end
printf "About to run `%s' %d times.\n", $command, $count
set $i=0
while $i < $count
eval "%s", $command
set $i++
end
end
end
eval
但是,当这些字符串包含双引号时,使用双引号将字符串括起来是有问题的。
(gdb) repeat 3 set $a="foo"
A syntax error in expression, near `foo""'.
推荐阅读
- ios - 在 iOS 应用中启动音频控制中心的 API 是什么?
- google-sheets - Google 表格:CountIf 和日期
- java - Java: How to create a public class that can be subclassed within package, while preventing outsiders from inheriting?
- apache-spark - Hive 数据库仅列出默认数据库
- pebble - 控制 Pebble 模板中的空白
- mysql - Command UPDATE using existing data
- django-models - 如何将 Django 类名引用到函数中?
- python - How to read file's content from GitHub file with a specific branch name?
- java - 关于在春季启动时引发异常时的 log4j2 内存泄漏
- java - 黑色在绘制的图像周围闪烁