Add a new module: the debug-logger.  For a while now, we've used the
same logging mechanism to emit both end-user messages and debugging-
valgrind-itself messages.  This commit creates a new mechanism for the
latter purpose.

The main feature of m_debuglog is that it has zero dependencies on any
other module and therefore can safely operate all the way through
stage1 and stage2 startup.  It is restricted to emitting debug info on
file descriptor 2 (stderr), but that's not a big deal.

As a result of this change the basic formatted-print routines
(vprintf) have been moved from vg_mylibc.c into m_debuglog, so that
m_debuglog remains standalone.

The %y format string is currently disabled, since supporting it ("show
symbol corresponding to this address") would create a dependency from
m_debuglog to the entire debug-info reading machinery and all the
stuff that depends on, thereby making a nonsense of m_debuglog being
standalone.  Its omission does not seem to cause any regression tests
to fail, though.

The debug logger is activated with "-d".  More "-d"s make it more
verbose.

m_debuglog.o is linked into both stage1 and stage2, but as it is
completely standalone this causes no particular problems.




git-svn-id: svn://svn.valgrind.org/valgrind/trunk@3559 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/coregrind/vg_main.c b/coregrind/vg_main.c
index c9b184d..7e5c717 100644
--- a/coregrind/vg_main.c
+++ b/coregrind/vg_main.c
@@ -34,6 +34,7 @@
 #include "ume.h"
 #include "pub_core_execontext.h"
 #include "pub_core_errormgr.h"
+#include "pub_core_debuglog.h"
 
 #include <dirent.h>
 #include <dlfcn.h>
@@ -1672,6 +1673,10 @@
                VG_CLO_STREQ(arg, "--quiet"))
          VG_(clo_verbosity)--;
 
+      else if (VG_CLO_STREQ(arg, "-d")) {
+         /* do nothing */
+      }
+
       else VG_BOOL_CLO(arg, "--branchpred",       VG_(clo_branchpred))
       else VG_BOOL_CLO(arg, "--db-attach",        VG_(clo_db_attach))
       else VG_BOOL_CLO(arg, "--demangle",         VG_(clo_demangle))
@@ -2406,6 +2411,22 @@
    return True;
 }
 
+/* This may be needed before m_mylibc is OK to run. */
+static Int local_strcmp ( const HChar* s1, const HChar* s2 )
+{
+   while (True) {
+      if (*s1 == 0 && *s2 == 0) return 0;
+      if (*s1 == 0) return -1;
+      if (*s2 == 0) return 1;
+
+      if (*(UChar*)s1 < *(UChar*)s2) return -1;
+      if (*(UChar*)s1 > *(UChar*)s2) return 1;
+
+      s1++; s2++;
+   }
+}
+
+
 int main(int argc, char **argv, char **envp)
 {
    char **cl_argv;
@@ -2420,7 +2441,7 @@
    Addr sp_at_startup;     /* client's SP at the point we gained control. */
    UInt * client_auxv;
    struct vki_rlimit zero = { 0, 0 };
-   Int padfile;
+   Int padfile, loglevel, i;
 
    //============================================================
    // Nb: startup is complex.  Prerequisites are shown at every step.
@@ -2428,6 +2449,26 @@
    // *** Be very careful when messing with the order ***
    //============================================================
 
+   //--------------------------------------------------------------
+   // Start up the logging mechanism
+   //   p: none
+   //--------------------------------------------------------------
+   /* Start the debugging-log system ASAP.  First find out how many 
+      "-d"s were specified.  This is a pre-scan of the command line. */
+   loglevel = 0;
+   for (i = 1; i < argc; i++) {
+     if (argv[i][0] != '-')
+        break;
+     if (0 == local_strcmp(argv[i], "--")) 
+        break;
+     if (0 == local_strcmp(argv[i], "-d")) 
+        loglevel++;
+   }
+
+   /* ... and start the debug logger.  Now we can safely emit logging
+      messages all through startup. */
+   VG_(debugLog_startup)(loglevel, "Stage 2");
+
    //============================================================
    // Command line argument handling order:
    // * If --help/--help-debug are present, show usage message