summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSpencer Low <CompareAndSwap@gmail.com>2018-09-03 16:03:22 -0700
committerSpencer Low <CompareAndSwap@gmail.com>2018-09-03 16:11:33 -0700
commit50beee3a2c1c552b6e87657f87f35ff95764cdb2 (patch)
treee8337a780713e47319b9a9e3d1e96e4ac87165d3
parent640ceee567c7f7e3832f9de189580fd3e138e951 (diff)
downloadsystem_core-50beee3a2c1c552b6e87657f87f35ff95764cdb2.tar.gz
system_core-50beee3a2c1c552b6e87657f87f35ff95764cdb2.tar.bz2
system_core-50beee3a2c1c552b6e87657f87f35ff95764cdb2.zip
adb: win32: set thread names
Use a new Windows 10 API, SetThreadDescription(). Background info from a Chrome developer: https://randomascii.wordpress.com/2015/10/26/thread-naming-in-windows-time-for-something-better/ Test: Win10, Vista, adb shell Test: Ran windbg -pn adb.exe, used .dump /ma to create a dump, ran `dx -g @$curprocess.Threads' on the dump and it showed the thread names. Change-Id: I14ea7121605cb3fa45ce7b59e2ba5882a215b59f Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
-rw-r--r--adb/sysdeps.h7
-rw-r--r--adb/sysdeps_win32.cpp23
2 files changed, 24 insertions, 6 deletions
diff --git a/adb/sysdeps.h b/adb/sysdeps.h
index 0c2e45cb4..08fe02934 100644
--- a/adb/sysdeps.h
+++ b/adb/sysdeps.h
@@ -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)
{
diff --git a/adb/sysdeps_win32.cpp b/adb/sysdeps_win32.cpp
index c94d13f96..026dd1c0d 100644
--- a/adb/sysdeps_win32.cpp
+++ b/adb/sysdeps_win32.cpp
@@ -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;
+}