[VM] Implement GetCurrentThreadName() on Android

TEST=I recorded a trace using the Perfetto recorder on an Android device and ensured that the "DartDev Runner" track name was correct.

Change-Id: I87ef31ff5b4d7817e77536430cf474cf34c22678
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279200
Reviewed-by: Ben Konyi <bkonyi@google.com>
Commit-Queue: Derek Xu <derekx@google.com>
This commit is contained in:
Derek Xu 2023-02-27 21:24:47 +00:00 committed by Commit Queue
parent 67f3e1f4a0
commit 14af4fac49
2 changed files with 14 additions and 9 deletions

View file

@ -11,6 +11,9 @@
#include <sys/resource.h> // NOLINT
#include <sys/syscall.h> // NOLINT
#include <sys/time.h> // NOLINT
#if defined(DART_HOST_OS_ANDROID)
#include <sys/prctl.h>
#endif // defined(DART_HOST_OS_ANDROID)
#include "platform/address_sanitizer.h"
#include "platform/assert.h"
@ -230,16 +233,16 @@ ThreadId OSThread::GetCurrentThreadTraceId() {
#endif // SUPPORT_TIMELINE
char* OSThread::GetCurrentThreadName() {
#if defined(DART_HOST_OS_ANDROID)
// TODO(derekx): |pthread_getname_np| isn't defined on Android, so we need to
// find an alternative solution.
return nullptr;
#elif defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_MACOS)
const intptr_t kNameBufferSize = 16;
char* name = static_cast<char*>(malloc(kNameBufferSize));
#if defined(DART_HOST_OS_ANDROID)
prctl(PR_GET_NAME, name);
#elif defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_MACOS)
pthread_getname_np(pthread_self(), name, kNameBufferSize);
return name;
#endif
return name;
}
ThreadJoinId OSThread::GetCurrentThreadJoinId(OSThread* thread) {

View file

@ -10,6 +10,7 @@
#include <errno.h> // NOLINT
#include <stdio.h>
#include <sys/prctl.h>
#include <sys/resource.h> // NOLINT
#include <sys/time.h> // NOLINT
@ -218,9 +219,10 @@ ThreadId OSThread::GetCurrentThreadTraceId() {
#endif // SUPPORT_TIMELINE
char* OSThread::GetCurrentThreadName() {
// TODO(derekx): |pthread_getname_np| isn't defined on Android, so we need to
// find an alternative solution.
return nullptr;
const intptr_t kNameBufferSize = 16;
char* name = static_cast<char*>(malloc(kNameBufferSize));
prctl(PR_GET_NAME, name);
return name;
}
ThreadJoinId OSThread::GetCurrentThreadJoinId(OSThread* thread) {