On Fedora 33 Workstation if I try to put a break point on printf()
before the program is executed, I’m informed the function does not exist
by gdb. If I break on main(), I’m allowed put a break on printf() that’s never reached when execution is continued. If I break on main() and
step into printf() I reach _IO_puts().
Are you using the name of the function “printf” as your breakpoint, or the line that printf is on?
One will drop you inside of printf, which is a glibc function, and will require that you install the packages containing the debug symbols in order to actually get any useful information beyond the assembly code, and the other will stop you at the line that your printf is on.
If all you’re trying to do, is step to the line, you could also use “start” and then “next”/“step”. You can also use “break” when doing this, to add a break at a specific line for the next run.
If you try to place a breakpoint before starting the program, you haven’t entered the scope of main(), so gdb will have no context for printf(), as it’s, again, a glibc function, and not a part of your program.
(gdb) break main
Breakpoint 1 at 0x40112a: file hello.c, line 4.
(gdb) break printf
Function “printf” not defined.
(gdb)
If I step through the code starting at the break on main() I reach:
int
_IO_puts (const char *str)
{
int result = EOF;
size_t len = strlen (str);
_IO_acquire_lock (stdout);
if ((_IO_vtable_offset (stdout) != 0
|| _IO_fwide (stdout, -1) == -1)
&& _IO_sputn (stdout, str, len) == len
&& _IO_putc_unlocked (‘\n’, stdout) != EOF)
result = MIN (INT_MAX, len + 1);
That’s the correct behavior in gdb. “break <function>” is “break on entry of <function>”. This breaks at the first line of the function, any time it’s called.
There seems some compiler optimization foolery is going on. It looks almost like, on the first installs, printf() was compiled in as puts(), as it’s often faster, and you weren’t using any of the special formatting that printf() would be needed for. So, your source says printf(), but the binary is linked to puts() (IO_puts()).
Try explicitly using “-O0” or “-Og” when you’re compiling.