Final commit for the initial modularisation pass:

- Broke part of m_scheduler off into a new module m_threadstate.  It
  contains ThreadState, VG_(threads)[] and some basic operations on the
  thread table.  All simple stuff, the complex stuff stays in m_scheduler.
  This avoids lots of circular dependencies between m_scheduler and other
  modules.

- Managed to finally remove core.h and tool.h, double hurrah!

- Introduced pub_tool_basics.h and pub_core_basics.h, one of which is
  include by every single C file.

- Lots of little cleanups and changes related to the above.

- I even did a small amount of documentation updating.




git-svn-id: svn://svn.valgrind.org/valgrind/trunk@3944 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/Makefile.am b/Makefile.am
index e8ec095..f8285be 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -3,7 +3,6 @@
 
 include $(top_srcdir)/Makefile.all.am 
 
-## include must be first for tool.h
 ## addrcheck must come after memcheck, for mac_*.o
 TOOLS =		memcheck \
 		cachegrind \
diff --git a/auxprogs/valgrind-listener.c b/auxprogs/valgrind-listener.c
index a59b515..a565d2e 100644
--- a/auxprogs/valgrind-listener.c
+++ b/auxprogs/valgrind-listener.c
@@ -44,7 +44,7 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 
-#include "core.h"
+#include "pub_core_basics.h"
 #include "pub_core_libcassert.h"    // For VG_BUGS_TO
 #include "pub_core_libcfile.h"      // For VG_CLO_DEFAULT_LOGPORT
 
diff --git a/cachegrind/cg-amd64.c b/cachegrind/cg-amd64.c
index 824a4f8..8de4936 100644
--- a/cachegrind/cg-amd64.c
+++ b/cachegrind/cg-amd64.c
@@ -28,12 +28,13 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "tool.h"
-#include "cg_arch.h"
+#include "pub_tool_basics.h"
 #include "pub_tool_libcbase.h"
 #include "pub_tool_libcassert.h"
 #include "pub_tool_libcprint.h"
 
+#include "cg_arch.h"
+
 // All CPUID info taken from sandpile.org/a32/cpuid.htm */
 // Probably only works for Intel and AMD chips, and probably only for some of
 // them. 
diff --git a/cachegrind/cg-arm.c b/cachegrind/cg-arm.c
index eb1a587..de612e2 100644
--- a/cachegrind/cg-arm.c
+++ b/cachegrind/cg-arm.c
@@ -28,7 +28,6 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "tool.h"
 #include "cg_arch.h"
 
 void VGA_(configure_caches)(cache_t* I1c, cache_t* D1c, cache_t* L2c,
diff --git a/cachegrind/cg-x86.c b/cachegrind/cg-x86.c
index d0dc588..be2f457 100644
--- a/cachegrind/cg-x86.c
+++ b/cachegrind/cg-x86.c
@@ -28,11 +28,12 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "tool.h"
+#include "pub_tool_basics.h"
 #include "pub_tool_cpuid.h"
 #include "pub_tool_libcbase.h"
 #include "pub_tool_libcassert.h"
 #include "pub_tool_libcprint.h"
+
 #include "cg_arch.h"
 
 // All CPUID info taken from sandpile.org/a32/cpuid.htm */
diff --git a/cachegrind/cg_main.c b/cachegrind/cg_main.c
index 64c90c3..f2f0612 100644
--- a/cachegrind/cg_main.c
+++ b/cachegrind/cg_main.c
@@ -29,7 +29,7 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "tool.h"
+#include "pub_tool_basics.h"
 #include "pub_tool_debuginfo.h"
 #include "pub_tool_hashtable.h"
 #include "pub_tool_libcbase.h"
diff --git a/corecheck/cc_main.c b/corecheck/cc_main.c
index 61f9255..dda1146 100644
--- a/corecheck/cc_main.c
+++ b/corecheck/cc_main.c
@@ -29,7 +29,7 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "tool.h"
+#include "pub_tool_basics.h"
 #include "pub_tool_libcassert.h"
 #include "pub_tool_tooliface.h"
 
diff --git a/coregrind/Makefile.am b/coregrind/Makefile.am
index a0f87e3..fce5f56 100644
--- a/coregrind/Makefile.am
+++ b/coregrind/Makefile.am
@@ -31,9 +31,9 @@
 	vg_preload_core.so
 
 noinst_HEADERS = \
-	core.h			\
 	coregrind.h		\
 	pub_core_aspacemgr.h	\
+	pub_core_basics.h	\
 	pub_core_cpuid.h	\
 	pub_core_debuginfo.h	\
 	pub_core_debuglog.h	\
@@ -65,6 +65,7 @@
 	pub_core_stacktrace.h	\
 	pub_core_syswrap.h	\
 	pub_core_threadmodel.h	\
+	pub_core_threadstate.h	\
 	pub_core_tooliface.h	\
 	pub_core_trampoline.h	\
 	pub_core_translate.h	\
@@ -117,6 +118,7 @@
 	m_stacktrace.c \
 	m_syscall.c \
 	m_threadmodel.c \
+	m_threadstate.c \
 	m_tooliface.c \
 	m_trampoline.S \
 	m_translate.c \
diff --git a/coregrind/m_aspacemgr/aspacemgr.c b/coregrind/m_aspacemgr/aspacemgr.c
index 613cf22..0c4208e 100644
--- a/coregrind/m_aspacemgr/aspacemgr.c
+++ b/coregrind/m_aspacemgr/aspacemgr.c
@@ -30,7 +30,8 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
+#include "pub_core_threadstate.h"
 #include "pub_core_aspacemgr.h"
 #include "pub_core_libcbase.h"
 #include "pub_core_libcassert.h"
diff --git a/coregrind/m_aspacemgr/read_procselfmaps.c b/coregrind/m_aspacemgr/read_procselfmaps.c
index 0e6381d..f7b17df 100644
--- a/coregrind/m_aspacemgr/read_procselfmaps.c
+++ b/coregrind/m_aspacemgr/read_procselfmaps.c
@@ -29,8 +29,8 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-
-#include "core.h"
+#include "pub_core_basics.h"
+#include "pub_core_threadstate.h"   // needed for pub_core_aspacemgr.h
 #include "pub_core_aspacemgr.h"
 #include "pub_core_libcbase.h"
 #include "pub_core_libcassert.h"
diff --git a/coregrind/m_cpuid.S b/coregrind/m_cpuid.S
index c08a151..fd40a77 100644
--- a/coregrind/m_cpuid.S
+++ b/coregrind/m_cpuid.S
@@ -28,7 +28,7 @@
   The GNU General Public License is contained in the file COPYING.
 */
 
-#include "pub_basics_asm.h"
+#include "pub_tool_basics_asm.h"
 
 /*
     Bool VG_(has_cpuid)(void)
diff --git a/coregrind/m_debuginfo/dwarf.c b/coregrind/m_debuginfo/dwarf.c
index 3b89dbe..2e77dd7 100644
--- a/coregrind/m_debuginfo/dwarf.c
+++ b/coregrind/m_debuginfo/dwarf.c
@@ -28,7 +28,7 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
 #include "pub_core_libcbase.h"
 #include "pub_core_libcassert.h"
 #include "pub_core_libcprint.h"
diff --git a/coregrind/m_debuginfo/stabs.c b/coregrind/m_debuginfo/stabs.c
index 14e0a38..3540695 100644
--- a/coregrind/m_debuginfo/stabs.c
+++ b/coregrind/m_debuginfo/stabs.c
@@ -28,7 +28,7 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
 #include "pub_core_libcbase.h"
 #include "pub_core_libcassert.h"
 #include "pub_core_libcprint.h"
diff --git a/coregrind/m_debuginfo/symtab.c b/coregrind/m_debuginfo/symtab.c
index 5815a7f..a381162 100644
--- a/coregrind/m_debuginfo/symtab.c
+++ b/coregrind/m_debuginfo/symtab.c
@@ -29,7 +29,8 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
+#include "pub_core_threadstate.h"
 #include "pub_core_aspacemgr.h"
 #include "pub_core_demangle.h"
 #include "pub_core_libcbase.h"
diff --git a/coregrind/m_debuginfo/symtypes.c b/coregrind/m_debuginfo/symtypes.c
index 0b74a4e..6147a7f 100644
--- a/coregrind/m_debuginfo/symtypes.c
+++ b/coregrind/m_debuginfo/symtypes.c
@@ -28,7 +28,7 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
 #include "pub_core_debuginfo.h"
 #include "pub_core_debuglog.h"    /* VG_(debugLog_vprintf) */
 #include "pub_core_libcbase.h"
diff --git a/coregrind/m_debuglog.c b/coregrind/m_debuglog.c
index 8c47ccc..8cd0136 100644
--- a/coregrind/m_debuglog.c
+++ b/coregrind/m_debuglog.c
@@ -42,12 +42,12 @@
    latter depend on this module.  DO NOT MESS WITH THESE INCLUDES
    UNLESS YOU ARE 100% CERTAIN YOU UNDERSTAND THE CONSEQUENCES.
 */
-/* This module is also different from all others in the sense that it
-   is linked into both stage1 and stage2.  
-*/
-#include "basic_types.h"        /* basic types */
-#include "pub_core_debuglog.h"  /* our own iface */
 
+/* This module is also notable because it is linked into both 
+   stage1 and stage2. */
+
+#include "pub_core_basics.h"     /* basic types */
+#include "pub_core_debuglog.h"   /* our own iface */
 
 /*------------------------------------------------------------*/
 /*--- Stuff to make us completely independent.             ---*/
diff --git a/coregrind/m_demangle/cp-demangle.c b/coregrind/m_demangle/cp-demangle.c
index 33095ff..0e22abc 100644
--- a/coregrind/m_demangle/cp-demangle.c
+++ b/coregrind/m_demangle/cp-demangle.c
@@ -40,7 +40,7 @@
 #include <string.h>
 #endif*/
 
-#include "core.h"
+#include "pub_core_basics.h"
 #include "pub_core_libcbase.h"
 #include "pub_core_libcassert.h"
 #include "pub_core_mallocfree.h"
diff --git a/coregrind/m_demangle/cplus-dem.c b/coregrind/m_demangle/cplus-dem.c
index d1da6f2..b8bcc9a 100644
--- a/coregrind/m_demangle/cplus-dem.c
+++ b/coregrind/m_demangle/cplus-dem.c
@@ -37,7 +37,7 @@
 #endif
 
 #include "safe-ctype.h"
-#include "core.h"
+#include "pub_core_basics.h"
 #include "pub_core_libcbase.h"
 #include "pub_core_libcassert.h"
 #include "pub_tool_libcprint.h"
diff --git a/coregrind/m_demangle/demangle.c b/coregrind/m_demangle/demangle.c
index 7100ba8..82f8e18 100644
--- a/coregrind/m_demangle/demangle.c
+++ b/coregrind/m_demangle/demangle.c
@@ -28,7 +28,7 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
 #include "pub_core_demangle.h"
 #include "pub_core_libcbase.h"
 #include "pub_core_mallocfree.h"
diff --git a/coregrind/m_demangle/dyn-string.c b/coregrind/m_demangle/dyn-string.c
index c5220fc..1c0466b 100644
--- a/coregrind/m_demangle/dyn-string.c
+++ b/coregrind/m_demangle/dyn-string.c
@@ -31,7 +31,7 @@
 #include <stdlib.h>
 #endif*/
 
-#include "core.h"
+#include "pub_core_basics.h"
 #include "pub_core_libcbase.h"
 #include "pub_core_libcassert.h"
 #include "pub_core_mallocfree.h"
diff --git a/coregrind/m_dispatch/dispatch-amd64.S b/coregrind/m_dispatch/dispatch-amd64.S
index f328352..9c178d9 100644
--- a/coregrind/m_dispatch/dispatch-amd64.S
+++ b/coregrind/m_dispatch/dispatch-amd64.S
@@ -29,7 +29,7 @@
   The GNU General Public License is contained in the file COPYING.
 */
 
-#include "pub_basics_asm.h"
+#include "pub_tool_basics_asm.h"
 #include "pub_core_dispatch_asm.h"
 #include "pub_core_transtab_asm.h"
 #include "libvex_guest_offsets.h"	/* for OFFSET_amd64_RIP */
diff --git a/coregrind/m_dispatch/dispatch-arm.S b/coregrind/m_dispatch/dispatch-arm.S
index 3372a0e..a7cf36f 100644
--- a/coregrind/m_dispatch/dispatch-arm.S
+++ b/coregrind/m_dispatch/dispatch-arm.S
@@ -29,7 +29,7 @@
   The GNU General Public License is contained in the file COPYING.
 */
 
-#include "pub_basics_asm.h"
+#include "pub_tool_basics_asm.h"
 #include "pub_core_dispatch_asm.h"
 #include "pub_core_transtab_asm.h"
 
diff --git a/coregrind/m_dispatch/dispatch-x86.S b/coregrind/m_dispatch/dispatch-x86.S
index d90bb3d..1e97563 100644
--- a/coregrind/m_dispatch/dispatch-x86.S
+++ b/coregrind/m_dispatch/dispatch-x86.S
@@ -29,7 +29,7 @@
   The GNU General Public License is contained in the file COPYING.
 */
 
-#include "pub_basics_asm.h"
+#include "pub_tool_basics_asm.h"
 #include "pub_core_dispatch_asm.h"
 #include "pub_core_transtab_asm.h"
 #include "libvex_guest_offsets.h"	/* for OFFSET_x86_EIP */
diff --git a/coregrind/m_errormgr.c b/coregrind/m_errormgr.c
index 7ac1394..a192e35 100644
--- a/coregrind/m_errormgr.c
+++ b/coregrind/m_errormgr.c
@@ -28,7 +28,8 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
+#include "pub_core_threadstate.h"      // needed for pub_core_main.h
 #include "pub_core_debuginfo.h"
 #include "pub_core_errormgr.h"
 #include "pub_core_execontext.h"
@@ -90,7 +91,7 @@
 /* Errors.  Extensible (via the 'extra' field).  Tools can use a normal
    enum (with element values in the normal range (0..)) for 'ekind'. 
    Functions for getting/setting the tool-relevant fields are in
-   include/tool.h.
+   include/pub_tool_errormgr.h.
 
    When errors are found and recorded with VG_(maybe_record_error)(), all
    the tool must do is pass in the four parameters;  core will
@@ -174,7 +175,7 @@
    SuppLoc;
 
 /* Suppressions.  Tools can get/set tool-relevant parts with functions
-   declared in include/tool.h.  Extensible via the 'extra' field. 
+   declared in include/pub_tool_errormgr.h.  Extensible via the 'extra' field. 
    Tools can use a normal enum (with element values in the normal range
    (0..)) for 'skind'. */
 struct _Supp {
diff --git a/coregrind/m_execontext.c b/coregrind/m_execontext.c
index 15b5d70..649e746 100644
--- a/coregrind/m_execontext.c
+++ b/coregrind/m_execontext.c
@@ -28,7 +28,7 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
 #include "pub_core_execontext.h"
 #include "pub_core_libcassert.h"
 #include "pub_core_libcprint.h"
diff --git a/coregrind/m_hashtable.c b/coregrind/m_hashtable.c
index c5312c5..fd8ace0 100644
--- a/coregrind/m_hashtable.c
+++ b/coregrind/m_hashtable.c
@@ -28,7 +28,7 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
 #include "pub_core_hashtable.h"
 #include "pub_core_libcassert.h"
 #include "pub_core_mallocfree.h"
diff --git a/coregrind/m_libcassert.c b/coregrind/m_libcassert.c
index d5825bd..ecaf6b6 100644
--- a/coregrind/m_libcassert.c
+++ b/coregrind/m_libcassert.c
@@ -28,12 +28,14 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
+#include "pub_core_threadstate.h"
 #include "pub_core_libcbase.h"
 #include "pub_core_libcassert.h"
 #include "pub_core_libcprint.h"
 #include "pub_core_libcproc.h"
-#include "pub_core_main.h"
+#include "pub_core_main.h"          // for VG_(bbs_done) -- stupid!
+#include "pub_core_options.h"       // for VG_(bbs_done) -- stupid!
 #include "pub_core_stacktrace.h"
 #include "pub_core_syscall.h"
 #include "pub_core_tooliface.h"
@@ -70,6 +72,21 @@
    vg_assert(2+2 == 5);
 }
 
+// Print the scheduler status.
+static void pp_sched_status ( void )
+{
+   Int i; 
+   VG_(printf)("\nsched status:\n"); 
+   VG_(printf)("  running_tid=%d\n", VG_(get_running_tid)());
+   for (i = 1; i < VG_N_THREADS; i++) {
+      if (VG_(threads)[i].status == VgTs_Empty) continue;
+      VG_(printf)( "\nThread %d: status = %s\n", i, 
+                   VG_(name_of_ThreadStatus)(VG_(threads)[i].status) );
+      VG_(get_and_pp_StackTrace)( i, VG_(clo_backtrace_size) );
+   }
+   VG_(printf)("\n");
+}
+
 __attribute__ ((noreturn))
 static void report_and_quit ( const Char* report, Addr ip, Addr sp, Addr fp )
 {
@@ -95,7 +112,7 @@
 
    VG_(printf)("\nBasic block ctr is approximately %llu\n", VG_(bbs_done) );
 
-   VG_(pp_sched_status)();
+   pp_sched_status();
    VG_(printf)("\n");
    VG_(printf)("Note: see also the FAQ.txt in the source distribution.\n");
    VG_(printf)("It contains workarounds to several common problems.\n");
@@ -196,7 +213,7 @@
       "Valgrind has to exit now.  Sorry.  Bye!");
    VG_(message)(Vg_UserMsg,
       "");
-   VG_(pp_sched_status)();
+   pp_sched_status();
    VG_(exit)(1);
 }
 
