GDB multi-thread debugging

Source: Internet
Author: User
Http://hi.baidu.com/hcq11/blog/item/9f5bfc6e696209d680cb4a25.html

Http://hi.baidu.com/litto/blog/item/759389dd198111375882dd1e.html

Http://blogold.chinaunix.net/u3/94700/showart_2389432.html <recommended reading>

 

First, we will introduce the basic commands for GDB multi-threaded debugging.

Info threadsDisplay All the currently debuggable threads. Each thread has an ID allocated to it by GDB, which will be used in subsequent thread operations. There are * threads currently being debugged.

Thread IDSwitch the thread of the current debugging to the thread with the specified ID.

Breakthread_test.c: 123 thread allSet breakpoints on corresponding lines in all threads

Thread apply id1 Id2 commandLet one or more threads execute the gdb command.

Thread apply all commandLet all the debugging threads execute the gdb command.

Set scheduler-locking off | on | stepIt is estimated that all users who have used multi-thread debugging can find that when using the step or continue command to debug the currently debugged thread, other threads are also executed at the same time.ProgramWhat about execution? You can use this command to achieve this requirement. Off does not lock any thread, that is, all threads are executed. This is the default value. On is executed only by the currently debugged program. Step in a single step, except for the next function (people familiar with the situation may know that this is actually an action to set the breakpoint and then continue, only the current thread will execute.

 

GDB supports the following multi-threaded program debugging:

    • Thread-generated notification: when a new thread is generated, GDB will provide a prompt.

(GDB) r
Starting program:/root/thread
[New threads 1073951360 (lwp12900)]
[New thread 1082342592 (lwp 12907)] ---The following three newly generated threads
[New thread 1090731072 (lwp 12908)]
[New thread 1099119552 (lwp 12909)]

    • View threads: use info threads to view running threads.

(GDB) info threads
4Thread 1099119552 (lwp 12940) 0xffffe002 in ?? ()
3Thread 1090731072 (lwp 12939) 0xffffe002 in ?? ()
2Thread 1082342592 (lwp 12938) 0xffffe002 in ?? ()
*1Thread 1073951360 (lwp 12931) Main (argc = 1, argv = 0xbfffda04) at thread. C: 21
(GDB)

Note:BlueSpecifies the thread number allocated by GDB. This number is used when switching a thread, instead of the green number marked above.

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

    • Switch thread: UseThread threadnumberSwitch,ThreadnumberIt is the thread number mentioned above. The following example shows how to switch 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)

 

The next step is to directly set the breakpoint in your thread function and then continue to that breakpoint. Generally, when multithreading occurs, it is best to set the breakpoint because it runs at the same time.Set scheduler-locking on

In this case, only the current thread is debugged.

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.