Avoid use of pthread_getschedparam (except on the Mac)

Squashed commit of two cherry-picks:

    During Thread::DumpState the pthread may have exited.
    We have the tid, use that instead.

    Test: m -j50 test-art-host -k
    Test: adb shell kill -3 `pid system_server`
    Bug: 36445592

    (cherry picked from commit 08e762782a1e352c329945e0df3f35da7e71ad8a)

    Restore broken code for the Mac

    Test: m -j48
    Bug: 36445592

    (cherry picked from commit 4b16f0b860f4d59601706305b2949b98db586a47)

Test: m -j48 && flashall && m -j8 test-art-host
Bug: 36445592
Change-Id: I96d51614f569338d85de0fd59188445df3289dfd
diff --git a/runtime/thread.cc b/runtime/thread.cc
index 30a4046..008c388 100644
--- a/runtime/thread.cc
+++ b/runtime/thread.cc
@@ -16,6 +16,10 @@
 
 #include "thread.h"
 
+#if !defined(__APPLE__)
+#include <sched.h>
+#endif
+
 #include <pthread.h>
 #include <signal.h>
 #include <sys/resource.h>
@@ -1591,8 +1595,21 @@
   if (thread != nullptr) {
     int policy;
     sched_param sp;
+#if !defined(__APPLE__)
+    // b/36445592 Don't use pthread_getschedparam since pthread may have exited.
+    policy = sched_getscheduler(tid);
+    if (policy == -1) {
+      PLOG(WARNING) << "sched_getscheduler(" << tid << ")";
+    }
+    int sched_getparam_result = sched_getparam(tid, &sp);
+    if (sched_getparam_result == -1) {
+      PLOG(WARNING) << "sched_getparam(" << tid << ", &sp)";
+      sp.sched_priority = -1;
+    }
+#else
     CHECK_PTHREAD_CALL(pthread_getschedparam, (thread->tlsPtr_.pthread_self, &policy, &sp),
                        __FUNCTION__);
+#endif
     os << " sched=" << policy << "/" << sp.sched_priority
        << " handle=" << reinterpret_cast<void*>(thread->tlsPtr_.pthread_self);
   }