diff --git a/coregrind/m_libcbase.c b/coregrind/m_libcbase.c
index 1ab10d3..7369cb3 100644
--- a/coregrind/m_libcbase.c
+++ b/coregrind/m_libcbase.c
@@ -28,7 +28,7 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"       // XXX: temporary, for NULL, VG_(), Char, etc
+#include "pub_core_basics.h"
 #include "pub_core_libcbase.h"
 
 /* ---------------------------------------------------------------------
diff --git a/coregrind/m_libcfile.c b/coregrind/m_libcfile.c
index aa217b2..0f4e6c1 100644
--- a/coregrind/m_libcfile.c
+++ b/coregrind/m_libcfile.c
@@ -28,7 +28,7 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
 #include "pub_core_libcbase.h"
 #include "pub_core_libcassert.h"
 #include "pub_core_libcfile.h"
diff --git a/coregrind/m_libcmman.c b/coregrind/m_libcmman.c
index d3c66f1..a39d472 100644
--- a/coregrind/m_libcmman.c
+++ b/coregrind/m_libcmman.c
@@ -28,7 +28,7 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
 #include "pub_core_aspacemgr.h"
 #include "pub_core_libcbase.h"
 #include "pub_core_libcassert.h"
diff --git a/coregrind/m_libcprint.c b/coregrind/m_libcprint.c
index b4bdda3..88e5a7e 100644
--- a/coregrind/m_libcprint.c
+++ b/coregrind/m_libcprint.c
@@ -28,7 +28,7 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
 #include "pub_core_debuglog.h"
 #include "pub_core_libcbase.h"
 #include "pub_core_libcassert.h"
diff --git a/coregrind/m_libcproc.c b/coregrind/m_libcproc.c
index bb6997c..f5249b5 100644
--- a/coregrind/m_libcproc.c
+++ b/coregrind/m_libcproc.c
@@ -28,7 +28,7 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
 #include "pub_core_libcbase.h"
 #include "pub_core_libcassert.h"
 #include "pub_core_libcprint.h"
diff --git a/coregrind/m_libcsignal.c b/coregrind/m_libcsignal.c
index d32d7ee..4810da3 100644
--- a/coregrind/m_libcsignal.c
+++ b/coregrind/m_libcsignal.c
@@ -28,7 +28,7 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
 #include "pub_core_libcbase.h"
 #include "pub_core_libcassert.h"
 #include "pub_core_libcsignal.h"
diff --git a/coregrind/m_machine.c b/coregrind/m_machine.c
index 09cebc3..bca38a9 100644
--- a/coregrind/m_machine.c
+++ b/coregrind/m_machine.c
@@ -28,11 +28,11 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
+#include "pub_core_threadstate.h"
 #include "pub_core_libcassert.h"
 #include "pub_core_libcbase.h"
 #include "pub_core_machine.h"
-#include "pub_core_scheduler.h"
 
 #define INSTR_PTR(regs)    ((regs).vex.VGA_INSTR_PTR)
 #define STACK_PTR(regs)    ((regs).vex.VGA_STACK_PTR)
diff --git a/coregrind/m_main.c b/coregrind/m_main.c
index 702df47..3c465c4 100644
--- a/coregrind/m_main.c
+++ b/coregrind/m_main.c
@@ -30,8 +30,8 @@
 
 #define _FILE_OFFSET_BITS 64
 
-#include "core.h"
-#include "ume.h"
+#include "pub_core_basics.h"
+#include "pub_core_threadstate.h"
 #include "pub_core_aspacemgr.h"
 #include "pub_core_debuglog.h"
 #include "pub_core_errormgr.h"
@@ -49,11 +49,13 @@
 #include "pub_core_options.h"
 #include "pub_core_profile.h"
 #include "pub_core_redir.h"
+#include "pub_core_scheduler.h"
 #include "pub_core_signals.h"
 #include "pub_core_syswrap.h"
 #include "pub_core_tooliface.h"
 #include "pub_core_trampoline.h"
 #include "pub_core_transtab.h"
+#include "ume.h"
 
 #include <dirent.h>
 #include <dlfcn.h>
diff --git a/coregrind/m_mallocfree.c b/coregrind/m_mallocfree.c
index 3e74e3d..28ea67c 100644
--- a/coregrind/m_mallocfree.c
+++ b/coregrind/m_mallocfree.c
@@ -29,8 +29,7 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-
-#include "core.h"
+#include "pub_core_basics.h"
 #include "pub_core_aspacemgr.h"
 #include "pub_core_libcbase.h"
 #include "pub_core_libcassert.h"
diff --git a/coregrind/m_options.c b/coregrind/m_options.c
index 40f2251..24d76f8 100644
--- a/coregrind/m_options.c
+++ b/coregrind/m_options.c
@@ -29,7 +29,7 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
 #include "pub_core_options.h"
 
 // See pub_{core,tool}_options.h for explanations of all these.
diff --git a/coregrind/m_profile.c b/coregrind/m_profile.c
index 965503a..212358f 100644
--- a/coregrind/m_profile.c
+++ b/coregrind/m_profile.c
@@ -28,7 +28,7 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
 #include "pub_core_libcassert.h"
 #include "pub_core_libcprint.h"
 #include "pub_core_profile.h"
diff --git a/coregrind/m_pthreadmodel.c b/coregrind/m_pthreadmodel.c
index 33fd2bf..76c0bd3 100644
--- a/coregrind/m_pthreadmodel.c
+++ b/coregrind/m_pthreadmodel.c
@@ -53,7 +53,7 @@
    pthread_t identifiers and internal thread identifiers.
  */
 
-#include "core.h"
+#include "pub_core_basics.h"
 
 #if 0
 
diff --git a/coregrind/m_redir.c b/coregrind/m_redir.c
index 3d5988c..155812f 100644
--- a/coregrind/m_redir.c
+++ b/coregrind/m_redir.c
@@ -29,13 +29,14 @@
 
    The GNU General Public License is contained in the file COPYING.
 */
-#include "core.h"
 
+#include "pub_core_basics.h"
+#include "pub_core_threadstate.h"   // needed for pub_core_main.h
 #include "pub_core_aspacemgr.h"
 #include "pub_core_libcbase.h"
 #include "pub_core_libcassert.h"
 #include "pub_core_libcprint.h"
-#include "pub_core_main.h"
+#include "pub_core_main.h"       // for VG_(set_libc_freeres_wrapper_addr)
 #include "pub_core_mallocfree.h"
 #include "pub_core_options.h"
 #include "pub_core_redir.h"
diff --git a/coregrind/m_replacemalloc/replacemalloc_core.c b/coregrind/m_replacemalloc/replacemalloc_core.c
index 76dbdc6..952e089 100644
--- a/coregrind/m_replacemalloc/replacemalloc_core.c
+++ b/coregrind/m_replacemalloc/replacemalloc_core.c
@@ -28,7 +28,7 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
 #include "pub_core_libcbase.h"
 #include "pub_core_libcprint.h"
 #include "pub_core_mallocfree.h"
diff --git a/coregrind/m_replacemalloc/vg_replace_malloc.c b/coregrind/m_replacemalloc/vg_replace_malloc.c
index be1a26f..df5603e 100644
--- a/coregrind/m_replacemalloc/vg_replace_malloc.c
+++ b/coregrind/m_replacemalloc/vg_replace_malloc.c
@@ -47,7 +47,7 @@
 #include "valgrind.h"               // for VALGRIND_NON_SIMD_CALL[12]
 #include "coregrind.h"              // for VALGRIND_INTERNAL_PRINTF
 
-#include "core.h"
+#include "pub_core_basics.h"
 #include "pub_core_debuginfo.h"     // needed for pub_core_redir.h :(
 #include "pub_core_mallocfree.h"    // for VG_MIN_MALLOC_SZB, VG_AR_CLIENT
 #include "pub_core_redir.h"         // for VG_REPLACE_FUNCTION
diff --git a/coregrind/m_scheduler/scheduler.c b/coregrind/m_scheduler/scheduler.c
index 1d149e5..762e80e 100644
--- a/coregrind/m_scheduler/scheduler.c
+++ b/coregrind/m_scheduler/scheduler.c
@@ -60,7 +60,8 @@
 #include "valgrind.h"   // for VG_USERREQ__*
 #include "coregrind.h"  // for VG_USERREQ__*
 
-#include "core.h"
+#include "pub_core_basics.h"
+#include "pub_core_threadstate.h"
 #include "pub_core_aspacemgr.h"
 #include "pub_core_dispatch.h"
 #include "pub_core_errormgr.h"
