Linux: gdb debugging multithreading

We still list the basic operations of gdb here:

• list(l) line number: Displays the source code. Display 10 lines at a time
• list (l) function name: list the source code of a function
• r or run: run the program
• s or step: enter the function call
• breaktrace (bt): view the function call stack
• info (i) locals: view the value of the local variables of the current stack frame
info break: view the breakpoint information
finish: execute until the current function returns, then stop and wait for the command
print(p): print the value of the expression
set var: modify the value of the variable value
quit(q): exit gdb
break(b) line number: set a breakpoint on a line
break function name: set a breakpoint at the beginning of a function
continue(c): continue from the current position instead of a single Step execution program
run(r): execute the program continuously from the beginning instead of single stepping
delete(d)break: delete all breakpoints
delete(d)break n: delete the breakpoint number n
disable break: disable break point
• enable break: enable breakpoint
• info(i) break: refer to the currently set breakpoint
• next(n): execute a single line

Multithreading can be debugged by default under Linux. Next, let's write a simple test case first.

#include <stdio.h>
#include <pthread.h>

void* thread1(void* arg)
{
  printf("i am %s!\n",(char*)arg);

  return (void*)0;
}

void* thread2(void* arg)
{
  printf("i am %s!\n",(char*)arg);

  return (void*)0;
}

void* thread3(void* arg)
{
  printf("i am %s!\n",(char*)arg);

  return (void*)0;
}

int main()
{
  pthread_t tid1, tid2, tid3;

  pthread_create(&tid1, NULL, thread1, (void*)"thread1");
  pthread_create(&tid2, NULL, thread2, (void*)"thread2");
  pthread_create(&tid3, NULL, thread3, (void*)"thread3");

  pthread_join(tid1, NULL);
  pthread_join(tid2, NULL);
  pthread_join(tid3, NULL);

  return 0;
}

Three threads are created here, so adding the main thread makes a total of four threads. Under gdb, multi-threaded debugging is possible by default. This is different from a process, so no options need to be set. We start gdb debugging of the test code at this time. First we enter the gdb debug mode. Then list to view the source code. I won't go into details here. Here we first break the point on line 30. At this time, thread one has just been created, and there are two threads (main thread and thread one) in the process at this time.
write picture description here
After hitting the breakpoint, we can use the command run(r) to make the process run, and then the breakpoint is reached. The code that created the first thread has finished executing. At this time, we use the instruction info threadsto view the current debuggable thread. It is found that there are two threads numbered 1 and 2 respectively.
write picture description here
This time *represents the thread being debugged. The number is the gdb number, which has nothing to do with our thread ID. This number is for the convenience of switching the thread we want to debug when we debug. At this time, we will make a breakpoint on line 31. Also use it info threadsto check. write picture description here
At this time, we found that the adjustable thread became No. 1 and No. 3. Why is this? The reason is that the above thread one has already run, so there is no thread one inside the process. So there are only two threads left, the main thread and thread two. Thread two is numbered 3. If we want to switch the debugging thread, we can threadadd the thread number to switch.
write picture description here
After the switch is completed, we use it again info threadsto check, this time *next to the thread number we switched, that is to say, the thread number three is now being debugged.
When we can switch the thread to debug, we can debug any thread.


Welcome to discuss together, if there are any mistakes, please contact the author to point out and correct them. thank you all!

Guess you like

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