gdb:使用 if...else 定义具有多个参数的函数

gdb: Defining a function with multiple arguments using if...else

如何定义带有可选参数的 gdb 便利函数?我在下面尝试过,但不起作用:

(gdb) define v
Type commands for definition of "v".
End with a line saying just "end".
>if $argc == 1
 >echo 1
 >else if $argc == 2
 >echo 2
 >else
 >echo neither
 >end
>end
(gdb) v
neither(gdb) v 1
1Undefined command: "else".  Try "help".
(gdb) v 2
1Undefined command: "else".  Try "help".

I've tried below, which doesn't work:

它不起作用,因为 GDB 脚本语言中没有else

您可以通过以下方式解决此问题:

define v
  if $argc == 1
    echo 1
  end
  if $argc == 2
    echo 2
  end
  if $argc < 1 || 2 < $argc 
    echo neither
  end
end

但您可能会在 deferring to the embedded Python 之前获得更好的结果。