Linux-gdb debugging multithreading

Before we described how to debug multi-process with gdb, now let's talk about how to debug multi-thread with gdb.
First of all, we must understand the basic commands of gdb to debug multithreading:

Tables Are
info threads Displays all currently debuggable threads, GDB assigns an ID to each thread.
The thread preceded by a * is the thread currently being debugged.
thread ID Switch the currently debugged thread to the thread with the specified ID.
thread apply all command Causes all debugged threads to execute the command command.
thread apply ID1 ID2 … command Let the threads whose thread numbers are ID1, ID2... etc. execute the command command.
set scheduler-locking off/on/step When using the step or continue command to debug the currently debugged thread, other threads are also executed at the same time. If we only want the debugged thread to execute, and other threads stop waiting, we need to lock the thread to be debugged and only let him run.
off No thread is locked, all threads execute.
on Only the currently debugged thread will execute.
step Prevents other threads from preempting the current thread while the current thread is single-stepping. Only when next, continue, util and finish do other threads get rerun
show scheduler-locking View the mode of the current locked thread.

For example:
first write a relatively simple test code:

#include <stdio.h>
#include <pthread.h>
void *thread_1(void* arg)
{
    char* msg=(char*)arg;
    printf("i am %s\n",msg);
}
int main()
{
    printf("i am main thread:%lu\n",pthread_self());
    pthread_t id1;
    pthread_create(&id1,NULL,thread_1,(void**)"thread 1");
    pthread_join(id1,NULL);

    return 0;
}

Debug with gdb:

set scheduler-locking on: lock other threads, only the current process executes.
show scheduler-locking: View the current mode of locking threads.
(must be in execution state)
write picture description here

View all threads:
info threads: with * indicates the thread being executed
write picture description here

Switch threads:
thread n:

write code snippet here

Here's the complete debug code:
write picture description here

write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326520841&siteId=291194637