Move libbacktrace off cutils.

There's still <cutils/atomic.h> in a test, but I don't understand why
that isn't just std::atomic.

Also add a shared tgkill wrapper to libbase.

Bug: N/A
Test: ran tests
Change-Id: Idd4baa1e1670a84b3a8f35803cc5ffe5aae008a6
This commit is contained in:
Elliott Hughes 2018-07-11 11:13:16 -07:00
parent 1db3789252
commit 38488907a0
15 changed files with 38 additions and 95 deletions

View file

@ -23,3 +23,8 @@ namespace base {
uint64_t GetThreadId();
}
} // namespace android
#if defined(__GLIBC__)
// bionic has this Linux-specifix call, but glibc doesn't.
extern "C" int tgkill(int tgid, int tid, int sig);
#endif

View file

@ -46,3 +46,9 @@ uint64_t GetThreadId() {
} // namespace base
} // namespace android
#if defined(__GLIBC__)
int tgkill(int tgid, int tid, int sig) {
return syscall(__NR_tgkill, tgid, tid, sig);
}
#endif

View file

@ -42,7 +42,6 @@ libbacktrace_sources = [
"Backtrace.cpp",
"BacktraceCurrent.cpp",
"BacktracePtrace.cpp",
"thread_utils.c",
"ThreadEntry.cpp",
"UnwindStack.cpp",
"UnwindStackMap.cpp",
@ -94,7 +93,6 @@ cc_library {
],
static_libs: [
"libcutils",
"libprocinfo",
],
@ -145,7 +143,6 @@ cc_test {
"backtrace_offline_test.cpp",
"backtrace_test.cpp",
"GetPss.cpp",
"thread_utils.c",
],
cflags: [
@ -159,7 +156,6 @@ cc_test {
"libbacktrace",
"libdexfile",
"libbase",
"libcutils",
"liblog",
"libunwindstack",
],

View file

@ -23,6 +23,7 @@
#include <string>
#include <android-base/stringprintf.h>
#include <android-base/threads.h>
#include <backtrace/Backtrace.h>
#include <backtrace/BacktraceMap.h>
@ -31,7 +32,6 @@
#include "BacktraceLog.h"
#include "UnwindStack.h"
#include "thread_utils.h"
using android::base::StringPrintf;
@ -124,7 +124,7 @@ Backtrace* Backtrace::Create(pid_t pid, pid_t tid, BacktraceMap* map) {
if (pid == BACKTRACE_CURRENT_PROCESS) {
pid = getpid();
if (tid == BACKTRACE_CURRENT_THREAD) {
tid = gettid();
tid = android::base::GetThreadId();
}
} else if (tid == BACKTRACE_CURRENT_THREAD) {
tid = pid;

View file

@ -28,13 +28,13 @@
#include <string>
#include <android-base/threads.h>
#include <backtrace/Backtrace.h>
#include <backtrace/BacktraceMap.h>
#include "BacktraceAsyncSafeLog.h"
#include "BacktraceCurrent.h"
#include "ThreadEntry.h"
#include "thread_utils.h"
bool BacktraceCurrent::ReadWord(uint64_t ptr, word_t* out_value) {
if (!VerifyReadWordArgs(ptr, out_value)) {
@ -76,7 +76,7 @@ bool BacktraceCurrent::Unwind(size_t num_ignore_frames, void* ucontext) {
return UnwindFromContext(num_ignore_frames, ucontext);
}
if (Tid() != gettid()) {
if (Tid() != android::base::GetThreadId()) {
return UnwindThread(num_ignore_frames);
}
@ -114,16 +114,17 @@ class ErrnoRestorer {
static void SignalLogOnly(int, siginfo_t*, void*) {
ErrnoRestorer restore;
BACK_ASYNC_SAFE_LOGE("pid %d, tid %d: Received a spurious signal %d\n", getpid(), gettid(),
THREAD_SIGNAL);
BACK_ASYNC_SAFE_LOGE("pid %d, tid %d: Received a spurious signal %d\n", getpid(),
static_cast<int>(android::base::GetThreadId()), THREAD_SIGNAL);
}
static void SignalHandler(int, siginfo_t*, void* sigcontext) {
ErrnoRestorer restore;
ThreadEntry* entry = ThreadEntry::Get(getpid(), gettid(), false);
ThreadEntry* entry = ThreadEntry::Get(getpid(), android::base::GetThreadId(), false);
if (!entry) {
BACK_ASYNC_SAFE_LOGE("pid %d, tid %d entry not found", getpid(), gettid());
BACK_ASYNC_SAFE_LOGE("pid %d, tid %d entry not found", getpid(),
static_cast<int>(android::base::GetThreadId()));
return;
}

View file

@ -32,8 +32,6 @@
#include <procinfo/process_map.h>
#endif
#include "thread_utils.h"
using android::base::StringPrintf;
std::string backtrace_map_t::Name() const {

View file

@ -28,7 +28,6 @@
#include "BacktraceLog.h"
#include "BacktracePtrace.h"
#include "thread_utils.h"
#if !defined(__APPLE__)
static bool PtraceRead(pid_t tid, uint64_t addr, word_t* out_value) {

View file

@ -23,10 +23,6 @@
#include <set>
#include <string>
#if !defined(__ANDROID__)
#include <cutils/threads.h>
#endif
#include <backtrace/Backtrace.h>
#include <demangle.h>
#include <unwindstack/Elf.h>

View file

@ -27,6 +27,7 @@
#include <string>
#include <android-base/file.h>
#include <android-base/threads.h>
#include <benchmark/benchmark.h>
@ -154,7 +155,7 @@ using BacktraceCreateFn = decltype(Backtrace::Create);
static void CreateBacktrace(benchmark::State& state, BacktraceMap* map, BacktraceCreateFn fn) {
while (state.KeepRunning()) {
std::unique_ptr<Backtrace> backtrace(fn(getpid(), gettid(), map));
std::unique_ptr<Backtrace> backtrace(fn(getpid(), android::base::GetThreadId(), map));
backtrace->Unwind(0);
}
}

View file

@ -31,9 +31,9 @@
#include <android-base/macros.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <android-base/threads.h>
#include <backtrace/Backtrace.h>
#include <backtrace/BacktraceMap.h>
#include <cutils/threads.h>
#include <gtest/gtest.h>
@ -99,7 +99,7 @@ struct OfflineThreadArg {
static void* OfflineThreadFunc(void* arg) {
OfflineThreadArg* fn_arg = reinterpret_cast<OfflineThreadArg*>(arg);
fn_arg->tid = gettid();
fn_arg->tid = android::base::GetThreadId();
test_get_context_and_wait(&fn_arg->ucontext, &fn_arg->exit_flag);
return nullptr;
}

View file

@ -47,16 +47,15 @@
#include <android-base/macros.h>
#include <android-base/stringprintf.h>
#include <android-base/test_utils.h>
#include <android-base/threads.h>
#include <android-base/unique_fd.h>
#include <cutils/atomic.h>
#include <cutils/threads.h>
#include <gtest/gtest.h>
// For the THREAD_SIGNAL definition.
#include "BacktraceCurrent.h"
#include "backtrace_testlib.h"
#include "thread_utils.h"
// Number of microseconds per milliseconds.
#define US_PER_MSEC 1000
@ -525,7 +524,7 @@ TEST(libbacktrace, ptrace_threads) {
}
void VerifyLevelThread(void*) {
std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid()));
std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), android::base::GetThreadId()));
ASSERT_TRUE(backtrace.get() != nullptr);
ASSERT_TRUE(backtrace->Unwind(0));
VERIFY_NO_ERROR(backtrace->GetError().error_code);
@ -538,7 +537,7 @@ TEST(libbacktrace, thread_current_level) {
}
static void VerifyMaxThread(void*) {
std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid()));
std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), android::base::GetThreadId()));
ASSERT_TRUE(backtrace.get() != nullptr);
ASSERT_TRUE(backtrace->Unwind(0));
ASSERT_EQ(BACKTRACE_UNWIND_ERROR_EXCEED_MAX_FRAMES_LIMIT, backtrace->GetError().error_code);
@ -553,7 +552,7 @@ TEST(libbacktrace, thread_current_max) {
static void* ThreadLevelRun(void* data) {
thread_t* thread = reinterpret_cast<thread_t*>(data);
thread->tid = gettid();
thread->tid = android::base::GetThreadId();
EXPECT_NE(test_level_one(1, 2, 3, 4, ThreadSetState, data), 0);
return nullptr;
}
@ -644,7 +643,7 @@ TEST(libbacktrace, thread_ignore_frames) {
static void* ThreadMaxRun(void* data) {
thread_t* thread = reinterpret_cast<thread_t*>(data);
thread->tid = gettid();
thread->tid = android::base::GetThreadId();
EXPECT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, ThreadSetState, data), 0);
return nullptr;
}
@ -994,7 +993,7 @@ static void InitMemory(uint8_t* memory, size_t bytes) {
static void* ThreadReadTest(void* data) {
thread_t* thread_data = reinterpret_cast<thread_t*>(data);
thread_data->tid = gettid();
thread_data->tid = android::base::GetThreadId();
// Create two map pages.
// Mark the second page as not-readable.
@ -1816,7 +1815,8 @@ TEST(libbacktrace, unwind_remote_through_signal_using_action) {
static void TestFrameSkipNumbering(create_func_t create_func, map_create_func_t map_create_func) {
std::unique_ptr<BacktraceMap> map(map_create_func(getpid(), false));
std::unique_ptr<Backtrace> backtrace(create_func(getpid(), gettid(), map.get()));
std::unique_ptr<Backtrace> backtrace(
create_func(getpid(), android::base::GetThreadId(), map.get()));
backtrace->Unwind(1);
ASSERT_NE(0U, backtrace->NumFrames());
ASSERT_EQ(0U, backtrace->GetFrame(0)->num);

View file

@ -1,29 +0,0 @@
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "thread_utils.h"
#if !defined(__BIONIC__)
// glibc doesn't implement or export tgkill.
#include <unistd.h>
#include <sys/syscall.h>
int tgkill(int tgid, int tid, int sig) {
return syscall(__NR_tgkill, tgid, tid, sig);
}
#endif

View file

@ -1,32 +0,0 @@
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _LIBBACKTRACE_THREAD_UTILS_H
#define _LIBBACKTRACE_THREAD_UTILS_H
#include <unistd.h>
#if !defined(__ANDROID__)
#include <cutils/threads.h>
#endif
__BEGIN_DECLS
int tgkill(int tgid, int tid, int sig);
__END_DECLS
#endif /* _LIBBACKTRACE_THREAD_UTILS_H */

View file

@ -32,6 +32,8 @@
#include "ScopedDisableMalloc.h"
#include "ScopedPipe.h"
#include <android-base/threads.h>
using namespace std::chrono_literals;
namespace android {
@ -260,7 +262,7 @@ TEST_F(ThreadCaptureTest, capture_kill) {
ThreadCapture thread_capture(ret, heap);
thread_capture.InjectTestFunc([&](pid_t tid) {
syscall(SYS_tgkill, ret, tid, SIGKILL);
tgkill(ret, tid, SIGKILL);
usleep(10000);
});
auto list_tids = allocator::vector<pid_t>(heap);
@ -319,7 +321,7 @@ TEST_F(ThreadCaptureTest, capture_signal) {
ThreadCapture thread_capture(child, heap);
thread_capture.InjectTestFunc([&](pid_t tid) {
syscall(SYS_tgkill, child, tid, sig);
tgkill(child, tid, sig);
usleep(10000);
});
auto list_tids = allocator::vector<pid_t>(heap);

View file

@ -32,6 +32,7 @@
#include <vector>
#include <android-base/stringprintf.h>
#include <android-base/threads.h>
#include <unwindstack/Maps.h>
#include <unwindstack/Memory.h>
@ -231,8 +232,7 @@ TEST_F(UnwindTest, from_context) {
usleep(1000);
}
ASSERT_NE(0, tid.load());
// Portable tgkill method.
ASSERT_EQ(0, syscall(__NR_tgkill, getpid(), tid.load(), SIGUSR1)) << "Error: " << strerror(errno);
ASSERT_EQ(0, tgkill(getpid(), tid.load(), SIGUSR1)) << "Error: " << strerror(errno);
// Wait for context data.
void* ucontext;