C++ 如何在 gdb 中打印长字符串的完整值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/233328/
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-27 13:55:31  来源:igfitidea点击:

How do I print the full value of a long string in gdb?

c++cstringdebugginggdb

提问by John Carter

I want to print the full length of a C-string in GDB. By default it's being abbreviated, how do I force GDB to print the whole string?

我想在 GDB 中打印 C 字符串的全长。默认情况下它是缩写的,如何强制 GDB 打印整个字符串?

回答by John Carter

set print elements 0

From the GDB manual:

从 GDB 手册

set print elements number-of-elements
Set a limit on how many elements of an array GDB will print. If GDB is printing a large array, it stops printing after it has printed the number of elements set by the set print elementscommand. This limit also applies to the display of strings. When GDB starts, this limit is set to 200. Setting number-of-elementsto zero means that the printing is unlimited.

回答by duskwuff -inactive-

As long as your program's in a sane state, you can also call (void)puts(your_string)to print it to stdout. Same principle applies to all functions available to the debugger, actually.

只要您的程序处于正常状态,您也可以call (void)puts(your_string)将其打印到标准输出。实际上,相同的原则适用于调试器可用的所有功能。

回答by Wichert Akkerman

There is a third option: the x command, which allows you to set a different limit for the specific command instead of changing a global setting. To print the first 300 characters of a string you can use x/300s your_string. The output might be a bit harder to read. For example printing a SQL query results in:

还有第三个选项:x 命令,它允许您为特定命令设置不同的限制,而不是更改全局设置。要打印字符串的前 300 个字符,您可以使用x/300s your_string. 输出可能有点难以阅读。例如打印 SQL 查询结果:

(gdb) x/300sb stmt.c_str()
0x9cd948:    "SELECT article.r"...
0x9cd958:    "owid FROM articl"...
..

回答by korry

The printfcommand will print the complete strings:

printf命令将打印完整的字符串:

(gdb) printf "%s\n", string

回答by abstraktor

Just to complete it:

只是为了完成它:

(gdb) p (char[10]) *($ebx)
 =   "asdfasdfe\n"

You must give a length, but may change the representation of that string:

您必须给出一个长度,但可以更改该字符串的表示形式:

(gdb) p/x (char[10]) *($ebx)
 =   {0x61,
  0x73,
  0x64,
  0x66,
  0x61,
  0x73,
  0x64,
  0x66,
  0x65,
  0xa}

This may be useful if you want to debug by their values

如果您想通过它们的值进行调试,这可能很有用

回答by mrtimdog

Using set elements ...isn't always the best way. It would be useful if there were a distinct set string-elements ....

使用set elements ...并不总是最好的方法。如果有一个不同的set string-elements ....

So, I use these functions in my .gdbinit:

所以,我在我的 .gdbinit 中使用这些函数:

define pstr
  ptype $arg0._M_dataplus._M_p
  printf "[%d] = %s\n", $arg0._M_string_length, $arg0._M_dataplus._M_p
end

define pcstr
  ptype $arg0
  printf "[%d] = %s\n", strlen($arg0), $arg0
end

Caveats:

注意事项:

  • The first is c++ lib dependent as it accesses members of std::string, but is easily adjusted.
  • The second can only be used on a running program as it calls strlen.
  • 第一个依赖于 c++ lib,因为它访问 std::string 的成员,但很容易调整。
  • 第二个只能用于正在运行的程序,因为它调用 strlen。