
Misc changes needed so that Valgrind can run itself.


 coregrind/core.h             |    4 ++
 coregrind/linux/core_os.c    |   16 +++++++-
 coregrind/stage1.c           |    4 +-
 coregrind/vg_main.c          |   14 +------
 coregrind/vg_messages.c      |    6 ++-
 coregrind/vg_procselfmaps.c  |    7 ++-
 coregrind/vg_scheduler.c     |   10 ++++-
 coregrind/vg_signals.c       |   78 +++++++++++++++++++++++++++++--------------
 coregrind/vg_symtab2.c       |   14 ++++++-
 coregrind/vg_syscalls.c      |   26 +++++++++-----
 coregrind/vg_transtab.c      |   43 ++++++++++++-----------
 include/x86-linux/vki_arch.h |    8 ++--
 12 files changed, 149 insertions(+), 81 deletions(-)

diff -puN coregrind/vg_syscalls.c~selfrun coregrind/vg_syscalls.c
--- valgrind/coregrind/vg_syscalls.c~selfrun	2005-01-10 16:40:48.000000000 -0800
+++ valgrind-jeremy/coregrind/vg_syscalls.c	2005-01-11 10:13:09.000000000 -0800
@@ -117,7 +117,7 @@ Bool VG_(valid_client_addr)(Addr start, 
    if (size == 0)
       return True;
 
-   if (cl_base < 0x10000)
+   if (0 && cl_base < 0x10000)
       cl_base = 0x10000;
 
    ret =
@@ -892,7 +892,9 @@ static Addr do_brk(Addr newbrk)
       VG_(printf)("do_brk: brk_base=%p brk_limit=%p newbrk=%p\n",
 		  VG_(brk_base), VG_(brk_limit), newbrk);
 
-   if (newbrk < VG_(brk_base) || newbrk >= VG_(client_end))
+   if (newbrk < VG_(brk_base) || /* too low */
+       newbrk >= VG_(client_end) || /* too high */
+       (newbrk - VG_(brk_base)) > VG_(client_rlimit_data).rlim_cur) /* out of limits */
       return VG_(brk_limit);
 
    /* brk isn't allowed to grow over anything else */
@@ -915,7 +917,6 @@ static Addr do_brk(Addr newbrk)
 
       /* new brk in a new page - fix the mappings */
       if (newbrk > VG_(brk_limit)) {
-	 
 	 if (debug)
 	    VG_(printf)("  extending brk: current=%p newaddr=%p delta=%d\n",
 			current, newaddr, newaddr-current);
@@ -4327,6 +4328,14 @@ PRE(sys_open, MayBlock)
                     const char *, filename, int, flags);
    }
    PRE_MEM_RASCIIZ( "open(filename)", arg1 );
+
+   if (VG_(strcmp)((Char *)arg1, "/proc/self/exe") == 0) {
+      Char name[20];
+      VG_(sprintf)(name, "/proc/self/fd/%d", VG_(clexecfd));
+
+      set_result(VG_(open)(name, arg2, arg3));
+      *flags |= Done;
+   }
 }
 
 POST(sys_open)
@@ -4466,7 +4475,7 @@ PRE(sys_readlink, Special)
 
    set_result( VG_(do_syscall)(saved, arg1, arg2, arg3));
    if ((Int)SYSRES == -2) {
-      char name[25];
+      Char name[25];
 
       VG_(sprintf)(name, "/proc/%d/exe", VG_(getpid)());
    
@@ -4664,7 +4673,7 @@ PRE(sys_setrlimit, 0)
          set_result( -VKI_EPERM );
       }
       else {
-         VG_(client_rlimit_data) = *(struct vki_rlimit *)arg2;
+	 VG_(client_rlimit_data) = *(struct vki_rlimit *)arg2;
          set_result( 0 );
       }
    }
