乐闻世界logo
搜索文章和话题

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

1个答案

1

When debugging programs with GDB (GNU Debugger), printing the full value of long strings is a common requirement, especially when the string length exceeds GDB's default display limit. By default, GDB may truncate long strings. Here are several methods to view the complete long string in GDB:

1. Modify the Print Limit

GDB has an internal limit controlling the maximum number of characters displayed when printing strings. You can use the set print elements command to increase this limit. For example:

gdb
(gdb) set print elements 0

This command sets the print limit to 0, meaning GDB will print the entire string without truncation. If you know the approximate length of the string, you can set a specific larger number:

gdb
(gdb) set print elements 2000

2. Use the printf Command

In GDB, you can also use the printf command to format the output of strings. This allows more flexible control over the output, especially when you're only interested in specific parts of the string. For example:

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

This command attempts to print the complete content of the longString variable.

Practical Example

Assume we are debugging a C program containing a very long string variable longString.

c
char longString[] = "Very very long string... (omitted thousands of characters)... end";

In GDB, we can print the full string as follows:

  1. Set the print limit:

    gdb
    (gdb) set print elements 0 (gdb) p longString
  2. Use printf to print the full string:

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

Through these methods, you can flexibly view and debug long string variables in GDB.

2024年7月5日 10:38 回复

你的答案