debugging multi-threaded threads with GDB summary

Source: Internet
Author: User

Go: Debug multithreaded Threads with GDB summary

GDB has been the multi-threaded debugging contact is not much, recently because the work has some contact, simply make a point record it. First, we introduce the basic command of GDB multi-threaded debugging. Info Threads shows all threads currently available for debugging, each with a GDB-assigned ID, which is used when the thread is being manipulated. Previous * is the thread that is currently being debugged. The thread ID Toggles the currently debugged thread for the specified ID. Break thread_test.c:123 Thread all sets a breakpoint on the corresponding line in all threads thread apply ID1 ID2 command Have one or more threads execute GDB command commands. The thread apply all command lets all the debugged threads execute GDB command commands. The set scheduler-locking off|on|step estimate is that anyone who has actually used multithreaded debugging can find that other threads are executing at the same time using the step or Continue command to debug the currently debugged thread. How do you just let the debugger execute it? This requirement can be achieved with this command. Off does not lock any threads, that is, all threads are executed, which is the default value. On only the currently debugged program will execute. Step in a single step, in addition to the case of next over a function (the person familiar with the situation may know that this is actually a set breakpoint and then continue behavior), only when the front-thread will execute.

GDB has the following support for debugging multi-threaded threads:

    • Thread Generation Notification: GDB gives a hint when a new thread is generated

(GDB) R

Starting program:/root/thread

[New Thread 1073951360 (LWP 12900)]

[New Thread 1082342592 (LWP 12907)]---The following three new threads are created

[New Thread 1090731072 (LWP 12908)]

[New Thread 1099119552 (LWP 12909)]

    • View Threads: Use info threads to view the threads that are running.

(GDB) Info threads

4 Thread 1099119552 (LWP 12940) 0xffffe002 in?? ()

3 Thread 1090731072 (LWP 12939) 0xffffe002 in?? ()

2 Thread 1082342592 (LWP 12938) 0xffffe002 in?? ()

* 1 Thread 1073951360 (LWP 12931) Main (argc=1, argv=0xbfffda04) at thread.c:21

(GDB)

Note that the blue text at the beginning of the line is the thread number assigned by GDB, which is used when switching threads, instead of the green number indicated above.

In addition, the red asterisk at the beginning of the line identifies the currently active thread

    • Toggle Thread: Use thread threadnumber to switch threadnumber to the thread number mentioned above. The following example shows switching the active thread from 1 to 4.

(GDB) Info threads

4 Thread 1099119552 (LWP 12940) 0xffffe002 in?? ()

3 Thread 1090731072 (LWP 12939) 0xffffe002 in?? ()

2 Thread 1082342592 (LWP 12938) 0xffffe002 in?? ()

* 1 Thread 1073951360 (LWP 12931) Main (argc=1, argv=0xbfffda04) at thread.c:21

(GDB) Thread 4

[Switching to thread 4 (thread 1099119552 (LWP 12940)]) #0 0xffffe002 in?? ()

(GDB) Info threads

* 4 Thread 1099119552 (LWP 12940) 0xffffe002 in?? ()

3 Thread 1090731072 (LWP 12939) 0xffffe002 in?? ()

2 Thread 1082342592 (LWP 12938) 0xffffe002 in?? ()

1 Thread 1073951360 (LWP 12931) Main (argc=1, argv=0xbfffda04) at thread.c:21 (GDB)

These are some of the basic commands for debugging multithreading with GDB. In addition, GDB provides breakpoint settings on threads and commands for issuing commands to specified or all threads.

The first contact with GDB under the multi-threaded debugging, often ignores the concept of the active thread in GDB. In general, when debugging with GDB, only one thread is the active thread, and if you want to get the output from another thread, you must use the thread command to switch to the specified thread to debug or observe the output.

GDB Debug Main command: Debug Program with GDB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.