Summary of multi-threaded program debugging using gdb and summary of multi-threaded program debugging using gdb

Source: Internet
Author: User

Summary of multi-threaded program debugging using gdb and summary of multi-threaded program debugging using gdb
Go to: Summary of using gdb to debug multi-threaded programs

I have never had much access to GDB multi-threaded debugging. Recently, I have had some contact with my work. Let's simply record it. 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 threadsThread 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 also execute at the same time, how can I only execute the program to be debugged? 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 Thread 1073951360 (LWP 12900)]

[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

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 allocated by gdb. This number is used when switching the thread, instead of the green number marked above.

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

  • Thread switching: Use thread THREADNUMBER to switch. THREADNUMBER 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)

These are some basic commands for multi-thread debugging provided by gdb. In addition, gdb provides breakpoint settings for threads and commands for issuing commands to specified or all threads.

When debugging with multiple threads under gdb for the first time, the concept of active threads in gdb is often ignored. Generally, when gdb is used for debugging, only one thread is an active thread. To obtain output from other threads, you must use the thread command to switch to the specified thread, to debug the thread or observe the output results.

 

Gdb Debugging commands:Debug the program with GDB


How can I debug a multi-threaded program under GDB?

You can use the gdb command to debug multiple threads.
In the debugging process,
You can enter the command: thread, which lists the process numbers of multiple threads.
Then you can select the thread to be debugged.
You only need to enter the thread process number.
There is no major difference with single-threaded debugging methods.
View the help command by yourself.
Please be sure to add points.

The name of the function used for gdb debugging is a question mark. Why?

The multi-threaded Program runs for a period of time to generate the core file. When debugging by gdb, it is displayed that a segment error occurs, but the position of the segment error is well located. The result is displayed as follows: [*] Program terminated with signal 11, segmentation fault. [*] #0 0x47e10000 in? () [*] (Gdb) bt [*] #0 0x47e10000 in? () [*] #1 0x47e10000 in? () Backtrace stopped: previous frame identical to this frame (upt stack ?) The dynamic library used by the program has been set through the set solib-absolute-prefix and set solib-search-path commands, and no information such as symbols cannot be found is prompted during gdb. However, the function name has always been a question mark. I don't know why. There is no recursive call in the program. Why is the address of frame 0 and frame 1 the same? In addition to using gdb, is there any other way to locate the error?

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.