Skip to content

Reverse PInvokes do not call TraceCall on non-x86 platforms #47037

@davmason

Description

@davmason

Description

Filed after discussion in #46238 (comment)

There is no call to Debugger::TraceCall for reverse pinvokes on non-x86 platforms. This means when doing mixed mode debugging on x64, if a breakpoint is set in a reverse pinvoked function as the first call on a new thread the breakpoint will not be hit.

Example program:

using System;
using System.Runtime.InteropServices;
using System.Threading;

namespace ReversePInvokeBP
{
    class Program
    {
        [DllImport("PInvokeDLL.dll")]
        private static extern unsafe void PassReversePInvokeAddr(delegate* unmanaged<void> pCalInfoEnumProcExEx);

        [UnmanagedCallersOnly]
        private static void CallMe()
        {
            Console.WriteLine("Here!");
        }

        static unsafe void Main(string[] args)
        {
            PassReversePInvokeAddr(&CallMe);

            Thread.Sleep(Timeout.Infinite);
        }
    }
}
#include "pch.h"
#include <thread>

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

typedef void (*myFunc)(void);

VOID ThreadFunc(void* addr)
{
    myFunc target = (myFunc)addr;
    printf("Calling managed function...\n");
    target();
}

VOID APIENTRY PassReversePInvokeAddr(void *addr)
{
    std::thread thread(ThreadFunc, addr);
    thread.join();
}

On x86 a breakpoint in CallMe will be hit in mixed mode debugging, on x64 it will not. Managed debugging hits the breakpoint for both x86 and x64. Similarly, if the reverse pinvoke is done on the current managed thread it will work for both x86 and x64 even with mixed mode debugging.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions