mirror of
https://github.com/chromium/crashpad.git
synced 2025-03-09 14:06:33 +00:00
[win] Fix ScopedSetThreadName for Windows 7
Windows 7 doesn't support SetThreadDescription/GetThreadDescription. Add an IsSupported to ScopedSetThreadName test to wrap unsupported calls. Change-Id: I70d4e20b94efea03e41c5f7ed8d8e1b886192923 Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/3722556 Commit-Queue: Justin Cohen <justincohen@chromium.org> Reviewed-by: Mark Mentovai <mark@chromium.org>
This commit is contained in:
parent
1c37daa5ac
commit
80f383327e
@ -123,7 +123,9 @@ TEST(ProcessReaderWin, SelfOneThread) {
|
||||
ASSERT_GE(threads.size(), 1u);
|
||||
|
||||
EXPECT_EQ(threads[0].id, GetCurrentThreadId());
|
||||
EXPECT_EQ(threads[0].name, "SelfBasic");
|
||||
if (ScopedSetThreadName::IsSupported()) {
|
||||
EXPECT_EQ(threads[0].name, "SelfBasic");
|
||||
}
|
||||
EXPECT_NE(ProgramCounterFromCONTEXT(threads[0].context.context<CONTEXT>()),
|
||||
nullptr);
|
||||
EXPECT_EQ(threads[0].suspend_count, 0u);
|
||||
@ -174,28 +176,31 @@ class ProcessReaderChildThreadSuspendCount final : public WinMultiprocess {
|
||||
|
||||
const auto& threads = process_reader.Threads();
|
||||
ASSERT_GE(threads.size(), kCreatedThreads + 1);
|
||||
EXPECT_EQ(threads[0].name, "WinMultiprocessChild-Main");
|
||||
|
||||
const std::set<std::string> expected_thread_names = {
|
||||
"WinMultiprocessChild-1",
|
||||
"WinMultiprocessChild-2",
|
||||
"WinMultiprocessChild-3",
|
||||
};
|
||||
// Windows can create threads besides the ones created in
|
||||
// WinMultiprocessChild(), so keep track of the (non-main) thread names
|
||||
// and make sure all the expected names are present.
|
||||
std::set<std::string> thread_names;
|
||||
for (size_t i = 1; i < threads.size(); i++) {
|
||||
if (!threads[i].name.empty()) {
|
||||
thread_names.emplace(threads[i].name);
|
||||
}
|
||||
}
|
||||
|
||||
EXPECT_THAT(thread_names, IsSupersetOf(expected_thread_names));
|
||||
|
||||
for (const auto& thread : threads) {
|
||||
EXPECT_EQ(thread.suspend_count, 0u);
|
||||
}
|
||||
|
||||
if (ScopedSetThreadName::IsSupported()) {
|
||||
EXPECT_EQ(threads[0].name, "WinMultiprocessChild-Main");
|
||||
|
||||
const std::set<std::string> expected_thread_names = {
|
||||
"WinMultiprocessChild-1",
|
||||
"WinMultiprocessChild-2",
|
||||
"WinMultiprocessChild-3",
|
||||
};
|
||||
// Windows can create threads besides the ones created in
|
||||
// WinMultiprocessChild(), so keep track of the (non-main) thread names
|
||||
// and make sure all the expected names are present.
|
||||
std::set<std::string> thread_names;
|
||||
for (size_t i = 1; i < threads.size(); i++) {
|
||||
if (!threads[i].name.empty()) {
|
||||
thread_names.emplace(threads[i].name);
|
||||
}
|
||||
}
|
||||
|
||||
EXPECT_THAT(thread_names, IsSupersetOf(expected_thread_names));
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
|
@ -32,9 +32,14 @@ class ScopedSetThreadName final {
|
||||
|
||||
~ScopedSetThreadName();
|
||||
|
||||
#if BUILDFLAG(IS_WIN) || DOXYGEN
|
||||
//! \brief Returns `true` if Windows supports setting and getting thread name.
|
||||
static bool IsSupported();
|
||||
#endif
|
||||
|
||||
private:
|
||||
#if BUILDFLAG(IS_WIN)
|
||||
const std::wstring original_name_;
|
||||
std::wstring original_name_;
|
||||
#else
|
||||
const std::string original_name_;
|
||||
#endif
|
||||
|
@ -19,6 +19,7 @@
|
||||
#include "base/check.h"
|
||||
#include "base/logging.h"
|
||||
#include "base/strings/utf_string_conversions.h"
|
||||
#include "util/win/get_function.h"
|
||||
#include "util/win/scoped_local_alloc.h"
|
||||
|
||||
namespace crashpad {
|
||||
@ -26,30 +27,57 @@ namespace test {
|
||||
|
||||
namespace {
|
||||
|
||||
auto GetThreadDescriptionFuncPtr() {
|
||||
static const auto get_thread_description =
|
||||
GET_FUNCTION(L"kernel32.dll", ::GetThreadDescription);
|
||||
return get_thread_description;
|
||||
}
|
||||
|
||||
auto SetThreadDescriptionFuncPtr() {
|
||||
static const auto set_thread_description =
|
||||
GET_FUNCTION(L"kernel32.dll", ::SetThreadDescription);
|
||||
return set_thread_description;
|
||||
}
|
||||
|
||||
std::wstring GetCurrentThreadName() {
|
||||
wchar_t* thread_description;
|
||||
HRESULT hr = GetThreadDescription(GetCurrentThread(), &thread_description);
|
||||
const auto get_thread_description = GetThreadDescriptionFuncPtr();
|
||||
DCHECK(get_thread_description);
|
||||
HRESULT hr = get_thread_description(GetCurrentThread(), &thread_description);
|
||||
CHECK(SUCCEEDED(hr)) << "GetThreadDescription: "
|
||||
<< logging::SystemErrorCodeToString(hr);
|
||||
ScopedLocalAlloc thread_description_owner(thread_description);
|
||||
return std::wstring(thread_description);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
ScopedSetThreadName::ScopedSetThreadName(const std::string& new_thread_name)
|
||||
: original_name_(GetCurrentThreadName()) {
|
||||
const std::wstring wnew_thread_name = base::UTF8ToWide(new_thread_name);
|
||||
void SetCurrentThreadName(const std::wstring& new_thread_name) {
|
||||
const auto set_thread_description = SetThreadDescriptionFuncPtr();
|
||||
DCHECK(set_thread_description);
|
||||
HRESULT hr =
|
||||
SetThreadDescription(GetCurrentThread(), wnew_thread_name.c_str());
|
||||
set_thread_description(GetCurrentThread(), new_thread_name.c_str());
|
||||
CHECK(SUCCEEDED(hr)) << "SetThreadDescription: "
|
||||
<< logging::SystemErrorCodeToString(hr);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
ScopedSetThreadName::ScopedSetThreadName(const std::string& new_thread_name)
|
||||
: original_name_() {
|
||||
if (IsSupported()) {
|
||||
original_name_.assign(GetCurrentThreadName());
|
||||
SetCurrentThreadName(base::UTF8ToWide(new_thread_name));
|
||||
}
|
||||
}
|
||||
|
||||
ScopedSetThreadName::~ScopedSetThreadName() {
|
||||
HRESULT hr = SetThreadDescription(GetCurrentThread(), original_name_.c_str());
|
||||
CHECK(SUCCEEDED(hr)) << "SetThreadDescription: "
|
||||
<< logging::SystemErrorCodeToString(hr);
|
||||
if (IsSupported()) {
|
||||
SetCurrentThreadName(original_name_);
|
||||
}
|
||||
}
|
||||
|
||||
// static
|
||||
bool ScopedSetThreadName::IsSupported() {
|
||||
return GetThreadDescriptionFuncPtr() && SetThreadDescriptionFuncPtr();
|
||||
}
|
||||
|
||||
} // namespace test
|
||||
|
Loading…
x
Reference in New Issue
Block a user