Get source code for libc for debugging in gdb - c

I am currently debugging an api from shared library libc6 using gdb
getaddrinfo()
which is being called from python module:
socket.getaddrinfo()
I have managed to set a breakpoint at getaddrinfo.c and I wish to navigate the execution in single steps.
Source files:
ldd --version
tells that I have glibc 2.19, and I have downloaded the source from here
http://ftp.gnu.org/gnu/libc/
But during single step debugging using gdb, it doesnt look like the line numbers are matching.
Is there a cleaner way to download the source for libc6
PS: I am new to gdb debugging, please do point out any needed info.

Ubuntu 22.04 minimal runnable example
See also: https://askubuntu.com/questions/487222/how-to-install-debug-symbols-for-installed-packages/1434174#1434174
In the case of glibc, the debug symbols already appear to come preinstalled, otherwise we would need to get them with:
printf "deb http://ddebs.ubuntu.com %s main restricted universe multiverse\n" $(lsb_release -cs){,-updates,-security,-proposed} | \
sudo tee -a /etc/apt/sources.list.d/ddebs.list
sudo apt install ubuntu-dbgsym-keyring
sudo apt update
sudo apt install coreutils-dbgsym
sudo apt install libc6-dbg
and then get source with:
sudo cp /etc/apt/sources.list /etc/apt/sources.list~
sudo sed -Ei 's/^# deb-src /deb-src /'
/etc/apt/sources.list sudo apt-get update
apt source libc6
which produces directory: glibc-2.35.
We then make a C hello world to test with:
main.c
#include <stdio.h>
int main(void) {
puts("hello");
}
and compile and GDB into it with:
gcc -ggdb3 -O0 -std=c99 -Wall -Wextra -pedantic -o main.out main.c
gdb -ex 'set substitute-path . glibc-2.35' main.out
and then test it out as:
(gdb) start
Temporary breakpoint 1 at 0x1151: file main.c, line 4.
Starting program: /home/ciro/tmp/main.out
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Temporary breakpoint 1, main () at main.c:4
4 puts("hello");
(gdb) s
__GI__IO_puts (str=0x555555556004 "hello") at ./libio/ioputs.c:33
33 {
(gdb) l
28 #include <string.h>
29 #include <limits.h>
30
31 int
32 _IO_puts (const char *str)
33 {
34 int result = EOF;
35 size_t len = strlen (str);
36 _IO_acquire_lock (stdout);
37
"We're in".
Some other versions of Ubuntu had sources at an absolute location like:
/tmp/build138741687/
in which case you would instead want:
set substitute-path /tmp/build138741687/ glibc-2.35

Related

How compile c code with python header file

I downloaded python source code using the command
git clone https://github.com/python/cpython
Now I created a main.c like so
#include <stdio.h>
#include <python.h>
int main(void)
{
return 0;
}
when I try to compile using this command
gcc main.c -L /f/<redacted>/cpython/Lib -I /f/<redacted>/cpython/Include -lpython
I get this error
$ gcc main.c -L /f/<redacted>/cpython/Lib -I /f/<redacted>/cpython/Include -lpython
In file included from main.c:2:
F:/<redacted>/cpython/Include/python.h:12:10: fatal error: pyconfig.h: No such file or directory
12 | #include "pyconfig.h"
| ^~~~~~~~~~~~
compilation terminated.
I know that pyconfig.h can be obtained using sudo apt install python-dev on Linux, so I tried pacman -S python-devel but this does not seem to fix the issue
I also tried to use locate pyconfig.h to link it using the -I but it is nowhere to be found
My machine is a windows 10 and I am using MSYS2withMINGW64 to compile this code

Can't use "/usr/bin/env" in C execve() call on macOS

I couldn't really find any relevant results after looking for a while, so I'm asking this myself. I have the following C code, which tries to execute nasm through execve() via /usr/bin/env so I don't have to hardcode the path to the executable.
#include <unistd.h>
int main(int argc, char**argv) {
char *cmds[10] = {"/usr/bin/env", "nasm", "--version", NULL};
execve(cmds[0], cmds, NULL);
}
On linux (Ubuntu 18/20), it works fine:
linux $ gcc test.c && ./a.out
NASM version 2.13.02
linux $
However, on macOS (Catalina, at least), I get the following:
macos $ gcc test.c && ./a.out
env: nasm: No such file or directory
macos $
I'm not exactly sure what the difference is. I do have NASM installed, I can run nasm --version on my terminal just fine. It also works fine if I hardcode the path /usr/local/bin/nasm to the execve() call. If I try to run the following manually on my terminal, it also seems to work:
macos $ /usr/bin/env nasm --version
NASM version 2.15.05 compiled on Aug 29 2020
macos $
The reason I want to use /usr/bin/env is that the default install location for nasm is different for different OSs, and I don't want to just hardcode the paths.
The problem is that you use execve.
The e suffix means you pass an environment to the exec system call, but the environment you pass is NULL. Which means no environment at all.
Use execv instead to use the same environment as the calling program.

How to debug using QEMU in Debian 10

Here is my program:
#include <stdio.h>
int main(void) {
printf("Hello world\n");
return 0;
}
That's how I compile it
aarch64-linux-gnu-gcc -O0 -g3 main.c
I started the a.out like this:
qemu-aarch64-static -L /usr/aarch64-linux-gnu -singlestep -g 1234 ./a.out
And then I started gdb
$ gdb-multiarch -q ./a.out
Reading symbols from ./a.out...done.
(gdb) set sysroot /usr/aarch64-linux-gnu/
(gdb) target remote :1234
Remote debugging using :1234
Reading symbols from /usr/aarch64-linux-gnu/lib/ld-linux-aarch64.so.1...(no debugging symbols found)...done.
0x0000004000814040 in ?? ()
from /usr/aarch64-linux-gnu/lib/ld-linux-aarch64.so.1
(gdb) break main
Breakpoint 1 at 0x76c: file main.c, line 4.
(gdb) c
Continuing.
[Inferior 1 (Remote target) exited normally]
My question: why gdb didn't stop on main entrance and program just finished without any break? What am I do wrong?
I've repeated these steps on Ubuntu 20.04 and everything works fine. To be fair, I use ubuntu for a long time and could just forget what I did before, but Debian is fresh.
Versions info:
OS: Debian GNU/Linux 10 (buster)
QEMU: qemu-aarch64 version 3.1.0 (Debian 1:3.1+dfsg-8+deb10u8)
GDB: GNU gdb (Debian 8.2.1-2+b3) 8.2.1
GCC: aarch64-linux-gnu-gcc (Debian 8.3.0-2) 8.3.0

Cannot compile a simple C program on Elementary distro

I'm using elementary OS 5.1.7 Hera (based on Ubuntu 18.04.4 LT) I created a very simple c program:
#include <stdio.h>
int main()
{
printf( "Hello World!\n" );
return 0;
}
and executing the following :
gcc -o simple simple.c
I get this error:
/usr/lib/gcc/x86_64-linux-gnu/7/cc1: error while loading shared libraries: libisl.so.19: cannot open shared object file: No such file or directory
Any suggestions?
I solved with these:
sudo apt-get remove libisl19
sudo apt update
sudo apt install build-essential

PBC libraries implementation

I am trying to implement a cryptography code and for that I need to include the pbc libraries into my code the basic code is as follows
#include "pbc.h"
int main(void)
{
/* call PBC functions */
return 0;
printf("it's there");
}
and I am working in ubuntu environment and after installing the pbc and gmu I am using this command
gcc -o foo foo.c -I home/Mtech/Desktop/Alok/pbc-0.5.14/include/pbc/ -L home/Mtech/Desktop/Alok/pbc-0.5.14/include/ -l pbc
but the system shows me the following error
:~/Desktop/Alok$ gcc -o foo foo.c -I home/Mtech/Desktop/Alok/pbc-0.5.14/include/pbc/ -L home/Mtech/Desktop/Alok/pbc-0.5.14/include/ -l pbc
foo.c:1:17: fatal error: pbc.h: No such file or directory
`enter code here`compilation terminated.
install gmp library first
sudo apt-get install libgmp3-dev
install pbc library,pbc uses gmp library
[download]: https://crypto.stanford.edu/pbc/download.html "follow guide for installation"
for compiling
gcc <program_name>.c -lgmp -lpbc
for c++
g++ <program_name>.cpp -lgmp -lgmpxx -lpbc
run
./a.out

Resources