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