What do I need to debug pthreads? - linux

I want to debug pthreads on my custom linux distribution but I am missing something. My host is Ubuntu 12.04, my target is an i486 custom embedded Linux built with a crosstool-NG cross compiler toolset, the rest of the OS is made with Buildroot.
I will list the facts:
I can run multi-threaded applications on my target
Google Breakpad fails to create a crash report when I run a multi-threaded application on the target. The exact same application with the exact same build of Breakpad libraries will succeed when I run it on my host.
GDB fails to debug multithreaded applications on my target.
e.g.
$./gdb -n -ex "thread apply all backtrace" ./a.out --pid 716
dlopen failed on 'libthread_db.so.1' - /lib/libthread_db.so.1: undefined symbol: ps_lgetfpregs
GDB will not be able to debug pthreads.
GNU gdb 6.8
I don't think ps_lgetfpregs is a problem because of this.
My crosstool build created the libthread_db.so file, and I put it on the target.
My crosstool build created the gdb for my target, so it should have been linked against the same libraries that I run on the target.
If I run gdb on my host, against my test app, I get a backtrace of each running thread.
I suspect the problem with Breakpad is related to the problem with GDB, but I cannot substantiate this. The only commonality is lack of multithreaded debug.
There is some crucial difference between my host and target that stops me from being able to debug pthreads on the target.
Does anyone know what it is?
EDIT:
Denys Dmytriyenko from TI says:
Normally, GDB is not very picky and you can mix and match different
versions of gdb and gdbserver. But, unfortunately, if you need to
debug multi-threaded apps, there are some dependencies for specific
APIs...
For example, this is one of the messages you may see if you didn't
build GDB properly for the thread support:
dlopen failed on 'libthread_db.so.1' - /lib/libthread_db.so.1:
undefined symbol: ps_lgetfpregs GDB will not be able to debug
pthreads.
Note that this error is the same as the one that I get but he doesn't go in to detail about how to build GDB "properly".
and the GDB FAQ says:
(Q) GDB does not see any threads besides the one in which crash occurred;
or SIGTRAP kills my program when I set a breakpoint.
(A) This frequently
happen on Linux, especially on embedded targets. There are two common
causes:
you are using glibc, and you have stripped libpthread.so.0
mismatch between libpthread.so.0 and libthread_db.so.1
GDB itself does
not know how to decode "thread control blocks" maintained by glibc and
considered to be glibc private implementation detail. It uses
libthread_db.so.1 (part of glibc) to help it do so. Therefore,
libthread_db.so.1 and libpthread.so.0 must match in version and
compilation flags. In addition, libthread_db.so.1 requires certain
non-global symbols to be present in libpthread.so.0.
Solution: use
strip --strip-debug libpthread.so.0 instead of strip libpthread.so.0.
I tried a non-stripped libpthread.so.0 but it didn't make a difference. I will investigate any mismatch between pthread and thread_db.

This:
dlopen failed on 'libthread_db.so.1' - /lib/libthread_db.so.1: undefined symbol: ps_lgetfpregs
GDB will not be able to debug pthreads.
means that the libthread_db.so.1 library was not able to find the symbol ps_lgetfpregs in gdb.
Why?
Because I built gdb using Crosstoolg-NG with the "Build a static native gdb" option and this adds the -static option to gcc.
The native gdb is built with the -rdynamic option and this populates the .dynsym symbol table in the ELF file with all symbols, even unused ones. libread_db uses this symbol table to find ps_lgetfpregs from gdb.
But -static strips the .dynsym table from the ELF file.
At this point there are two options:
Don't build a static native gdb if you want to debug threads.
Build a static gdb and a static libthread_db (not tested)
Edit:
By the way, this does not explain why Breakpad in unable to debug multithreaded applications on my target.

Just a though... To use the gdb debugger, you need to compile your code with -g option. For instance, gcc -g -c *.c.

Related

Error running cross-compiled code with pthread

I'm uing ARM_EABI cross-compiler to compile a code that makes use of pthreads to run at an ARM Cortex A9 simulation.
While I'm able to compile it with no problems (just as I've did with others non-pthread applications, which ran fine in the simulation), I'm having an error message when trying to run my pthread application at the simulated ARM (which is running Linux as OS). It's the following:
./pttest.exe: /lib/libpthread.so.0: no version information available (required by ./pttest.exe)
I did my research and found out that's because it's a dynamic lib, and I'm compiling the application with a higher version than the one available on my simulator.
My question is: how to find force my cross-compiler to compile the application with the same pthread lib version of my simulator? Is there anywhere I can download different versions of pthreads? And how to set it?
Sorry, I'm quite a newbie in that area.
Try compiling your application statically, e.g.
gcc -static -o myapplication myapplication.c

No symbol table info available

