Several functions about C/C ++ program debugging

Source: Internet
Author: User

From: http://blog.sina.com.cn/s/blog_9f4f649d010163sa.html

1. Call Stack Series
The following is a function prototype:

1234
 # Include "execinfo. H"  Int Backtrace (  Void   ** Buffer, Int Size )  ;  Char   ** Backtrace_symbols (  Void   * Const   * Buffer, Int Size )  ;  Void Backtrace_symbols_fd (  Void   *  Const   * Buffer, Int Size, Int FD )  ; 

Next, we will introduce the above three functions:
(1) backtrace is used to obtain the currentProgramStack to store the results in the buffer. Generally, we use GDB to debug the program and set the appropriate breakpoint.
The backtrace (BT) command shows the current call stack. However, sometimes, when conditional breakpoints are used, the gdb function is not as powerful as the program itself.
The backtrace function is called in the program to obtain the call stack.

(2) backtrace_symbols converts the call stack obtained by backtrace into a string array and returns it as a string array. You need to release the memory occupied by the returned string array outside.

(3) backtrace_symbols_fd writes the call stack information obtained by backtrace to the file specified by FD.

1
 Void *_ Builtin_return_address(Unsigned IntLevel)

This function is used to obtain the current function, or the return address of the function that calls it. After obtaining this address, you can obtain information about the called function through GDB disassembly, this is also a way to obtain the call stack in the application.

2. Memory Allocation and release series

12
# Include "malloc. H"Size_tMalloc_usable_size((Void *_ PTR));

This function is used to return the size of the available memory actually allocated after calling malloc. We all know that in C ++, operator new () can overload a variety of versions and pass in the relevant information during the call to track memory allocation, but operator Delete () however, there are only two forms and there is no option to reload them at will, especially the Global operator Delete (). There is only one version, which makes it quite painful. How much memory is actually released? At this time, the malloc_usable_size () function is useful. You can call it to obtain the size of the memory currently released. Note that if malloc_usable_size is used in Delete to calculate the size of the released memory, use it in new to calculate the opened memory. When calling malloc, the actual memory allocated is much larger than the one applied by the user. If the statistical methods on both sides do not match, the statistical results are also highly deterministic.

I will not describe the new/delete overload details here. I have written an article about , if you do not understand, you can read this article elaborate on new and delete in C ++ .

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.