[test] Fix test build failures in Chromium

Importing Crashpad into Chromium revealed a few build failures:

1) The MSVC compiler needed assistance constructing SleepingThreads

2) scoped_set_thread_name_posix.cc did not build on Android, where
   BUILDFLAG(IS_LINUX) is not defined and __ANDROID_API__ must be
   set to 24 or higher to use pthread_getname_np()

This fixes the build failures, which I tested with a Chromium CQ
dry-run:

https://crrev.com/c/3703491

Change-Id: Ibde7cacaa45d384272890ea9b1ee2d707048ab03
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/3703446
Commit-Queue: Mark Mentovai <mark@chromium.org>
Reviewed-by: Joshua Peraza <jperaza@chromium.org>
This commit is contained in:
Mark Mentovai 2022-06-14 19:00:14 -04:00 committed by Crashpad LUCI CQ
parent 02bdf8f9d7
commit 3ae34b169b
2 changed files with 24 additions and 13 deletions

View File

@ -145,7 +145,7 @@ class ProcessReaderChildThreadSuspendCount final : public WinMultiprocess {
class SleepingThread : public Thread {
public:
SleepingThread(const std::string& thread_name)
explicit SleepingThread(const std::string& thread_name)
: done_(nullptr), thread_name_(thread_name) {}
void SetHandle(Semaphore* done) {
@ -222,9 +222,9 @@ class ProcessReaderChildThreadSuspendCount final : public WinMultiprocess {
// Create three dummy threads so we can confirm we read successfully read
// more than just the main thread.
std::array<SleepingThread, kCreatedThreads> threads = {
"WinMultiprocessChild-1",
"WinMultiprocessChild-2",
"WinMultiprocessChild-3",
SleepingThread(std::string("WinMultiprocessChild-1")),
SleepingThread(std::string("WinMultiprocessChild-2")),
SleepingThread(std::string("WinMultiprocessChild-3")),
};
Semaphore done(0);
for (auto& thread : threads)

View File

@ -20,10 +20,13 @@
#include <string>
#include "base/check.h"
#include "base/check_op.h"
#include "build/build_config.h"
#if BUILDFLAG(IS_APPLE)
#include <mach/thread_info.h>
#elif BUILDFLAG(IS_ANDROID)
#include <sys/prctl.h>
#endif
namespace crashpad {
@ -31,36 +34,44 @@ namespace test {
namespace {
#if BUILDFLAG(IS_LINUX)
#if BUILDFLAG(IS_APPLE)
constexpr size_t kPthreadNameMaxLen = MAXTHREADNAMESIZE;
#elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS)
// The kernel headers define this in linux/sched.h as TASK_COMM_LEN, but the
// userspace copy of that header does not define it.
constexpr size_t kPthreadNameMaxLen = 16;
#elif BUILDFLAG(IS_APPLE)
constexpr size_t kPthreadNameMaxLen = MAXTHREADNAMESIZE;
#else
#error Port to your platform
#endif
void SetCurrentThreadName(const std::string& thread_name) {
#if BUILDFLAG(IS_LINUX)
PCHECK((errno = pthread_setname_np(pthread_self(), thread_name.c_str())) == 0)
<< "pthread_setname_np";
#elif BUILDFLAG(IS_APPLE)
#if BUILDFLAG(IS_APPLE)
// Apple's pthread_setname_np() sets errno instead of returning it.
PCHECK(pthread_setname_np(thread_name.c_str()) == 0) << "pthread_setname_np";
#elif BUILDFLAG(IS_ANDROID) && __ANDROID_API__ < 24
// pthread_setname_np() requires Android API 24 or later.
CHECK_LT(thread_name.length(), kPthreadNameMaxLen);
PCHECK(prctl(PR_SET_NAME, thread_name.c_str()) == 0) << "prctl(PR_SET_NAME)";
#else
#error Port to your platform
PCHECK((errno = pthread_setname_np(pthread_self(), thread_name.c_str())) == 0)
<< "pthread_setname_np";
#endif
}
std::string GetCurrentThreadName() {
std::string result(kPthreadNameMaxLen, '\0');
#if BUILDFLAG(IS_ANDROID) && __ANDROID_API__ < 24
static constexpr char kGetThreadNameFunctionName[] = "prctl";
PCHECK(prctl(PR_GET_NAME, result.data()) == 0) << "prctl(PR_GET_NAME)";
#else
static constexpr char kGetThreadNameFunctionName[] = "pthread_getname_np";
PCHECK((errno = pthread_getname_np(
pthread_self(), result.data(), result.length())) == 0)
<< "pthread_getname_np";
#endif
const auto result_nul_idx = result.find('\0');
CHECK(result_nul_idx != std::string::npos)
<< "pthread_getname_np did not NUL terminate";
<< kGetThreadNameFunctionName << " did not NUL terminate";
result.resize(result_nul_idx);
return result;
}