@@ -5330,7 +5339,7 @@ PRE(sys_rt_sigtimedwait, MayBlock)
    if (arg2 != 0)
       PRE_MEM_WRITE( "rt_sigtimedwait(info)", arg2, sizeof(vki_siginfo_t) );
    PRE_MEM_READ( "rt_sigtimedwait(timeout)",
-                 arg4, sizeof(struct vki_timespec) );
+                 arg3, sizeof(struct vki_timespec) );
 }
 
 POST(sys_rt_sigtimedwait)
@@ -5854,9 +5863,8 @@ void VG_(client_syscall) ( ThreadId tid 
    } else {
       (sys->before)(tst->tid, tst, &flags);
 
-      if ((Word)SYSRES <= 0) {
-	 /* "before" decided the syscall wasn't viable, so don't do
-	    anything - just pretend the syscall happened. */
+      if ((Word)SYSRES <= 0 || (flags & Done)) {
+	 /* "before" did the syscall, so don't do anything else. */
          PRINT(" ==> %lld (0x%llx)\n", (Long)(Word)SYSRES, (ULong)SYSRES);
       } else if (mayBlock) {
 	 /* Syscall may block, so run it asynchronously */
diff -puN coregrind/core.h~selfrun coregrind/core.h
--- valgrind/coregrind/core.h~selfrun	2005-01-10 16:40:48.000000000 -0800
+++ valgrind-jeremy/coregrind/core.h	2005-01-11 10:13:09.000000000 -0800
@@ -837,6 +837,9 @@ extern void VG_(resume_scheduler) ( Thre
    ones (SIGKILL and SIGSTOP). */
 extern vki_sigset_t	VG_(blocked_mask);
 
+/* Highest signal the kernel will let us use */
+extern Int VG_(max_signal);
+
 extern void VG_(sigstartup_actions) ( void );
 
 extern void VG_(deliver_signal) ( ThreadId tid, const vki_siginfo_t * );
@@ -1264,6 +1267,7 @@ void VG_(record_fd_open)(Int tid, Int fd
 #define Special    (1 << 0)
 #define MayBlock   (1 << 1)
 #define PostOnFail (1 << 2)
+#define Done       (1 << 3)	/* used if a PRE() did the syscall */
 
 // For each generic ("gen") wrapper, we declare the pre-wrapper, the
 // post-wrapper (which is actually not always needed), and the associated
diff -puN coregrind/stage1.c~selfrun coregrind/stage1.c
--- valgrind/coregrind/stage1.c~selfrun	2005-01-10 16:40:48.000000000 -0800
+++ valgrind-jeremy/coregrind/stage1.c	2005-01-10 16:40:48.000000000 -0800
@@ -115,8 +115,8 @@ static void *fix_auxv(void *v_init_esp, 
    seen = 0;
    for(; auxv->a_type != AT_NULL; auxv++) {
       if (0)
-	 printf("doing auxv %p %4lld: %lld %p\n",
-                auxv, (ULong)auxv->a_type, (ULong)auxv->u.a_val, auxv->u.a_ptr);
+	 printf("doing auxv %p %5d: %d %p\n",
+                auxv, auxv->a_type, auxv->u.a_val, auxv->u.a_ptr);
 
       switch(auxv->a_type) {
       case AT_PHDR:
diff -puN coregrind/vg_main.c~selfrun coregrind/vg_main.c
--- valgrind/coregrind/vg_main.c~selfrun	2005-01-10 16:40:48.000000000 -0800
+++ valgrind-jeremy/coregrind/vg_main.c	2005-01-11 10:13:09.000000000 -0800
@@ -858,7 +858,6 @@ static char **fix_environment(char **ori
 }
 
 extern char **environ;		/* our environment */
-//#include <error.h>
 
 /* Add a string onto the string table, and return its address */
 static char *copy_str(char **tab, const char *str)
@@ -1062,10 +1061,7 @@ static Addr setup_client_stack(void* ini
 	 break;
 
       case AT_BASE:
-	 if (info->interp_base == 0)
-	    auxv->a_type = AT_IGNORE;
-	 else
-	    auxv->u.a_val = info->interp_base;
+	 auxv->u.a_val = info->interp_base;
 	 break;
 
       case AT_PLATFORM:		/* points to a platform description string */
@@ -2319,10 +2315,6 @@ static void build_valgrind_map_callback 
 {
    UInt prot  = 0;
    UInt flags = SF_MMAP|SF_NOSYMS;
-   Bool is_stack_segment;
-
-   is_stack_segment = 
-      (start == VG_(clstk_base) && (start+size) == VG_(clstk_end));
 
    /* Only record valgrind mappings for now, without loading any
       symbols.  This is so we know where the free space is before we
@@ -2506,7 +2498,7 @@ static int prmap(char *start, char *end,
    return True;
 }
 
-int main(int argc, char **argv)
+int main(int argc, char **argv, char **envp)
 {
    char **cl_argv;
    const char *tool = NULL;
@@ -2624,7 +2616,7 @@ int main(int argc, char **argv)
    //   p: set-libdir  [for VG_(libdir)]
    //   p: load_tool() [for 'preload']
    //--------------------------------------------------------------
-   env = fix_environment(environ, preload);
+   env = fix_environment(envp, preload);
 
    //--------------------------------------------------------------
    // Setup client stack, eip, and VG_(client_arg[cv])
diff -puN coregrind/vg_procselfmaps.c~selfrun coregrind/vg_procselfmaps.c
--- valgrind/coregrind/vg_procselfmaps.c~selfrun	2005-01-10 16:40:48.000000000 -0800
+++ valgrind-jeremy/coregrind/vg_procselfmaps.c	2005-01-10 16:40:48.000000000 -0800
@@ -238,9 +238,10 @@ void VG_(parse_procselfmaps) (
          foffset = 0;
       }
 
-      (*record_mapping) ( start, endPlusOne-start, 
-                          rr, ww, xx, maj * 256 + min, ino,
-                          foffset, filename );
+      if (start < VG_(valgrind_last))
+	 (*record_mapping) ( start, endPlusOne-start, 
+			     rr, ww, xx, maj * 256 + min, ino,
+			     foffset, filename );
 
       if ('\0' != tmp) {
          filename[i_eol - i] = tmp;
diff -puN coregrind/vg_messages.c~selfrun coregrind/vg_messages.c
--- valgrind/coregrind/vg_messages.c~selfrun	2005-01-10 16:40:48.000000000 -0800
+++ valgrind-jeremy/coregrind/vg_messages.c	2005-01-10 16:40:48.000000000 -0800
@@ -102,6 +102,7 @@ int VG_(start_msg) ( VgMsgKind kind )
 {
    Char ts[32];
    Char c;
+   static const Char pfx[] = ">>>>>>>>>>>>>>>>";
    vg_n_mbuf = 0;
    vg_mbuf[vg_n_mbuf] = 0;
    if (VG_(clo_time_stamp))
@@ -115,8 +116,9 @@ int VG_(start_msg) ( VgMsgKind kind )
       case Vg_ClientMsg:     c = '*'; break;
       default:               c = '?'; break;
    }
-   return VG_(add_to_msg)( "%c%c%s%d%c%c ", 
-                           c,c, ts, VG_(getpid)(), c,c );
+   return VG_(add_to_msg)( "%s%c%c%s%d%c%c ", 
+			   &pfx[sizeof(pfx)-1-RUNNING_ON_VALGRIND],
+			   c,c, ts, VG_(getpid)(), c,c );
 }
 
 
diff -puN include/x86-linux/vki_arch.h~selfrun include/x86-linux/vki_arch.h
--- valgrind/include/x86-linux/vki_arch.h~selfrun	2005-01-10 16:40:48.000000000 -0800
+++ valgrind-jeremy/include/x86-linux/vki_arch.h	2005-01-10 16:40:48.000000000 -0800
@@ -135,10 +135,9 @@ typedef struct {
 #define VKI_SIGRTMAX	_VKI_NSIG
 
 /* Use high signals because native pthreads wants to use low */
-#define VKI_SIGVGKILL       (VKI_SIGRTMAX-0) // [[internal: kill]]
-#define VKI_SIGVGCHLD       (VKI_SIGRTMAX-1) // [[internal: thread death]]
-#define VKI_SIGVGRTUSERMAX  (VKI_SIGRTMAX-2) // [[internal: last
-                                             //   user-usable RT signal]]
+#define VKI_SIGVGKILL       (VG_(max_signal)-0) // [[internal: kill]]
+#define VKI_SIGVGCHLD       (VG_(max_signal)-1) // [[internal: thread death]]
+#define VKI_SIGVGRTUSERMAX  (VG_(max_signal)-2) // [[internal: last user-usable RT signal]]
 
 #define VKI_SA_NOCLDSTOP	0x00000001u
 #define VKI_SA_NOCLDWAIT	0x00000002u
@@ -269,6 +268,7 @@ struct vki_sigcontext {
 
 #define VKI_O_RDONLY	     00
 #define VKI_O_WRONLY	     01
+#define VKI_O_RDWR	     02
 #define VKI_O_CREAT	   0100	/* not fcntl */
 #define VKI_O_EXCL	   0200	/* not fcntl */
 #define VKI_O_TRUNC	  01000	/* not fcntl */
diff -puN coregrind/vg_signals.c~selfrun coregrind/vg_signals.c
--- valgrind/coregrind/vg_signals.c~selfrun	2005-01-10 16:40:48.000000000 -0800
+++ valgrind-jeremy/coregrind/vg_signals.c	2005-01-11 10:13:09.000000000 -0800
@@ -99,6 +99,9 @@ static const Char *signame(Int sigNo);
 
 vki_sigset_t VG_(blocked_mask);
 
+/* Maximum usable signal. */
+Int VG_(max_signal) = _VKI_NSIG;
+
 /* ---------------------------------------------------------------------
    HIGH LEVEL STUFF TO DO WITH SIGNALS: POLICY (MOSTLY)
    ------------------------------------------------------------------ */
@@ -243,19 +246,17 @@ void calculate_SKSS_from_SCSS ( SKSS* ds
 	 skss_handler = vg_sync_signalhandler;
 	 break;
 
-      case VKI_SIGVGKILL:
-	 skss_handler = sigvgkill_handler;
-	 break;
-
-      case VKI_SIGVGCHLD:
-	 skss_handler = sigvgchld_handler;
-	 break;
-
       default:
-	 if (scss_handler == VKI_SIG_IGN)
-	    skss_handler = VKI_SIG_IGN;
-	 else 
-	    skss_handler = vg_async_signalhandler;
+	 if (sig == VKI_SIGVGKILL)
+	    skss_handler = sigvgkill_handler;
+	 else if (sig == VKI_SIGVGCHLD)
+	    skss_handler = sigvgchld_handler;
+	 else {
+	    if (scss_handler == VKI_SIG_IGN)
+	       skss_handler = VKI_SIG_IGN;
+	    else 
+	       skss_handler = vg_async_signalhandler;
+	 }
 	 break;
       }
 
@@ -319,7 +320,7 @@ static void handle_SCSS_change ( Bool fo
 
    /* Compare the new SKSS entries vs the old ones, and update kernel
       where they differ. */
-   for (sig = 1; sig <= _VKI_NSIG; sig++) {
+   for (sig = 1; sig <= VG_(max_signal); sig++) {
 
       /* Trying to do anything with SIGKILL is pointless; just ignore
          it. */
@@ -468,7 +469,7 @@ Int VG_(do_sys_sigaction) ( ThreadId tid
       succeed. */
 
    /* Reject out-of-range signal numbers. */
-   if (signo < 1 || signo > _VKI_NSIG) goto bad_signo;
+   if (signo < 1 || signo > VG_(max_signal)) goto bad_signo;
 
    /* don't let them use our signals */
    if ( (signo == VKI_SIGVGCHLD || signo == VKI_SIGVGKILL)
@@ -685,7 +686,7 @@ void vg_push_signal_frame ( ThreadId tid
    ThreadState* tst;
    Int		sigNo = siginfo->si_signo;
 
-   vg_assert(sigNo >= 1 && sigNo <= _VKI_NSIG);
+   vg_assert(sigNo >= 1 && sigNo <= VG_(max_signal));
    vg_assert(VG_(is_valid_tid)(tid));
    tst = & VG_(threads)[tid];
 
@@ -790,7 +791,7 @@ static const Char *signame(Int sigNo)
 #undef S
 
    case VKI_SIGRTMIN ... VKI_SIGRTMAX:
-      VG_(sprintf)(buf, "SIGRT%d", sigNo);
+      VG_(sprintf)(buf, "SIGRT%d", sigNo-VKI_SIGRTMIN);
       return buf;
 
    default:
@@ -1494,10 +1495,10 @@ void vg_sync_signalhandler ( Int sigNo, 
 	     sigNo == VKI_SIGILL);
 
    if (VG_(clo_trace_signals)) {
-      VG_(message)(Vg_DebugMsg, "signal %d arrived ... si_code=%d",
-                   sigNo, info->si_code );
+      VG_(message)(Vg_DebugMsg, "signal %d arrived ... si_code=%d, EIP=%p",
+                   sigNo, info->si_code, ARCH_INSTR_PTR(VG_(threads)[tid].arch) );
    }
-   vg_assert(sigNo >= 1 && sigNo <= _VKI_NSIG);
+   vg_assert(sigNo >= 1 && sigNo <= VG_(max_signal));
 
    /* Special fault-handling case. We can now get signals which can
       act upon and immediately restart the faulting instruction.
@@ -1645,10 +1646,14 @@ static void sigvgkill_handler(int signo,
 {
    ThreadId tid = VG_(get_lwp_tid)(VG_(gettid)());
 
-   //VG_(printf)("sigvgkill for lwp %d tid %d\n", VG_(gettid)(), tid);
+   if (VG_(clo_trace_signals))
+      VG_(message)(Vg_DebugMsg, "sigvgkill for lwp %d tid %d", VG_(gettid)(), tid);
 
    vg_assert(signo == VKI_SIGVGKILL);
    vg_assert(si->si_signo == signo);
+   vg_assert(VG_(threads)[tid].status == VgTs_WaitSys);
+
+   VG_(set_running)(tid);
 
    /* Check that the signal comes from within, and ignore it if not. */
    if (si->si_code != VKI_SI_TKILL || 
@@ -1673,7 +1678,7 @@ void pp_vg_ksigaction ( struct vki_sigac
    VG_(printf)("vg_ksigaction: handler %p, flags 0x%x, restorer %p\n", 
                sa->ksa_handler, (UInt)sa->sa_flags, sa->sa_restorer);
    VG_(printf)("vg_ksigaction: { ");
-   for (i = 1; i <= _VKI_NSIG; i++)
+   for (i = 1; i <= VG_(max_signal); i++)
       if (VG_(sigismember(&(sa->sa_mask),i)))
          VG_(printf)("%d ", i);
    VG_(printf)("}\n");
@@ -1700,7 +1705,7 @@ void VG_(poll_signals)(ThreadId tid)
       the thread */
    if (VG_(sigtimedwait)(&pollset, &si, &zero) > 0) {
       if (VG_(clo_trace_signals))
-	 VG_(message)(Vg_DebugMsg, "poll_signals: got signal %d for thread %d\n", si.si_signo, tid);
+	 VG_(message)(Vg_DebugMsg, "poll_signals: got signal %d for thread %d", si.si_signo, tid);
       VG_(deliver_signal)(tid, &si);
    }
 }
@@ -1731,10 +1736,32 @@ void VG_(sigstartup_actions) ( void )
 
    /* Copy per-signal settings to SCSS. */
    for (i = 1; i <= _VKI_NSIG; i++) {
-
       /* Get the old host action */
       ret = VG_(sigaction)(i, NULL, &sa);
-      vg_assert(ret == 0);
+
+      if (ret != 0)
+	 break;
+
+      /* Try setting it back to see if this signal is really
+	 available */
+      if (i >= VKI_SIGRTMIN) {
+	 struct vki_sigaction tsa;
+
+	 tsa.ksa_handler = (void *)vg_sync_signalhandler;
+	 tsa.sa_flags = VKI_SA_SIGINFO;
+	 VG_(sigfillset)(&tsa.sa_mask);
+
+	 /* try setting it to some arbitrary handler */
+	 if (VG_(sigaction)(i, &tsa, NULL) != 0) {
+	    /* failed - not really usable */
+	    break;
+	 }
+
+	 ret = VG_(sigaction)(i, &sa, NULL);
+	 vg_assert(ret == 0);
+      }
+
+      VG_(max_signal) = i;
 
       if (VG_(clo_trace_signals))
          VG_(printf)("snaffling handler 0x%x for signal %d\n", 
@@ -1746,6 +1773,9 @@ void VG_(sigstartup_actions) ( void )
       vg_scss.scss_per_sig[i].scss_restorer = sa.sa_restorer;
    }
 
+   if (VG_(clo_trace_signals))
+      VG_(message)(Vg_DebugMsg, "Max kernel-supported signal is %d", VG_(max_signal));
+
    /* Our private internal signals are treated as ignored */
    vg_scss.scss_per_sig[VKI_SIGVGCHLD].scss_handler = VKI_SIG_IGN;
    vg_scss.scss_per_sig[VKI_SIGVGCHLD].scss_flags   = VKI_SA_SIGINFO;
diff -puN coregrind/linux/core_os.c~selfrun coregrind/linux/core_os.c
--- valgrind/coregrind/linux/core_os.c~selfrun	2005-01-10 16:40:48.000000000 -0800
+++ valgrind-jeremy/coregrind/linux/core_os.c	2005-01-11 10:13:09.000000000 -0800
@@ -29,6 +29,16 @@ void VGA_(thread_wrapper)(ThreadId tid)
       VG_(printf)("thread tid %d started: stack = %p\n",
 		  tid, &tid);
 
+   if (tid == 1) {
+	   /* Thread 1 has its stack on the main process stack, and so
+	      is expandable.  Do this so that backtraces are printed
+	      properly. 
+	      XXX Perhaps it should have a private stack too?
+	   */
+	   tst->os_state.stack = (UInt *)VG_(valgrind_last) - VG_STACK_SIZE_W;
+	   tst->os_state.stacksize = VG_STACK_SIZE_W;
+   }
+
    tst->os_state.lwpid = VG_(gettid)();
    tst->os_state.threadgroup = VG_(getpid)();
 
@@ -110,7 +120,8 @@ void VGA_(reap_threads)(ThreadId self)
 
       vg_assert(sig == VKI_SIGVGCHLD);
       vg_assert(si.si_signo == VKI_SIGVGCHLD);
-   }      
+   }
+   vg_assert(VG_(count_living_threads)() == 1);
 }
 
 /* The we need to know the address of it so it can be
@@ -122,7 +133,8 @@ void VGA_(intercept_libc_freeres_wrapper
    __libc_freeres_wrapper = addr;
 }
 
-/* Clean up the client by calling __libc_freeres() (if requested) */
+/* Final clean-up before terminating the process.  
+   Clean up the client by calling __libc_freeres() (if requested) */
 void VGA_(final_tidyup)(ThreadId tid)
 {
    struct vki_sigaction sa;
diff -puN coregrind/vg_symtab2.c~selfrun coregrind/vg_symtab2.c
--- valgrind/coregrind/vg_symtab2.c~selfrun	2005-01-10 16:40:48.000000000 -0800
+++ valgrind-jeremy/coregrind/vg_symtab2.c	2005-01-10 16:40:48.000000000 -0800
@@ -492,11 +492,12 @@ void canonicaliseSymtab ( SegInfo* si )
       if(VG_(strncmp)(si->symtab[i].name, VG_INTERCEPT_PREFIX,
                       VG_INTERCEPT_PREFIX_LEN) == 0) {
          int len = VG_(strlen)(si->symtab[i].name);
-         char *buf = VG_(malloc)(len), *colon;
+         char *buf = VG_(arena_malloc)(VG_AR_SYMTAB, len), *colon;
          intercept_demangle(si->symtab[i].name, buf, len);
 	 colon = buf + VG_(strlen)(buf) - 1;
 	 while(*colon != ':') colon--;
 	 VG_(strncpy_safely)(si->symtab[i].name, colon+1, len);
+	 VG_(arena_free)(VG_AR_SYMTAB, buf);
       }
    }
 
@@ -2383,7 +2384,16 @@ static Bool resolve_redir(CodeRedirect *
 	 VG_(invalidate_translations)(redir->from_addr, 1, True);
       }
 
-      VG_(SkipList_Insert)(&sk_resolved_redir, redir);
+      {
+	 CodeRedirect *r = VG_(SkipList_Find)(&sk_resolved_redir, &redir->from_addr);
+
+	 if (r == NULL || r->from_addr != redir->from_addr)
+	    VG_(SkipList_Insert)(&sk_resolved_redir, redir);
+	 else if (verbose_redir)
+	    VG_(message)(Vg_DebugMsg, "  redir %s:%s:%p->%s:%s:%p duplicated\n",
+			 redir->from_lib, redir->from_sym, redir->from_addr,
+			 redir->to_lib, redir->to_sym, redir->to_addr);
+      }
    }
 
    return resolved;
diff -puN coregrind/vg_transtab.c~selfrun coregrind/vg_transtab.c
--- valgrind/coregrind/vg_transtab.c~selfrun	2005-01-10 16:40:48.000000000 -0800
+++ valgrind-jeremy/coregrind/vg_transtab.c	2005-01-10 16:40:48.000000000 -0800
@@ -30,6 +30,7 @@
 */
 
 #include "core.h"
+#include "memcheck/memcheck.h"
 
 /* #define DEBUG_TRANSTAB */
 
@@ -360,11 +361,11 @@ void discard_oldest_sector ( void )
       VG_(sprintf)(msg, "before discard of sector %d (%d bytes)", 
                         s, vg_tc_used[s]);
 
+      VALGRIND_DISCARD_TRANSLATIONS(vg_tc[s], vg_tc_used[s]);
       for(i = 0; i < VG_TC_N_SECTORS; i++) {
 	 if (i != s && vg_tc[i] != NULL)
 	    unchain_sector(i, (Addr)vg_tc[s], vg_tc_used[s]);
       }
-
       pp_tt_tc_status ( msg );
       overall_out_count += vg_tc_stats_count[s];
       overall_out_osize += vg_tc_stats_osize[s];
@@ -400,27 +401,29 @@ Int maybe_commission_sector ( void )
    }
    for (s = 0; s < VG_TC_N_SECTORS; s++) {
       if (vg_tc[s] == NULL) {
-#if 1
-         vg_tc[s] = VG_(get_memory_from_mmap) 
-                       ( vg_tc_sector_szB, "trans-cache(sector)" );
-#else
-         // Alternative: put translations in an mmap'd file.  The main
-         // reason is to help OProfile -- OProfile can assign time spent in
-         // translations to a particular file.  The file format doesn't
-         // really matter, which is good because it's not really readable,
-         // being generated code but not a proper ELF file.
-	 Char buf[20];
-	 static Int count = 0;
-	 Int fd;
+	 if (1)
+	    vg_tc[s] = VG_(get_memory_from_mmap) 
+	       ( vg_tc_sector_szB, "trans-cache(sector)" );
+	 else {
+	    // Alternative: put translations in an mmap'd file.  The main
+	    // reason is to help OProfile -- OProfile can assign time spent in
+	    // translations to a particular file.  The file format doesn't
+	    // really matter, which is good because it's not really readable,
+	    // being generated code but not a proper ELF file.
+	    Char buf[20];
+	    static Int count = 0;
+	    Int fd;
 	 
-	 VG_(sprintf)(buf, ".transtab.%d", count++);
+	    VG_(sprintf)(buf, ".transtab.%d", count++);
+
+	    fd = VG_(open)(buf, VKI_O_RDWR|VKI_O_CREAT|VKI_O_TRUNC, 0700);
+	    //VG_(unlink)(buf);
+	    VG_(do_syscall)(__NR_ftruncate, fd, PGROUNDUP(vg_tc_sector_szB));
+	    vg_tc[s] = VG_(mmap)(0, PGROUNDUP(vg_tc_sector_szB), VKI_PROT_READ|VKI_PROT_WRITE|VKI_PROT_EXEC, VKI_MAP_SHARED, 0, fd, 0);
+	    VG_(close)(fd);
+	 }
 
-	 fd = VG_(open)(buf, VKI_O_RDWR|VKI_O_CREAT|VKI_O_TRUNC, 0700);
-	 //VG_(unlink)(buf);
-	 VG_(do_syscall)(__NR_ftruncate, fd, PGROUNDUP(vg_tc_sector_szB));
-	 vg_tc[s] = VG_(mmap)(0, PGROUNDUP(vg_tc_sector_szB), VKI_PROT_READ|VKI_PROT_WRITE|VKI_PROT_EXEC, VKI_MAP_SHARED, 0, fd, 0);
-	 VG_(close)(fd);
-#endif
+	 VALGRIND_MAKE_WRITABLE(vg_tc[s], vg_tc_sector_szB);
          vg_tc_used[s] = 0;
          VG_(sprintf)(msg, "after  allocation of sector %d (size %d)", 
                            s, vg_tc_sector_szB );
diff -puN coregrind/vg_scheduler.c~selfrun coregrind/vg_scheduler.c
--- valgrind/coregrind/vg_scheduler.c~selfrun	2005-01-10 16:40:48.000000000 -0800
+++ valgrind-jeremy/coregrind/vg_scheduler.c	2005-01-11 10:13:09.000000000 -0800
@@ -446,6 +446,8 @@ void VG_(vg_yield)(void)
     */
    VG_(nanosleep)(&ts);
    VG_(set_running)(tid);
+
+   VG_(poll_signals)(tid);	/* something might have happened */
 }
 
 /* Copy the state of a thread from VG_(baseBlock), presumably after it
@@ -658,6 +660,10 @@ static void handle_syscall(ThreadId tid)
       syscall runs. */
    SCHEDSETJMP(tid, sigcode, VG_(client_syscall)(tid));
 
+   if (!VG_(is_running_thread)(tid))
+      VG_(printf)("tid %d not running; running_tid=%d, tid %d status %d\n",
+		  tid, running_tid, tid, tst->status);
+
    vg_assert(VG_(is_running_thread)(tid));
    
    switch(sigcode) {
@@ -969,7 +975,7 @@ void do_client_request ( ThreadId tid )
          break;
 
       case VG_USERREQ__RUNNING_ON_VALGRIND:
-         SET_CLREQ_RETVAL(tid, 1);
+         SET_CLREQ_RETVAL(tid, RUNNING_ON_VALGRIND+1);
          break;
 
       case VG_USERREQ__READ_MILLISECOND_TIMER:
@@ -1111,7 +1117,7 @@ void do_client_request ( ThreadId tid )
          } else {
 	    static Bool whined = False;
 
-	    if (!whined) {
+	    if (!whined && VG_(clo_verbosity) > 2) {
                // Allow for requests in core, but defined by tools, which
                // have 0 and 0 in their two high bytes.
                Char c1 = (arg[0] >> 24) & 0xff;

_
