Fix thread names.

The prctl() call we were using politely truncates overly-long strings.
The pthread_setname_np() call returns ERANGE.  This adds a buffer
copy to create a truncated string.

I'm strongly tempted to just back out the previous change and go
back to using prctl(), but there's probably some value in using a
pthread call, even if it is _np (and appears to exist but declared
differently on other platforms).

Change-Id: Ib692ea0e225f691e4888b27883c61f7701272245
diff --git a/vm/Thread.c b/vm/Thread.c
index 389021f..b3b171e 100644
--- a/vm/Thread.c
+++ b/vm/Thread.c
@@ -1352,12 +1352,19 @@
         s = threadName + len - 15;
     }
 #if defined(HAVE_ANDROID_PTHREAD_SETNAME_NP)
-    if (pthread_setname_np(pthread_self(), s) != 0)
-        LOGW("Unable to set the name of the current thread\n");
+    /* pthread_setname_np fails rather than truncating long strings */
+    char buf[16];       // MAX_TASK_COMM_LEN=16 is hard-coded into bionic
+    strncpy(buf, s, sizeof(buf)-1);
+    buf[sizeof(buf)-1] = '\0';
+    int err = pthread_setname_np(pthread_self(), buf);
+    if (err != 0) {
+        LOGW("Unable to set the name of current thread to '%s': %s\n",
+            buf, strerror(err));
+    }
 #elif defined(HAVE_PRCTL)
     prctl(PR_SET_NAME, (unsigned long) s, 0, 0, 0);
 #else
-    LOGD("Unable to set current thread's name: %s\n", s);
+    LOGD("No way to set current thread's name (%s)\n", s);
 #endif
 }