Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GDB print all values in char array

Tags:

c

gdb

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.

like image 291
mrQWERTY Avatar asked Apr 09 '15 02:04

mrQWERTY


People also ask

What does F do in GDB?

f - Runs until the current function is finished. s - Runs the next line of the program. s N - Runs the next N lines of the program.

What is P command in GDB?

The usual way to examine data in your program is with the print command (abbreviated p ), or its synonym inspect . It evaluates and prints the value of an expression of the language your program is written in (see section Using GDB with Different Languages).

What does the LIST command do in GDB?

To print lines from a source file, use the list command (abbreviated l ). By default, ten lines are printed. There are several ways to specify what part of the file you want to print; see Specify Location, for the full list.

How do I list breakpoints in GDB?

You can see these breakpoints with the GDB maintenance command `maint info breakpoints' . Using the same format as `info breakpoints' , display both the breakpoints you've set explicitly, and those GDB is using for internal purposes. Internal breakpoints are shown with negative breakpoint numbers.


2 Answers

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.

like image 60
sbz Avatar answered Oct 27 '22 23:10

sbz


You can use x/999bc, where 999 is the size of your array, for instance:

paul@thoth:~/src/sandbox$ gdb ./str
GNU gdb (GDB) 7.4.1-debian
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/paul/src/sandbox/str...done.
(gdb) list
1   int main(void) {
2       char * p = "hello\0world\0hahaha";
3       return 0;
4   }
5   
(gdb) b 3
Breakpoint 1 at 0x4004b8: file str.c, line 3.
(gdb) run
Starting program: /home/paul/src/sandbox/str 

Breakpoint 1, main () at str.c:3
3       return 0;
(gdb) print p
$1 = 0x40056c "hello"
(gdb) x/19bc p
0x40056c:   104 'h' 101 'e' 108 'l' 108 'l' 111 'o' 0 '\000'    119 'w' 111 'o'
0x400574:   114 'r' 108 'l' 100 'd' 0 '\000'    104 'h' 97 'a'  104 'h' 97 'a'
0x40057c:   104 'h' 97 'a'  0 '\000'
(gdb) 
like image 25
Crowman Avatar answered Oct 27 '22 22:10

Crowman