I am in the process of studying the book Inside Windows Debugging
and cannot fully understand the technique used in the chapter Listing parameters and Locals for System Code
. As the name suggests, I'm trying to get a list of arguments passed to some function. An example of a stack:
0:012:x86> k
*** Stack trace for last set context - .thread/.cxr resets it
# ChildEBP RetAddr
WARNING: Frame IP not in any known module. Following frames may be wrong.
0e 053eecec 04561d6e 0x4562f2e
0f 053eed30 04561c52 0x4561d6e
10 053eed54 72617118 0x4561c52
11 053eed60 72616cc0 mscorlib_ni!System.Threading.Tasks.Task.InnerInvoke()$##6003FA0+0x28
12 053eed84 726170ea mscorlib_ni!System.Threading.Tasks.Task.Execute()$##6003F91+0x30
13 053eedec 72633fd6 mscorlib_ni!System.Threading.Tasks.Task.ExecutionContextCallback(System.Object)$##6003F9F+0x1a
14 053eee00 72616f68 mscorlib_ni!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)$##6003AD3+0x16
15 053eee6c 72616e72 mscorlib_ni!System.Threading.Tasks.Task.ExecuteWithThreadLocal(System.Threading.Tasks.Task ByRef)$##6003F9E+0xd8
16 053eee7c 7268ac9c mscorlib_ni!System.Threading.Tasks.Task.ExecuteEntry(Boolean)$##6003F9D+0xb2
17 053eee8c 726340c5 mscorlib_ni!System.Threading.Tasks.ThreadPoolTaskScheduler.LongRunningThreadWork(System.Object)$##60040E8+0x1c
18 053eeef0 72633fd6 mscorlib_ni!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)$##6003AD4+0xe5
19 053eef04 72633f91 mscorlib_ni!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)$##6003AD3+0x16
1a 053eef20 72688c8e mscorlib_ni!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)$##6003AD2+0x41
1b 053eef38 736eebf6 mscorlib_ni!System.Threading.ThreadHelper.ThreadStart(System.Object)$##6003BE3+0x4e
In general, I have a full stack, but nevertheless I would like to be able to work with the output of commands like k
:
0:012:x86> !clrstack
OS Thread Id: 0x2518 (12)
Child SP IP Call Site
053eecac 04562f2e Namespace.ProcessingManager.B(System.String, System.Threading.CancellationToken, System.Collections.Generic.List`1, Int32)
053eed00 04561d6e Namespace.ProcessingManager.A(System.String, System.Threading.CancellationToken, Int32)
053eed40 04561c52 Namespace.ProcessingManager+c__DisplayClass25_0.b__0()
053eed5c 72617118 System.Threading.Tasks.Task.InnerInvoke()
053eed68 72616cc0 System.Threading.Tasks.Task.Execute()
053eed8c 726170ea System.Threading.Tasks.Task.ExecutionContextCallback(System.Object)
053eed90 726340c5 System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
053eedfc 72633fd6 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
053eee10 72616f68 System.Threading.Tasks.Task.ExecuteWithThreadLocal(System.Threading.Tasks.Task ByRef)
053eee74 72616e72 System.Threading.Tasks.Task.ExecuteEntry(Boolean)
053eee84 7268ac9c System.Threading.Tasks.ThreadPoolTaskScheduler.LongRunningThreadWork(System.Object)
053eee88 726070e3 System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
053eee94 726340c5 System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
053eef00 72633fd6 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
053eef14 72633f91 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
053eef2c 72688c8e System.Threading.ThreadHelper.ThreadStart(System.Object)
I am interested in the arguments of method B
having the following signature:
void B (string code, CancellationToken token, List<SomeObj> items, int taskCount)
My research is conducted on the assumption that .NET
uses the __stdcall calling convention:
0:012:x86> dd 053eecec
053eecec 053eed30 04561d6e 00000002 1259e288
053eecfc 0163dae4 12592918 01698868 01639394
053eed0c 01639394 015fe3b4 00000000 00000000
053eed1c 00000000 00000000 053eed30 01791aa4
053eed2c 01791abc 053eed54 04561c52 00000005
053eed3c 0163dae4 016da0e4 0163dae4 0167402c
053eed4c 01791b04 053eedc8 053eed60 72617118
053eed5c 01791b04 053eed84 72616cc0 0163db2c
053eed30
: Saved EBP
(Previous Frame Pointer - A).
04561d6e
: RetAddr
points to the next piece of actual code.
00000002
: Arg: taskCount (I suppose that values like 00000002
represent values of primitive types).
1259e288
: Arg: items (Checked via !do 1259e288
).
0163dae4
: Arg: token (Checked via !do 0163dae4
).
12592918
: It was assumed that the given address will contain the string code
argument, but commands like !do
, du
do not return anything. In general, after this value, string code
(01639394
) is found, but it seems to refer to the previous method, because the value at 01698868
creates at method A
.
How do I get the first argument?
UPD
From the answer mentioned in the comments:
__clrcall is the calling convention for managed code. It is a blend of the other ones, this pointer passing like __thiscall, optimized argument passing like __fastcall, argument order like __cdecl and caller cleanup like __stdcall.
From __cdecl description:
Argument-passing order: Right to left. Stack-maintenance responsibility: Calling function pops the arguments from the stack.
This is similar to what I got.
0:012:x86> .frame /c /r 0e
0e 053eecec 04561d6e 0x4562f2e
eax=00000000 ebx=19223de4 ecx=00000000 edx=00000000 esi=0000000a edi=0000000a
eip=04562f2e esp=053eeca8 ebp=053eecec iopl=0 nv up ei pl nz ac po nc
cs=0023 ss=002b ds=002b es=002b fs=0053 gs=002b efl=00000212
04562f2e eb75 jmp 04562fa5
From __fastcall description:
Argument-passing order: The first two DWORD or smaller arguments that are found in the argument list from left to right are passed in ECX and EDX registers; all other arguments are passed on the stack from right to left. Stack-maintenance responsibility: Called function pops the arguments from the stack.
eip
, esp
, ebp
are "service" registers, as I understand it, parameters are not passed through them. ebx
?