Fix the thread-name facility and associated test on Darwin.  
Patch from Rhys Kidd (rhyskidd@gmail.com).  Fixes #339442.



git-svn-id: svn://svn.valgrind.org/valgrind/trunk@14622 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/coregrind/m_syswrap/syswrap-darwin.c b/coregrind/m_syswrap/syswrap-darwin.c
index 4bb88b3..e04e39a 100644
--- a/coregrind/m_syswrap/syswrap-darwin.c
+++ b/coregrind/m_syswrap/syswrap-darwin.c
@@ -3915,9 +3915,41 @@
 {
 #if VG_WORDSIZE == 4
    vg_assert(SUCCESS);
+
+   // Intercept internal call to proc_setcontrol() where flavor = 2, arg = 0
+   if (ARG1 == 5 && ARG3 == 2 && LOHI64(ARG4,ARG5) == 0 )
+   {
+       const HChar* new_name = (const HChar*) ARG6;
+       if (new_name) {    // Paranoia
+          ThreadState* tst = VG_(get_ThreadState)(tid);
+          SizeT new_len = VG_(strlen)(new_name);
+           
+          /* Don't bother reusing the memory. This is a rare event. */
+          tst->thread_name =
+             VG_(realloc)("syscall(proc_info)", tst->thread_name, new_len + 1);
+          VG_(strcpy)(tst->thread_name, new_name);
+       }
+   }
+    
    POST_MEM_WRITE(ARG6, ARG7);
 #else
    vg_assert(SUCCESS);
+
+   // Intercept internal call to proc_setcontrol() where flavor = 2, arg = 0
+   if (ARG1 == 5 && ARG3 == 2 && ARG4 == 0 )
+   {
+      const HChar* new_name = (const HChar*) ARG5;
+      if (new_name) {    // Paranoia
+         ThreadState* tst = VG_(get_ThreadState)(tid);
+         SizeT new_len = VG_(strlen)(new_name);
+            
+         /* Don't bother reusing the memory. This is a rare event. */
+         tst->thread_name =
+            VG_(realloc)("syscall(proc_info)", tst->thread_name, new_len + 1);
+         VG_(strcpy)(tst->thread_name, new_name);
+       }
+   }
+
    POST_MEM_WRITE(ARG5, ARG6);
 #endif
 }
diff --git a/memcheck/tests/threadname.c b/memcheck/tests/threadname.c
index c08f8d8..91e7f83 100644
--- a/memcheck/tests/threadname.c
+++ b/memcheck/tests/threadname.c
@@ -21,7 +21,11 @@
 {
   const char* threadname = "012345678901234";
 
+#  if !defined(VGO_darwin)
   pthread_setname_np(pthread_self(), threadname);
+#  else
+  pthread_setname_np(threadname);
+#  endif
 
   bad_things(4);
 
@@ -33,7 +37,11 @@
   const char* threadname = "try1";
   int r;
 
+#  if !defined(VGO_darwin)
   pthread_setname_np(pthread_self(), threadname);
+#  else
+  pthread_setname_np(threadname);
+#  endif
 
   bad_things(3);
 
diff --git a/memcheck/tests/threadname.stderr.exp b/memcheck/tests/threadname.stderr.exp
index 8ee6752..1cf2313 100644
--- a/memcheck/tests/threadname.stderr.exp
+++ b/memcheck/tests/threadname.stderr.exp
@@ -1,50 +1,50 @@
 Invalid write of size 1
    at 0x........: bad_things (threadname.c:16)
-   by 0x........: main (threadname.c:68)
+   by 0x........: main (threadname.c:76)
  Address 0x........ is 0 bytes after a block of size 1 alloc'd
    at 0x........: malloc (vg_replace_malloc.c:...)
    by 0x........: bad_things (threadname.c:15)
-   by 0x........: main (threadname.c:68)
+   by 0x........: main (threadname.c:76)
 
 Thread 2:
 Invalid write of size 1
    at 0x........: bad_things (threadname.c:16)
-   by 0x........: child_fn_0 (threadname.c:53)
+   by 0x........: child_fn_0 (threadname.c:61)
    ...
  Address 0x........ is 0 bytes after a block of size 2 alloc'd
    at 0x........: malloc (vg_replace_malloc.c:...)
    by 0x........: bad_things (threadname.c:15)
-   by 0x........: child_fn_0 (threadname.c:53)
+   by 0x........: child_fn_0 (threadname.c:61)
    ...
 
 Thread 3 try1:
 Invalid write of size 1
    at 0x........: bad_things (threadname.c:16)
-   by 0x........: child_fn_1 (threadname.c:38)
+   by 0x........: child_fn_1 (threadname.c:46)
    ...
  Address 0x........ is 0 bytes after a block of size 3 alloc'd
    at 0x........: malloc (vg_replace_malloc.c:...)
    by 0x........: bad_things (threadname.c:15)
-   by 0x........: child_fn_1 (threadname.c:38)
+   by 0x........: child_fn_1 (threadname.c:46)
    ...
 
 Thread 4 012345678901234:
 Invalid write of size 1
    at 0x........: bad_things (threadname.c:16)
-   by 0x........: child_fn_2 (threadname.c:26)
+   by 0x........: child_fn_2 (threadname.c:30)
    ...
  Address 0x........ is 0 bytes after a block of size 4 alloc'd
    at 0x........: malloc (vg_replace_malloc.c:...)
    by 0x........: bad_things (threadname.c:15)
-   by 0x........: child_fn_2 (threadname.c:26)
+   by 0x........: child_fn_2 (threadname.c:30)
    ...
 
 Thread 1:
 Invalid write of size 1
    at 0x........: bad_things (threadname.c:16)
-   by 0x........: main (threadname.c:76)
+   by 0x........: main (threadname.c:84)
  Address 0x........ is 0 bytes after a block of size 5 alloc'd
    at 0x........: malloc (vg_replace_malloc.c:...)
    by 0x........: bad_things (threadname.c:15)
-   by 0x........: main (threadname.c:76)
+   by 0x........: main (threadname.c:84)
 
diff --git a/memcheck/tests/threadname.vgtest b/memcheck/tests/threadname.vgtest
index 22d04f8..2692bd0 100644
--- a/memcheck/tests/threadname.vgtest
+++ b/memcheck/tests/threadname.vgtest
@@ -1,3 +1,3 @@
 prog: threadname
-prereq: test -e ./threadname && ../../tests/os_test linux
+prereq: test -e ./threadname
 vgopts: -q