View gdb common Debugging commands and multi-thread stacks

Source: Internet
Author: User

View gdb common Debugging commands and multi-thread stacks

GDB is a powerful UNIX program debugging tool released by the GNU open-source organization. Maybe you prefer the GUI, such as VC, BCB, and other IDE debugging methods. But if you are running software on a UNIX platform, you will find that the GDB debugging tool has more powerful functions than the visual debugger of VC and BCB. This is the so-called "have an inch, have a small size and have a short size.

Generally, GDB mainly debugs C/C ++ programs. To debug a C/C ++ program, we must add the debugging information to the executable file during compilation. This can be done using the-g parameter of the compiler (cc/gcc/g ++. For example:

> Cc-g hello. c-o hello
> G ++-g hello. cpp-o hello

Gdb + program name running (gdb main)

You can use the "-- tui" parameter to display a source code display window in the upper part of the terminal window.

Gdb -- tui main

List (l) view the source code of the last 10 lines

List fun view fun function source code

List file: view the fun function source code in the file.

List num1 num2 view num1 ~ Num2 source code

Break row number

Break fun sets breakpoints in Functions

Break file: row number

Break file: fun

Break if When conditions are met, the program stops.

Info break (I B) view breakpoints

Watch expr when the value of expr changes, the program stops

Delete n delete a breakpoint

Run/r start the program

Continue (c) runs to the next breakpoint

Step (s) One-step tracking, entering the Function

Next (n) single-step tracking without entering the Function

Finish running the program until the current function finishes returning, and print the stack address, return value, and parameter information when the function returns.

Until run the program until the program exits the loop body

Print I (p I) view the variable value

Ptype

Print array

Print [email protected] view dynamic memory

Print x = 5 Change the runtime data

Print & array: view the address of the array

Enter represents the previous command

Core File

When the program crashes, a file is usually generated, called a core file. The core file records the memory image when the program crashes and adds debugging information. The core file generation process is called core dump.

Set to generate a core File

Ulimit-c check the core-dump status

Ulimit-c Number

Ulimit-c unlimited

Gdb uses the core file for debugging

Gdb file name core File

Backtrace (bt) view Stack

 

Multi-thread debugging

Info threads displays all threads currently debuggable

Thread ID: Switch the thread currently being debugged to the thread with the specified ID

Attach process-id in gdb status, start to debug a running process

Thread apply all command all threads Execute command

Next we will give you a fun multi-threaded program. The answer is in the stack of a thread. Instead of looking at the answer below, we will try to use gdb (Linux 64bit ).

Http://download.csdn.net/detail/nk_test/9452236

The answer is shown in the following figure ..


The answer is orangutan.

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.