Linux 使用GDB调试时如何打印指针指向的字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4264930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:05:39  来源:igfitidea点击:

How to print the string a pointer points to while debugging using GDB?

clinuxpointersgdb

提问by 341008

How do I inspect a string a pointer is pointing to when stepping through a program using GDB?

在使用 GDB 单步执行程序时,如何检查指针指向的字符串?

I can see the a pointer is pointing to 0x82c6e10. I know it is a string. How do I print it?

我可以看到 a 指针指向0x82c6e10. 我知道这是一个字符串。如何打印?

Using printf("%s\n", 0x82c6e10)gives Bad format string, missing '"'.The fact that gdb does not complain of unknown command tells me that the solution is some variation of what I am doing. Am I right? I tried escaping the quotes but that did not help.

使用printf("%s\n", 0x82c6e10)给出Bad format string, missing '"'.了 gdb 没有抱怨未知命令的事实告诉我,解决方案是我正在做的事情的一些变化。我对吗?我尝试转义引号,但这没有帮助。

采纳答案by wnoise

Here printfis not a function, but a gdbcommand. Omit the parentheses.

这里printf不是一个函数,而是一个gdb命令。省略括号。

Better yet, just use the printcommand, or the xcommand with format /s

更好的是,只需使用print命令,或x带格式的命令/s

(You can actually call the C function printf()with the callcommand.)`

(您实际上可以printf()使用该call命令调用 C 函数。)`

gdbhas voluminous help available with the helpcommand. Try it.

gdbhelp命令有大量可用的帮助。尝试一下。

回答by Jacek Konieczny

Try:

尝试:

print (char *)pointer

回答by Thomas Padron-McCarthy

print (char*)0x82c6e10

打印(字符*)0x82c6e10

回答by Paul R

Use xrather than p:

使用x而不是p

x /s 0x82c6e10