Flavius
Flavius

Reputation: 13816

Print string starting with NUL in gdb

I have a string which starts with the character \0 - it's by design and I know its actual length.

How can I print its representation using escapes like "\0foo\0bar\0", if I know its length?

Upvotes: 4

Views: 1738

Answers (2)

Macmade
Macmade

Reputation: 54010

You can use GDB printf command.

You just need to print the char *, incremented by one, so you don't have the NULL character.

If you have the following code:

int main( void )
{
    char * s1 = "abcd";
    char * s2 = "\0abcd";
    return 0;
}

Compile your program with:

gcc -Wall -g -o test test.c

Then run GDB:

gdb test
(gdb) break main
(gdb) run
Breakpoint 1 at 0x100000f04: file test.c, line 3.
(gdb) si
1       char * s1 = "abcd";
(gdb) si
2       char * s2 = "\0abcd";
(gdb) printf "%s", s1
abcd
(gdb) printf "%s", s2+1
abcd

Upvotes: 0

Šimon Tóth
Šimon Tóth

Reputation: 36441

Well, you should be able to print it as an array:

print *str@length

Upvotes: 6

Related Questions