gdb printf 的奇怪行为

Strange behavior with gdb printf

我在 9.1 版

中遇到 gdb printf 的问题
echo -e '#include<stdio.h> \n int main(){ \n printf("Hello"); \n }' > test.c

gcc -g test.c -o test

echo 'break test.c:4' > test.gdb
echo 'run' >> test.gdb
echo 'set $aux = (char*)malloc(256)' >> test.gdb
echo 'set $e = strcpy($aux, "abc")' >> test.gdb
echo 'printf "%s", $aux' >> test.gdb

gdb --batch --command=test.gdb test

gdb 9.1 的输出:

Breakpoint 1 at 0x1167: file test.c, line 4.

Breakpoint 1, main () at test.c:4
4    }
�e���

预期输出(与 gdb v8 相同):

Breakpoint 1 at 0x1167: file test.c, line 4.

Breakpoint 1, main () at test.c:4
4    }
abc

我检查过charset,但似乎没问题。 对此有什么想法吗?

这是 Bug 25650 - GDB can't 'printf' a convenience variable holding an inferior address,已在 gdb 9.2 中修复。

如果您无法升级到 gdb 9.2 但可以重新编译现有发行版的 gdb 9.1,则有两行 patch.

在 Ubuntu 20.04 上,gdb 9.1-0ubuntu1 附带:

  1. 运行 apt build-dep gdb 从源代码中获取构建 gdb 所需的包
  2. 运行apt install dpkg-dev(得到/usr/bin/dpkg-source
  3. 取消注释 /etc/apt/sources.list
  4. 中的 deb-src
  5. 运行 apt update
  6. cd 到一个空目录和 运行 apt source gdb 。在一个全新的目录中执行此操作将使编译后更容易清理。
  7. cd 到 gdb-9.1 并将 patch 应用到 gdb/printcmd.c
  8. 构建并安装 gdb。例如,要将其放入默认值 /usr/local/bin,您需要 运行
      mkdir build
      cd build
      ../configure
      make
      make install