@@ -90,18 +91,13 @@
    Types and globals for the scheduler.
    ------------------------------------------------------------------ */
 
-/* ThreadId and ThreadState are defined in core.h. */
+/* ThreadId and ThreadState are defined elsewhere*/
 
 /* Defines the thread-scheduling timeslice, in terms of the number of
    basic blocks we attempt to run each thread for.  Smaller values
    give finer interleaving but much increased scheduling overheads. */
 #define SCHEDULING_QUANTUM   50000
 
-/* Globals.  A statically allocated array of threads.  NOTE: [0] is
-   never used, to simplify the simulation of initialisers for
-   LinuxThreads. */
-ThreadState VG_(threads)[VG_N_THREADS];
-
 /* If true, a fault is Valgrind-internal (ie, a bug) */
 Bool VG_(my_fault) = True;
 
@@ -112,7 +108,6 @@
 static void do_client_request ( ThreadId tid );
 static void scheduler_sanity ( ThreadId tid );
 static void mostly_clear_thread_record ( ThreadId tid );
-static const HChar *name_of_thread_state ( ThreadStatus );
 
 /* Stats. */
 static UInt n_scheduling_events_MINOR = 0;
@@ -128,58 +123,12 @@
 
 /* CPU semaphore, so that threads can run exclusively */
 static vg_sema_t run_sema;
-static ThreadId running_tid = VG_INVALID_THREADID;
 
 
 /* ---------------------------------------------------------------------
    Helper functions for the scheduler.
    ------------------------------------------------------------------ */
 
-__inline__
-Bool VG_(is_valid_tid) ( ThreadId tid )
-{
-   /* tid is unsigned, hence no < 0 test. */
-   if (tid == 0) return False;
-   if (tid >= VG_N_THREADS) return False;
-   if (VG_(threads)[tid].status == VgTs_Empty) return False;
-   return True;
-}
-
-
-/* For constructing error messages only: try and identify a thread
-   whose stack satisfies the predicate p, or return VG_INVALID_THREADID
-   if none do.
-*/
-ThreadId VG_(first_matching_thread_stack)
-              ( Bool (*p) ( Addr stack_min, Addr stack_max, void* d ),
-                void* d )
-{
-   ThreadId tid;
-
-   for (tid = 1; tid < VG_N_THREADS; tid++) {
-      if (VG_(threads)[tid].status == VgTs_Empty) continue;
-
-      if ( p ( VG_(get_SP)(tid),
-               VG_(threads)[tid].client_stack_highest_word, d ) )
-         return tid;
-   }
-   return VG_INVALID_THREADID;
-}
- 
-/* Print the scheduler status. */
-void VG_(pp_sched_status) ( void )
-{
-   Int i; 
-   VG_(printf)("\nsched status:\n"); 
-   VG_(printf)("  running_tid=%d\n", running_tid);
-   for (i = 1; i < VG_N_THREADS; i++) {
-      if (VG_(threads)[i].status == VgTs_Empty) continue;
-      VG_(printf)("\nThread %d: status = %s\n", i, name_of_thread_state(VG_(threads)[i].status));
-      VG_(get_and_pp_StackTrace)( i, VG_(clo_backtrace_size) );
-   }
-   VG_(printf)("\n");
-}
-
 static
 void print_sched_event ( ThreadId tid, Char* what )
 {
@@ -201,20 +150,6 @@
   }
 }
 
-static
-const HChar* name_of_thread_state ( ThreadStatus state )
-{
-   switch (state) {
-   case VgTs_Empty:     return "VgTs_Empty";
-   case VgTs_Init:      return "VgTs_Init";
-   case VgTs_Runnable:  return "VgTs_Runnable";
-   case VgTs_WaitSys:   return "VgTs_WaitSys";
-   case VgTs_Yielding:  return "VgTs_Yielding";
-   case VgTs_Zombie:    return "VgTs_Zombie";
-   default:             return "VgTs_???";
-  }
-}
-
 /* Allocate a completely empty ThreadState record. */
 ThreadId VG_(alloc_ThreadState) ( void )
 {
@@ -232,25 +167,6 @@
    /*NOTREACHED*/
 }
 
-ThreadState *VG_(get_ThreadState)(ThreadId tid)
-{
-   vg_assert(tid >= 0 && tid < VG_N_THREADS);
-   return &VG_(threads)[tid];
-}
-
-/* Given an LWP id (ie, real kernel thread id), find the corresponding
-   ThreadId */
-ThreadId VG_(get_lwp_tid)(Int lwp)
-{
-   ThreadId tid;
-   
-   for(tid = 1; tid < VG_N_THREADS; tid++)
-      if (VG_(threads)[tid].status != VgTs_Empty && VG_(threads)[tid].os_state.lwpid == lwp)
-	 return tid;
-
-   return VG_INVALID_THREADID;
-}
-
 /* 
    Mark a thread as Runnable.  This will block until the run_sema is
    available, so that we get exclusive access to all the shared
@@ -268,44 +184,15 @@
    tst->status = VgTs_Runnable;
    
    VG_(sema_down)(&run_sema);
-   if (running_tid != VG_INVALID_THREADID)
-      VG_(printf)("tid %d found %d running\n", tid, running_tid);
-   vg_assert(running_tid == VG_INVALID_THREADID);
-   running_tid = tid;
+   if (VG_(running_tid) != VG_INVALID_THREADID)
+      VG_(printf)("tid %d found %d running\n", tid, VG_(running_tid));
+   vg_assert(VG_(running_tid) == VG_INVALID_THREADID);
+   VG_(running_tid) = tid;
 
    if (VG_(clo_trace_sched))
       print_sched_event(tid, "now running");
 }
 
-ThreadId VG_(get_running_tid)(void)
-{
-   return running_tid;
-}
-
-Bool VG_(is_running_thread)(ThreadId tid)
-{
-   ThreadState *tst = VG_(get_ThreadState)(tid);
-
-   return 
-//      tst->os_state.lwpid == VG_(gettid)() &&	/* check we're this tid */
-      running_tid == tid	           &&	/* and that we've got the lock */
-      tst->status == VgTs_Runnable;		/* and we're runnable */
-}
-
-/* Return the number of non-dead Threads */
-Int VG_(count_living_threads)(void)
-{
-   Int count = 0;
-   ThreadId tid;
-
-   for(tid = 1; tid < VG_N_THREADS; tid++)
-      if (VG_(threads)[tid].status != VgTs_Empty &&
-	  VG_(threads)[tid].status != VgTs_Zombie)
-	 count++;
-
-   return count;
-}
-
 /* 
    Set a thread into a sleeping state, and give up exclusive access to
    the CPU.  On return, the thread must be prepared to block until it
@@ -324,8 +211,8 @@
 
    tst->status = sleepstate;
 
-   vg_assert(running_tid == tid);
-   running_tid = VG_INVALID_THREADID;
+   vg_assert(VG_(running_tid) == tid);
+   VG_(running_tid) = VG_INVALID_THREADID;
 
    /* Release the run_sema; this will reschedule any runnable
       thread. */
@@ -334,19 +221,11 @@
    if (VG_(clo_trace_sched)) {
       Char buf[50];
       VG_(sprintf)(buf, "now sleeping in state %s", 
-                        name_of_thread_state(sleepstate));
+                        VG_(name_of_ThreadStatus)(sleepstate));
       print_sched_event(tid, buf);
    }
 }
 
-/* Return true if the thread is still alive but in the process of
-   exiting. */
-inline Bool VG_(is_exiting)(ThreadId tid)
-{
-   vg_assert(VG_(is_valid_tid)(tid));
-   return VG_(threads)[tid].exitreason != VgSrc_None;
-}
-
 /* Clear out the ThreadState and release the semaphore. Leaves the
    ThreadState in VgTs_Zombie state, so that it doesn't get
    reallocated until the caller is really ready. */
@@ -357,7 +236,7 @@
    vg_assert(VG_(is_exiting)(tid));
 
    mostly_clear_thread_record(tid);
-   running_tid = VG_INVALID_THREADID;
+   VG_(running_tid) = VG_INVALID_THREADID;
 
    /* There should still be a valid exitreason for this thread */
    vg_assert(VG_(threads)[tid].exitreason != VgSrc_None);
