Modularised vg_main.c as m_main.c.  Moved some stuff out of it to reduce the
number of files that depend on it, but there are still some which should be
removed in the future.


git-svn-id: svn://svn.valgrind.org/valgrind/trunk@3819 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/configure.in b/configure.in
index 7f34ac0..65b3004 100644
--- a/configure.in
+++ b/configure.in
@@ -1,6 +1,6 @@
 # Process this file with autoconf to produce a configure script.
 AC_INIT(Valgrind, 3.0.0.SVN, valgrind-users@lists.sourceforge.net)
-AC_CONFIG_SRCDIR(coregrind/vg_main.c)
+AC_CONFIG_SRCDIR(coregrind/m_main.c)
 AM_CONFIG_HEADER(config.h)
 AM_INIT_AUTOMAKE
 
diff --git a/coregrind/Makefile.am b/coregrind/Makefile.am
index b6f2dbe..7c52a53 100644
--- a/coregrind/Makefile.am
+++ b/coregrind/Makefile.am
@@ -44,6 +44,7 @@
 	pub_core_errormgr.h	\
 	pub_core_execontext.h	\
 	pub_core_hashtable.h	\
+	pub_core_main.h		\
 	pub_core_mallocfree.h	\
 	pub_core_options.h	\
 	pub_core_redir.h	\
@@ -84,6 +85,7 @@
 	m_errormgr.c \
 	m_execontext.c \
 	m_hashtable.c \
+	m_main.c \
 	m_mallocfree.c \
 	m_options.c \
 	m_redir.c \
@@ -92,9 +94,8 @@
 	m_tooliface.c \
 	m_translate.c \
 	m_transtab.c \
-	ume.c \
 	\
-	vg_main.c \
+	ume.c \
 	vg_messages.c \
 	vg_mylibc.c \
 	vg_dummy_profile.c \
diff --git a/coregrind/amd64/state.c b/coregrind/amd64/state.c
index 19bb9d9..0d96992 100644
--- a/coregrind/amd64/state.c
+++ b/coregrind/amd64/state.c
@@ -115,8 +115,9 @@
 /*--- pointercheck                                         ---*/
 /*------------------------------------------------------------*/
 
