GDB debugging skills

Source: Internet
Author: User
The second method is undoubtedly much easier than the first method. We only need to add the new compilation parameter-g3 to the global Makefile to support debugging of all macros in the entire project code. Of course, this method also has a disadvantage, that is, the g3 debugging information will be larger than the default g2 debugging information-naturally, otherwise, how does gdb know how to expand the macro

The second method is undoubtedly much easier than the first method. We only need to add the new compilation parameter-g3 to the global Makefile to support debugging of all macros in the entire project code. Of course, this method also has a disadvantage, that is, the g3 debugging information will be larger than the default g2 debugging information-naturally, otherwise, how does gdb know how to expand the macro

The second method is undoubtedly much easier than the first method. We only need to add the new compilation parameter-g3 to the global Makefile to support debugging of all macros in the entire project code. Of course, this method also has a disadvantage, that is, the debugging information of g3 will be larger than the debugging information of the default g2-naturally, how else does gdb know how to expand the macro definition.

2. multi-thread debugging

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 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)

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.