how to use gdb in multiple files in open source - c

i am trying to gdb trietool to understand trietool source code.But it always failed to gdb.
step as below:
(1)./configure CFLAGS=-g
(2)make
and then cd /mnt/hgfs/code/libdatrie-0.2.5/tools,ls -lrt
and gdb trietool-0.2,it will report error as below
/mnt/hgfs/code/libdatrie-0.2.5/tools/trietool-0.2": not in executable format:
and gdb trietool-0.2.o,it is ok,and then type command b ../datrie/trie.c:289,it report
(gdb) b ../datrie/trie.c:289
No source file named ../datrie/trie.c.
But in fact there is trie.c file in datrie dir.
How to fix this issue to get gdb work?Do i miss sth?
Why gdb trietool-0.2 is not ok?
milan#milan:/mnt/hgfs/code/libdatrie-0.2.5/tools$ file trietool-0.2
trietool-0.2: Bourne-Again shell script text executable
/mnt/hgfs/code/libdatrie-0.2.5/tools$ file trietool.o
trietool.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped

Related

Could not open '/lib/ld.so.1': No such file or directory when cross compiling mips

I am trying to compile some C code with the intention of executing it on a Mips Linux o32 embedded system. I have simple C code,
#include <stdio.h>
void main(){
printf("Hello world");
}
and compiled it with
mips-linux-gnu-gcc -mfp32 helloworld.c
I know I cant run it on my linux machine so to test it first I got qemu and tried to run it with
qemu-mips ./a.out
But I am getting an error
qemu-mips: Could not open '/lib/ld.so.1': No such file or directory
Which I suspect has to do with the fact that I am including stdio.h but did not link it. How could I fix this.
I expected the code to run.
I am getting an error
qemu-mips: Could not open '/lib/ld.so.1': No such file or directory
Which I suspect has to do with the fact that I am including stdio.h but did not link it.
This is because you have compiled a dynamically linked executable. You should be able to see it in the readelf -l a.out | grep interpreter output. So when you run it the QEMU needs the target dynamic linker, but it cannot find one, because the /lib contains your host binaries and there's no ld.so.1 in it.
It can be fixed by building static executable (mips-linux-gnu-gcc -mfp32 -static helloworld.c), or by supplying the location of the target MIPS rootfs (containing lib/ld.so.1 and other needed libraries) in the QEMU command line (qemu-mips -L<path to directory with MIPS rootfs> ./a.out).

"cannot execute binary file: Exec format error"occured after compile c program to mips binary file

The details of warnings, binary file and system arch are as followed. The c program contains a main and a simple addation function. No external files to be included.
zfq#inginging:~/study/sandbox$ mips64-linux-gnuabi64-gcc sandbox.c
zfq#inginging:~/study/sandbox$ ls
a.out sandbox.c sandbox.h sandbox.s
zfq#inginging:~/study/sandbox$ file a.out
a.out: ELF 64-bit MSB executable, MIPS, MIPS64 rel2 version 1 (SYSV), dynamically linked, interpreter /lib64/ld.so.1, BuildID[sha1]=a16c9c7aba78aa5adcf89b053bdd3c58a7cbe330, for GNU/Linux 3.2.0, not stripped
zfq#inginging:~/study/sandbox$ cat /proc/version
Linux version 5.4.0-65-generic (buildd#lgw01-amd64-048) (gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)) #73~18.04.1-Ubuntu SMP Tue Jan 19 09:02:24 UTC 2021
zfq#inginging:~/study/sandbox$ ./a.out
bash: ./a.out: cannot execute binary file: Exec format error
The binary files still have format error even the system is x86_64 architecture and the elf file is also 64bit. Has anyone ever encountered with such a problem?
If you cross-compile, you are telling: Take this source code and translate it to machine-readable format not for this computer but for some other computer/architecture/system.
Therefore a file translated for MIPS can't run on amd64 as these are quite different architecures with completely different instructions.
Try to run in at appropriate architecture - in your case a MIPS64 GNU/Linux computer
Or simly use a native gcc or other cc to produce a binary for the system you are using to compile.

linux - gbdk returns a fatal error whenever I try to use it

Gbdk works fine on windows but it doesn't work if I try it on linux:
When I run
/usr/lib/gbdk/bin/lcc -Wa-1 -Wl-m -Wl-j -DUSE_SFR_FOR_REG -c main.c -o main.o
it returns this error:
/usr/lib/gbdk/bin/lcc: fatal error in /usr/lib/gbdk/bin/sdcc
That's it. Can anyone please help?
Running cat /etc/os-release returns:
NAME="Linux Mint"
VERSION="19.3 (Tricia)"
ID=linuxmint
ID_LIKE=ubuntu
PRETTY_NAME="Linux Mint 19.3"
VERSION_ID="19.3"
HOME_URL="https://www.linuxmint.com/"
SUPPORT_URL="https://forums.ubuntu.com/"
BUG_REPORT_URL="http://linuxmint-troubleshooting-guide.readthedocs.io/en/latest/"
PRIVACY_POLICY_URL="https://www.linuxmint.com/"
VERSION_CODENAME=tricia
UBUNTU_CODENAME=bionic
Running uname -r returns
5.3.0-46-generic
Running file /usr/lib/gbdk/bin/sdcc returns
/usr/lib/gbdk/bin/sdcc: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-, for GNU/Linux 2.0.0, stripped
Running file /usr/lib/gbdk/bin/lcc returns
/usr/lib/gbdk/bin/lcc: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-, for GNU/Linux 2.0.0, stripped
Please use https://github.com/gbdk-2020/gbdk-2020 if you were using 20 years old GBDK.
To see the actual error message run sdcc manually, you can feed lcc -v to see which programs it calls with which arguments.
Last time I've seen something like /usr/lib/gbdk/bin/lcc: fatal error in /usr/lib/gbdk/bin/sdcc, it was a segmentation fault. lcc sadly catches errors and replaces them with an unhelpful error message.

Using gdb to debug C program, but it cannot locate source file correctly

I use the command gcc -std=gnu99 -g -pthread dotProduct_critical_ompi.c -I. to compile my C program and then use gdb to debug it with the command gdb dotproduct.
But the source file which gdb shows in terminal named dotProduct_critical.c is not the source file I compiled ( my source file is dotProduct_critical_ompi.c ). Why? Thanks for your help.
Update
The problem was solved after I remove the output file and recompile several times. But today, after I modified my source file, the error occured once more. And the method doesn't work any more. What can I do? (My source file is still dotProduct_critical_ompi.c)
the output of command gdb -batch dotproduct -ex "info sources" is:

Using GDB Debugger to See Hidden Code

I have been given a binary file with embedded C code which I cannot see when I run it in the GDB GCC Debugger. I imagine the C code has been hidden by the compilation / formation of the binary code. I have tried the following:
gdb> file myFile
gdb> list main
The output I get is:
myFile.c: No such file or directory
I know there is code written in C in this binary file. The executable runs when I type ./myFile
I have installed 32-bit libraries as this is needed for this situation and I'm running Ubuntu 16.04
Any help is appreciated.
It could be are a compilation issue. Try to look option for debugging
$ gcc -g myFile.c -o myFile
$ gdb myFile
(gdb) list main
If you compile without "-g" option, the debugger will never show you th C code but only the assembly code.
In your case, if you run these commands, you will see the disassembled code
(gdb) info file
Then take the address of the entry point
Entry point: 0x(address)
For show disassembly code
(gdb) break 0x(address)
(gdb) run
When the breakpoint gets caught
(gdb) x/20i $pc

Resources