@@ -388,7 +267,7 @@
 void VG_(vg_yield)(void)
 {
    struct vki_timespec ts = { 0, 1 };
-   ThreadId tid = running_tid;
+   ThreadId tid = VG_(running_tid);
 
    vg_assert(tid != VG_INVALID_THREADID);
    vg_assert(VG_(threads)[tid].os_state.lwpid == VG_(gettid)());
@@ -609,7 +488,7 @@
 static void sched_fork_cleanup(ThreadId me)
 {
    ThreadId tid;
-   vg_assert(running_tid == me);
+   vg_assert(VG_(running_tid) == me);
 
    VG_(threads)[me].os_state.lwpid = VG_(gettid)();
    VG_(threads)[me].os_state.threadgroup = VG_(getpid)();
@@ -708,8 +587,8 @@
    SCHEDSETJMP(tid, jumped, 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_(printf)("tid %d not running; VG_(running_tid)=%d, tid %d status %d\n",
+		  tid, VG_(running_tid), tid, tst->status);
    vg_assert(VG_(is_running_thread)(tid));
    
    if (jumped) {
@@ -1159,7 +1038,7 @@
    if (!VG_(is_running_thread)(tid)) {
       VG_(message)(Vg_DebugMsg,
 		   "Thread %d is supposed to be running, but doesn't own run_sema (owned by %d)\n", 
-		   tid, running_tid);
+		   tid, VG_(running_tid));
       bad = True;
    }
 
diff --git a/coregrind/m_scheduler/sema.c b/coregrind/m_scheduler/sema.c
index a01aed7..0d12923 100644
--- a/coregrind/m_scheduler/sema.c
+++ b/coregrind/m_scheduler/sema.c
@@ -28,7 +28,7 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
 #include "pub_core_libcassert.h"
 #include "pub_core_libcfile.h"
 #include "pub_core_libcproc.h"
diff --git a/coregrind/m_sigframe/sigframe-amd64-linux.c b/coregrind/m_sigframe/sigframe-amd64-linux.c
index 6d0780e..a3246bc 100644
--- a/coregrind/m_sigframe/sigframe-amd64-linux.c
+++ b/coregrind/m_sigframe/sigframe-amd64-linux.c
@@ -29,7 +29,8 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
+#include "pub_core_threadstate.h"
 #include "pub_core_aspacemgr.h"
 #include "pub_core_libcbase.h"
 #include "pub_core_libcassert.h"
@@ -41,8 +42,6 @@
 #include "pub_core_tooliface.h"
 #include "pub_core_trampoline.h"
 
-#include "libvex_guest_amd64.h"
-
 
 /* This module creates and removes signal frames for signal deliveries
    on amd64-linux.
diff --git a/coregrind/m_sigframe/sigframe-arm-linux.c b/coregrind/m_sigframe/sigframe-arm-linux.c
index 49704f3..767162e 100644
--- a/coregrind/m_sigframe/sigframe-arm-linux.c
+++ b/coregrind/m_sigframe/sigframe-arm-linux.c
@@ -28,7 +28,7 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
 
 #include "libvex_guest_arm.h"
 
diff --git a/coregrind/m_sigframe/sigframe-x86-linux.c b/coregrind/m_sigframe/sigframe-x86-linux.c
index 6000657..05af319 100644
--- a/coregrind/m_sigframe/sigframe-x86-linux.c
+++ b/coregrind/m_sigframe/sigframe-x86-linux.c
@@ -29,7 +29,8 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
+#include "pub_core_threadstate.h"
 #include "pub_core_aspacemgr.h" /* find_segment */
 #include "pub_core_libcbase.h"
 #include "pub_core_libcassert.h"
@@ -41,8 +42,6 @@
 #include "pub_core_tooliface.h"
 #include "pub_core_trampoline.h"
 
-#include "libvex_guest_x86.h"
-
 
 /* This module creates and removes signal frames for signal deliveries
    on x86-linux.
diff --git a/coregrind/m_signals.c b/coregrind/m_signals.c
index 7f082ad..63dfd3a 100644
--- a/coregrind/m_signals.c
+++ b/coregrind/m_signals.c
@@ -79,8 +79,8 @@
    always unblocked).
  */
 
-#include "core.h"
-
+#include "pub_core_basics.h"
+#include "pub_core_threadstate.h"
 #include "pub_core_aspacemgr.h"
 #include "pub_core_errormgr.h"
 #include "pub_core_libcbase.h"
@@ -93,6 +93,7 @@
 #include "pub_core_main.h"
 #include "pub_core_mallocfree.h"
 #include "pub_core_options.h"
+#include "pub_core_scheduler.h"
 #include "pub_core_signals.h"
 #include "pub_core_sigframe.h"
 #include "pub_core_stacktrace.h"
diff --git a/coregrind/m_skiplist.c b/coregrind/m_skiplist.c
index 0a80f5c..796b1d3 100644
--- a/coregrind/m_skiplist.c
+++ b/coregrind/m_skiplist.c
@@ -86,7 +86,7 @@
    won't be accidentally used.
  */
 
-#include "core.h"
+#include "pub_core_basics.h"
 #include "pub_core_libcbase.h"
 #include "pub_core_libcassert.h"
 #include "pub_core_libcprint.h"
diff --git a/coregrind/m_stacktrace.c b/coregrind/m_stacktrace.c
index 2f5fb83..38adf15 100644
--- a/coregrind/m_stacktrace.c
+++ b/coregrind/m_stacktrace.c
@@ -28,7 +28,8 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
+#include "pub_core_threadstate.h"
 #include "pub_core_aspacemgr.h"
 #include "pub_core_libcbase.h"
 #include "pub_core_libcassert.h"
diff --git a/coregrind/m_syscall.c b/coregrind/m_syscall.c
index 06b6cf0..b3a7036 100644
--- a/coregrind/m_syscall.c
+++ b/coregrind/m_syscall.c
@@ -28,7 +28,7 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
 #include "pub_core_syscall.h"
 
 /* ---------------------------------------------------------------------
diff --git a/coregrind/m_syswrap/syscall-amd64-linux.S b/coregrind/m_syswrap/syscall-amd64-linux.S
index 200c1b8..6fa4271 100644
--- a/coregrind/m_syswrap/syscall-amd64-linux.S
+++ b/coregrind/m_syswrap/syscall-amd64-linux.S
@@ -28,7 +28,7 @@
   The GNU General Public License is contained in the file COPYING.
 */
 
-#include "pub_basics_asm.h"
+#include "pub_tool_basics_asm.h"
 #include "vki_unistd.h"
 #include "libvex_guest_offsets.h"
 
diff --git a/coregrind/m_syswrap/syscall-arm-linux.S b/coregrind/m_syswrap/syscall-arm-linux.S
index c1c319a..4423863 100644
--- a/coregrind/m_syswrap/syscall-arm-linux.S
+++ b/coregrind/m_syswrap/syscall-arm-linux.S
@@ -28,7 +28,7 @@
   The GNU General Public License is contained in the file COPYING.
 */
 
-#include "pub_basics_asm.h"
+#include "pub_tool_basics_asm.h"
 #include "vki_unistd.h"
 
 # XXX: must reinstate comments also -- see x86-linux/syscall.S
diff --git a/coregrind/m_syswrap/syscall-x86-linux.S b/coregrind/m_syswrap/syscall-x86-linux.S
index 14986f1..9b2761a 100644
--- a/coregrind/m_syswrap/syscall-x86-linux.S
+++ b/coregrind/m_syswrap/syscall-x86-linux.S
@@ -28,7 +28,7 @@
   The GNU General Public License is contained in the file COPYING.
 */
 
-#include "pub_basics_asm.h"
+#include "pub_tool_basics_asm.h"
 #include "vki_unistd.h"
 #include "libvex_guest_offsets.h"
 		
diff --git a/coregrind/m_syswrap/syswrap-amd64-linux.c b/coregrind/m_syswrap/syswrap-amd64-linux.c
index 9baa6f2..f8dd3d2 100644
--- a/coregrind/m_syswrap/syswrap-amd64-linux.c
+++ b/coregrind/m_syswrap/syswrap-amd64-linux.c
@@ -28,7 +28,8 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
+#include "pub_core_threadstate.h"
 #include "pub_core_debuglog.h"
 #include "pub_core_aspacemgr.h"
 #include "pub_core_options.h"
@@ -38,6 +39,8 @@
 #include "pub_core_libcprint.h"
 #include "pub_core_libcproc.h"
 #include "pub_core_libcsignal.h"
+#include "pub_core_main.h"
+#include "pub_core_scheduler.h"
 #include "pub_core_sigframe.h"
 #include "pub_core_signals.h"
 #include "pub_core_syscall.h"
diff --git a/coregrind/m_syswrap/syswrap-arm-linux.c b/coregrind/m_syswrap/syswrap-arm-linux.c
index 735d1cc..e24df5c 100644
--- a/coregrind/m_syswrap/syswrap-arm-linux.c
+++ b/coregrind/m_syswrap/syswrap-arm-linux.c
@@ -28,12 +28,6 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
-
-
-// See the comment accompanying the declaration of VGA_(thread_syscall)() in
-// coregrind/core.h for an explanation of what this does, and why.
-//
 // XXX: this function and these variables should be assembly code!  See the
 // x86 version.
 const Addr VGA_(sys_before), VGA_(sys_restarted),
diff --git a/coregrind/m_syswrap/syswrap-generic.c b/coregrind/m_syswrap/syswrap-generic.c
index 44d25f3..ad719a7 100644
--- a/coregrind/m_syswrap/syswrap-generic.c
+++ b/coregrind/m_syswrap/syswrap-generic.c
@@ -28,7 +28,8 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
+#include "pub_core_threadstate.h"
 #include "pub_core_debuglog.h"
 #include "pub_core_aspacemgr.h"
 #include "pub_core_libcbase.h"
@@ -43,6 +44,7 @@
 #include "pub_core_stacktrace.h"
 #include "pub_core_tooliface.h"
 #include "pub_core_options.h"
+#include "pub_core_scheduler.h"
 #include "pub_core_signals.h"
 #include "pub_core_syscall.h"
 #include "pub_core_syswrap.h"
diff --git a/coregrind/m_syswrap/syswrap-linux.c b/coregrind/m_syswrap/syswrap-linux.c
index 5fb88df..00af0fe 100644
--- a/coregrind/m_syswrap/syswrap-linux.c
+++ b/coregrind/m_syswrap/syswrap-linux.c
@@ -28,7 +28,8 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
+#include "pub_core_threadstate.h"
 #include "pub_core_aspacemgr.h"
 #include "pub_core_debuglog.h"
 #include "pub_core_libcbase.h"
@@ -39,6 +40,7 @@
 #include "pub_core_mallocfree.h"
 #include "pub_core_tooliface.h"
 #include "pub_core_options.h"
+#include "pub_core_scheduler.h"
 #include "pub_core_signals.h"
 #include "pub_core_syscall.h"
 
diff --git a/coregrind/m_syswrap/syswrap-main.c b/coregrind/m_syswrap/syswrap-main.c
index d775efa..c26e1d7 100644
--- a/coregrind/m_syswrap/syswrap-main.c
+++ b/coregrind/m_syswrap/syswrap-main.c
@@ -29,12 +29,14 @@
 */
 
 #include "libvex_guest_offsets.h"
-#include "core.h"
+#include "pub_core_basics.h"
+#include "pub_core_threadstate.h"
 #include "pub_core_libcbase.h"
 #include "pub_core_libcassert.h"
 #include "pub_core_libcprint.h"
 #include "pub_core_libcproc.h"
 #include "pub_core_libcsignal.h"
+#include "pub_core_scheduler.h"
 #include "pub_core_stacktrace.h"
 #include "pub_core_tooliface.h"
 #include "pub_core_options.h"
diff --git a/coregrind/m_syswrap/syswrap-x86-linux.c b/coregrind/m_syswrap/syswrap-x86-linux.c
index e800393..5168e21 100644
--- a/coregrind/m_syswrap/syswrap-x86-linux.c
+++ b/coregrind/m_syswrap/syswrap-x86-linux.c
@@ -33,7 +33,8 @@
    to assign the shadow state.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
+#include "pub_core_threadstate.h"
 #include "pub_core_debuglog.h"
 #include "pub_core_aspacemgr.h"
 #include "pub_core_options.h"
@@ -43,7 +44,9 @@
 #include "pub_core_libcprint.h"
 #include "pub_core_libcproc.h"
 #include "pub_core_libcsignal.h"
+#include "pub_core_main.h"
 #include "pub_core_mallocfree.h"
+#include "pub_core_scheduler.h"
 #include "pub_core_sigframe.h"
 #include "pub_core_signals.h"
 #include "pub_core_syscall.h"
diff --git a/coregrind/m_threadmodel.c b/coregrind/m_threadmodel.c
index c920430..9787ac9 100644
--- a/coregrind/m_threadmodel.c
+++ b/coregrind/m_threadmodel.c
@@ -60,9 +60,8 @@
    introduce a separate notion of a "thread" for modelling purposes.
  */
 
-//:: 
-//:: #include "core.h"
-//:: 
+#include "pub_core_basics.h"
+
 //:: struct thread;
 //:: struct mutex;
 //:: struct condvar;
diff --git a/coregrind/m_threadstate.c b/coregrind/m_threadstate.c
new file mode 100644
index 0000000..b38642b
--- /dev/null
+++ b/coregrind/m_threadstate.c
@@ -0,0 +1,148 @@
+
+/*--------------------------------------------------------------------*/
+/*--- The thread state.                            m_threadstate.c ---*/
+/*--------------------------------------------------------------------*/
+
+/*
+   This file is part of Valgrind, a dynamic binary instrumentation
+   framework.
+
+   Copyright (C) 2000-2005 Julian Seward 
+      jseward@acm.org
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307, USA.
+
+   The GNU General Public License is contained in the file COPYING.
+*/
+
+#include "pub_core_basics.h"
+#include "pub_core_threadstate.h"
+#include "pub_core_libcassert.h"
+#include "pub_core_machine.h"       // For VG_(get_SP)
+
+/*------------------------------------------------------------*/
+/*--- Data structures.                                     ---*/
+/*------------------------------------------------------------*/
+
+ThreadId VG_(running_tid) = VG_INVALID_THREADID;
+
+ThreadState VG_(threads)[VG_N_THREADS];
+
+/*------------------------------------------------------------*/
+/*--- Operations.                                          ---*/
+/*------------------------------------------------------------*/
+
+const HChar* VG_(name_of_ThreadStatus) ( ThreadStatus status )
+{
+   switch (status) {
+   case VgTs_Empty:     return "VgTs_Empty";
+   case VgTs_Init:      return "VgTs_Init";
+   case VgTs_Runnable:  return "VgTs_Runnable";
+   case VgTs_WaitSys:   return "VgTs_WaitSys";
+   case VgTs_Yielding:  return "VgTs_Yielding";
+   case VgTs_Zombie:    return "VgTs_Zombie";
+   default:             return "VgTs_???";
+  }
+}
+
+ThreadState *VG_(get_ThreadState)(ThreadId tid)
+{
+   vg_assert(tid >= 0 && tid < VG_N_THREADS);
+   return &VG_(threads)[tid];
+}
+
+Bool VG_(is_valid_tid) ( ThreadId tid )
+{
+   /* tid is unsigned, hence no < 0 test. */
+   if (tid == 0) return False;
+   if (tid >= VG_N_THREADS) return False;
+   if (VG_(threads)[tid].status == VgTs_Empty) return False;
+   return True;
+}
+
+// This function is for tools to call.
+ThreadId VG_(get_running_tid)(void)
+{
+   return VG_(running_tid);
+}
+
+Bool VG_(is_running_thread)(ThreadId tid)
+{
+   ThreadState *tst = VG_(get_ThreadState)(tid);
+
+   return 
+//      tst->os_state.lwpid == VG_(gettid)() &&	// check we're this tid
+      VG_(running_tid) == tid	           &&	// and that we've got the lock
+      tst->status == VgTs_Runnable;		// and we're runnable
+}
+
+/* Return true if the thread is still alive but in the process of exiting. */
+inline Bool VG_(is_exiting)(ThreadId tid)
+{
+   vg_assert(VG_(is_valid_tid)(tid));
+   return VG_(threads)[tid].exitreason != VgSrc_None;
+}
+
+/* Return the number of non-dead Threads */
+Int VG_(count_living_threads)(void)
+{
+   Int count = 0;
+   ThreadId tid;
+
+   for(tid = 1; tid < VG_N_THREADS; tid++)
+      if (VG_(threads)[tid].status != VgTs_Empty &&
+	  VG_(threads)[tid].status != VgTs_Zombie)
+	 count++;
+
+   return count;
+}
+
+/* Given an LWP id (ie, real kernel thread id), find the corresponding
+   ThreadId */
+ThreadId VG_(get_lwp_tid)(Int lwp)
+{
+   ThreadId tid;
+   
+   for(tid = 1; tid < VG_N_THREADS; tid++)
+      if (VG_(threads)[tid].status != VgTs_Empty && VG_(threads)[tid].os_state.lwpid == lwp)
+	 return tid;
+
+   return VG_INVALID_THREADID;
+}
+
+/* For constructing error messages only: try and identify a thread
+   whose stack satisfies the predicate p, or return VG_INVALID_THREADID
+   if none do.
+*/
+ThreadId VG_(first_matching_thread_stack)
+              ( Bool (*p) ( Addr stack_min, Addr stack_max, void* d ),
+                void* d )
+{
+   ThreadId tid;
+
+   for (tid = 1; tid < VG_N_THREADS; tid++) {
+      if (VG_(threads)[tid].status == VgTs_Empty) continue;
+
+      if ( p ( VG_(get_SP)(tid),
+               VG_(threads)[tid].client_stack_highest_word, d ) )
+         return tid;
+   }
+   return VG_INVALID_THREADID;
+}
+ 
+/*--------------------------------------------------------------------*/
+/*--- end                                                          ---*/
+/*--------------------------------------------------------------------*/
diff --git a/coregrind/m_tooliface.c b/coregrind/m_tooliface.c
index fa0feda..c17e40a 100644
--- a/coregrind/m_tooliface.c
+++ b/coregrind/m_tooliface.c
@@ -29,7 +29,7 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
 #include "pub_core_aspacemgr.h"
 #include "pub_core_libcbase.h"
 #include "pub_core_libcassert.h"
diff --git a/coregrind/m_trampoline.S b/coregrind/m_trampoline.S
index c4d0d98..c57737c 100644
--- a/coregrind/m_trampoline.S
+++ b/coregrind/m_trampoline.S
@@ -28,7 +28,7 @@
   The GNU General Public License is contained in the file COPYING.
 */
 
-#include "pub_basics_asm.h"
+#include "pub_tool_basics_asm.h"
 #include "vki_unistd.h"
 
 /* ------------------ SIMULATED CPU HELPERS ------------------ */
diff --git a/coregrind/m_translate.c b/coregrind/m_translate.c
index 5442fa5..d64914d 100644
--- a/coregrind/m_translate.c
+++ b/coregrind/m_translate.c
@@ -29,7 +29,8 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
+#include "pub_core_threadstate.h"      // needed for pub_core_main.h
 #include "pub_core_aspacemgr.h"
 #include "pub_core_cpuid.h"
 #include "pub_core_libcbase.h"
diff --git a/coregrind/m_transtab.c b/coregrind/m_transtab.c
index 8ef0f9f..e74cbcc 100644
--- a/coregrind/m_transtab.c
+++ b/coregrind/m_transtab.c
@@ -29,7 +29,7 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "core.h"
+#include "pub_core_basics.h"
 #include "pub_core_debuginfo.h"
 #include "pub_core_libcbase.h"
 #include "pub_core_libcassert.h"
diff --git a/coregrind/core.h b/coregrind/pub_core_basics.h
similarity index 69%
rename from coregrind/core.h
rename to coregrind/pub_core_basics.h
index a7d927b..42d6f20 100644
--- a/coregrind/core.h
+++ b/coregrind/pub_core_basics.h
@@ -1,14 +1,13 @@
 
 /*--------------------------------------------------------------------*/
-/*--- A header file for various private parts of Valgrind's core.  ---*/
-/*---                                                       core.h ---*/
+/*--- Header included by every core C file.      pub_core_basics.h ---*/
 /*--------------------------------------------------------------------*/
- 
+
 /*
    This file is part of Valgrind, a dynamic binary instrumentation
    framework.
 
-   Copyright (C) 2000-2005 Julian Seward 
+   Copyright (C) 2000-2005 Julian Seward
       jseward@acm.org
 
    This program is free software; you can redistribute it and/or
@@ -29,14 +28,26 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#ifndef __CORE_H
-#define __CORE_H
+#ifndef __PUB_CORE_BASICS_H
+#define __PUB_CORE_BASICS_H
 
-#include "tool.h"          // tool stuff
+//--------------------------------------------------------------------
+// PURPOSE: This header should be imported by every single C file
+// in the core.  It contains the basic types and other things needed
+// everywhere.
+//--------------------------------------------------------------------
+
+#include "pub_tool_basics.h"
+
+/* ---------------------------------------------------------------------
+   Other headers to include
+   ------------------------------------------------------------------ */
+
+// Might as well have the following two in here, their contents are used so
+// broadly (eg. in pub_core_threadstate.h).
 
 #include "libvex.h"
 
-// XXX: this is needed because pub_core_scheduler needs VexGuestXXXState...
 #if defined(VGA_x86)
 #  include "libvex_guest_x86.h"
 #elif defined(VGA_amd64)
@@ -45,19 +56,10 @@
 #  error Unknown arch
 #endif
 
-#include <setjmp.h>        // for jmp_buf
+#include <setjmp.h>
 
-#include "pub_core_scheduler.h"   // for types 'ThreadArchState'
-
-/* ---------------------------------------------------------------------
-   Finally - autoconf-generated settings
-   ------------------------------------------------------------------ */
-
-#include "config.h"
-
-#endif /* ndef __CORE_H */
+#endif   // __PUB_CORE_BASICS_H
 
 /*--------------------------------------------------------------------*/
 /*--- end                                                          ---*/
 /*--------------------------------------------------------------------*/
-
diff --git a/coregrind/pub_core_debuglog.h b/coregrind/pub_core_debuglog.h
index 218c355..1027fc7 100644
--- a/coregrind/pub_core_debuglog.h
+++ b/coregrind/pub_core_debuglog.h
@@ -47,7 +47,7 @@
    portable way to avoid using stdarg.h. */
 #include <stdarg.h>
 
-#include "pub_basics_asm.h"   /* For definition of VG_ macro */
+#include "pub_tool_basics.h"  /* For definition of VG_ macro */
 
 /* There are no tool-visible exports from m_debuglog, hence no header
    file for it. */
diff --git a/coregrind/pub_core_errormgr.h b/coregrind/pub_core_errormgr.h
index a40cddd..4483555 100644
--- a/coregrind/pub_core_errormgr.h
+++ b/coregrind/pub_core_errormgr.h
@@ -38,8 +38,6 @@
 
 #include "pub_tool_errormgr.h"
 
-//#include "pub_core_stacktrace.h"
-
 // XXX: should this be in pthreadmodel.c?
 // These must be negative, so as to not overlap with tool error kinds.
 typedef
diff --git a/coregrind/pub_core_main.h b/coregrind/pub_core_main.h
index 99b148e..667d9db 100644
--- a/coregrind/pub_core_main.h
+++ b/coregrind/pub_core_main.h
@@ -57,6 +57,13 @@
 // Set up the libc freeres wrapper 
 extern void VG_(set_libc_freeres_wrapper_addr)(Addr);
 
+// Do everything which needs doing before the process finally ends,
+// like printing reports, etc
+extern void VG_(shutdown_actions_NORETURN) (
+               ThreadId tid, 
+               VgSchedReturnCode tids_schedretcode 
+            );
+
 #endif   // __PUB_CORE_MAIN_H
 
 /*--------------------------------------------------------------------*/
diff --git a/coregrind/pub_core_scheduler.h b/coregrind/pub_core_scheduler.h
index 33e9e6b..7940357 100644
--- a/coregrind/pub_core_scheduler.h
+++ b/coregrind/pub_core_scheduler.h
@@ -37,165 +37,6 @@
 // It's at the centre of everything.
 //--------------------------------------------------------------------
 
-/* 
-   Thread state machine:
-
-   Empty -> Init -> Runnable <=> WaitSys/Yielding
-     ^                 |
-     \---- Zombie -----/		       
- */
-typedef
-   enum ThreadStatus { 
-      VgTs_Empty,      /* this slot is not in use */
-      VgTs_Init,       /* just allocated */
-      VgTs_Runnable,   /* ready to run */
-      VgTs_WaitSys,    /* waiting for a syscall to complete */
-      VgTs_Yielding,   /* temporarily yielding the CPU */
-      VgTs_Zombie,     /* transient state just before exiting */
-   }
-   ThreadStatus;
-
-/* Return codes from the scheduler. */
-typedef
-   enum { 
-      VgSrc_None,	 /* not exiting yet */
-      VgSrc_ExitSyscall, /* client called exit().  This is the normal
-                            route out. */
-      VgSrc_FatalSig	 /* Killed by the default action of a fatal
-			    signal */
-   }
-   VgSchedReturnCode;
-
-
-#if defined(VGA_x86)
-   typedef VexGuestX86State   VexGuestArchState;
-#elif defined(VGA_amd64)
-   typedef VexGuestAMD64State VexGuestArchState;
-#elif defined(VGA_arm)
-   typedef VexGuestARMState   VexGuestArchState;
-#else
-#  error Unknown architecture
-#endif
-
-
-typedef 
-   struct {
-      /* --- BEGIN vex-mandated guest state --- */
-
-      /* Saved machine context. */
-      VexGuestArchState vex;
-
-      /* Saved shadow context. */
-      VexGuestArchState vex_shadow;
-
-      /* Spill area. */
-      UChar vex_spill[LibVEX_N_SPILL_BYTES];
-
-      /* --- END vex-mandated guest state --- */
-   } 
-   ThreadArchState;
-
-/* OS-specific thread state */
-typedef struct {
-   /* who we are */
-   Int	lwpid;			// PID of kernel task
-   Int	threadgroup;		// thread group id
-
-   ThreadId parent;		// parent tid (if any)
-
-   /* runtime details */
-   Addr  valgrind_stack_base;	// Valgrind's stack base
-   SizeT valgrind_stack_szB;	// stack size in bytes
-
-   /* exit details */
-   Int  exitcode;		// in the case of exitgroup, set by someone else
-   Int  fatalsig;		// fatal signal
-} os_thread_t;
-
-
-typedef struct {
-   /* ThreadId == 0 (and hence vg_threads[0]) is NEVER USED.
-      The thread identity is simply the index in vg_threads[].
-      ThreadId == 1 is the root thread and has the special property
-      that we don't try and allocate or deallocate its stack.  For
-      convenience of generating error message, we also put the
-      ThreadId in this tid field, but be aware that it should
-      ALWAYS == the index in vg_threads[]. */
-   ThreadId tid;
-
-   /* Current scheduling status. */
-   ThreadStatus status;
-
-   /* This is set if the thread is in the process of exiting for any
-      reason.  The precise details of the exit are in the OS-specific
-      state. */
-   VgSchedReturnCode exitreason;
-
-   /* Architecture-specific thread state. */
-   ThreadArchState arch;
-
-   /* This thread's blocked-signals mask.  Semantics is that for a
-      signal to be delivered to this thread, the signal must not be
-      blocked by this signal mask.  If more than one thread accepts a
-      signal, then it will be delivered to one at random.  If all
-      threads block the signal, it will remain pending until either a
-      thread unblocks it or someone uses sigwaitsig/sigtimedwait. */
-   vki_sigset_t sig_mask;
-
-   /* tmp_sig_mask is usually the same as sig_mask, and is kept in
-      sync whenever sig_mask is changed.  The only time they have
-      different values is during the execution of a sigsuspend, where
-      tmp_sig_mask is the temporary mask which sigsuspend installs.
-      It is only consulted to compute the signal mask applied to a
-      signal handler. */
-   vki_sigset_t tmp_sig_mask;
-
-   /* A little signal queue for signals we can't get the kernel to
-      queue for us.  This is only allocated as needed, since it should
-      be rare. */
-   struct SigQueue *sig_queue;
-
-   /* Client stacks.  When a thread slot is freed, we don't deallocate its
-      stack; we just leave it lying around for the next use of the
-      slot.  If the next use of the slot requires a larger stack,
-      only then is the old one deallocated and a new one
-      allocated. 
-
-      For the main thread (threadid == 0), this mechanism doesn't
-      apply.  We don't know the size of the stack since we didn't
-      allocate it, and furthermore we never reallocate it. */
-
-   /* The allocated size of this thread's stack (permanently zero
-      if this is ThreadId == 0, since we didn't allocate its stack) */
-   SizeT client_stack_szB;
-
-   /* Address of the highest legitimate word in this stack.  This is
-      used for error messages only -- not critical for execution
-      correctness.  Is is set for all stacks, specifically including
-      ThreadId == 0 (the main thread). */
-   Addr client_stack_highest_word;
-
-   /* Alternate signal stack */
-   vki_stack_t altstack;
-
-   /* OS-specific thread state */
-   os_thread_t os_state;
-
-   /* Used in the syscall handlers.  Set to True to indicate that the
-      PRE routine for a syscall has set the syscall result already and
-      so the syscall does not need to be handed to the kernel. */
-   Bool syscall_result_set;
-   
-   /* Per-thread jmp_buf to resume scheduler after a signal */
-   Bool    sched_jmpbuf_valid;
-   jmp_buf sched_jmpbuf;
-}
-ThreadState;
-
-
-/* The thread table. */
-extern ThreadState VG_(threads)[VG_N_THREADS];
-
 /* Allocate a new ThreadState */
 extern ThreadId VG_(alloc_ThreadState)(void);
 
@@ -207,25 +48,6 @@
    exitcode. */
 extern void VG_(kill_thread)(ThreadId tid);
 
-/* Check that tid is in range and denotes a non-Empty thread. */
-extern Bool VG_(is_valid_tid) ( ThreadId tid );
-
-/* Get the ThreadState for a particular thread */
-extern ThreadState *VG_(get_ThreadState)(ThreadId tid);
-
-/* Given an LWP id (ie, real kernel thread id), find the corresponding
-   ThreadId */
-extern ThreadId VG_(get_lwp_tid)(Int lwpid);
-
-/* Returns true if a thread is currently running (ie, has the CPU lock) */
-extern Bool VG_(is_running_thread)(ThreadId tid);
-
-/* Returns true if the thread is in the process of exiting */
-extern Bool VG_(is_exiting)(ThreadId tid);
-
-/* Return the number of non-dead Threads */
-extern Int VG_(count_living_threads)(void);
-
 /* Nuke all threads except tid. */
 extern void VG_(nuke_all_threads_except) ( ThreadId me,
                                            VgSchedReturnCode reason );
@@ -253,17 +75,8 @@
 // The scheduler.
 extern VgSchedReturnCode VG_(scheduler) ( ThreadId tid );
 
-// Do everything which needs doing before the process finally ends,
-// like printing reports, etc
-extern void VG_(shutdown_actions_NORETURN) (
-               ThreadId tid, 
-               VgSchedReturnCode tids_schedretcode 
-            );
-
 extern void VG_(scheduler_init) ( void );
 
-extern void VG_(pp_sched_status) ( void );
-
 /* Stats ... */
 extern void VG_(print_scheduler_stats) ( void );
 
diff --git a/coregrind/pub_core_threadstate.h b/coregrind/pub_core_threadstate.h
new file mode 100644
index 0000000..148d18e
--- /dev/null
+++ b/coregrind/pub_core_threadstate.h
@@ -0,0 +1,247 @@
+
+/*--------------------------------------------------------------------*/
+/*--- The thread state.                     pub_core_threadstate.h ---*/
+/*--------------------------------------------------------------------*/
+
+/*
+   This file is part of Valgrind, a dynamic binary instrumentation
+   framework.
+
+   Copyright (C) 2000-2005 Julian Seward
+      jseward@acm.org
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307, USA.
+
+   The GNU General Public License is contained in the file COPYING.
+*/
+
+#ifndef __PUB_CORE_THREADSTATE_H
+#define __PUB_CORE_THREADSTATE_H
+
+//--------------------------------------------------------------------
+// PURPOSE: This module defines the ThreadState type and the
+// VG_(threads)[] data structure which holds all the important thread
+// state.  It also defines some simple operations on the data structure
+// that don't require any external help.  (m_scheduler does the complex
+// stuff).
+//--------------------------------------------------------------------
+
+#include "pub_tool_threadstate.h"
+
+/*------------------------------------------------------------*/
+/*--- Types                                                ---*/
+/*------------------------------------------------------------*/
+
+/* 
+   Thread state machine:
+
+   Empty -> Init -> Runnable <=> WaitSys/Yielding
+     ^                 |
+     \---- Zombie -----/		       
+ */
+typedef
+   enum ThreadStatus { 
+      VgTs_Empty,      /* this slot is not in use */
+      VgTs_Init,       /* just allocated */
+      VgTs_Runnable,   /* ready to run */
+      VgTs_WaitSys,    /* waiting for a syscall to complete */
+      VgTs_Yielding,   /* temporarily yielding the CPU */
+      VgTs_Zombie,     /* transient state just before exiting */
+   }
+   ThreadStatus;
+
+/* Return codes from the scheduler. */
+typedef
+   enum { 
+      VgSrc_None,	 /* not exiting yet */
+      VgSrc_ExitSyscall, /* client called exit().  This is the normal
+                            route out. */
+      VgSrc_FatalSig	 /* Killed by the default action of a fatal
+			    signal */
+   }
+   VgSchedReturnCode;
+
+
+#if defined(VGA_x86)
+   typedef VexGuestX86State   VexGuestArchState;
+#elif defined(VGA_amd64)
+   typedef VexGuestAMD64State VexGuestArchState;
+#elif defined(VGA_arm)
+   typedef VexGuestARMState   VexGuestArchState;
+#else
+#  error Unknown architecture
+#endif
+
+
+typedef 
+   struct {
+      /* --- BEGIN vex-mandated guest state --- */
+
+      /* Saved machine context. */
+      VexGuestArchState vex;
+
+      /* Saved shadow context. */
+      VexGuestArchState vex_shadow;
+
+      /* Spill area. */
+      UChar vex_spill[LibVEX_N_SPILL_BYTES];
+
+      /* --- END vex-mandated guest state --- */
+   } 
+   ThreadArchState;
+
+/* OS-specific thread state */
+typedef struct {
+   /* who we are */
+   Int	lwpid;			// PID of kernel task
+   Int	threadgroup;		// thread group id
+
+   ThreadId parent;		// parent tid (if any)
+
+   /* runtime details */
+   Addr  valgrind_stack_base;	// Valgrind's stack base
+   SizeT valgrind_stack_szB;	// stack size in bytes
+
+   /* exit details */
+   Int  exitcode;		// in the case of exitgroup, set by someone else
+   Int  fatalsig;		// fatal signal
+} os_thread_t;
+
+
+typedef struct {
+   /* ThreadId == 0 (and hence vg_threads[0]) is NEVER USED.
+      The thread identity is simply the index in vg_threads[].
+      ThreadId == 1 is the root thread and has the special property
+      that we don't try and allocate or deallocate its stack.  For
+      convenience of generating error message, we also put the
+      ThreadId in this tid field, but be aware that it should
+      ALWAYS == the index in vg_threads[]. */
+   ThreadId tid;
+
+   /* Current scheduling status. */
+   ThreadStatus status;
+
+   /* This is set if the thread is in the process of exiting for any
+      reason.  The precise details of the exit are in the OS-specific
+      state. */
+   VgSchedReturnCode exitreason;
+
+   /* Architecture-specific thread state. */
+   ThreadArchState arch;
+
+   /* This thread's blocked-signals mask.  Semantics is that for a
+      signal to be delivered to this thread, the signal must not be
+      blocked by this signal mask.  If more than one thread accepts a
+      signal, then it will be delivered to one at random.  If all
+      threads block the signal, it will remain pending until either a
+      thread unblocks it or someone uses sigwaitsig/sigtimedwait. */
+   vki_sigset_t sig_mask;
+
+   /* tmp_sig_mask is usually the same as sig_mask, and is kept in
+      sync whenever sig_mask is changed.  The only time they have
+      different values is during the execution of a sigsuspend, where
+      tmp_sig_mask is the temporary mask which sigsuspend installs.
+      It is only consulted to compute the signal mask applied to a
+      signal handler. */
+   vki_sigset_t tmp_sig_mask;
+
+   /* A little signal queue for signals we can't get the kernel to
+      queue for us.  This is only allocated as needed, since it should
+      be rare. */
+   struct SigQueue *sig_queue;
+
+   /* Client stacks.  When a thread slot is freed, we don't deallocate its
+      stack; we just leave it lying around for the next use of the
+      slot.  If the next use of the slot requires a larger stack,
+      only then is the old one deallocated and a new one
+      allocated. 
+
+      For the main thread (threadid == 0), this mechanism doesn't
+      apply.  We don't know the size of the stack since we didn't
+      allocate it, and furthermore we never reallocate it. */
+
+   /* The allocated size of this thread's stack (permanently zero
+      if this is ThreadId == 0, since we didn't allocate its stack) */
+   SizeT client_stack_szB;
+
+   /* Address of the highest legitimate word in this stack.  This is
+      used for error messages only -- not critical for execution
+      correctness.  Is is set for all stacks, specifically including
+      ThreadId == 0 (the main thread). */
+   Addr client_stack_highest_word;
+
+   /* Alternate signal stack */
+   vki_stack_t altstack;
+
+   /* OS-specific thread state */
+   os_thread_t os_state;
+
+   /* Used in the syscall handlers.  Set to True to indicate that the
+      PRE routine for a syscall has set the syscall result already and
+      so the syscall does not need to be handed to the kernel. */
+   Bool syscall_result_set;
+   
+   /* Per-thread jmp_buf to resume scheduler after a signal */
+   Bool    sched_jmpbuf_valid;
+   jmp_buf sched_jmpbuf;
+}
+ThreadState;
+
+
+/*------------------------------------------------------------*/
+/*--- The thread table.                                    ---*/
+/*------------------------------------------------------------*/
+
+/* A statically allocated array of threads.  NOTE: [0] is
+   never used, to simplify the simulation of initialisers for
+   LinuxThreads. */
+extern ThreadState VG_(threads)[VG_N_THREADS];
+
+// The running thread.  m_scheduler should be the only other module
+// to write to this.
+extern ThreadId VG_(running_tid);
+
+/*------------------------------------------------------------*/
+/*--- Basic operations on the thread table.                ---*/
+/*------------------------------------------------------------*/
+
+// Convert a ThreadStatus to a string.
+const HChar* VG_(name_of_ThreadStatus) ( ThreadStatus status );
+
+/* Get the ThreadState for a particular thread */
+extern ThreadState *VG_(get_ThreadState) ( ThreadId tid );
+
+/* Check that tid is in range and denotes a non-Empty thread. */
+extern Bool VG_(is_valid_tid) ( ThreadId tid );
+
+/* Returns true if a thread is currently running (ie, has the CPU lock) */
+extern Bool VG_(is_running_thread)(ThreadId tid);
+
+/* Returns true if the thread is in the process of exiting */
+extern Bool VG_(is_exiting)(ThreadId tid);
+
+/* Return the number of non-dead Threads */
+extern Int VG_(count_living_threads)(void);
+
+/* Given an LWP id (ie, real kernel thread id), find the corresponding
+   ThreadId */
+extern ThreadId VG_(get_lwp_tid)(Int lwpid);
+
+#endif   // __PUB_CORE_THREADSTATE_H
+
+/*--------------------------------------------------------------------*/
+/*--- end                                                          ---*/
+/*--------------------------------------------------------------------*/
diff --git a/coregrind/pub_core_tooliface.h b/coregrind/pub_core_tooliface.h
index b779327..341fc29 100644
--- a/coregrind/pub_core_tooliface.h
+++ b/coregrind/pub_core_tooliface.h
@@ -55,7 +55,7 @@
 
 /* These structs are not exposed to tools to mitigate possibility of
    binary-incompatibilities when the core/tool interface changes.  Instead,
-   set functions are provided (see include/tool.h). */
+   set functions are provided (see include/pub_tool_tooliface.h). */
 
 /* ---------------------------------------------------------------------
    'Details'
diff --git a/coregrind/stage1.c b/coregrind/stage1.c
index 45a1999..fc61276 100644
--- a/coregrind/stage1.c
+++ b/coregrind/stage1.c
@@ -41,12 +41,12 @@
 #include <sys/resource.h>
 #include <unistd.h>
 
-#include "core.h"
-#include "ume.h"
 #include "memcheck/memcheck.h"
+#include "pub_core_basics.h"
 #include "pub_core_debuglog.h"
 #include "pub_core_libcbase.h"
 #include "pub_core_libcproc.h"
+#include "ume.h"
 
 
 static int stack[SIGSTKSZ*4];
diff --git a/coregrind/ume.c b/coregrind/ume.c
index 983dfe8..032dcf2 100644
--- a/coregrind/ume.c
+++ b/coregrind/ume.c
@@ -33,8 +33,6 @@
 #define _GNU_SOURCE
 #define _FILE_OFFSET_BITS 64
 
-#include "core.h"
-
 #include <sys/mman.h>
 #include <fcntl.h>
 #include <errno.h>
@@ -44,9 +42,10 @@
 #include <unistd.h>
 #include <assert.h>
 
-#include "ume.h"
+#include "pub_core_basics.h"
 #include "pub_core_libcbase.h"
 #include "pub_core_machine.h"
+#include "ume.h"
 
 #if	ELFSZ == 64
 #define ESZ(x)	Elf64_##x
diff --git a/coregrind/ume.h b/coregrind/ume.h
index a2679c7..beb4d99 100644
--- a/coregrind/ume.h
+++ b/coregrind/ume.h
@@ -35,8 +35,6 @@
 #include <elf.h>
 #include <sys/types.h>
 
-#include "basic_types.h"
-
 /*------------------------------------------------------------*/
 /*--- General stuff                                        ---*/
 /*------------------------------------------------------------*/
diff --git a/coregrind/vg_preloaded.c b/coregrind/vg_preloaded.c
index 11c1507..d76267c 100644
--- a/coregrind/vg_preloaded.c
+++ b/coregrind/vg_preloaded.c
@@ -45,7 +45,7 @@
 #include "valgrind.h"
 #include "coregrind.h"
 
-#include "core.h"
+#include "pub_core_basics.h"
 #include "pub_core_debuginfo.h"     // needed for pub_core_redir.h :(
 #include "pub_core_redir.h"
 
diff --git a/docs/directory-structure b/docs/directory-structure
index e8a6e40..a03e3fd 100644
--- a/docs/directory-structure
+++ b/docs/directory-structure
@@ -1,16 +1,14 @@
 -------------------------------------------------------------------
 Guide to the directory structure
 -------------------------------------------------------------------
-[16-May-2005: a lot of the stuff about arch/OS/platform specific code
- is out of date in here.  Hopefully, things have gotten simpler.  --njn]
+[This should be merged with coregrind/README_MODULES.txt]
 
 Valgrind has 2 main levels of genericity.
  
  1. Multiple tools, plus the core.
  2. Multiple architectures, OSes, and platforms (arch/OS combinations).
 
-This requires a moderately complex directory structure.  This file is a
-guide to where different things live.
+This file is a guide to where different things live.
 
 
 Basic layout
@@ -18,7 +16,8 @@
 1. Core stuff lives in:
     - include/              for declarations that must be seen by tools
     - coregrind/            for code that need not be seen by tools
-    - coregrind/demangle/   the demangler
+
+   Some subdirs of coregrind/ hold modules that consist of multiple files.
 
    Tool stuff lives in:
     - $TOOL/                main files
@@ -33,49 +32,41 @@
 
 2. Generic things go in the directory specified in (1).  
 
-   Arch-specific, OS-specific, or platform-specific things go within a
-   subdirectory of the directory specified by step (1) above.
+   Arch-specific, OS-specific, or platform-specific things are sprinkled
+   throughout the code -- there is no single place for all the
+   architecture-specific things, for example.  
+   
+   Sometimes we have a whole file holding things specific to a particular
+   arch/OS/platform.  Such files have an appropriate suffix, eg.
+   sigframe-x86-linux.c.
 
-   For example: 
-   - Cachegrind's main generic code goes in cachegrind/.
-   - Cachegrind's x86-specific code goes in cachegrind/x86/.
-   - Cachegrind's x86-specific tests go  in cachegrind/tests/x86/.
-   - x86/Linux-specific declarations that must be visible to tools go in
-     include/x86-linux/.
+   More often we use #ifdefs inside source files to specify the different
+   cases for different archs/OSes/platforms.  It's pretty straightforward.
+
+   A final case:  arch-specific regression tests for tools go in a
+   subdirectory, eg. cachegrind/tests/x86/.
 
    
 Guide to headers
 ----------------
-Finding declarations in headers is not always easy.  The above rules dictate
-which directory something should go in.  As well as that, there are some things
-that must be visible in both C and assembly code files, but most things only be
-visible in C files; things that must be both C-visible and asm-visible go in
-files called *_asm.h.
+See coregrind/README_MODULES.txt for details of the core/tool header file
+split.
 
-The most important header files are (at the time of writing):
-- include/tool.h     (auto-generated from tool.h.base + toolfuncs.def)
-- include/tool_asm.h
-- $ARCH/tool_arch.h
-- $OS/vki.h
-- $PLATFORM/vki*.h
-- coregrind/{core.h,core_asm.h}
-- $ARCH/{core_arch.h,core_arch_asm.h}
-- $OS/core_os.h
-- $PLATFORM/{core_platform.h,vki_unistd.h}
+Note that every single C file will #include pub_basics.h.  Every single asm
+file will #include pub_basics_asm.h.
 
-There are a few others, but that is most of them.
-
-Having read the above text, it should be clear what is found in each of them.
-For example, coregrind/core_asm.h contains all the generic
-(non-arch/OS/platform-specific) declarations which should not be seen by
-tools and must be visible to both C and asm code.
+Our versions of kernel types are in the vki*.h headers.
 
 When searching for something, rgrep is very useful.  If you don't have a
 version of rgrep, use a command something like this:
 
    find . -name '*.h' | xargs grep <pattern>
 
-The comment at the top of coregrind/core.h has some additional information
-about the exact hierarchy of the header files, but those details aren't
-important for just finding and placing things.
+   find . -name '*.h' \
+          -type f \
+          -not -path '*.svn\/*' | xargs grep "$1"
+
+The -name option gives the file wildcard, the -type says "look in normal
+files only" and the -not -path tells it to not look in Subversions hidden
+directories.
 
diff --git a/docs/xml/manual-core.xml b/docs/xml/manual-core.xml
index d2be7cb..2d1c09c 100644
--- a/docs/xml/manual-core.xml
+++ b/docs/xml/manual-core.xml
@@ -343,7 +343,7 @@
 stop your program and fix it, because Valgrind won't tell you
 anything else useful after this.  Note that the 300/30000 limits
 apply after suppressed errors are removed.  These limits are
-defined in <filename>core.h</filename> and can be increased
+defined in <filename>m_errormgr.c</filename> and can be increased
 if necessary.</para>
 
 <para>To avoid this cutoff you can use the
diff --git a/docs/xml/writing-tools.xml b/docs/xml/writing-tools.xml
index f91672f..2bc552e 100644
--- a/docs/xml/writing-tools.xml
+++ b/docs/xml/writing-tools.xml
@@ -23,6 +23,10 @@
 quite a few things about Valgrind, it is much easier than
 instrumenting a program from scratch yourself.</para>
 
+<para>[Nb: What follows is slightly out of date.  In particular,
+there are various references to a file include/tool.h which has been
+split into a number of header files in include/.]</para>
+
 </sect2>
 
 
diff --git a/helgrind/hg_main.c b/helgrind/hg_main.c
index f54b7c1..372f6f8 100644
--- a/helgrind/hg_main.c
+++ b/helgrind/hg_main.c
@@ -29,7 +29,8 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "tool.h"
+#include "pub_tool_basics.h"
+#include "pub_tool_threadstate.h"
 #include "pub_tool_aspacemgr.h"
 #include "pub_tool_debuginfo.h"
 #include "pub_tool_hashtable.h"
diff --git a/include/Makefile.am b/include/Makefile.am
index 5bf9b1d..50624ab 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -2,9 +2,8 @@
 incincdir = $(includedir)/valgrind
 
 incinc_HEADERS = \
-	basic_types.h 			\
-	tool.h 				\
-	pub_basics_asm.h 		\
+	pub_tool_basics.h 		\
+	pub_tool_basics_asm.h 		\
 	pub_tool_aspacemgr.h 		\
 	pub_tool_cpuid.h 		\
 	pub_tool_errormgr.h 		\
diff --git a/include/basic_types.h b/include/pub_tool_basics.h
similarity index 65%
rename from include/basic_types.h
rename to include/pub_tool_basics.h
index 62d5ae1..03a9b7a 100644
--- a/include/basic_types.h
+++ b/include/pub_tool_basics.h
@@ -1,6 +1,6 @@
 
 /*--------------------------------------------------------------------*/
-/*--- Header included by every C file.               basic_types.h ---*/
+/*--- Header included by every tool C file.      pub_tool_basics.h ---*/
 /*--------------------------------------------------------------------*/
 
 /*
@@ -28,18 +28,45 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#ifndef __BASIC_TYPES_H
-#define __BASIC_TYPES_H
+#ifndef __PUB_TOOL_BASICS_H
+#define __PUB_TOOL_BASICS_H
+
+//--------------------------------------------------------------------
+// PURPOSE: This header should be imported by every single C file in
+// tools.  It contains the basic types and other things needed everywhere.
+// There is no corresponding C file because this isn't a module
+// containing executable code, it's all just declarations.
+//--------------------------------------------------------------------
+
+/* ---------------------------------------------------------------------
+   Other headers to include
+   ------------------------------------------------------------------ */
+
+// VEX defines Char, UChar, Short, UShort, Int, UInt, Long, ULong,
+// Addr32, Addr64, HWord, HChar, Bool, False and True.
+#include "libvex_basictypes.h"
+
+// For the VG_() macro
+#include "pub_tool_basics_asm.h"
+
+// For varargs types
+#include <stdarg.h>
+
+// Autoconf-generated settings
+#include "config.h"
+
+// Kernel types.  Might as well have them here, they're used so broadly
+// (eg. in pub_core_threadstate.h).
+#if defined(VGO_linux)
+#  include "vki-linux.h"
+#else
+#  error Unknown OS
+#endif
 
 /* ---------------------------------------------------------------------
    builtin types
    ------------------------------------------------------------------ */
 
-#include "libvex_basictypes.h"
-
-/* VEX defines Char, UChar, Short, UShort, Int, UInt, Long, ULong,
-   Addr32, Addr64, HWord, HChar, Bool, False and True. */
-
 // By choosing the right types, we can get these right for 32-bit and 64-bit
 // platforms without having to do any conditional compilation or anything.
 // 
@@ -57,14 +84,38 @@
 
 typedef  Word                   OffT;     // 32             64
 
-
-/* This is going to be either 4 or 8. */
-#define VG_WORDSIZE VEX_HOST_WORDSIZE
-
 #if !defined(NULL)
 #  define NULL ((void*)0)
 #endif
 
+/* ---------------------------------------------------------------------
+   non-builtin types
+   ------------------------------------------------------------------ */
+
+// These probably shouldn't be here, but moving them to their logical
+// modules results in a lot more #includes...
+
+/* ThreadIds are simply indices into the VG_(threads)[] array. */
+typedef UInt ThreadId;
+
+/* An abstraction of syscall return values.
+   When .isError == False, val holds the return value.
+   When .isError == True,  val holds the error code.
+*/
+typedef struct { 
+   UWord val;
+   Bool  isError;
+}
+SysRes;
+
+/* ---------------------------------------------------------------------
+   Miscellaneous
+   ------------------------------------------------------------------ */
+
+/* This is going to be either 4 or 8. */
+// It should probably be in m_machine.
+#define VG_WORDSIZE VEX_HOST_WORDSIZE
+
 #if defined(VGA_x86)
 #  define VGA_REGPARM(n)            __attribute__((regparm(n)))
 #elif defined(VGA_amd64) || defined(VGA_arm)
@@ -73,35 +124,7 @@
 #  error Unknown arch
 #endif
 
-/* ---------------------------------------------------------------------
-   non-builtin types
-   ------------------------------------------------------------------ */
-
-// XXX: these probably shouldn't be here...
-
-/* ThreadIds are simply indices into the VG_(threads)[] array. */
-typedef
-   UInt
-   ThreadId;
-
-/* Special magic value for an invalid ThreadId.  It corresponds to
-   LinuxThreads using zero as the initial value for
-   pthread_mutex_t.__m_owner and pthread_cond_t.__c_waiting. */
-#define VG_INVALID_THREADID ((ThreadId)(0))
-
-
-/* An abstraction of syscall return values.
-   When .isError == False, val holds the return value.
-   When .isError == True,  val holds the error code.
-*/
-typedef
-   struct { 
-      UWord val;
-      Bool  isError;
-   }
-   SysRes;
-
-#endif /* __BASIC_TYPES_H */
+#endif /* __PUB_TOOL_BASICS_H */
 
 /*--------------------------------------------------------------------*/
 /*--- end                                                          ---*/
diff --git a/coregrind/core.h b/include/pub_tool_basics_asm.h
similarity index 61%
copy from coregrind/core.h
copy to include/pub_tool_basics_asm.h
index a7d927b..4de39fe 100644
--- a/coregrind/core.h
+++ b/include/pub_tool_basics_asm.h
@@ -1,9 +1,10 @@
 
 /*--------------------------------------------------------------------*/
-/*--- A header file for various private parts of Valgrind's core.  ---*/
-/*---                                                       core.h ---*/
+/*--- Header imported directly by every asm file, and indirectly   ---*/
+/*--- (via pub_tool_basics.h) by every C file.                     ---*/
+/*---                                        pub_tool_basics_asm.h ---*/
 /*--------------------------------------------------------------------*/
- 
+
 /*
    This file is part of Valgrind, a dynamic binary instrumentation
    framework.
@@ -29,35 +30,21 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#ifndef __CORE_H
-#define __CORE_H
+#ifndef __PUB_TOOL_BASICS_ASM_H
+#define __PUB_TOOL_BASICS_ASM_H
 
-#include "tool.h"          // tool stuff
+/* All symbols externally visible from Valgrind are prefixed
+   as specified here to avoid namespace conflict problems.  */
 
-#include "libvex.h"
+#define VGAPPEND(str1,str2) str1##str2
 
-// XXX: this is needed because pub_core_scheduler needs VexGuestXXXState...
-#if defined(VGA_x86)
-#  include "libvex_guest_x86.h"
-#elif defined(VGA_amd64)
-#  include "libvex_guest_amd64.h"
-#else
-#  error Unknown arch
-#endif
+#define VG_(str)    VGAPPEND(vgPlain_,    str)
+#define VGA_(str)   VGAPPEND(vgArch_,     str)
+#define VGO_(str)   VGAPPEND(vgOS_,       str)
+#define VGP_(str)   VGAPPEND(vgPlatform_, str)
 
-#include <setjmp.h>        // for jmp_buf
-
-#include "pub_core_scheduler.h"   // for types 'ThreadArchState'
-
-/* ---------------------------------------------------------------------
-   Finally - autoconf-generated settings
-   ------------------------------------------------------------------ */
-
-#include "config.h"
-
-#endif /* ndef __CORE_H */
+#endif /* __PUB_TOOL_BASICS_ASM_H */
 
 /*--------------------------------------------------------------------*/
 /*--- end                                                          ---*/
 /*--------------------------------------------------------------------*/
-
diff --git a/include/pub_tool_profile.h b/include/pub_tool_profile.h
index df9df66..a04b989 100644
--- a/include/pub_tool_profile.h
+++ b/include/pub_tool_profile.h
@@ -31,10 +31,9 @@
 #ifndef __PUB_TOOL_PROFILE_H
 #define __PUB_TOOL_PROFILE_H
 
-// Define this to turn on profiling.
+// Define this to turn on profiling.  (But it's currently not working...)
 //#define  VG_DO_PROFILING   1
 
-/* Override the empty definitions from tool.h */
 #ifdef VG_DO_PROFILING
 #  define VGP_PUSHCC(x)   if (VG_(clo_profile)) VG_(pushcc)(x)
 #  define VGP_POPCC(x)    if (VG_(clo_profile)) VG_(popcc)(x)
diff --git a/include/pub_tool_stacktrace.h b/include/pub_tool_stacktrace.h
index f74a1b1..b332bd8 100644
--- a/include/pub_tool_stacktrace.h
+++ b/include/pub_tool_stacktrace.h
@@ -31,8 +31,6 @@
 #ifndef __PUB_TOOL_STACKTRACE_H
 #define __PUB_TOOL_STACKTRACE_H
 
-#include "basic_types.h"
-
 // The basic stack trace type:  just an array of code addresses.
 typedef Addr* StackTrace;
 
diff --git a/include/tool.h b/include/pub_tool_threadstate.h
similarity index 65%
rename from include/tool.h
rename to include/pub_tool_threadstate.h
index 52a3a1d..439fec0 100644
--- a/include/tool.h
+++ b/include/pub_tool_threadstate.h
@@ -1,5 +1,6 @@
-/*-*- c -*- ----------------------------------------------------------*/
-/*--- Header for lots of tool stuff.                        tool.h ---*/
+
+/*--------------------------------------------------------------------*/
+/*--- The thread state.                     pub_tool_threadstate.h ---*/
 /*--------------------------------------------------------------------*/
 
 /*
@@ -27,23 +28,8 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#ifndef __TOOL_H
-#define __TOOL_H
-
-#include <stdarg.h>       /* ANSI varargs stuff  */
-
-#include "basic_types.h"
-#include "pub_basics_asm.h"      /* asm stuff */
-
-#if defined(VGO_linux)
-#  include "vki-linux.h"
-#else
-#  error Unknown OS
-#endif
-
-/*====================================================================*/
-/*=== Build options and table sizes.                               ===*/
-/*====================================================================*/
+#ifndef __PUB_TOOL_THREADSTATE_H
+#define __PUB_TOOL_THREADSTATE_H
 
 /* The maximum number of pthreads that we support.  This is
    deliberately not very high since our implementation of some of the
@@ -52,12 +38,10 @@
    programs do not need many threads. */
 #define VG_N_THREADS 100
 
-/*====================================================================*/
-/*=== Useful stuff to call from generated code                     ===*/
-/*====================================================================*/
-
-/* ------------------------------------------------------------------ */
-/* Thread-related stuff */
+/* Special magic value for an invalid ThreadId.  It corresponds to
+   LinuxThreads using zero as the initial value for
+   pthread_mutex_t.__m_owner and pthread_cond_t.__c_waiting. */
+#define VG_INVALID_THREADID ((ThreadId)(0))
 
 /* Get the TID of the thread which currently has the CPU. */
 extern ThreadId VG_(get_running_tid) ( void );
@@ -68,6 +52,8 @@
                         ( Bool (*p) ( Addr stack_min, Addr stack_max, void* d ),
                           void* d );
 
-#endif   /* __TOOL_H */
+#endif   // __PUB_TOOL_THREADSTATE_H
 
-
+/*--------------------------------------------------------------------*/
+/*--- end                                                          ---*/
+/*--------------------------------------------------------------------*/
diff --git a/lackey/lk_main.c b/lackey/lk_main.c
index db39de3..ccb4a4d 100644
--- a/lackey/lk_main.c
+++ b/lackey/lk_main.c
@@ -29,7 +29,7 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "tool.h"
+#include "pub_tool_basics.h"
 #include "pub_tool_tooliface.h"
 #include "pub_tool_libcassert.h"
 #include "pub_tool_libcprint.h"
diff --git a/massif/ms_main.c b/massif/ms_main.c
index 2a65bbd..18890a1 100644
--- a/massif/ms_main.c
+++ b/massif/ms_main.c
@@ -34,7 +34,7 @@
 // be inspected step-wise to an appropriate depth.  See comments on data
 // structures below for more info on how things work.
 
-#include "tool.h"
+#include "pub_tool_basics.h"
 #include "pub_tool_debuginfo.h"
 #include "pub_tool_hashtable.h"
 #include "pub_tool_libcbase.h"
@@ -48,6 +48,7 @@
 #include "pub_tool_profile.h"
 #include "pub_tool_replacemalloc.h"
 #include "pub_tool_stacktrace.h"
+#include "pub_tool_threadstate.h"
 #include "pub_tool_tooliface.h"
 
 #include "valgrind.h"           // For {MALLOC,FREE}LIKE_BLOCK
diff --git a/memcheck/mac_leakcheck.c b/memcheck/mac_leakcheck.c
index f8bda97..9c68d64 100644
--- a/memcheck/mac_leakcheck.c
+++ b/memcheck/mac_leakcheck.c
@@ -30,16 +30,26 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include <setjmp.h>
-#include "mac_shared.h"
+#include "pub_tool_basics.h"
 #include "pub_tool_aspacemgr.h"
+#include "pub_tool_errormgr.h"      // For mac_shared.h
+#include "pub_tool_execontext.h"    // For mac_shared.h
+#include "pub_tool_hashtable.h"     // For mac_shared.h
 #include "pub_tool_libcbase.h"
 #include "pub_tool_libcassert.h"
 #include "pub_tool_libcprint.h"
 #include "pub_tool_libcsignal.h"
 #include "pub_tool_machine.h"
+#include "pub_tool_mallocfree.h"
+#include "pub_tool_options.h"
+#include "pub_tool_profile.h"       // For mac_shared.h
 #include "pub_tool_signals.h"
 
+#include "mac_shared.h"
+
+#include <setjmp.h>                 // For jmp_buf
+
+
 /* Define to debug the memory-leak-detector. */
 #define VG_DEBUG_LEAKCHECK 0
 #define VG_DEBUG_CLIQUE	   0
diff --git a/memcheck/mac_malloc_wrappers.c b/memcheck/mac_malloc_wrappers.c
index 92489be..5b928b5 100644
--- a/memcheck/mac_malloc_wrappers.c
+++ b/memcheck/mac_malloc_wrappers.c
@@ -30,10 +30,19 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "mac_shared.h"
+#include "pub_tool_basics.h"
+#include "pub_tool_errormgr.h"      // For mac_shared.h
+#include "pub_tool_execontext.h"    // For mac_shared.h
+#include "pub_tool_hashtable.h"     // For mac_shared.h
 #include "pub_tool_libcbase.h"
 #include "pub_tool_libcassert.h"
 #include "pub_tool_libcprint.h"
+#include "pub_tool_mallocfree.h"
+#include "pub_tool_options.h"
+#include "pub_tool_profile.h"       // For mac_shared.h
+#include "pub_tool_replacemalloc.h"
+#include "pub_tool_threadstate.h"
+#include "mac_shared.h"
 
 /*------------------------------------------------------------*/
 /*--- Defns                                                ---*/
diff --git a/memcheck/mac_replace_strmem.c b/memcheck/mac_replace_strmem.c
index ff89685..aab197c 100644
--- a/memcheck/mac_replace_strmem.c
+++ b/memcheck/mac_replace_strmem.c
@@ -30,10 +30,17 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
+#include "pub_tool_basics.h"
+#include "pub_tool_errormgr.h"
+#include "pub_tool_execontext.h"
+#include "pub_tool_hashtable.h"
+#include "pub_tool_profile.h"
+#include "pub_tool_redir.h"
+#include "pub_tool_tooliface.h"
+#include "valgrind.h"
+
 #include "mc_include.h"
 #include "memcheck.h"
-#include "valgrind.h"
-#include "pub_tool_redir.h"
 
 /* ---------------------------------------------------------------------
    We have our own versions of these functions for two reasons:
diff --git a/memcheck/mac_shared.c b/memcheck/mac_shared.c
index ebd698c..6c56ca2 100644
--- a/memcheck/mac_shared.c
+++ b/memcheck/mac_shared.c
@@ -30,11 +30,20 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "mac_shared.h"
+#include "pub_tool_basics.h"
+#include "pub_tool_errormgr.h"      // For mac_shared.h
+#include "pub_tool_execontext.h"    // For mac_shared.h
+#include "pub_tool_hashtable.h"     // For mac_shared.h
 #include "pub_tool_libcassert.h"
 #include "pub_tool_libcbase.h"
 #include "pub_tool_libcprint.h"
+#include "pub_tool_mallocfree.h"
 #include "pub_tool_machine.h"
+#include "pub_tool_options.h"
+#include "pub_tool_profile.h"       // For mac_shared.h
+#include "pub_tool_replacemalloc.h"
+#include "pub_tool_threadstate.h"
+#include "mac_shared.h"
 #include "memcheck.h"   /* for VG_USERREQ__* */
 
 /*------------------------------------------------------------*/
diff --git a/memcheck/mac_shared.h b/memcheck/mac_shared.h
index d76a7a0..785ef97 100644
--- a/memcheck/mac_shared.h
+++ b/memcheck/mac_shared.h
@@ -36,14 +36,6 @@
 #ifndef __MAC_SHARED_H
 #define __MAC_SHARED_H
 
-#include "tool.h"
-#include "pub_tool_hashtable.h"
-#include "pub_tool_mallocfree.h"
-#include "pub_tool_options.h"
-#include "pub_tool_profile.h"
-#include "pub_tool_replacemalloc.h"
-#include "pub_tool_tooliface.h"
-
 #define MAC_(str)    VGAPPEND(vgMAC_,str)
 
 /*------------------------------------------------------------*/
@@ -626,5 +618,5 @@
 #endif   /* __MAC_SHARED_H */
 
 /*--------------------------------------------------------------------*/
-/*--- end                                             mac_shared.h ---*/
+/*--- end                                                          ---*/
 /*--------------------------------------------------------------------*/
diff --git a/memcheck/mc_main.c b/memcheck/mc_main.c
index ea8edef..6a1365b 100644
--- a/memcheck/mc_main.c
+++ b/memcheck/mc_main.c
@@ -36,13 +36,24 @@
    only for 8-byte validity on the fast path
 */
 
-#include "mc_include.h"
-#include "memcheck.h"   /* for client requests */
+#include "pub_tool_basics.h"
 #include "pub_tool_aspacemgr.h"
+#include "pub_tool_errormgr.h"      // For mac_shared.h
+#include "pub_tool_execontext.h"    // For mac_shared.h
+#include "pub_tool_hashtable.h"     // For mac_shared.h
 #include "pub_tool_libcbase.h"
 #include "pub_tool_libcassert.h"
 #include "pub_tool_libcprint.h"
 #include "pub_tool_machine.h"
+#include "pub_tool_mallocfree.h"
+#include "pub_tool_options.h"
+#include "pub_tool_profile.h"       // For mac_shared.h
+#include "pub_tool_replacemalloc.h"
+#include "pub_tool_tooliface.h"
+#include "pub_tool_threadstate.h"
+
+#include "mc_include.h"
+#include "memcheck.h"   /* for client requests */
 
 
 #define EXPECTED_TAKEN(cond)     __builtin_expect((cond),1)
diff --git a/memcheck/mc_translate.c b/memcheck/mc_translate.c
index de6bb0b..992ac6d 100644
--- a/memcheck/mc_translate.c
+++ b/memcheck/mc_translate.c
@@ -29,9 +29,13 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "mc_include.h"
+#include "pub_tool_basics.h"
+#include "pub_tool_hashtable.h"     // For mac_shared.h
 #include "pub_tool_libcassert.h"
 #include "pub_tool_libcprint.h"
+#include "pub_tool_profile.h"
+#include "pub_tool_tooliface.h"
+#include "mc_include.h"
 
 
 /*------------------------------------------------------------*/
diff --git a/memcheck/tests/Makefile.am b/memcheck/tests/Makefile.am
index 6b8fffe..ac95d10 100644
--- a/memcheck/tests/Makefile.am
+++ b/memcheck/tests/Makefile.am
@@ -200,7 +200,7 @@
 hello_DEPENDENCIES 	= ../../coregrind/stage2.lds
 endif
 
-vgtest_ume_CFLAGS	= -DVGA_$(VG_ARCH)
+vgtest_ume_CFLAGS	= -DVGA_$(VG_ARCH) -DVGO_$(VG_OS)
 vgtest_ume_SOURCES	= vgtest_ume.c
 vgtest_ume_LDADD	= ../../coregrind/ume.o
 
diff --git a/memcheck/tests/vgtest_ume.c b/memcheck/tests/vgtest_ume.c
index b35a650..ad43e33 100644
--- a/memcheck/tests/vgtest_ume.c
+++ b/memcheck/tests/vgtest_ume.c
@@ -6,6 +6,7 @@
 #include <stdio.h>
 #include <assert.h>
 #include <elf.h>
+#include "../../include/pub_tool_basics.h"
 #include "../../coregrind/ume.h"
 
 #define STKSZ   (64*1024)
diff --git a/none/nl_main.c b/none/nl_main.c
index 30088c5..04f304f 100644
--- a/none/nl_main.c
+++ b/none/nl_main.c
@@ -28,7 +28,7 @@
    The GNU General Public License is contained in the file COPYING.
 */
 
-#include "tool.h"
+#include "pub_tool_basics.h"
 #include "pub_tool_libcassert.h"
 #include "pub_tool_tooliface.h"