Change --trace-pthread= flag to accept none|some|all, for finer level
of pthread event tracing.  And allow this info to be passed across to
the client, where vg_libpthread.c uses it to also control verbosity.


git-svn-id: svn://svn.valgrind.org/valgrind/trunk@91 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/coregrind/arch/x86-linux/vg_libpthread.c b/coregrind/arch/x86-linux/vg_libpthread.c
index ab39745..11f5200 100644
--- a/coregrind/arch/x86-linux/vg_libpthread.c
+++ b/coregrind/arch/x86-linux/vg_libpthread.c
@@ -27,29 +27,36 @@
 #include <unistd.h>
 #include <string.h>
 
-/* ---------------------------------------------------------------------
-   Mini-configuration.
-   ------------------------------------------------------------------ */
-
-/* Set to 1 to see IGNORED debugging messages. */
-static int show_IGNORED = 1;
-
 
 /* ---------------------------------------------------------------------
    Helpers.  We have to be pretty self-sufficient.
    ------------------------------------------------------------------ */
 
+/* Extract from Valgrind the value of VG_(clo_trace_pthread_level).
+   Returns 0 (none) if not running on Valgrind. */
+static
+int get_pt_trace_level ( void )
+{
+   int res;
+   VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */,
+                           VG_USERREQ__GET_PTHREAD_TRACE_LEVEL,
+                           0, 0, 0, 0);
+   return res;
+}
+
+
+
 static
 void myexit ( int arg )
 {
-  int __res;
+   int __res;
    __asm__ volatile ("movl %%ecx, %%ebx ; int $0x80"
                      : "=a" (__res)
                      : "0" (__NR_exit),
                        "c" (arg) );
-  /* We don't bother to mention the fact that this asm trashes %ebx,
-     since it won't return.  If you ever do let it return ... fix
-     this! */
+   /* We don't bother to mention the fact that this asm trashes %ebx,
+      since it won't return.  If you ever do let it return ... fix
+      this! */
 }
 
 
@@ -95,12 +102,13 @@
 
 static void ignored ( char* msg )
 {
-   char* ig = "vg_libpthread.so: IGNORED call to: ";
-   if (!show_IGNORED) return;
-   write(2, ig, strlen(ig));
-   write(2, msg, strlen(msg));
-   ig = "\n";
-   write(2, ig, strlen(ig));
+   if (get_pt_trace_level() >= 1) {
+      char* ig = "vg_libpthread.so: IGNORED call to: ";
+      write(2, ig, strlen(ig));
+      write(2, msg, strlen(msg));
+      ig = "\n";
+      write(2, ig, strlen(ig));
+   }
 }
 
 
