开发者

Print string starting with NUL in gdb

开发者 https://www.devze.com 2023-04-05 03:20 出处:网络
I have a strin开发者_JAVA技巧g which starts with the character \\0 - it\'s by design and I know its actual length.

I have a strin开发者_JAVA技巧g 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?


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

print *str@length


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号