Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ae16b35d authored by Josh Gao's avatar Josh Gao Committed by android-build-merger
Browse files

Merge "adb: win32: set thread names" am: c8d3853a

am: 24e13fde

Change-Id: I5394f389aa9ef96b468e8c80f10eebae6cce076f
parents 4ad20306 24e13fde
Loading
Loading
Loading
Loading
+1 −6
Original line number Diff line number Diff line
@@ -72,12 +72,7 @@ static __inline__ bool adb_is_separator(char c) {
    return c == '\\' || c == '/';
}

static __inline__ int adb_thread_setname(const std::string& name) {
    // TODO: See https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx for how to set
    // the thread name in Windows. Unfortunately, it only works during debugging, but
    // our build process doesn't generate PDB files needed for debugging.
    return 0;
}
extern int adb_thread_setname(const std::string& name);

static __inline__ void  close_on_exec(int  fd)
{
+23 −0
Original line number Diff line number Diff line
@@ -2742,3 +2742,26 @@ char* adb_getcwd(char* buf, int size) {

    return buf;
}

// The SetThreadDescription API was brought in version 1607 of Windows 10.
typedef HRESULT(WINAPI* SetThreadDescription)(HANDLE hThread, PCWSTR lpThreadDescription);

// Based on PlatformThread::SetName() from
// https://cs.chromium.org/chromium/src/base/threading/platform_thread_win.cc
int adb_thread_setname(const std::string& name) {
    // The SetThreadDescription API works even if no debugger is attached.
    auto set_thread_description_func = reinterpret_cast<SetThreadDescription>(
            ::GetProcAddress(::GetModuleHandleW(L"Kernel32.dll"), "SetThreadDescription"));
    if (set_thread_description_func) {
        std::wstring name_wide;
        if (!android::base::UTF8ToWide(name.c_str(), &name_wide)) {
            return errno;
        }
        set_thread_description_func(::GetCurrentThread(), name_wide.c_str());
    }

    // Don't use the thread naming SEH exception because we're compiled with -fno-exceptions.
    // https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code?view=vs-2017

    return 0;
}