@@ -190,8 +198,6 @@
                        const  pthread_mutexattr_t *mutexattr)
 {
    int res;
-   //  char* str = "pthread_mutex_init\n";
-   //  write(2, str, strlen(str));
    ensure_valgrind("pthread_mutex_init");
    VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */,
                            VG_USERREQ__PTHREAD_MUTEX_INIT,
@@ -214,7 +220,8 @@
 int pthread_mutex_lock(pthread_mutex_t *mutex)
 {
    int res;
-   if (!(RUNNING_ON_VALGRIND)) {
+   static int moans = 5;
+   if (!(RUNNING_ON_VALGRIND) && moans-- > 0) {
       char* str = "pthread_mutex_lock-NOT-INSIDE-VALGRIND\n";
       write(2, str, strlen(str));
       return 0;
@@ -229,7 +236,8 @@
 int pthread_mutex_unlock(pthread_mutex_t *mutex)
 {
    int res;
-   if (!(RUNNING_ON_VALGRIND)) {
+   static int moans = 5;
+   if (!(RUNNING_ON_VALGRIND) && moans-- > 0) {
       char* str = "pthread_mutex_unlock-NOT-INSIDE-VALGRIND\n";
       write(2, str, strlen(str));
       return 0;
@@ -256,7 +264,8 @@
 int pthread_mutex_destroy(pthread_mutex_t *mutex)
 {
    int res;
-   if (!(RUNNING_ON_VALGRIND)) {
+   static int moans = 5;
+   if (!(RUNNING_ON_VALGRIND) && moans-- > 0) {
       char* str = "pthread_mutex_destroy-NOT-INSIDE-VALGRIND\n";
       write(2, str, strlen(str));
       return 0;
@@ -335,9 +344,7 @@
               const struct sigaction *act,  
               struct  sigaction *oldact)
 {
-  //  char* str = "sigaction\n";
-  //  write(2, str, strlen(str));
-  return __libc_sigaction(signum, act, oldact);
+   return __libc_sigaction(signum, act, oldact);
 }
 
 
@@ -349,9 +356,7 @@
              const  struct  sockaddr  *serv_addr, 
              socklen_t addrlen)
 {
-  //  char* str = "connect\n";
-  //  write(2, str, strlen(str));
-  return __libc_connect(sockfd, serv_addr, addrlen);
+   return __libc_connect(sockfd, serv_addr, addrlen);
 }
 
 
@@ -359,9 +364,7 @@
 int __libc_fcntl(int fd, int cmd, long arg);
 int fcntl(int fd, int cmd, long arg)
 {
-  //  char* str = "fcntl\n";
-  //  write(2, str, strlen(str));
-  return __libc_fcntl(fd, cmd, arg);
+   return __libc_fcntl(fd, cmd, arg);
 }
 
 
@@ -369,9 +372,7 @@
 ssize_t __libc_write(int fd, const void *buf, size_t count);
 ssize_t write(int fd, const void *buf, size_t count)
 {
-  //  char* str = "write\n";
-  //  write(2, str, strlen(str));
-  return __libc_write(fd, buf, count);
+   return __libc_write(fd, buf, count);
 }
 
 
@@ -379,9 +380,7 @@
 ssize_t __libc_read(int fd, void *buf, size_t count);
 ssize_t read(int fd, void *buf, size_t count)
 {
-  //  char* str = "read\n";
-  //  write(2, str, strlen(str));
-  return __libc_read(fd, buf, count);
+   return __libc_read(fd, buf, count);
 }
 
 
@@ -389,9 +388,7 @@
 int __libc_open(const char *pathname, int flags);
 int open(const char *pathname, int flags)
 {
-  //  char* str = "open\n";
-  //  write(2, str, strlen(str));
-  return __libc_open(pathname, flags);
+   return __libc_open(pathname, flags);
 }
 
 
@@ -399,9 +396,7 @@
 int __libc_close(int fd);
 int close(int fd)
 {
-  //  char* str = "open\n";
-  //  write(2, str, strlen(str));
-  return __libc_close(fd);
+   return __libc_close(fd);
 }
 
 
@@ -409,9 +404,7 @@
 int __libc_accept(int s, struct sockaddr *addr, socklen_t *addrlen);
 int accept(int s, struct sockaddr *addr, socklen_t *addrlen)
 {
-  //  char* str = "accept\n";
-  //  write(2, str, strlen(str));
-  return __libc_accept(s, addr, addrlen);
+   return __libc_accept(s, addr, addrlen);
 }
 
 
@@ -419,9 +412,7 @@
 pid_t __libc_fork(void);
 pid_t fork(void)
 {
-  //  char* str = "fork\n";
-  //  write(2, str, strlen(str));
-  return __libc_fork();
+   return __libc_fork();
 }
 
 
@@ -429,9 +420,7 @@
 pid_t __libc_waitpid(pid_t pid, int *status, int options);
 pid_t waitpid(pid_t pid, int *status, int options)
 {
-  //  char* str = "waitpid\n";
-  //  write(2, str, strlen(str));
-  return __libc_waitpid(pid, status, options);
+   return __libc_waitpid(pid, status, options);
 }
 
 
@@ -446,14 +435,14 @@
 int __libc_fsync(int fd);
 int fsync(int fd)
 {
-  return __libc_fsync(fd);
+   return __libc_fsync(fd);
 }
 
 extern
 off_t __libc_lseek(int fildes, off_t offset, int whence);
 off_t lseek(int fildes, off_t offset, int whence)
 {
-  return __libc_lseek(fildes, offset, whence);
+   return __libc_lseek(fildes, offset, whence);
 }
 
 extern  
@@ -470,6 +459,7 @@
    return __libc_send(s, msg, len, flags);
 }
 
+
 /*--------------------------------------------------*/
 
 /* I've no idea what these are, but they get called quite a lot.
@@ -666,7 +656,7 @@
       /* fprintf(stderr, "MY_SELECT: nanosleep\n"); */
       /* nanosleep and go round again */
       nanosleep_interval.tv_sec = 0;
-      nanosleep_interval.tv_nsec = 20 * 1000 * 1000; /* 20 milliseconds */
+      nanosleep_interval.tv_nsec = 40 * 1000 * 1000; /* 40 milliseconds */
       /* It's critical here that valgrind's nanosleep implementation
          is nonblocking. */
       (void)my_do_syscall2(__NR_nanosleep, 
diff --git a/coregrind/docs/manual.html b/coregrind/docs/manual.html
index ab638af..c441db1 100644
--- a/coregrind/docs/manual.html
+++ b/coregrind/docs/manual.html
@@ -565,9 +565,10 @@
       <p>Enable/disable tracing of thread scheduling events.</li><br>
       <p>
 
-  <li><code>--trace-pthread=no</code> [default]<br>
-      <code>--trace-pthread=yes</code>
-      <p>Enable/disable tracing of pthread-related events.</li><br>
+  <li><code>--trace-pthread=none</code> [default]<br>
+      <code>--trace-pthread=some</code> <br>
+      <code>--trace-pthread=all</code>
+      <p>Specifies amount of trace detail for pthread-related events.</li><br>
       <p>
 
   <li><code>--trace-symtab=no</code> [default]<br>
diff --git a/coregrind/valgrind.in b/coregrind/valgrind.in
index ea7d757..40bb6c9 100755
--- a/coregrind/valgrind.in
+++ b/coregrind/valgrind.in
@@ -87,8 +87,9 @@
     --trace-malloc=no)      vgopts="$vgopts $arg"; shift;;
     --trace-sched=yes)      vgopts="$vgopts $arg"; shift;;
     --trace-sched=no)       vgopts="$vgopts $arg"; shift;;
-    --trace-pthread=yes)    vgopts="$vgopts $arg"; shift;;
-    --trace-pthread=no)     vgopts="$vgopts $arg"; shift;;
+    --trace-pthread=none)   vgopts="$vgopts $arg"; shift;;
+    --trace-pthread=some)   vgopts="$vgopts $arg"; shift;;
+    --trace-pthread=all)    vgopts="$vgopts $arg"; shift;;
     --stop-after=*)         vgopts="$vgopts $arg"; shift;;
     --dump-error=*)         vgopts="$vgopts $arg"; shift;;
     -*)                     dousage=1; break;;
