Missing debugging information with gdb and nasm - linux

I have a simple assembly program with the following makefile:
all : calc
calc : calc.o
gcc -m32 -g -o calc calc.o
calc.o : calc.s
nasm -f elf -g -F stabs calc.s
I try to debug it using gdb but it always says:
Single stepping until exit from function asc2int, which has no line number information.
I tried many solutions to the problem, including -F dwarf but none of them works. Can you please help me resolve this issue?

I don't see any problems with your makefile.
Quick google search gives following nasm. The documentation example shows you need to disassemble to look through the code, but its kind of weird, as already assembly code is being debugged why would the debugger ask to disassemble it further. However the disassembled code is inline with assembly source file.
Form what you have mentioned it seems you are able to debug. use nexti in case if you are using next instruction to step through. You can you disassemble command to every time see where the execution control lies. Or if you want it automated you can save following as a script. asm_next becomes the new instruction for you step in through the code.
set language asm
set disassembly-flavor intel
define asm_next
nexti
disassemble
end
Run it using the command
gdb -x <script> calc
P.S. for larger programs you might need to ad -O0 flag to get correlation between debugger disassembled code and your assembly code in .s file.

Related

Is there any reason why the library OMPtrace does not work on large codes?

I have a code that I parallelized using OpenMP. I want to make sure that the threads deployed follow the same sequence diagram that I have in mind. In order to do that, I looked for an efficient tool (library, software,..) and found the lib OMPTrace which is a simple tool for tracing and visualizing OpenMP program execution. it's shown here how to install and use it. The file I want to compile is called hecese_OpenMP.f90 (Fortran).
I executed the following block of commands in order to vizualize the behaviour of the threads.
flang -g -mp hecese_OpenMP.f90 -L/home/hakim/llvm-openmp-install/lib -o heceseflang
14 h 40
objdump -d heceseflang >heceseflang.objdump
14 h 40
LD_PRELOAD=/home/hakim/llvm-openmp-install/lib/libomp.so:/home/hakim/llvm-openmp/BUILD/omptrace/build/libomptrace.so ./heceseflang
and it shows:
140557273808912: ompt_event_release_nest_lock_last: wait_id=140558880879816, codeptr_ra=0x411abe
140557273808912: ompt_event_wait_nest_lock: wait_id=140558880879816, hint=0, impl=2, codeptr_ra=0x41199f
140557273808912: ompt_event_acquired_nest_lock_first: wait_id=140558880879816, codeptr_ra=0x41199f
140557273808912: ompt_event_release_nest_lock_last: wait_id=140558880879816, codeptr_ra=0x411abe
Erreur de segmentation (core dumped)
I tested the library on a hello_world program and it worked perfectly.
I tried to reproduce the sequence diagram I have in mind and it gave me:
The problem is that if I compile my code with gfortran and execute it, I don't get a segmentation fault (everything works fine) but when I try with OMPTrace I get a segmentation fault.
My question is very simple. Does the problem come from the library itself (knowing that it worked for the hello_world code) or it can come from my code (knowing that it works without the lib) ? I doubt that this library does not support large codes. My code contains 1200 lines.

Linux randomly deleted my file while compiling what do I do?

gcc -L/root/Desktop - Wall -o prog3.c -pthread -lcopy
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.0: In function '_start': (.text+0x20): undefined reference to 'main'
collect2: error: ld returned 1 exit status
This is my error code. prog3.c is nowhere to be found, what on earth happened is there any way to get my file back?? The bold is the command I ran and the rest is the resultant console output
Your problem is here: -o prog3.c. gcc’s -o option is used to tell gcc which name it should give to the executable it generates. So here, you’re basically asking your compiler to replace your prog3.c source file by an executable. Sadly your code is gone...
Little addendum of your options in such scenario:
It was Git (or any other version control) repository. In such case, you can simply bring it from previous commit
Your editor/IDE has some back-up system. Sometimes I need to bring back a file I've thought was needless. For such case, my favourite text editor should have create already back-up file in appropriate location (e.g. $XDG_DATA_HOME/vim/backup in my case).
If none of above, but you still have previously correctly compiled binary file
You can try to decompile, but this process - even if successful - isn't lossless (e.g. code is basically spaghetti).
Had you compiled with -g flag, you could possibly retrieve the code from debug info.
You can at least de-assemble to Assembly code.

Dump debug_loc section from executable

How do I dump debug_loc section from an executable on Linux if default options (-g) are provided to GCC while compiling a C file? I use readelf linux utility.
GCC by default produces DWARF info in DWARF4 format, so if I pass -g-dwarf2 I can see .debug_loc section but how do inspect that section if info is generated with default options as I don't see the section in dump?
Do you use a recent version of binutils? On Ubuntu 14.04 I can build executable with "gcc -g test.c -o test".
With "readelf --debug-dump=info test" I can confirm it's using DWARF 4.
After that, you have (at least) two ways to dump the contents of .debug_line section.
readelf --debug-dump=decodedline test
This will dump decoded line number information. You get line==address mappings directly.
There is also:
readelf --debug-dump=rawline test
This gets you raw debug_line contents.
Usually, .debug_loc is generated for optimized code. Try optimizing your code using compiler flags.

Objdump -S does not show the source code listing of Linux kernel module

I am trying to debug a crash from one of my kernel module ; I am trying to get source code listing along with output of objdump but it is not listing. Is there something I am missing ?
mips-linux-objdump -S <filename.o> > temp
Most likely either a) all debugging information was stripped off the kernel module object file at some point during the build or b) even if the debugging information is there, objdump can't locate the source code files, in which case you might try to cd to where the source files are before running objdump.
You need to compile your kernel module with the debug information to have the interleaved source code in the dumped output. Recompile your kernel module with -g -ggdb for CFLAGS.

Debug assembly code using Kdbg

I have a project with one .c C source code and one .S assembly source code. Once compiled and linked, is there any way to debug .S code using Kdbg? I am calling one .S function from .c file but no code loads in Kdbg.
Add a .file directive in your source, like: .file "sourceasm.s". Kdbg will then use it as expected.
I I just tried kdbg (the KDE front-end for gdb, not the Linux kernel debugger kgdb of almost the same name).
It doesn't seem to have a proper disassembly mode like regular gdb's layout asm. You can set the "memory" window to disassembly and the address to $pc (and it updates as you single step), but that ties up the memory window and isn't very flexible for setting breakpoints or scrolling backwards to instructions before the current RIP/EIP.
Even if you're debugging asm source, you sometimes want to have the debugger show you the real disassembly, as well / instead of the asm source. For example in code that uses macros, or NASM %rep to repeat blocks.
AFAICT, kdbg is not a very good choice for asm debugging. text-mode GDB with layout asm / layout reg is ok; see the bottom of the x86 tag wiki for tips. I've also tried https://github.com/cs01/gdbgui. It has a disassembly mode, but it's not very nice.
As #ivan says, kgdb will let you do source level debugging of asm source files if you add enough metadata for it to know what source file the object came from.
gcc: Build with gcc -g foo.S
NASM: Assemble with nasm -felf64 -g -Fdwarf to include DWARF debug info. (The NASM default is STABS debug info, which also works.)
YASM: Assemble with yasm -felf64 -gdwarf2.
See Assembling 32-bit binaries on a 64-bit system (GNU toolchain) for more about building static / dynamic binaries from asm source.

Resources