So, I'm learning how to program in C, and I'm having (or at elast, trying to) having some fun with GDB.
So I wrote this simple code:
#include <stdio.h>
int main (int argc, char *argv[]){
int i;
int n = atoi(argv[2]);
for (i=0; i<n ; i++){
printf("%s \n",i+1,argv[1]); // prints the string provided in
} // the arguments for n times
return 0;
}
and I was trying to usd GDB to obtain some infos on it. So I used it to try and get the arguments from the memory addresses, but this is what I get:
(gdb) break main
Breakpoint 1 at 0x4005d7: file repeat2.c, line 14.
(gdb) break 17
Breakpoint 2 at 0x40062c: file repeat2.c, line 17.
(gdb) run hello 5
Starting program: /root/Scrivania/Programmazione/repeat2 hello 5
warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7ffff7ffa000
Breakpoint 1, main (argc=3, argv=0x7fffffffe948) at repeat2.c:14
14 int n = atoi(argv[2]);
(gdb) cont
Continuing.
1 ------> hello
2 ------> hello
3 ------> hello
4 ------> hello
5 ------> hello
Breakpoint 2, main (argc=3, argv=0x7fffffffe948) at repeat2.c:18
18 return 0;
(gdb) x/3xw 0x7fffffffe948 (I try to read what argv contains)
0x7fffffffe948: 0xffffebbc 0x00007fff 0xffffebe3
(gdb) x/s 0xffffebbc (I try to read one of the argoments in the array)
0xffffebbc: <Address 0xffffebbc out of bounds>
Why do I keep getting this error? I'm on a 64-bit, and I'm using Kali Linux
The program, if compiled, works, it's just that I can't understand why I can't read those values with GDB.
@DrakaSAN found the bug in your program. As for your gdb question:
x/3xw
prints out 3 4-byte words. argv
is an array of char *
pointers.
Since you're on a 64-bit system, pointers are 8 bytes, so instead of w
you want to use g
(giant, 8 bytes) or a
(address), which will select the correct size automatically:
(gdb) break 7
Breakpoint 1 at 0x40058c: file repeat2.c, line 7.
(gdb) run hello 5
Starting program: /tmp/repeat2 hello 5
Breakpoint 1, main (argc=3, argv=0x7fffffffdfe8) at repeat2.c:7
7 int n = atoi(argv[2]);
(gdb) x/3xg 0x7fffffffdfe8
0x7fffffffdfe8: 0x00007fffffffe365 0x00007fffffffe372
0x7fffffffdff8: 0x00007fffffffe378
(gdb) x/3xa 0x7fffffffdfe8
0x7fffffffdfe8: 0x7fffffffe365 0x7fffffffe372
0x7fffffffdff8: 0x7fffffffe378
(gdb) x/s 0x7fffffffe365
0x7fffffffe365: "/tmp/repeat2"
(gdb) x/s 0x7fffffffe372
0x7fffffffe372: "hello"
(gdb) x/s 0x7fffffffe378
0x7fffffffe378: "5"
Thanks to @adpeace for suggesting the a
modifier.
Welcome to SO, +1 for having a well asked first question.
printf("%s \n",i+1,argv[1]);
You try to put a int (i) when printf expect a string (%s). I think what you wanted to do was:
for (i=0; i<n ; i++)
{
printf("%s \n", argv[1]);
}
Althought, I m surprised your compiler didn t scream at you for this.
(As a note... Kali Linux isn t supposed to be used as a development OS, you may want to use a Debian or Ubuntu...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With