-Bool VGA_(setup_pointercheck)(void)
+Bool VGA_(setup_pointercheck)(Addr client_base, Addr client_end)
 {
+   vg_assert(0 != client_end);
    if (0) 
       VG_(message)(Vg_DebugMsg, "ignoring --pointercheck (unimplemented)");
    return True;
diff --git a/coregrind/arm/state.c b/coregrind/arm/state.c
index 62368d4..aca5d1f 100644
--- a/coregrind/arm/state.c
+++ b/coregrind/arm/state.c
@@ -92,7 +92,7 @@
 /*--- pointercheck                                         ---*/
 /*------------------------------------------------------------*/
 
-Bool VGA_(setup_pointercheck)(void)
+Bool VGA_(setup_pointercheck)(Addr client_base, Addr client_end)
 {
    I_die_here;
 #if 0
diff --git a/coregrind/core.h b/coregrind/core.h
index 8dbfa0e..3f8aec3 100644
--- a/coregrind/core.h
+++ b/coregrind/core.h
@@ -213,6 +213,15 @@
 __attribute__ ((__noreturn__))
 extern void  VG_(core_panic_at)   ( Char* str, StackTrace ips );
 
+/* Called when some unhandleable client behaviour is detected.
+   Prints a msg and aborts. */
+extern void VG_(unimplemented) ( Char* msg )
+            __attribute__((__noreturn__));
+
+/* Tell the logging mechanism whether we are logging to a file
+   descriptor or a socket descriptor. */
+extern Bool VG_(logging_to_socket);
+
 /* Tools use VG_(strdup)() which doesn't expose ArenaId */
 extern Char* VG_(arena_strdup) ( ArenaId aid, const Char* s);
 
@@ -269,57 +278,6 @@
 
 
 /* ---------------------------------------------------------------------
-   Exports of vg_main.c
-   ------------------------------------------------------------------ */
-
-/* Tell the logging mechanism whether we are logging to a file
-   descriptor or a socket descriptor. */
-extern Bool VG_(logging_to_socket);
-
-/* Sanity checks which may be done at any time.  The scheduler decides when. */
-extern void VG_(sanity_check_general) ( Bool force_expensive );
-
-/* Address space */
-extern Addr VG_(client_base);	/* client address space limits */
-extern Addr VG_(client_end);
-extern Addr VG_(client_mapbase); /* base of mappings */
-extern Addr VG_(clstk_base);	/* client stack range */
-extern Addr VG_(clstk_end);
-extern Addr VG_(client_trampoline_code);
-
-extern Addr VG_(brk_base);	/* start of brk */
-extern Addr VG_(brk_limit);	/* current brk */
-extern Addr VG_(shadow_base);	/* tool's shadow memory */
-extern Addr VG_(shadow_end);
-extern Addr VG_(valgrind_base);	/* valgrind's address range */
-extern Addr VG_(valgrind_last); // Nb: last byte, rather than one past the end
-
-extern struct vki_rlimit VG_(client_rlimit_data); /* client's original rlimit data */
-extern struct vki_rlimit VG_(client_rlimit_stack); /* client's original rlimit stack */
-
-/* client executable file descriptor */
-extern Int  VG_(clexecfd);
-
-// Help set up the child used when doing execve() with --trace-children=yes
-Char* VG_(build_child_VALGRINDCLO) ( Char* exename );
-Char* VG_(build_child_exename)     ( void );
-
-/* Called when some unhandleable client behaviour is detected.
-   Prints a msg and aborts. */
-extern void VG_(unimplemented) ( Char* msg )
-            __attribute__((__noreturn__));
-
-/* Something of a function looking for a home ... start up debugger. */
-extern void VG_(start_debugger) ( ThreadId tid );
-
-/* Stats ... */
-extern void VG_(print_scheduler_stats) ( void );
-
-/* 64-bit counter for the number of basic blocks done. */
-extern ULong VG_(bbs_done);
-
-
-/* ---------------------------------------------------------------------
    Exports of vg_syscall.S
    ------------------------------------------------------------------ */
 
@@ -407,7 +365,7 @@
 extern Bool VGA_(client_request)(ThreadId tid, UWord *args);
 
 // Pointercheck
-extern Bool VGA_(setup_pointercheck) ( void );
+extern Bool VGA_(setup_pointercheck) ( Addr client_base, Addr client_end );
 
 // For attaching the debugger
 extern Int  VGA_(ptrace_setregs_from_tst) ( Int pid, ThreadArchState* arch );
diff --git a/coregrind/m_aspacemgr/aspacemgr.c b/coregrind/m_aspacemgr/aspacemgr.c
index eb34a0c..797aec7 100644
--- a/coregrind/m_aspacemgr/aspacemgr.c
+++ b/coregrind/m_aspacemgr/aspacemgr.c
@@ -45,6 +45,30 @@
 static const Bool mem_debug = False;
 
 /*--------------------------------------------------------------*/
+/*--- Basic globals about the address space.                 ---*/
+/*--------------------------------------------------------------*/
+
+/* Client address space, lowest to highest (see top of ume.c) */
+Addr VG_(client_base);           /* client address space limits */
+Addr VG_(client_end);
+Addr VG_(client_mapbase);
+Addr VG_(client_trampoline_code);
+Addr VG_(clstk_base);
+Addr VG_(clstk_end);
+
+Addr VG_(brk_base);	         /* start of brk */
+Addr VG_(brk_limit);	         /* current brk */
+
+Addr VG_(shadow_base);	         /* tool's shadow memory */
+Addr VG_(shadow_end);
+
+Addr VG_(valgrind_base);	 /* valgrind's address range */
+
+// Note that VG_(valgrind_last) names the last byte of the section, whereas
+// the VG_(*_end) vars name the byte one past the end of the section.
+Addr VG_(valgrind_last);
+
+/*--------------------------------------------------------------*/
 /*--- A simple, self-contained ordered array of segments.    ---*/
 /*--------------------------------------------------------------*/
 
diff --git a/coregrind/m_errormgr.c b/coregrind/m_errormgr.c
index cb6d731..3c1b761 100644
--- a/coregrind/m_errormgr.c
+++ b/coregrind/m_errormgr.c
@@ -31,6 +31,7 @@
 #include "core.h"
 #include "pub_core_errormgr.h"
 #include "pub_core_execontext.h"
+#include "pub_core_main.h"          // for VG_(start_debugger)()
 #include "pub_core_options.h"
 #include "pub_core_stacktrace.h"
 #include "pub_core_tooliface.h"
diff --git a/coregrind/vg_main.c b/coregrind/m_main.c
similarity index 97%
rename from coregrind/vg_main.c
rename to coregrind/m_main.c
index 936bdb2..0709cd3 100644
--- a/coregrind/vg_main.c
+++ b/coregrind/m_main.c
@@ -1,6 +1,6 @@
 
 /*--------------------------------------------------------------------*/
-/*--- Startup: the real stuff                            vg_main.c ---*/
+/*--- Startup: the real stuff                             m_main.c ---*/
 /*--------------------------------------------------------------------*/
 
 /*
@@ -36,6 +36,7 @@
 #include "pub_core_debuglog.h"
 #include "pub_core_errormgr.h"
 #include "pub_core_execontext.h"
+#include "pub_core_main.h"
 #include "pub_core_options.h"
 #include "pub_core_redir.h"
 #include "pub_core_syscalls.h"
@@ -101,26 +102,6 @@
    Startup stuff                            
    ------------------------------------------------------------------ */
 
-/* Client address space, lowest to highest (see top of ume.c) */
-Addr VG_(client_base);           /* client address space limits */
-Addr VG_(client_end);
-Addr VG_(client_mapbase);
-Addr VG_(client_trampoline_code);
-Addr VG_(clstk_base);
-Addr VG_(clstk_end);
-
-Addr VG_(brk_base);	         /* start of brk */
-Addr VG_(brk_limit);	         /* current brk */
-
-Addr VG_(shadow_base);	         /* tool's shadow memory */
-Addr VG_(shadow_end);
-
-Addr VG_(valgrind_base);	 /* valgrind's address range */
-
-// Note that VG_(valgrind_last) names the last byte of the section, whereas
-// the VG_(*_end) vars name the byte one past the end of the section.
-Addr VG_(valgrind_last);
-
 struct vki_rlimit VG_(client_rlimit_data);
 struct vki_rlimit VG_(client_rlimit_stack);
 
@@ -155,10 +136,6 @@
 /* 64-bit counter for the number of basic blocks done. */
 ULong VG_(bbs_done) = 0;
 
-/* Tell the logging mechanism whether we are logging to a file
-   descriptor or a socket descriptor. */
-Bool VG_(logging_to_socket) = False;
-
 
 /*====================================================================*/
 /*=== Counters, for profiling purposes only                        ===*/
@@ -285,34 +262,6 @@
 }
 
 
-/* Print some helpful-ish text about unimplemented things, and give
-   up. */
-void VG_(unimplemented) ( Char* msg )
-{
-   VG_(message)(Vg_UserMsg, "");
-   VG_(message)(Vg_UserMsg, 
-      "Valgrind detected that your program requires");
-   VG_(message)(Vg_UserMsg, 
-      "the following unimplemented functionality:");
-   VG_(message)(Vg_UserMsg, "   %s", msg);
-   VG_(message)(Vg_UserMsg,
-      "This may be because the functionality is hard to implement,");
-   VG_(message)(Vg_UserMsg,
-      "or because no reasonable program would behave this way,");
-   VG_(message)(Vg_UserMsg,
-      "or because nobody has yet needed it.  In any case, let us know at");
-   VG_(message)(Vg_UserMsg,
-      "%s and/or try to work around the problem, if you can.", VG_BUGS_TO);
-   VG_(message)(Vg_UserMsg,
-      "");
-   VG_(message)(Vg_UserMsg,
-      "Valgrind has to exit now.  Sorry.  Bye!");
-   VG_(message)(Vg_UserMsg,
-      "");
-   VG_(pp_sched_status)();
-   VG_(exit)(1);
-}
-
 /* Get the simulated stack pointer */
 Addr VG_(get_SP) ( ThreadId tid )
 {
@@ -2817,10 +2766,12 @@
 
    //--------------------------------------------------------------
    // Setup pointercheck
+   //   p: layout_remaining_space() [for VG_(client_{base,end})]
    //   p: process_cmd_line_options() [for VG_(clo_pointercheck)]
    //--------------------------------------------------------------
    if (VG_(clo_pointercheck))
-      VG_(clo_pointercheck) = VGA_(setup_pointercheck)();
+      VG_(clo_pointercheck) =
+         VGA_(setup_pointercheck)( VG_(client_base), VG_(client_end));
 
    //--------------------------------------------------------------
    // Run!
@@ -2920,5 +2871,5 @@
 }
 
 /*--------------------------------------------------------------------*/
-/*--- end                                                vg_main.c ---*/
+/*--- end                                                          ---*/
 /*--------------------------------------------------------------------*/
diff --git a/coregrind/m_scheduler/scheduler.c b/coregrind/m_scheduler/scheduler.c
index 20f5832..ab07a94 100644
--- a/coregrind/m_scheduler/scheduler.c
+++ b/coregrind/m_scheduler/scheduler.c
@@ -60,8 +60,10 @@
                              VG_USERREQ__DISCARD_TRANSLATIONS, and others */
 #include "core.h"
 
+#include "pub_core_aspacemgr.h"
 #include "pub_core_dispatch.h"
 #include "pub_core_errormgr.h"
+#include "pub_core_main.h"
 #include "pub_core_options.h"
 #include "pub_core_replacemalloc.h"
 #include "pub_core_scheduler.h"
diff --git a/coregrind/m_stacktrace.c b/coregrind/m_stacktrace.c
index 7dab5be..82f69d8 100644
--- a/coregrind/m_stacktrace.c
+++ b/coregrind/m_stacktrace.c
@@ -29,6 +29,7 @@
 */
 
 #include "core.h"
+#include "pub_core_aspacemgr.h"
 #include "pub_core_options.h"
 #include "pub_core_stacktrace.h"
 
diff --git a/coregrind/m_syscalls/syscalls.c b/coregrind/m_syscalls/syscalls.c
index da83472..b8657a2 100644
--- a/coregrind/m_syscalls/syscalls.c
+++ b/coregrind/m_syscalls/syscalls.c
@@ -31,6 +31,7 @@
 #include "core.h"
 #include "pub_core_debuglog.h"
 #include "pub_core_aspacemgr.h"
+#include "pub_core_main.h"
 #include "pub_core_stacktrace.h"
 #include "pub_core_syscalls.h"
 #include "pub_core_tooliface.h"
diff --git a/coregrind/m_translate.c b/coregrind/m_translate.c
index 8f04780..0526067 100644
--- a/coregrind/m_translate.c
+++ b/coregrind/m_translate.c
@@ -31,6 +31,7 @@
 
 #include "core.h"
 #include "pub_core_aspacemgr.h"
+#include "pub_core_main.h"       // for VG_(bbs_done)
 #include "pub_core_options.h"
 #include "pub_core_redir.h"
 #include "pub_core_tooliface.h"
diff --git a/coregrind/pub_core_aspacemgr.h b/coregrind/pub_core_aspacemgr.h
index dd96193..1ea3cf2 100644
--- a/coregrind/pub_core_aspacemgr.h
+++ b/coregrind/pub_core_aspacemgr.h
@@ -41,6 +41,21 @@
 
 /* #include "pub_tool_aspacemgr.h" */
 
+// Address space globals
+extern Addr VG_(client_base);	 // client address space limits
+extern Addr VG_(client_end);
+extern Addr VG_(client_mapbase); // base of mappings
+extern Addr VG_(clstk_base);	 // client stack range
+extern Addr VG_(clstk_end);
+extern Addr VG_(client_trampoline_code);
+
+extern Addr VG_(brk_base);	 // start of brk
+extern Addr VG_(brk_limit);	 // current brk
+extern Addr VG_(shadow_base);	 // tool's shadow memory
+extern Addr VG_(shadow_end);
+extern Addr VG_(valgrind_base);	 // valgrind's address range
+extern Addr VG_(valgrind_last);  // Nb: last byte, rather than one past the end
+
 /* A Segment is mapped piece of client memory.  This covers all kinds
    of mapped memory (exe, brk, mmap, .so, shm, stack, etc)
 
diff --git a/coregrind/pub_core_main.h b/coregrind/pub_core_main.h
new file mode 100644
index 0000000..e0bcfc5
--- /dev/null
+++ b/coregrind/pub_core_main.h
@@ -0,0 +1,65 @@
+
+/*--------------------------------------------------------------------*/
+/*--- The main module.                             pub_core_main.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_MAIN_H
+#define __PUB_CORE_MAIN_H
+
+//--------------------------------------------------------------------
+// PURPOSE: This module is the main module, ie. the one holding main().
+// It arguably shouldn't export anything to other modules, since it depends
+// on almost every other module!  But currently it exports quite a few
+// things.
+//--------------------------------------------------------------------
+
+/* Sanity checks which may be done at any time.  The scheduler decides when. */
+extern void VG_(sanity_check_general) ( Bool force_expensive );
+
+extern struct vki_rlimit VG_(client_rlimit_data); /* client's original rlimit data */
+extern struct vki_rlimit VG_(client_rlimit_stack); /* client's original rlimit stack */
+
+/* client executable file descriptor */
+extern Int  VG_(clexecfd);
+
+// Help set up the child used when doing execve() with --trace-children=yes
+Char* VG_(build_child_VALGRINDCLO) ( Char* exename );
+Char* VG_(build_child_exename)     ( void );
+
+/* Something of a function looking for a home ... start up debugger. */
+extern void VG_(start_debugger) ( ThreadId tid );
+
+/* 64-bit counter for the number of basic blocks done. */
+extern ULong VG_(bbs_done);
+
+
+#endif   // __PUB_CORE_MAIN_H
+
+/*--------------------------------------------------------------------*/
+/*--- end                                                          ---*/
+/*--------------------------------------------------------------------*/
diff --git a/coregrind/pub_core_scheduler.h b/coregrind/pub_core_scheduler.h
index 9108d2f..db49232 100644
--- a/coregrind/pub_core_scheduler.h
+++ b/coregrind/pub_core_scheduler.h
@@ -251,6 +251,9 @@
 
 extern void VG_(pp_sched_status) ( void );
 
+/* Stats ... */
+extern void VG_(print_scheduler_stats) ( void );
+
 // Longjmp back to the scheduler and thus enter the sighandler immediately.
 extern void VG_(resume_scheduler) ( ThreadId tid );
 
diff --git a/coregrind/vg_mylibc.c b/coregrind/vg_mylibc.c
index 231dac6..a1aeb92 100644
--- a/coregrind/vg_mylibc.c
+++ b/coregrind/vg_mylibc.c
@@ -33,6 +33,7 @@
 #include "core.h"
 #include "pub_core_aspacemgr.h"
 #include "pub_core_debuglog.h"    /* VG_(debugLog_vprintf) */
+#include "pub_core_main.h"
 #include "pub_core_options.h"
 #include "pub_core_stacktrace.h"
 #include "pub_core_syscalls.h"
@@ -406,10 +407,9 @@
 }
 
 