@@ -148,7 +149,7 @@
    echo "    --trace-symtab=no|yes     show symbol table details? [no]"
    echo "    --trace-malloc=no|yes     show client malloc details? [no]"
    echo "    --trace-sched=no|yes      show thread scheduler details? [no]"
-   echo "    --trace-pthread=no|yes    show pthread event details? [no]"
+   echo "    --trace-pthread=none|some|all  show pthread event details? [no]"
    echo "    --stop-after=<number>     switch to real CPU after executing"
    echo "                              <number> basic blocks [infinity]"
    echo "    --dump-error=<number>     show translation for basic block"
diff --git a/coregrind/vg_include.h b/coregrind/vg_include.h
index 1fef7da..cd831c8 100644
--- a/coregrind/vg_include.h
+++ b/coregrind/vg_include.h
@@ -241,8 +241,9 @@
 extern Bool  VG_(clo_trace_malloc);
 /* DEBUG: print thread scheduling events?  default: NO */
 extern Bool  VG_(clo_trace_sched);
-/* DEBUG: print pthread (mutex etc) events?  default: NO */
-extern Bool  VG_(clo_trace_pthread);
+/* DEBUG: print pthread (mutex etc) events?  default: 0 (none), 1
+   (some), 2 (all) */
+extern Int   VG_(clo_trace_pthread_level);
 /* Stop after this many basic blocks.  default: Infinity. */
 extern ULong VG_(clo_stop_after);
 /* Display gory details for the k'th most popular error.  default:
@@ -405,6 +406,9 @@
 #define VG_USERREQ__PTHREAD_MUTEX_DESTROY   0x3007
 #define VG_USERREQ__PTHREAD_CANCEL          0x3008
 
+/* Cosmetic ... */
+#define VG_USERREQ__GET_PTHREAD_TRACE_LEVEL 0x3101
+
 /* 
 In vg_constants.h:
 #define VG_USERREQ__SIGNAL_RETURNS          0x4001
diff --git a/coregrind/vg_libpthread.c b/coregrind/vg_libpthread.c
index ab39745..11f5200 100644
--- a/coregrind/vg_libpthread.c
+++ b/coregrind/vg_libpthread.c
@@ -27,29 +27,36 @@
 #include <unistd.h>
 #include <string.h>
 
-/* ---------------------------------------------------------------------
-   Mini-configuration.
-   ------------------------------------------------------------------ */
-
-/* Set to 1 to see IGNORED debugging messages. */
-static int show_IGNORED = 1;
-
 
 /* ---------------------------------------------------------------------
    Helpers.  We have to be pretty self-sufficient.
    ------------------------------------------------------------------ */
 
