Need help with GDB (gdb version 8.1.1 in Ubuntu 18.04 32 bit Linux on Virtual Box & gcc version is 7.5.0). I included <string.h> in the program and using strcpy() in the program. Compiled with the option gcc -ggdb main.c -o mainCompiled
which will use gnu debug symbols in the compiled program.
But when I try to call strcpy() inside gdb using 'call' command I am getting below output
$5 = (char *(*)(char * restrict, const char * restrict)) 0xb7e64c80 <__strcpy_sse2>
Also my code hit the breakpoint put at main() function as expected. I run code with three parameters( See the code below, simple to understand). Here I was trying to copy argv[1] whose value "AAAA" to a dynamically allocated char pointer in gdb using malloc() called '$dyn'. But that's not working as expected. The value of argv[1] which is "AAAA" is not copied to $dyn in gdb. The code I wrote is given below ( A very basic program)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void FunctionShouldNotExecute(void)
{
printf("\n\nI should not execute!!!\n\n");
exit(0);
}
void EchoInput(char *userInput)
{
char buffer[20];
strcpy(buffer, userInput);
printf("\n\n%s\n\n", buffer);
}
int AddNumbers(int i, int j)
{
return i + j;
}
int main(int argc, char **argv)
{
int sum = 0;
EchoInput(argv[1]);
sum = AddNumbers(atoi(argv[2]), atoi(argv[3]));
printf("Sum of %s + %s is %d\n\n", argv[2], argv[3], sum );
return 0;
}
Below is the output from gdb and the series of commands I tried to execute.
(gdb) run AAAA 10 20
Starting program: /home/raybrown/Documents/MyCodes/GDB/7_lect/mainCompiled AAAA 10 20
Breakpoint 1, main (argc=4, argv=0xbfffefd4) at main.c:40
40 int sum = 0;
(gdb) set $dyn = (char *) malloc(10*sizeof(char))
(gdb) print $dyn
$4 = 0xb7fd2650 ""
(gdb) x/10xb &dyn
No symbol "dyn" in current context.
(gdb) x/10xb $dyn
0xb7fd2650: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0xb7fd2658: 0x00 0x00
(gdb) call strcpy($dyn,argv[1])
$5 = (char *(*)(char * restrict, const char * restrict)) 0xb7e64c80 <__strcpy_sse2>
(gdb) print $dyn
$6 = 0xb7fd2650 ""
(gdb) x/10xb $dyn
0xb7fd2650: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0xb7fd2658: 0x00 0x00
For any further information please comment below. I was trying to study the basic of GDB.