-/* A general replacement for printf().  Note that only low-level 
-   debugging info should be sent via here.  The official route is to
-   to use vg_message().  This interface is deprecated.
-*/
+/* Tell the logging mechanism whether we are logging to a file
+   descriptor or a socket descriptor. */
+Bool VG_(logging_to_socket) = False;
 
 /* Do the low-level send of a message to the logging sink. */
 static void send_bytes_to_logging_sink ( Char* msg, Int nbytes )
@@ -1051,6 +1051,34 @@
    panic(VG_(details).name, VG_(details).bug_reports_to, str, NULL);
 }
 
+/* Print some helpful-ish text about unimplemented things, and give
+   up. */
+void VG_(unimplemented) ( Char* msg )
+{
+   VG_(message)(Vg_UserMsg, "");
+   VG_(message)(Vg_UserMsg, 
+      "Valgrind detected that your program requires");
+   VG_(message)(Vg_UserMsg, 
+      "the following unimplemented functionality:");
+   VG_(message)(Vg_UserMsg, "   %s", msg);
+   VG_(message)(Vg_UserMsg,
+      "This may be because the functionality is hard to implement,");
+   VG_(message)(Vg_UserMsg,
+      "or because no reasonable program would behave this way,");
+   VG_(message)(Vg_UserMsg,
+      "or because nobody has yet needed it.  In any case, let us know at");
+   VG_(message)(Vg_UserMsg,
+      "%s and/or try to work around the problem, if you can.", VG_BUGS_TO);
+   VG_(message)(Vg_UserMsg,
+      "");
+   VG_(message)(Vg_UserMsg,
+      "Valgrind has to exit now.  Sorry.  Bye!");
+   VG_(message)(Vg_UserMsg,
+      "");
+   VG_(pp_sched_status)();
+   VG_(exit)(1);
+}
+
 
 /* ---------------------------------------------------------------------
    Primitive support for reading files.
diff --git a/coregrind/vg_signals.c b/coregrind/vg_signals.c
index 326de24..ec8d5ff 100644
--- a/coregrind/vg_signals.c
+++ b/coregrind/vg_signals.c
@@ -83,6 +83,7 @@
 
 #include "pub_core_aspacemgr.h"
 #include "pub_core_errormgr.h"
+#include "pub_core_main.h"
 #include "pub_core_options.h"
 #include "pub_core_sigframe.h"
 #include "pub_core_syscalls.h"
diff --git a/coregrind/x86/state.c b/coregrind/x86/state.c
index ff7754b..c6e345a 100644
--- a/coregrind/x86/state.c
+++ b/coregrind/x86/state.c
@@ -158,12 +158,13 @@
 /* Client address space segment limit descriptor entry */
 #define POINTERCHECK_SEGIDX  1
 
-Bool VGA_(setup_pointercheck)(void)
+Bool VGA_(setup_pointercheck)(Addr client_base, Addr client_end)
 {
+   vg_assert(0 != client_end);
    vki_modify_ldt_t ldt = { 
       POINTERCHECK_SEGIDX,       // entry_number
-      VG_(client_base),          // base_addr
-      (VG_(client_end)-VG_(client_base)) / VKI_PAGE_SIZE, // limit
+      client_base,               // base_addr
+      (client_end - client_base) / VKI_PAGE_SIZE, // limit
       1,                         // seg_32bit
       0,                         // contents: data, RW, non-expanding
       0,                         // ! read_exec_only