+/* Extract from Valgrind the value of VG_(clo_trace_pthread_level).
+   Returns 0 (none) if not running on Valgrind. */
+static
+int get_pt_trace_level ( void )
+{
+   int res;
+   VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */,
+                           VG_USERREQ__GET_PTHREAD_TRACE_LEVEL,
+                           0, 0, 0, 0);
+   return res;
+}
+
+
+
 static
 void myexit ( int arg )
 {
-  int __res;
+   int __res;
    __asm__ volatile ("movl %%ecx, %%ebx ; int $0x80"
                      : "=a" (__res)
                      : "0" (__NR_exit),
                        "c" (arg) );
-  /* We don't bother to mention the fact that this asm trashes %ebx,
-     since it won't return.  If you ever do let it return ... fix
-     this! */
+   /* We don't bother to mention the fact that this asm trashes %ebx,
+      since it won't return.  If you ever do let it return ... fix
+      this! */
 }
 
 
@@ -95,12 +102,13 @@
 
 static void ignored ( char* msg )
 {
-   char* ig = "vg_libpthread.so: IGNORED call to: ";
-   if (!show_IGNORED) return;
-   write(2, ig, strlen(ig));
-   write(2, msg, strlen(msg));
-   ig = "\n";
-   write(2, ig, strlen(ig));
+   if (get_pt_trace_level() >= 1) {
+      char* ig = "vg_libpthread.so: IGNORED call to: ";
+      write(2, ig, strlen(ig));
+      write(2, msg, strlen(msg));
+      ig = "\n";
+      write(2, ig, strlen(ig));
+   }
 }
 
 
