如何在 GDB 调试器中打印字符串值而不是十六进制?

发布于 2024-10-29 21:10:50 字数 357 浏览 0 评论 0原文

(gdb) run hello
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /Users/doug/langs/c/test hello

Breakpoint 1, main (argc=2, argv=0xbffffa7c) at hw3b.c:14
14     if (argc != 2) {
(gdb) printf "%s", argv
??????(gdb) 

我搜索了有关 SO 的其他问题,并尝试了找到的所有命令,但我不断收到???标记。这是为什么?

(gdb) run hello
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /Users/doug/langs/c/test hello

Breakpoint 1, main (argc=2, argv=0xbffffa7c) at hw3b.c:14
14     if (argc != 2) {
(gdb) printf "%s", argv
??????(gdb) 

I searched other questions on SO and I tried all the commands that I found but I keep getting ??? marks. Why is that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟火散人牵绊 2024-11-05 21:10:50

argv 不是一个字符串,它是一个 char** - 指向可能多个 C 字符串中第一个的指针。

我认为您正在寻找:

print argv[0]
print argv[1]
...

或者如果您想使用 printf:

printf "%s\n", argv[0]

但在如此简单的情况下确实没有理由这样做,因为 gdb 确实知道如何打印 char* 字符串。

或者,如果您想变得更奇特,这是可行的:

print *argv@argc

语法 FOO@NUM 告诉它打印从 FOO 开始的 NUM 元素数组。我不知道为什么解除引用会起作用,但它确实起作用 - 我想 gdb 就是这样。有人启发我吗?

argv isn't a string, it's a char** - a pointer to the first of possibly multiple C strings.

I think you're looking for:

print argv[0]
print argv[1]
...

Or if you want to use printf:

printf "%s\n", argv[0]

But there's really no reason to in such a simple case, since gdb does know how to print char* strings.

Or, if you want to be fancy, this works:

print *argv@argc

The syntax FOO@NUM tells it to print an array of NUM elements starting at FOO. And I have no idea why the dereferencing works, but it does - I guess gdb is just nice like that. Someone enlighten me?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文