One-step write algorithm (function stack display)

Source: Internet
Author: User

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

Before continuing with the graph discussion, let's discuss the basic principles of function stack today. Anyone who has programming experience knows that stack debugging is based onProgramA feature that is often used during development. Have you ever wondered how function stacks started? In fact, we can write a function stack output function for analysis.

In general, the function's pressure stack process is as follows:

| Parameter 3 |

| Parameter 2 |

| Parameter 1 |

| Address |

| EBP |

| Variable | <--------------------------------- EBP

How is the content in the stack printed?

 
Void stack_print () {int var_ebp ;__ ASM mov var_ebp, EBP; printf ("% 08x \ n", * (int *) (var_ebp) + 1 ));}

The aboveCodeOnly print the return address of the current function. What if it is continuous function printing? Print it to the main function.

 
Void stack_print () {int var_ebp ;__ ASM mov var_ebp, EBP; do {printf ("% 08x \ n", * (int *) (var_ebp) + 1 )); var_ebp = * (int *) (var_ebp);} while (var_ebp! = 0x0 );}

The code above shows a pair of addresses, so how can we match these addresses with function names, then we can only look up the table. Where is the table corresponding to the function? Don't worry. After reading one of the following, my friends will understand what's going on.

In this way, you can select generate mapfile during VC compilation to generate the *. Map File. The file contains the starting address of the main functions in the current file, which are sorted in ascending order. So as long as you find the start point of the corresponding function and determine whether the return address of our function is in the middle of the function, you can find the corresponding function name.

Summary:

(1) Today I summarized the basic principles of function stack display;

(2) knowing the basic principles of a function helps us understand many problems in essence. Even if many CPUs have different processing methods than x86, we can quickly master them using similar methods;

(3) The stack principle is very important. You should take a good look at it.

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.