gdbserver fails in WSL with pthread or anything that links libpthread.so

Viewed 296

If I tried to use gdbserver and set any breakpoint anywhere before using first continue in WSL, it fails after starting program with gdbserver: Cannot get thread handle for LWP ###: generic error.

If I tried to start program first, gdbserver is ignoring all commands from gdb, only after program finished, all commands written in gdb are starting to be processed.

Works: ldd ./test
linux-vdso.so.1 (0x00007ffff7063000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f4b608d0000)
/lib64/ld-linux-x86-64.so.2 (0x00007f4b61000000)

Doesn't: ldd ./test
linux-vdso.so.1 (0x00007fffd0105000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f443aff0000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f443abf0000)
/lib64/ld-linux-x86-64.so.2 (0x00007f443b600000)

cc --version
cc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0

Build:
cc -o test ./src/main.c -pthread

Code: (c) www.geeksforgeeks.org

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>  //Header file for sleep(). man 3 sleep for details.
#include <pthread.h>

// A normal C function that is executed as a thread
// when its name is specified in pthread_create()
void *myThreadFun(void *vargp)
{
    (void)vargp;
    sleep(1);
    printf("Printing GeeksQuiz from Thread \n");
    return NULL;
}

int main()
{
    pthread_t thread_id;
    printf("Before Thread\n");
    pthread_create(&thread_id, NULL, myThreadFun, NULL);
    pthread_join(thread_id, NULL);
    printf("After Thread\n");
    exit(0);
}

gdbserver
LD_PRELOAD=/lib/x86_64-linux-gnu/libpthread.so.0 gdbserver :54321 ./test
or gdbserver :54321 ./test

Process ./test created; pid = 11088
Listening on port 54321
Remote debugging from host 127.0.0.1
gdbserver: Cannot get thread handle for LWP 11088: generic error
Listening on port 54321
Terminated

gdb
echo target remote 127.0.0.1:54321 > /tmp/test_.gdbinit
gdb -x /tmp/test_.gdbinit
GNU gdb (Ubuntu 8.1.1-0ubuntu1) 8.1.1
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
http://www.gnu.org/software/gdb/bugs/.
Find the GDB manual and other documentation resources online at:
http://www.gnu.org/software/gdb/documentation/.
For help, type "help".
Type "apropos word" to search for commands related to "word".
Reading /home/mtunder/workspace/portfolio/test/test from remote target...
warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead.
Reading /home/mtunder/workspace/portfolio/test/test from remote target...
Reading symbols from target:/home/mtunder/workspace/portfolio/test/test...(no debugging symbols found)...done.
Reading /lib64/ld-linux-x86-64.so.2 from remote target...
Reading /lib64/ld-linux-x86-64.so.2 from remote target...
Reading /lib64/ld-2.27.so from remote target...
Reading /lib64/.debug/ld-2.27.so from remote target...
0x00007fffff401090 in ?? () from target:/lib64/ld-linux-x86-64.so.2
(gdb) b main
Breakpoint 1 at 0x80007c7
(gdb) c
Continuing.
Reading /lib/x86_64-linux-gnu/libpthread.so.0 from remote target...
Reading /lib/x86_64-linux-gnu/libc.so.6 from remote target...
Reading /lib/x86_64-linux-gnu/libc-2.27.so from remote target...
Reading /lib/x86_64-linux-gnu/.debug/libc-2.27.so from remote target...
(gdb) CTRL+Z
[1]+ Stopped

Some dkpg outputs:
|?|Name|Version|Architecture|Description|
|-|----|-------|------------|-----------|

dpkg -l *thread*
un libthread-queue-perl (no description available)
un libthreads-perl (no description available)
un libthreads-shared-perl (no description available)

dpkg -l *gdb*
ii gdb 8.1.1-0ubuntu1 amd64 GNU Debugger
un gdb-doc (no description available)
ii gdb-multiarch 8.1.1-0ubuntu1 amd64 GNU Debugger (with support for multiple architectures)
un gdbm-l10n (no description available)
ii gdbserver 8.1.1-0ubuntu1 amd64 GNU Debugger (remote server)
ii libgdbm-compat4:amd64 1.14.1-6 amd64 GNU dbm database routines (legacy support runtime version
ii libgdbm5:amd64 1.14.1-6 amd64 GNU dbm database routines (runtime version)
ii python3-gdbm:amd64 3.6.9-1~18.04 amd64 GNU dbm database support for Python 3.x
un python3-gdbm-dbg (no description available)
un python3.6-gdbm (no description available)
un python3.7-gdbm (no description available)
un python3.8-gdbm (no description available)

0 Answers
Related