PDA

View Full Version : Debugging with GDB



kinbalight
August 29th, 2016, 04:18 PM
Hi, I am taking a course and am trying to use the GDB and "step" through each line of the code.

However, when I come to the printf statements, GDB always gives the error message
"printf.c: No such file or directory"

This does not happen when I use the "next" function of GDB.

Could anyone point out to me what may be causing the error? The source code is included below for reference.

#include <stdio.h>


void sample_function()
{
int i = 0xFFFFFFFF;
char buffer[10];


printf("In sample_function(), i is stored at 0x%08x.\n", (unsigned int)&i);
printf("In sample_function(), buffer is stored at 0x%08x.\n", (unsigned int)&buffer);


printf("Value of i before calling gets(): 0x%08x\n", (unsigned int)i);
gets(buffer);
printf("Value of i after calling gets(): 0x%08x\n", (unsigned int)i);
return;
}


int main()
{
int x;


printf("In main(), x is stored at 0x%08x.\n", (unsigned int)&x);
sample_function();
}

Rocket2DMn
August 29th, 2016, 08:09 PM
I suggest reading up on the difference between step and next. In summary, next will continue to the next line in the file that you're currently debugging - any function calls (like printf) are run to completion without stopping. They have their own source code. When you step, it will try to step into the printf function code, but you don't have the sources available. The sources are probably in a package like glibc-source, but the installed glibc is probably optimized anyway, so you don't get much out of it.

kinbalight
August 30th, 2016, 12:01 AM
Hi, thanks for the reply.

Yup I understand that "step" goes through each instruction line by line and stops. This is the particular instruction I'm trying to run with gdb as my intention is to look at the register pointers at each phase of the code. With "next" I am unable to examine the registers point by point.

When you mention the printf source are in a package, could I download and install said package?

steeldriver
August 30th, 2016, 02:12 AM
You would need to install the glibc source I think - and then also add the relevant source directory to the gdb search path

To illustrate, I added source repositories, then created a /usr/local/src dir and ran



sudo apt-get source glibc


which (on 16.04) produces a /usr/local/src/glibc-2.23/ directory. You should find the printf.c source file in a stdio-common subdirectory.

[NOTE: you could apt-get the source into a user-owned directory of your choice WITHOUT needing sudo - I just wanted to put it somewhere common]

Then given



$ cat hello.c
#include <stdio.h>

int main(void)
{
printf("Hello world!\n");

return 0;
}

$ gcc -g -fno-builtin hello.c


if we do



$ gdb a.out
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1
.
.
.
(gdb)
(gdb) list
1 #include <stdio.h>
2
3 int main(void)
4 {
5 printf("Hello world!\n");
6
7 return 0;
8 }
(gdb) b 5
Breakpoint 1 at 0x40052a: file hello.c, line 5.
(gdb) r
Starting program: /home/steeldriver/forums/a.out

Breakpoint 1, main () at hello.c:5
5 printf("Hello world!\n");
(gdb) s
__printf (format=0x4005c4 "Hello world!\n") at printf.c:28
28 printf.c: No such file or directory.


whereas if we first add the source dir



(gdb) dir /usr/local/src/glibc-2.23/stdio-common/
Source directories searched: /usr/local/src/glibc-2.23/stdio-common:$cdir:$cwd
(gdb) b 5
Breakpoint 1 at 0x40052a: file hello.c, line 5.
(gdb) r
Starting program: /home/steeldriver/forums/a.out

Breakpoint 1, main () at hello.c:5
5 printf("Hello world!\n");
(gdb) s
__printf (format=0x4005c4 "Hello world!\n") at printf.c:28
28 {
(gdb)