Browse callstack (Call stack)-View Call stack information with debugger scripts

Source: Internet
Author: User
Tags printf

In the previous two articles, I described how to view the call stack in WinDbg (see the Call stack) (i), as well as the calling convention (for details, as described in the thumbnail call stack (invocation stacks) (ii)-the calling convention). Today's blog, on the basis of both, describes how to use the debugger script to observe the call stack. Friends interested in CallStack can develop more detailed scripts to observe callstack information on this basis, and friends interested in debugging can look at the usefulness of Dscript.

Let's take a look at an example, the following program is not a graceful program fragment, but it can help us to illustrate the problem. The program uses a simple recursion, and adds 1 to the sum of the parameter d. In Main, we set D to 10, so that at the breakpoint we get a call stack with a depth of 11.

#include <stdio.h>

int SumToOne(int d, int sum)
{
    sum += d;
    if (d != 1)
        sum = SumToOne(d-1, sum);
    else
        sum = sum; // 这条语句方便设置断点
    return sum;
}

void main()
{
    int sum = SumToOne(10, 0);
    printf("sum=%d", sum);
}

Then, under the current folder, edit the debugger script file DumpStack.txt, which reads

.printf "Dump %d frames\n", ${$arg1}
r $t1=@ebp;
.for (r $t0=1; $t0<=${$arg1}; r $t0=$t0+1)
{
    .printf "frame %d, d=%d sum=%d\n", $t0, poi($t1+8), poi($t1+c)
    r $t1=poi($t1)
}

In WinDbg, run the program to execute the script when the program stops at the breakpoint

$$>a< “dumpStack.txt”a

As shown in the following figure

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.