Allow unwinding main thread in ThreadUnwinder.

The check in ThreadUnwinder was overly aggressive, it should only
disallow unwinding itself. Unfortunately, it only disallowed
unwinding the main thread.

Add new unit tests to verify this behavior.

Test: New unit tests pass that verify thread unwinding behavior.

Signed-off-by: Sim Sun <simsun@fb.com>
Change-Id: If75399911f2f98f3155592fc292341d7b9e5e023
diff --git a/libunwindstack/ThreadUnwinder.cpp b/libunwindstack/ThreadUnwinder.cpp
index 9a8a0a6..2bdb910 100644
--- a/libunwindstack/ThreadUnwinder.cpp
+++ b/libunwindstack/ThreadUnwinder.cpp
@@ -149,7 +149,7 @@
                                       const std::vector<std::string>* initial_map_names_to_skip,
                                       const std::vector<std::string>* map_suffixes_to_ignore) {
   ClearErrors();
-  if (tid == pid_) {
+  if (tid == static_cast<pid_t>(android::base::GetThreadId())) {
     last_error_.code = ERROR_UNSUPPORTED;
     return;
   }
diff --git a/libunwindstack/tests/UnwindTest.cpp b/libunwindstack/tests/UnwindTest.cpp
index 80f931b..2ed1b9b 100644
--- a/libunwindstack/tests/UnwindTest.cpp
+++ b/libunwindstack/tests/UnwindTest.cpp
@@ -570,6 +570,28 @@
   EXPECT_EQ(ERROR_UNSUPPORTED, unwinder.LastErrorCode());
 }
 
+TEST_F(UnwindTest, thread_unwind_cur_thread) {
+  std::thread thread([]() {
+    ThreadUnwinder unwinder(512);
+    ASSERT_TRUE(unwinder.Init());
+    unwinder.UnwindWithSignal(SIGRTMIN, android::base::GetThreadId());
+    EXPECT_EQ(0U, unwinder.NumFrames());
+    EXPECT_EQ(ERROR_UNSUPPORTED, unwinder.LastErrorCode());
+  });
+  thread.join();
+}
+
+TEST_F(UnwindTest, thread_unwind_cur_pid_from_thread) {
+  std::thread thread([]() {
+    ThreadUnwinder unwinder(512);
+    ASSERT_TRUE(unwinder.Init());
+    unwinder.UnwindWithSignal(SIGRTMIN, getpid());
+    EXPECT_NE(0U, unwinder.NumFrames());
+    EXPECT_NE(ERROR_UNSUPPORTED, unwinder.LastErrorCode());
+  });
+  thread.join();
+}
+
 static std::thread* CreateUnwindThread(std::atomic_int& tid, ThreadUnwinder& unwinder,
                                        std::atomic_bool& start_unwinding,
                                        std::atomic_int& unwinders) {