Operating System - Linux
1748286 Members
3719 Online
108761 Solutions
New Discussion юеВ

How to get the source code from executable using gdb??

 
SOLVED
Go to solution
VEL_1
Valued Contributor

How to get the source code from executable using gdb??



How to get the source code from executable using gdb??

Can anyone explain me with simple c program?
4 REPLIES 4
Claudio Cilloni
Honored Contributor

Re: How to get the source code from executable using gdb??

binary executables doesn't contain the source code. They can be compiled including debug informations, so debuggers like gdb, ddd etc... could run step-by-step the executable while showing the source code, but you need the original source code file.

Ciao
Claudio
dirk dierickx
Honored Contributor

Re: How to get the source code from executable using gdb??

first, for gdb to be able to work the binary needs to be compiled with the debuging option.
second, if you want to debug an application/tool that was included in your linux distro, you can almost always download the source code and look at it, or compile it yourself this time including the debug option.
Solution

Re: How to get the source code from executable using gdb??

* You will first have to compile your executable using -g option. This will generate debug information in the binary and tell gdb where to look for source files and source and instruction correlations.
* Whiile debugging, the source files need to be avaliable for access by gdb.
* You can use "list" command to list a source file inside gdb.
* "Disassemble" command will show the assembly and source code interspersed.
Muthukumar_5
Honored Contributor

Re: How to get the source code from executable using gdb??

We can easily get source of a c program as,

-- exmaple program ---
//test.c
#include

main() {

printf("Hello World!!\n");
}

--Compilation--
cc -ggdb3 -o test test.c
where -ggdb3 will have all debugging symbols

--Debugging--
gdb -q test
(gdb) li
1 #include
2
3 main() {
4
5 printf("Hello World!!\n");
6 }
(gdb)q

That is all.

HTH.


Easy to suggest when don't know about the problem!