'GDB print all values in char array
I am storing various filenames in my array which are partitioned by null bytes. When debugging, I am only able to see the first filename. So, for instance if my array is like this: hello.txt00000hello2.txt, I am only able to see hello.txt. How can I print the entire array? I have trouble finding such command elsewhere.
Solution 1:[1]
With gdb, you can achieve to print the elements of your array using the following command:
(gdb) print *array@size
If my variable array is a type char*[] such as below
const char *array[] = {"first","second","third"};
Then I could display the 2 first char* entries of my array by using:
(gdb) print *array@2
$2 = { 0x..... "first", 0x..... "second"}
Using it in order to display the arguments of a program is very handy:
(gdb) print *argv@argc
It's also possible to do the same with x commands using x/Ns *argv, where N is the integer value of argc (i.e. for argc = 2, x/2s *argv)
The documentation for the whole magic of the print command is here.
Solution 2:[2]
you might try defining the array as:
char ** array;
array = malloc( NUM_ROWS*sizeof char* );
for( int i =0; i < NUM_ROWS; i++ )
{
*array[i] = malloc( NUM_COLUMNS )
}
then the code can
memset( array[x], '\0', NUM_COLUMNS );
strncpy(array[x], myString, NUM_COLUMNS-1);
where myString is the data to place in that row and
for( int i = 0; i < NUM_ROWS; i++ )
{
if( array[i] )
{ // only enters this code block if something placed in row
printf( "%s\n", array[x] );
}
}
then use 'p array[x]' for each row in the array
Solution 3:[3]
If you have a fixed-length array and want to see all the data in there - just ask to print the array and you will get the full output, because GDB knows about the size.
If you have a pointer to a fixed-length array then GDB assumes the most common case - a C string, so it stops the display at the first hex null. To see more: de-reference and cast the result as char array with the intended length you want to see.
#include <stdio.h>
static char myarr[55] = "hello.txt\x00\x00\x00\x000hello2.txt";
int main () {
char *p = myarr;
puts (p);
return 0;
}
compiled and run as gcc -g test.c && gdb -q -ex start ./a.out:
Reading symbols from /tmp/a.out...done.
Temporary breakpoint 1 at 0x400535: file test.c, line 5.
Starting program: /tmp/a.out
Temporary breakpoint 1, main () at test.c:5
5 char *p = myarr;
(gdb) step
6 puts (p);
(gdb) print p
$1 = 0x601060 <myarr> "hello.txt"
(gdb) print *p
$2 = 104 'h'
(gdb) print (char[20])*p
$3 = "hello.txt\000\000\000\000hello2."
(gdb) print (char[55])*p
$4 = "hello.txt\000\000\000\000hello2.txt", '\000' <repeats 31 times>
(gdb) detach
Detaching from program: /tmp/a.out, process 456
hello.txt
(gdb) quit
If you want that to print the sequences in hex instead of in octal - have a look at 54469844.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | user3629249 |
| Solution 3 |
