Step through the “managed to native transition” in

2020-06-01 19:50发布

While trying to answer this question I decided I need to step through the marshalling process by hand, in the debugger view.

Unfortunately, Visual Studio seems to jump right over all this interesting code. Here's the P/Invoke call to GetProfilesDirectory (a WinAPI function):

enter image description here

but after stepping into this call (F11), I don't find myself at FFF9BFD8; rather, I land right in the GetProfilesDirectory code:

enter image description here

There's also the [Managed to Native Transition] entry on the stack, hinting at the fact that Visual Studio had just jumped over a whole bunch of code.

How can I step through this transition?

2条回答
来,给爷笑一个
2楼-- · 2020-06-01 20:00

In Visual Studio, you cannot do that, i.e. there is no way to switch VS debugger into a mode to see what is going on during Managed to Native transition.

You could try WinDbg + SOS/SOSEX instead as WinDBG does not hide these kinds of details from you. However, that part is either .NET or Windows internal abstraction, so even if you could see it, you might not get much, as to see what's actually going on there (besides the assembly), you will need private symbols for mscorlib, mscoreei, or clr dlls (pdb files).

查看更多
你好瞎i
3楼-- · 2020-06-01 20:23

I'm surprised that no one has suggested that you show the external code in the debug window. If you right click on the [Native to Managed Transition] line and tick the Show External Code option, you will see exactly which methods are being called in the transition. This may give you a better idea. Here is an example:

Display Native to Managed Transition

You will then be able to double click on any (greyed out) line and a Disassembly page will open with the code. Another example:

enter image description here

查看更多
登录 后发表回答