0

Recently I've been learning about pthread. Then I suddenly came out of an idea that how does gdb know I create a new thread. Then I wrote down a test code below and started up gdb. I step into pthread_create() function, but instead of letting it return normally, I use return 0 to return pthread_create() function. But gdb still shows that I have only one thread. At first, I thought that gdb got thread information from the return value from the pthread_create() function then I thought gdb might also use child process info to the get thread info so I edited my test code. But the result wasn't what I thought of.

So how does gdb get thread info? What kind of information it needs to know how many threads the main thread have and which thread I'm on.

Code

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include "pthread.h"

void *foo(void *bar) {
        while(1) {
                printf("hello from thread: %d\n", pthread_self());
                sleep(2);
        }
}

int main() {
        printf("Before fake pthread_create");
        pid_t pid;
        if ((pid = fork()) == -1) {
                perror("fork error");
                exit(errno);
        }
        if (pid == 0) {
                while(1) {

                        sleep(3);
                }
        }

        if (pid > 0) {

                pthread_t thread;
                pthread_create(&thread, NULL, foo, NULL);
                while(1) {
                        printf("hello from thread: %d\n", pthread_self());
                        sleep(2);
                }
                return 0;
        }
}
1
  • for all practical purposes, set a break point on the first line of the thread function Commented Sep 16, 2019 at 22:47

2 Answers 2

2

How does gdb detect pthread?

GDB sets internal breakpoint on _dl_debug_state, which allows it track which shared libraries are loaded (this is necessary for debugging shared libraries).

When it observes that libpthread.so is loaded, it loads libthread_db.so.1 into its own process space (into GDB itself, not into the program being debugged), and asks that library to notify GDB when new threads are created and when they are destroyed. Documentation.

The libthread_db has intimate knowledge of the internals of libpthread, and installs appropriate hooks to achieve such notification.

Sign up to request clarification or add additional context in comments.

1 Comment

Except of course for static programs that don't load libthread_db.so.1, such as many of those written in R by default.
-1

There are 2 main mechanisms that debuggers use on linux, and neither of them are very pretty. There is so much detail here that I can only point you there and hope.

One is ptrace which allows the debugger to follow along as the program does things, such as executing system commands like pthread_create, or specific events happen, such as new threads starting, and control the monitored program: http://man7.org/linux/man-pages/man2/ptrace.2.html

The other is the /proc/ file system which reveals lots of information about a process: http://man7.org/linux/man-pages/man5/proc.5.html

In particular ls -l /proc/self/tasks shows you what threads ls has (only 1).

2 Comments

GDB uses libthread_db, which is neither of the mechanisms you listed.
True, but internally libthread_db, the user-side library, uses a number of mechanisms including, on linux at least, ptrace and /proc/ as well as its own secret helper threads. But yes, thanks for reminding me about libthread_db.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.