1

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.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
Electro Voyager
  • 107
  • 1
  • 8
  • You shouldn't put a blank line between each and every line of your code. – Jabberwocky Jul 09 '21 at 07:32
  • Is that had something to do with the error ? I think the preprocessor will remove all those extra lines and comments isn't it? – Electro Voyager Jul 09 '21 at 07:42
  • No, but it has to do with people reading it. If you need endless scrolling for code that would fit into half of the height, that could annoy anyone who might try to help you. Also anyone who might have to maintain or review your code etc. – Gerhardh Jul 09 '21 at 09:45
  • Sorry. I was thinking the opposite way. Putting space so that each line is sepertely visible or easy to get attention. Sometimes code becom bloated when we write it all together. – Electro Voyager Jul 10 '21 at 05:47

1 Answers1

1

(gdb) call strcpy($dyn,argv[1])
$5 = (char *(*)(char * restrict, const char * restrict)) 0xb7e64c80 <__strcpy_sse2>

The problem is that you called the wrong strcpy() (arguably GDB should be smarter about this).

In recent versions of GLIBC, strcpy() is a GNU_IFUNC (a GNU extension). This function is a selector, which at runtime determines the most efficient implementation of strcpy() (there are several) for the given processor, and returns its address. If does not itself perform any copying.

Here we can see that on your machine, the selector returns __strcpy_sse2.

If you want to call the actual strcpy(), do this:

(gdb) call ((char *(*)(char *, const char *))__strcpy_sse2)($dyn, argv[1])

This answer is relevant as well.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362