Skip to content

Commit

Permalink
Fix thread names for android traces
Browse files Browse the repository at this point in the history
Summary: Collecting traces from Android devices do not correctly show thread names for threads manually created outside of the JVM.   This change will correctly set the Java thread name to match the posix thread name.

Reviewed By: dreiss

Differential Revision: D20293251

fbshipit-source-id: 35b95c653ff1fd79e339dd2a854be8aaaa604ce2
  • Loading branch information
JoshingtonState authored and facebook-github-bot committed Mar 10, 2020
1 parent ae9f096 commit cad2e1c
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions first-party/fbjni/cxx/fbjni/detail/Environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

#include <fbjni/fbjni.h>

#ifdef __ANDROID__
#include <sys/prctl.h>
#endif // __ANDROID__

#include <functional>
#ifndef _WIN32
#include <pthread.h>
Expand Down Expand Up @@ -65,8 +69,33 @@ struct AttachTraits<jint(JavaVM::*)(void**, void*)> {
using EnvType = void*;
};

std::string getThreadName() {
#ifdef _WIN32
return "";
#else // _WIN32
constexpr int kMaxThreadNameSize = 100;
int ret = 0;
char threadName[kMaxThreadNameSize];
#ifdef __ANDROID__
ret = prctl(PR_GET_NAME, threadName);
#else
ret = pthread_getname_np(pthread_self(), threadName, sizeof(threadName));
#endif
if (ret != 0) {
return "";
}
return threadName;
#endif // _WIN32
}

JNIEnv* attachCurrentThread() {
JavaVMAttachArgs args{JNI_VERSION_1_6, nullptr, nullptr};

const auto threadName = getThreadName();
if (threadName.size()) {
args.name = threadName.c_str();
}

using AttachEnvType =
typename AttachTraits<decltype(&JavaVM::AttachCurrentThread)>::EnvType;
AttachEnvType env;
Expand Down

0 comments on commit cad2e1c

Please sign in to comment.