I'm testing a library from a third party and it crashes. When I wanted to see the reason of the crash my gdb told me that there were no debugging symbols available
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xb53ffb70 (LWP 3722)]
0x00172a89 in tsip_transac_send () from /usr/local/lib/libtinySIP.so.0
I issued bt full on the gdb console and I get a series of lines like below
#0 0x00172a89 in tsip_transac_send () from /usr/local/lib/libtinySIP.so.0
No symbol table info available
I recompiled the library after checking the CFLAGS in the Makefile. The values were fine all the time but I recompiled it anyway
CFLAGS = -g -O2
I ran the test again with the same luck, no debug symbols for the shared library.
What am I missing here?
I'm using Centos 6.0, and I installed the library in Opensuse before but I didn't have this problem. It probably has something to do with my Centos installation.
In case anyone cares, I'm testing Doubango's library for webrtc2sip.
EDIT:
Debug symbols are being loaded properly
(gdb) info sharedlibrary
From To Syms Read Shared Object Library
0x002fb830 0x0031339f Yes (*) /lib/ld-linux.so.2
0x00115040 0x00120028 Yes /usr/local/lib/libtinySAK.so.0
0x00133f30 0x0018b378 Yes /usr/local/lib/libtinySIP.so.0
0x001d8ac0 0x00201b98 Yes /usr/local/lib/libtinyNET.so.0
0x00215dd0 0x0023f638 Yes /usr/local/lib/libtinyDAV.so.0
0x0024eec0 0x00261728 Yes /usr/local/lib/libtinyMEDIA.so.0
0x0026bb00 0x002774d8 Yes /usr/local/lib/libtinyHTTP.so.0
0x002ae340 0x002b0358 Yes /usr/local/lib/libtinyXCAP.so.0
0x002b3990 0x002b8d18 Yes /usr/local/lib/libtinySMS.so.0
0x002be630 0x002c9388 Yes /usr/local/lib/libtinyMSRP.so.0
0x002de240 0x002e8e18 Yes /usr/local/lib/libtinySDP.so.0
0x00323060 0x00345778 Yes /usr/local/lib/libtinyRTP.so.0
Check file /usr/local/lib/libtinySIP.so.0. If it says stripped, check your lib's build process. It may invoke strip manually to strip the debugging symbols.
Well, it looks like it was a bug in gdb.
Centos 6 has the version 7.2-56.el6 precompiled in its repository. I updated (by compiling the sources) to the latest version of gdb and now it is working.
Thanks to all for your help.
It appears you have conflicting compilation flags. I'm not an expert but it looks like your -g flag (which is to generate debug symbols) is being mixed with -O2 flag (which is an optimization parameter).
Try using just -g and post the results.

Statically Linking NCurses Gives Error, for use in BusyBox environment

I wrote a very simple ncurses program to be run in BusyBox environment. However, it seems like that I cannot get my program to compile with everything. I used:
g++ menu.cpp -ohello -lncurses --> Works fine
g++ -static menu.cpp -ohello -lncurses --> Undefined reference to SP (many times)
I found this question but it ignores linking to ncurses. I need a very single executable. My targeted environment is fixed, so I do not concern portability.
You should paste the exact compiler calls and the exact error messages that you are getting.
Do you have a static version of the ncurses library?
More importantly, do you have a static version of the ncurses library compiled for your target environment? For example your target environment may be using ulibc instead of glibc or it could even be a whole different platform (hint: tell us what your target platform is).
Are you certain that you are compiling with the right flags? The compiler flags that you are showing seem more suited to compiling an application for use in the build host environment...

cross-gdb: fix a mismatch between libthread_db and libpthread

I' ve a cross-gdb configured with --host=i686-pc-linux-gnu --target=powerpc-e300c3-linux-gnu. i can correctly debug an application on a remote board with gdbserver but i get an error about a version mismatch between libthread_db and libpthread so i can't debug threads correctly (gdb recognizes only one thread instead of three threads). Maybe it's is due to a different version of libc: on host machine i've libc2.15 and on target machine lib2.5. I tried to rebuid libc2.5 for host in order to link gdb against to it but it's an hell. Before i get crazy to rebuild it, could someone confirm that it's a libc problem?
I tried to rebuid libc2.5 for host in order to link gdb against to it
That's not what you need.
What you need, is for gdb to find and load libthread_db.so.1, that matches your target libpthread.so.0.
For this, you need to
build libc-2.5 for host, and
set GDB's libthread-db-search-path such that it finds the libthread_db.so.1 built in step 1.
You don't actually need to build the entire libc in step 1. Something like this should suffice:
mkdir build && cd build
../configure --prefix=/usr
make -C ../nptl_db objdir=`pwd`
Update:
i have GDB 6.6 and there isn't libthread-db-search-path. What is another way to specify that path?
That GDB will just dlopen("libthread_db.so.1", ...). So to make it find the right libthread_db.so.1, you need to adjust LD_LIBRARY_PATH. Using bash:
LD_LIBRARY_PATH=/tmp/glibc-2.5/build/nptl_db gdb /path/to/target/a.out

reverse-step multithread error

I get the following message in gdb (version 7.1):
[Thread debugging using libthread_db enabled]
and the command reverse-step
results with the following error message:
(gdb) reverse-step
Target multi-thread does not support this command
I am debugging a serial code right now, so I definitely do not need multi-threading. Can I turn this off somehow so that I get the latest reverse-debug commands to work? Also, if the code is parallelised with OpenMPI, there will be no need for multi-thread debugging at all, right?
Edit: Is this set as a compilation flag that can be just excluded?
You don't mention which version of GDB you're using, but since a little while, the parameter libthread-db-search-path is available.
(gdb) set libthread-db-search-path /tmp
(gdb) start
Temporary breakpoint 1 at 0x400632: file threads.c, line 14.
warning: Unable to find libthread_db matching inferior's thread library, thread debugging will not be available.
will tell GDB to lookup it's helper library (libthread-db.so) in a directory where it isn't, so multithread debugging won't be enabled!
I'm not sure about OpenMPI parallel applications are multiprocesses (in contrast with OpenMP where they are multithreaded), so it won't change anything for you.
EDIT: Multithread debugging is usually only enabled when libpthread.so or equivalent is loaded by your process (ldd your-process to check if it's linked as a shared library) so if you don't need it, there might be a problem in your compilation script.

Resources