Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting thread name with RaiseException always renames the calling thread, not the target thread. #127

Closed
ravencgg opened this issue Jan 30, 2024 · 5 comments

Comments

@ravencgg
Copy link

#include <Windows.h>

const DWORD MS_VC_EXCEPTION = 0x406D1388;
#pragma pack(push,8)
typedef struct tagTHREADNAME_INFO
{
    DWORD dwType; // Must be 0x1000.
    LPCSTR szName; // Pointer to name (in user addr space).
    DWORD dwThreadID; // Thread ID (-1=caller thread).
    DWORD dwFlags; // Reserved for future use, must be zero.
 } THREADNAME_INFO;
#pragma pack(pop)
void SetThreadName(DWORD dwThreadID, const char* threadName) {
    THREADNAME_INFO info;
    info.dwType = 0x1000;
    info.szName = threadName;
    info.dwThreadID = dwThreadID;
    info.dwFlags = 0;
#pragma warning(push)
#pragma warning(disable: 6320 6322)
    __try{
        RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info);
    }
    __except (EXCEPTION_EXECUTE_HANDLER){
    }
#pragma warning(pop)
}

static DWORD WINAPI ThreadFunc(LPVOID param)
{
    for (int i = 0; i < 10; ++i)
    {
        Sleep(100);
    }
    return 0;
}

int main()
{
    DWORD flags = CREATE_SUSPENDED;
    DWORD stack_size = 0;
    DWORD thread_id = 0;
    HANDLE handle = CreateThread(NULL, stack_size, ThreadFunc, nullptr, flags, &thread_id);

    if (handle)
    {
        SetThreadName(thread_id, "TestThread");
        ResumeThread(handle);
        Sleep(1000);
    }
    return 0;
}

image
image

@ryanfleury
Copy link
Collaborator

Fixed in c700264.

@ravencgg
Copy link
Author

This fixed the issue with the main thread being renamed, but the example still doesn't work. It looks like the thread name can't be set on a thread that was created with CREATE_SUSPENDED until the thread resumes. From a quick look at the code it looks like there may not be a demon entity for the thread yet so the rename fails to find a target.

@ryanfleury
Copy link
Collaborator

That is what I would think is expected behavior, as the debugger has not even received "CreateThread" events for the thread in question yet, but it looks like VS actually handles this case, presumably because they're correlating names to the TID, rather than the thread lifetime (as delimited by the associated create/exit thread events). I'll update the debugger to handle this case as well.

@ryanfleury ryanfleury reopened this Jan 30, 2024
@ryanfleury
Copy link
Collaborator

Fixed in 6e45642.

@ravencgg
Copy link
Author

Looks good, all my PhysX worker threads are now properly named. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants