Dec 8, 2025
How to Print the Full Value of a Long C-String in GDB: Avoid Abbreviation and View Complete OutputDebugging C/C++ programs often involves inspecting string variables to diagnose issues like incorrect formatting, truncation, or unexpected data. The GNU Debugger (GDB) is a powerful tool for this, but by default, it abbreviates long strings to oid cluttering the output. This truncation—indicated by ... at the end of the string—can hide critical details when debugging lengthy strings (e.g., JSON payloads, log messages, or large text buffers).
In this blog, we’ll explore why GDB truncates strings, and detail four methods to print the full value of a long C-string. Whether you need a one-time fix or a persistent solution, we’ll cover techniques to ensure you see every character, no matter how long the string.
Table of Contents# Understanding GDB’s Default String Printing Why Abbreviation Happens: The print elements Setting Methods to Print Full C-Strings in GDB Method 1: Disable Truncation with set print elements Method 2: Use GDB’s printf Command Method 3: Examine Memory with x/s Method 4: Create a Custom GDB Command Advanced Techniques for Special Cases Non-Null-Terminated Strings Wide Strings (wchar_t*) Troubleshooting Common Issues Conclusion References 1. Understanding GDB’s Default String Printing#When you use GDB’s print command to inspect a C-string (a char* or char[]), GDB may truncate the output if the string exceeds a certain length. For example, if you he a string with 1,000 characters, GDB might only display the first 200-1000 characters (depending on your GDB version) followed by ... to indicate truncation.
Example of Default Truncation: Suppose you’re debugging this program:
#include int main() { char long_str[1024]; memset(long_str, 'A', 1023); // Fill with 1023 'A's long_str[1023] = '\0'; // Null-terminate return 0; // Breakpoint here }Compile with debug symbols: gcc -g -o long_str long_str.c, then run gdb ./long_str. Set a breakpoint at return 0 with break main.c:6, and run run. When the breakpoint hits, printing long_str with print long_str might show:
$1 = 'A' , "..."The ... indicates the string was truncated. To debug effectively, you need to see the full 1023 'A's.
2. Why Abbreviation Happens: The print elements Setting#GDB truncates strings (and arrays) using the print elements configuration variable. This variable defines the maximum number of elements GDB will display for arrays, strings, and other collections.
Default Value: Typically 200 (GDB ≤ 7.x) or 1000 (GDB ≥ 8.x). Behior: If a string has more characters than print elements, GDB shows the first print elements characters followed by ....To check your current print elements setting:
(gdb) show print elements Limit on number of elements printed for arrays, strings, etc. is 200. 3. Methods to Print Full C-Strings in GDB# Method 1: Disable Truncation with set print elements#The simplest way to print full strings is to disable truncation by setting print elements to unlimited (or 0, which is equivalent in GDB).
Steps:#Set print elements to unlimited:
(gdb) set print elements unlimitedOr, for older GDB versions:
(gdb) set print elements 0 # 0 = unlimitedPrint the string with print:
(gdb) print long_str $2 = 'A' , '\0' Notes:# This change is session-specific (resets when GDB restarts). To make it permanent, add set print elements unlimited to your ~/.gdbinit file. Use set print elements N (e.g., set print elements 200) to restore truncation later. Method 2: Use GDB’s printf Command#GDB includes a built-in printf command that mimics C’s printf. Unlike print, it bypasses GDB’s print elements limit and prints the entire string directly.
Steps:#Run printf with the %s format specifier:
(gdb) printf "%s\n", long_str # \n adds a newline for readability AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA Why This Works:#GDB’s printf delegates to the target program’s C library, which prints until the null terminator (\0) is found—no truncation.
Use Case:#Ideal for one-time inspections when you don’t want to modify global GDB settings.
Method 3: Examine Memory with x/s#The x (examine) command inspects memory directly. Using x/s (examine as string) can print the full string if combined with set print elements unlimited.
Steps:#(Optional) Disable truncation (if not already done):
(gdb) set print elements unlimitedExamine the string with x/s:
(gdb) x/s long_str 0x7fffffffde40: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" Notes:# Without set print elements unlimited, x/s will still truncate to print elements characters. Use x/Ns to print exactly N characters (e.g., x/1023s long_str), but this requires knowing the string length in advance. Method 4: Create a Custom GDB Command#For frequent debugging of long strings, define a custom GDB command to automate printing full strings without altering global settings permanently.
Steps:#Define a command (e.g., pfull) to:
Temporarily disable truncation. Print the string. Restore the original print elements value. (gdb) define pfull Type commands for definition of "pfull". End with a line saying just "end". >set print elements unlimited >print $arg0 >set print elements 200 # Restore default (adjust to your original value) >endUse the command with your string variable:
(gdb) pfull long_str $3 = 'A' , '\0' Notes:# Replace 200 with your original print elements value (check with show print elements first). Se the command permanently by adding it to ~/.gdbinit. 4. Advanced Techniques for Special Cases# Non-Null-Terminated Strings#If your string lacks a null terminator (e.g., a raw buffer), GDB’s print, printf, and x/s may print garbage until they hit an accidental \0 in memory. To oid this:
Use printf "%.Ns" to limit length: Print the first N characters (replace N with your buffer size):
(gdb) printf "%.50s\n", buffer # Print first 50 chars of bufferExamine as characters with x/Nc: Inspect N raw characters (no null terminator required):
(gdb) x/50c buffer # Print first 50 chars as 'A', 'B', etc. 0x7fffffffde40: 65 'A' 65 'A' 65 'A' ... (repeats 50 times) Wide Strings (wchar_t*)#For wide strings (wchar_t*), use:
set print elements unlimited + print:
(gdb) set print elements unlimited (gdb) print (wchar_t*)wide_str # Cast to wchar_t* if neededprintf "%ls" for wide string format:
(gdb) printf "%ls\n", wide_str # %ls = wide string (like C's wprintf) 5. Troubleshooting Common Issues# Issue: String Still Truncated After set print elements unlimited# Cause: The string may be extremely large (e.g., 1M+ characters), and GDB struggles to render it. Fix: Redirect output to a file with GDB’s logging: (gdb) set logging file gdb_output.txt (gdb) set logging on (gdb) print long_str # Output sed to gdb_output.txt (gdb) set logging off Issue: printf Command Fails with "No symbol" Error# Cause: Confusing GDB’s built-in printf with the C printf function. Fix: GDB’s printf is a command, not a function. Use the syntax printf "%s\n", str_var (no parentheses). Issue: x/s Shows Garbage Instead of the String# Cause: The address passed to x/s is invalid (e.g., uninitialized pointer). Fix: Verify the pointer is valid with print str_var (ensure it points to allocated memory). 6. Conclusion#GDB’s default string truncation can hide critical data during debugging, but with the right techniques, you can always view full C-strings:
For session-wide truncation disable: Use set print elements unlimited. For one-time prints: Use printf "%s\n", str_var. For memory inspection: Use x/s with set print elements unlimited. For frequent use: Define a custom command like pfull.By mastering these methods, you’ll oid missing key details in long strings and debug more effectively.
7. References# GDB Documentation: print elements GDB Documentation: printf Command GDB Documentation: x (Examine) Command GDB Documentation: Custom Commands 2025-12