0

so im trying to debug this simple c program

#include<stdio.h>
#include<string.h>

int main(){

char str_a[20];

strcpy(str_a,"Hello, world!\n");        
printf(str_a);

return 0;}

gcc -g -o prog prog.c

gdb -q ./prog

(gdb) list
1   #include<stdio.h>
2   #include<string.h>
3   
4   int main(){
5   
6       char str_a[20];
7       
8       strcpy(str_a,"Hello, world!\n");        
9       printf(str_a);
10  
(gdb) break 7
Breakpoint 1 at 0x757: file char_array.c, line 7.
(gdb) break strcpy
Function "strcpy" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 2 (strcpy) pending.
(gdb) break 9
Breakpoint 3 at 0x76a: file char_array.c, line 9.
(gdb) r
Starting program: /home/pinguino/Desktop/char_array 

Breakpoint 1, main () at char_array.c:8
8       strcpy(str_a,"Hello, world!\n");        
(gdb) c
Continuing.

Breakpoint 3, main () at char_array.c:9
9       printf(str_a);

As u can see it entirely skips the strcpy breakpoint... This question has already been posted on sites like stackoverflow but none of the answers there helped me: I've tried adding -fno-builtin but no results whatsoever. Help, I'm lost.

Some more info: uname --all Linux 4.10.0-33-generic #37-Ubuntu SMP Fri Aug 11 10:55:28 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

1 Answer 1

5

This is probably not a Ubuntu specific question, so please have a look at this answer on stackoverflow.

If that code :

#include<stdio.h>
#include<string.h>

int main(){

char str_a[20];

strcpy(str_a,"Hello, world!\n");        
printf(str_a);

return 0;}

is really the code you're trying to debug, gcc is probably clever enough to have simpified your code as printf("Hello world\n"); return 0, or simply use the builtin for strcpy.

So try --no-builtin, perphaps with -O0, it is at least working on gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609.

You can use objdump -d prog to see the disassembly of your code, there is no call to strcpy here.

For this question in particular

You're using break 7, but the line is empty, using break 8 should work.

1
  • So first of all thank you. I've tried --no-builtin and -o0 but they dont work. I remember having tried break 8 but it gave me the: " Note: breakpoint 1 also set at pc 0x757." and now I've understood that is due to the breakpoint at line 7, removing that, I was simply able to break 8 and you are right again in saying that there is no call to strcpy().
    – IDK
    Aug 31, 2017 at 11:22

Not the answer you're looking for? Browse other questions tagged .