@@ -190,8 +198,6 @@
                        const  pthread_mutexattr_t *mutexattr)
 {
    int res;
-   //  char* str = "pthread_mutex_init\n";
-   //  write(2, str, strlen(str));
    ensure_valgrind("pthread_mutex_init");
    VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */,
                            VG_USERREQ__PTHREAD_MUTEX_INIT,
@@ -214,7 +220,8 @@
 int pthread_mutex_lock(pthread_mutex_t *mutex)
 {
    int res;
-   if (!(RUNNING_ON_VALGRIND)) {
+   static int moans = 5;
+   if (!(RUNNING_ON_VALGRIND) && moans-- > 0) {
       char* str = "pthread_mutex_lock-NOT-INSIDE-VALGRIND\n";
       write(2, str, strlen(str));
       return 0;
@@ -229,7 +236,8 @@
 int pthread_mutex_unlock(pthread_mutex_t *mutex)
 {
    int res;
-   if (!(RUNNING_ON_VALGRIND)) {
+   static int moans = 5;
+   if (!(RUNNING_ON_VALGRIND) && moans-- > 0) {
       char* str = "pthread_mutex_unlock-NOT-INSIDE-VALGRIND\n";
       write(2, str, strlen(str));
       return 0;
@@ -256,7 +264,8 @@
 int pthread_mutex_destroy(pthread_mutex_t *mutex)
 {
    int res;
-   if (!(RUNNING_ON_VALGRIND)) {
+   static int moans = 5;
+   if (!(RUNNING_ON_VALGRIND) && moans-- > 0) {
       char* str = "pthread_mutex_destroy-NOT-INSIDE-VALGRIND\n";
       write(2, str, strlen(str));
       return 0;
@@ -335,9 +344,7 @@
               const struct sigaction *act,  
               struct  sigaction *oldact)
 {
-  //  char* str = "sigaction\n";
-  //  write(2, str, strlen(str));
-  return __libc_sigaction(signum, act, oldact);
+   return __libc_sigaction(signum, act, oldact);
 }
 
 
@@ -349,9 +356,7 @@
              const  struct  sockaddr  *serv_addr, 
              socklen_t addrlen)
 {
-  //  char* str = "connect\n";
-  //  write(2, str, strlen(str));
-  return __libc_connect(sockfd, serv_addr, addrlen);
+   return __libc_connect(sockfd, serv_addr, addrlen);
 }
 
 
@@ -359,9 +364,7 @@
 int __libc_fcntl(int fd, int cmd, long arg);
 int fcntl(int fd, int cmd, long arg)
 {
-  //  char* str = "fcntl\n";
-  //  write(2, str, strlen(str));
-  return __libc_fcntl(fd, cmd, arg);
+   return __libc_fcntl(fd, cmd, arg);
 }
 
 
@@ -369,9 +372,7 @@
 ssize_t __libc_write(int fd, const void *buf, size_t count);
 ssize_t write(int fd, const void *buf, size_t count)
 {
-  //  char* str = "write\n";
-  //  write(2, str, strlen(str));
-  return __libc_write(fd, buf, count);
+   return __libc_write(fd, buf, count);
 }
 
 
@@ -379,9 +380,7 @@
 ssize_t __libc_read(int fd, void *buf, size_t count);
 ssize_t read(int fd, void *buf, size_t count)
 {
-  //  char* str = "read\n";
-  //  write(2, str, strlen(str));
-  return __libc_read(fd, buf, count);
+   return __libc_read(fd, buf, count);
 }
 
 
@@ -389,9 +388,7 @@
 int __libc_open(const char *pathname, int flags);
 int open(const char *pathname, int flags)
 {
-  //  char* str = "open\n";
-  //  write(2, str, strlen(str));
-  return __libc_open(pathname, flags);
+   return __libc_open(pathname, flags);
 }
 
 
@@ -399,9 +396,7 @@
 int __libc_close(int fd);
 int close(int fd)
 {
-  //  char* str = "open\n";
-  //  write(2, str, strlen(str));
-  return __libc_close(fd);
+   return __libc_close(fd);
 }
 
 
@@ -409,9 +404,7 @@
 int __libc_accept(int s, struct sockaddr *addr, socklen_t *addrlen);
 int accept(int s, struct sockaddr *addr, socklen_t *addrlen)
 {
-  //  char* str = "accept\n";
-  //  write(2, str, strlen(str));
-  return __libc_accept(s, addr, addrlen);
+   return __libc_accept(s, addr, addrlen);
 }
 
 
@@ -419,9 +412,7 @@
 pid_t __libc_fork(void);
 pid_t fork(void)
 {
-  //  char* str = "fork\n";
-  //  write(2, str, strlen(str));
-  return __libc_fork();
+   return __libc_fork();
 }
 
 
@@ -429,9 +420,7 @@
 pid_t __libc_waitpid(pid_t pid, int *status, int options);
 pid_t waitpid(pid_t pid, int *status, int options)
 {
-  //  char* str = "waitpid\n";
-  //  write(2, str, strlen(str));
-  return __libc_waitpid(pid, status, options);
+   return __libc_waitpid(pid, status, options);
 }
 
 
@@ -446,14 +435,14 @@
 int __libc_fsync(int fd);
 int fsync(int fd)
 {
-  return __libc_fsync(fd);
+   return __libc_fsync(fd);
 }
 
 extern
 off_t __libc_lseek(int fildes, off_t offset, int whence);
 off_t lseek(int fildes, off_t offset, int whence)
 {
-  return __libc_lseek(fildes, offset, whence);
+   return __libc_lseek(fildes, offset, whence);
 }
 
 extern  
@@ -470,6 +459,7 @@
    return __libc_send(s, msg, len, flags);
 }
 
+
 /*--------------------------------------------------*/
 
 /* I've no idea what these are, but they get called quite a lot.
@@ -666,7 +656,7 @@
       /* fprintf(stderr, "MY_SELECT: nanosleep\n"); */
       /* nanosleep and go round again */
       nanosleep_interval.tv_sec = 0;
-      nanosleep_interval.tv_nsec = 20 * 1000 * 1000; /* 20 milliseconds */
+      nanosleep_interval.tv_nsec = 40 * 1000 * 1000; /* 40 milliseconds */
       /* It's critical here that valgrind's nanosleep implementation
          is nonblocking. */
       (void)my_do_syscall2(__NR_nanosleep, 
diff --git a/coregrind/vg_main.c b/coregrind/vg_main.c
index 7c5ac40..06b2e97 100644
--- a/coregrind/vg_main.c
+++ b/coregrind/vg_main.c
@@ -425,7 +425,7 @@
 Bool   VG_(clo_trace_symtab);
 Bool   VG_(clo_trace_malloc);
 Bool   VG_(clo_trace_sched);
-Bool   VG_(clo_trace_pthread);
+Int    VG_(clo_trace_pthread_level);
 ULong  VG_(clo_stop_after);
 Int    VG_(clo_dump_error);
 Int    VG_(clo_backtrace_size);
@@ -508,7 +508,7 @@
    VG_(clo_trace_symtab)     = False;
    VG_(clo_trace_malloc)     = False;
    VG_(clo_trace_sched)      = False;
-   VG_(clo_trace_pthread)    = False;
+   VG_(clo_trace_pthread_level) = 0;
    VG_(clo_stop_after)       = 1000000000000LL;
    VG_(clo_dump_error)       = 0;
    VG_(clo_backtrace_size)   = 4;
@@ -777,10 +777,12 @@
       else if (STREQ(argv[i], "--trace-sched=no"))
          VG_(clo_trace_sched) = False;
 
-      else if (STREQ(argv[i], "--trace-pthread=yes"))
-         VG_(clo_trace_pthread) = True;
-      else if (STREQ(argv[i], "--trace-pthread=no"))
-         VG_(clo_trace_pthread) = False;
+      else if (STREQ(argv[i], "--trace-pthread=none"))
+         VG_(clo_trace_pthread_level) = 0;
+      else if (STREQ(argv[i], "--trace-pthread=some"))
+         VG_(clo_trace_pthread_level) = 1;
+      else if (STREQ(argv[i], "--trace-pthread=all"))
+         VG_(clo_trace_pthread_level) = 2;
 
       else if (STREQN(13, argv[i], "--stop-after="))
          VG_(clo_stop_after) = VG_(atoll)(&argv[i][13]);
diff --git a/coregrind/vg_scheduler.c b/coregrind/vg_scheduler.c
index 33cb157..770fbd3 100644
--- a/coregrind/vg_scheduler.c
+++ b/coregrind/vg_scheduler.c
@@ -176,7 +176,7 @@
 static
 void print_sched_event ( ThreadId tid, Char* what )
 {
-   VG_(message)(Vg_DebugMsg, "SCHED[%d]: %s", tid, what );
+   VG_(message)(Vg_DebugMsg, "  SCHED[%d]: %s", tid, what );
 }
 
 
@@ -555,6 +555,8 @@
          SIMPLE_RETURN(tid);
       case VG_USERREQ__RUNNING_ON_VALGRIND:
          SIMPLE_RETURN(1);
+      case VG_USERREQ__GET_PTHREAD_TRACE_LEVEL:
+         SIMPLE_RETURN(VG_(clo_trace_pthread_level));
 
       default:
          /* Too hard; wimp out. */
@@ -1477,7 +1479,7 @@
    vg_mutexes[mid].owner = VG_INVALID_THREADID; /* irrelevant */
    mutex->__m_reserved = mid;
    mutex->__m_count = 1; /* initialised */
-   if (VG_(clo_trace_pthread)) {
+   if (VG_(clo_trace_pthread_level) >= 1) {
       VG_(sprintf)(msg_buf, "(initialise mutex) (%p) -> %d", 
                             mutex, mid );
       print_pthread_event(tid, msg_buf);
@@ -1497,7 +1499,7 @@
 
    initialise_mutex(tid, mutex);
 
-   if (VG_(clo_trace_pthread)) {
+   if (VG_(clo_trace_pthread_level) >= 1) {
       VG_(sprintf)(msg_buf, "pthread_mutex_init (%p) -> %d", 
                             mutex, mutex->__m_reserved );
       print_pthread_event(tid, msg_buf);
@@ -1541,7 +1543,7 @@
       return;
    }
 
-   if (VG_(clo_trace_pthread)) {
+   if (VG_(clo_trace_pthread_level) >= 2) {
       VG_(sprintf)(msg_buf, "pthread_mutex_lock   %d (%p)", 
                             mid, mutex );
       print_pthread_event(tid, msg_buf);
@@ -1562,9 +1564,9 @@
       vg_threads[tid].status = VgTs_WaitMX;
       vg_threads[tid].waited_on_mid = mid;
       /* No assignment to %EDX, since we're blocking. */
-      if (VG_(clo_trace_pthread)) {
-         VG_(sprintf)(msg_buf, "pthread_mutex_lock   %d (%p): BLOCK", 
-                               mid, mutex );
+      if (VG_(clo_trace_pthread_level) >= 1) {
+         VG_(sprintf)(msg_buf, "pthread_mutex_lock   %d: BLOCK", 
+                               mid );
          print_pthread_event(tid, msg_buf);
       }
    } else {
@@ -1597,7 +1599,7 @@
       return;
    }
 
-   if (VG_(clo_trace_pthread)) {
+   if (VG_(clo_trace_pthread_level) >= 2) {
       VG_(sprintf)(msg_buf, "pthread_mutex_unlock %d (%p)", 
                             mid, mutex );
       print_pthread_event(tid, msg_buf);
@@ -1633,7 +1635,7 @@
       vg_threads[i].status = VgTs_Runnable;
       vg_threads[i].m_edx = 0; /* pth_lock() success */
 
-      if (VG_(clo_trace_pthread)) {
+      if (VG_(clo_trace_pthread_level) >= 1) {
          VG_(sprintf)(msg_buf, "pthread_mutex_lock   %d: RESUME", 
                                mid );
          print_pthread_event(tid, msg_buf);
@@ -1664,7 +1666,7 @@
       return;
    }
 
-   if (VG_(clo_trace_pthread)) {
+   if (VG_(clo_trace_pthread_level) >= 1) {
       VG_(sprintf)(msg_buf, "pthread_mutex_destroy %d (%p)", 
                             mid, mutex );
       print_pthread_event(tid, msg_buf);