Linux system programming @ Multi-threaded and multi-process GDB debugging

Source: Internet
Author: User

Blog Content reference from

Http://www.cnblogs.com/xuxm2007/archive/2011/04/01/2002162.html

http://blog.csdn.net/pbymw8iwm/article/details/7876797

GDB Manual (Debug multiple program chapters + Debug multithreaded process chapters)

GDB multithreaded debugging

Basic commands

Info Threads displays all threads currently available for debugging, each of which has a GDB-assigned ID, which is used when the thread is later manipulated. Previous * is the thread that is currently being debugged.

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] to have one or more threads execute the GDB command.

thread apply all [command] lets all the debugged threads execute GDB command commands.

The set scheduler-locking off|on|step estimates 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.

Set Print thread-events on | Off to print messages at the beginning and end of the control thread         

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)

The following is directly in your thread function to set a breakpoint, and then continue to that breakpoint, generally multi-threaded, because it is running at the same time, it is best to set scheduler-locking on

In this case, only the current thread is debugged

GDB Multi-process debugging

By default, GDB only debugs the main process when debugging a multi-process program.

GDB can debug multiple programs at the same time. You only need to set Follow-fork-mode (default: Parent) and Detach-on-fork (default: On).

follow-fork-mode detach-on-fork Descriptionparent on Debug main process only (gdb default)
Child on only debugging sub-processes
parent off simultaneously debug two processes, GDB with main process, sub-process block at fork position
child off simultaneously debug two processes, GDB and subprocess, main process block at fork positionSetting Method: Set Follow-fork-mode [Parent|child] Set detach-on-fork [On|off]querying the process being debugged: info inferiors
Switch Debug process: inferior <infer number>
Add a new debug process: Add-inferior [-copies n] [-exec executable], you can use the file executable to assign to the inferior executable.
Others: Remove-inferiors infno, detach inferior print on the gdb The list of sub-processes that are created under control. This table includes fork ID, process id and the bits of the current process fork Fork-id Span style= "font-family: Song body, Sans-serif;" > switch to fork-id the specified process. Parameter fork-id is gdb internal is fork assigned, as command ' info Forks " The first of the displayed list column. process Process-id switch to the specified process. Parameter process-id must be ' info forks Detach Fork Fork-id detach a process specified by the fork-id identified by GDB , and then from the the fork list is deleted. This process will be allowed to continue running independently. Delete Fork Fork-idkill one byGDBidentified byFork-idthe specified process, and then from theForkThe list is deleted. If you want to debug avforkCreate nextexecthe process of the word,GDBwill be executed on this new target . a breakpoint. If you are in the original programA breakpoint is set on the main function of the child process, and the same breakpoint is on the main function on the subprocess. If the child process is executingvforkcalled, you cannot debug a child process or parent process. If you are inexecrun after call executionGDBof theRuncommand, the new target will be restarted. To restart the parent process, Runfilecommand, parent process executable programA masterpiece parameter. You can useCatchcommand to be infork,vforkorexeccall the time to letGDBinterrupts.

Linux system programming @ Multi-threaded and multi-process GDB debugging

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.