Ejemplo n.º 1
0
int main(int argc, char** argv, char** envp)
{
   int i, j, loglevel, r;
   const char *toolname = NULL;
   const char *clientname = NULL;
   const char *platform;
   const char *default_platform;
   const char *cp;
   char *toolfile;
   char launcher_name[PATH_MAX+1];
   char* new_line;
   char** new_env;

   /* Start the debugging-log system ASAP.  First find out how many 
      "-d"s were specified.  This is a pre-scan of the command line.
      At the same time, look for the tool name. */
   loglevel = 0;
   for (i = 1; i < argc; i++) {
      if (argv[i][0] != '-') {
         clientname = argv[i];
         break;
      }
      if (0 == strcmp(argv[i], "--")) {
         if (i+1 < argc)
            clientname = argv[i+1];
         break;
      }
      if (0 == strcmp(argv[i], "-d")) 
         loglevel++;
      if (0 == strncmp(argv[i], "--tool=", 7)) 
         toolname = argv[i] + 7;
   }

   /* ... and start the debug logger.  Now we can safely emit logging
      messages all through startup. */
   VG_(debugLog_startup)(loglevel, "Stage 1");

   /* Make sure we know which tool we're using */
   if (toolname) {
      VG_(debugLog)(1, "launcher", "tool '%s' requested\n", toolname);
   } else {
      VG_(debugLog)(1, "launcher", 
                       "no tool requested, defaulting to 'memcheck'\n");
      toolname = "memcheck";
   }

   /* Select a platform to use if we can't decide that by looking at
      the executable (eg because it's a shell script).  Note that the
      default_platform is not necessarily either the primary or
      secondary build target.  Instead it's chosen to maximise the
      chances that /bin/sh will work on it.  Hence for a primary
      target of ppc64-linux we still choose ppc32-linux as the default
      target, because on most ppc64-linux setups, the basic /bin,
      /usr/bin, etc, stuff is built in 32-bit mode, not 64-bit
      mode. */
   if ((0==strcmp(VG_PLATFORM,"x86-linux"))   ||
       (0==strcmp(VG_PLATFORM,"amd64-linux")) ||
       (0==strcmp(VG_PLATFORM,"ppc32-linux")) ||
       (0==strcmp(VG_PLATFORM,"ppc64-linux")) ||
       (0==strcmp(VG_PLATFORM,"arm-linux"))   ||
       (0==strcmp(VG_PLATFORM,"s390x-linux")) ||
       (0==strcmp(VG_PLATFORM,"mips32-linux")))
      default_platform = VG_PLATFORM;
   else
      barf("Unknown VG_PLATFORM '%s'", VG_PLATFORM);

   /* Work out what platform to use, or use the default platform if
      not possible. */
   if (clientname == NULL) {
      VG_(debugLog)(1, "launcher", 
                       "no client specified, defaulting platform to '%s'\n",
                        default_platform);
      platform = default_platform;
   } else if ((platform = select_platform(clientname)) != NULL) {
      VG_(debugLog)(1, "launcher", "selected platform '%s'\n", platform);
   } else {
      VG_(debugLog)(1, "launcher", 
                       "no platform detected, defaulting platform to '%s'\n",
                       default_platform);
      platform = default_platform;
   }
   
   /* Figure out the name of this executable (viz, the launcher), so
      we can tell stage2.  stage2 will use the name for recursive
      invocations of valgrind on child processes. */
   memset(launcher_name, 0, PATH_MAX+1);
   r = readlink("/proc/self/exe", launcher_name, PATH_MAX);
   if (r == -1) {
      /* If /proc/self/exe can't be followed, don't give up.  Instead
         continue with an empty string for VALGRIND_LAUNCHER.  In the
         sys_execve wrapper, this is tested, and if found to be empty,
         fail the execve. */
      fprintf(stderr, "valgrind: warning (non-fatal): "
                      "readlink(\"/proc/self/exe\") failed.\n");
      fprintf(stderr, "valgrind: continuing, however --trace-children=yes "
                      "will not work.\n");
   }

   /* tediously augment the env: VALGRIND_LAUNCHER=launcher_name */
   new_line = malloc(strlen(VALGRIND_LAUNCHER) + 1 
                     + strlen(launcher_name) + 1);
   if (new_line == NULL)
      barf("malloc of new_line failed.");
   strcpy(new_line, VALGRIND_LAUNCHER);
   strcat(new_line, "=");
   strcat(new_line, launcher_name);

   for (j = 0; envp[j]; j++)
      ;
   new_env = malloc((j+2) * sizeof(char*));
   if (new_env == NULL)
      barf("malloc of new_env failed.");
   for (i = 0; i < j; i++)
      new_env[i] = envp[i];
   new_env[i++] = new_line;
   new_env[i++] = NULL;
   assert(i == j+2);

   /* Establish the correct VALGRIND_LIB. */
   cp = getenv(VALGRIND_LIB);

   if (cp != NULL)
      valgrind_lib = cp;

   /* Build the stage2 invocation, and execve it.  Bye! */
   toolfile = malloc(strlen(valgrind_lib) + strlen(toolname) + strlen(platform) + 3);
   if (toolfile == NULL)
      barf("malloc of toolfile failed.");
   sprintf(toolfile, "%s/%s-%s", valgrind_lib, toolname, platform);

   VG_(debugLog)(1, "launcher", "launching %s\n", toolfile);

   execve(toolfile, argv, new_env);

   fprintf(stderr, "valgrind: failed to start tool '%s' for platform '%s': %s\n",
                   toolname, platform, strerror(errno));

   exit(1);
}
Ejemplo n.º 2
0
int main(int argc, char** argv, char** envp)
{
   int i, j, loglevel, r;
   const char *toolname = NULL;
   const char *clientname = NULL;
   const char *platform;
   const char *cp;
   char *toolfile;
   char launcher_name[PATH_MAX+1];
   char* new_line;
   char** new_env;

   /* Start the debugging-log system ASAP.  First find out how many 
      "-d"s were specified.  This is a pre-scan of the command line.
      At the same time, look for the tool name. */
   loglevel = 0;
   for (i = 1; i < argc; i++) {
      if (argv[i][0] != '-') {
         clientname = argv[i];
         break;
      }
      if (0 == strcmp(argv[i], "--")) {
         if (i+1 < argc)
            clientname = argv[i+1];
         break;
      }
      if (0 == strcmp(argv[i], "-d")) 
         loglevel++;
      if (0 == strncmp(argv[i], "--tool=", 7)) 
         toolname = argv[i] + 7;
   }

   /* ... and start the debug logger.  Now we can safely emit logging
      messages all through startup. */
   VG_(debugLog_startup)(loglevel, "Stage 1");

   /* Make sure we know which tool we're using */
   if (toolname) {
      VG_(debugLog)(1, "launcher", "tool '%s' requested\n", toolname);
   } else {
      VG_(debugLog)(1, "launcher", 
                       "no tool requested, defaulting to 'memcheck'\n");
      toolname = "memcheck";
   }

   /* Work out what platform to use */
   if (clientname == NULL) {
      VG_(debugLog)(1, "launcher", "no client specified, defaulting platform to '%s'\n", VG_PLATFORM);
      platform = VG_PLATFORM;
   } else if ((platform = select_platform(clientname)) != NULL) {
      VG_(debugLog)(1, "launcher", "selected platform '%s'\n", platform);
   } else {
      VG_(debugLog)(1, "launcher", "no platform detected, defaulting platform to '%s'\n", VG_PLATFORM);
      platform = VG_PLATFORM;
   }
   
   /* Figure out the name of this executable (viz, the launcher), so
      we can tell stage2.  stage2 will use the name for recursive
      invokations of valgrind on child processes. */
/* #if defined VGO_netbsdelf2 */
/* // ok /proc/self/exe is not a symlink for us, that means we ust put */
/* // that as the laungher name and see how it goes.  -XXX this may be */
/* // broken */
/*    snprintf(launcher_name,PATH_MAX,"%s","/proc/self/exe"); */
/* #else */
#ifndef VGO_netbsdelf2
   memset(launcher_name, 0, PATH_MAX+1);
   r = readlink("/proc/self/exe", launcher_name, PATH_MAX);
   if (r == -1) {
      /* If /proc/self/exe can't be followed, don't give up.  Instead
         continue with an empty string for VALGRIND_LAUNCHER.  In the
         sys_execve wrapper, this is tested, and if found to be empty,
         fail the execve. */
      fprintf(stderr, "valgrind: warning (non-fatal): "
                      "readlink(\"/proc/self/exe\") failed.\n");
      fprintf(stderr, "valgrind: continuing, however --trace-children=yes "
                      "will not work.\n");
   }
/* #endif */

   /* tediously augment the env: VALGRIND_LAUNCHER=launcher_name */
   new_line = malloc(strlen(VALGRIND_LAUNCHER) + 1 
                     + strlen(launcher_name) + 1);
   if (new_line == NULL)
      barf("malloc of new_line failed.");
   strcpy(new_line, VALGRIND_LAUNCHER);
   strcat(new_line, "=");
   strcat(new_line, launcher_name);

   for (j = 0; envp[j]; j++)
      ;
   new_env = malloc((j+2) * sizeof(char*)); // allocate space for the ptrs
   if (new_env == NULL)
      barf("malloc of new_env failed.");
   printf("envp[0] is %s\n",envp[0]);
   for (i = 0; i < j; i++) // envp[0] is what?!
      new_env[i] = envp[i];
   new_env[i++] = new_line;
   new_env[i++] = NULL;
   assert(i == j+2);
#endif 
   /* Establish the correct VALGRIND_LIB. */
   cp = getenv(VALGRIND_LIB);

   if (cp != NULL)
      valgrind_lib = cp;

   /* Build the stage2 invokation, and execve it.  Bye! */
   toolfile = malloc(strlen(valgrind_lib) + strlen(toolname) + strlen(platform) + 3);
   if (toolfile == NULL)
      barf("malloc of toolfile failed.");
   sprintf(toolfile, "%s/%s/%s", valgrind_lib, platform, toolname);

   VG_(debugLog)(1, "launcher", "launching %s\n", toolfile);
// error here
   printf("toolfile : %s \n argv: %s \n valgrind_launcher %s  \n",toolfile, *argv,VALGRIND_LAUNCHER);
#ifdef VGO_netbsdelf2
   execve(toolfile, argv, envp);
#else
   execve(toolfile, argv, new_env);
#endif
   fprintf(stderr, "valgrind: failed to start tool '%s' for platform '%s': %s\n"
	   "Please set the VALGRIND_LIB environment variable to point to the\n"
	   ".in_place directory under valgrind toplevel sourcedir for now\n",
                   toolname, platform, strerror(errno));

   exit(1);
}
Ejemplo n.º 3
0
/* Examine the client and work out which platform it is for */
static const char *select_platform(const char *clientname)
{
   int fd;
   char header[4096];
   ssize_t n_bytes;
   const char *platform = NULL;

   VG_(debugLog)(2, "launcher", "selecting platform for '%s'\n", clientname);

   if (strchr(clientname, '/') == NULL)
      clientname = find_client(clientname);

   VG_(debugLog)(2, "launcher", "selecting platform for '%s'\n", clientname);

   if ((fd = open(clientname, O_RDONLY)) < 0)
      return NULL;
   //   barf("open(%s): %s", clientname, strerror(errno));

   VG_(debugLog)(2, "launcher", "opened '%s'\n", clientname);

   n_bytes = read(fd, header, sizeof(header));
   close(fd);
   if (n_bytes < 2) {
      return NULL;
   }

   VG_(debugLog)(2, "launcher", "read %ld bytes from '%s'\n",
                    (long int)n_bytes, clientname);

   if (header[0] == '#' && header[1] == '!') {
      int i = 2;
      char *interp = (char *)header + 2;

      // Skip whitespace.
      while (1) {
         if (i == n_bytes) return NULL;
         if (' ' != header[i] && '\t' != header[i]) break;
         i++;
      }

      // Get the interpreter name.
      interp = &header[i];
      while (1) {
         if (i == n_bytes) break;
         if (isspace(header[i])) break;
         i++;
      }
      if (i == n_bytes) return NULL;
      header[i] = '\0';

      platform = select_platform(interp);

   } else if (n_bytes >= SELFMAG && memcmp(header, ELFMAG, SELFMAG) == 0) {

      if (n_bytes >= sizeof(Elf32_Ehdr) && header[EI_CLASS] == ELFCLASS32) {
         const Elf32_Ehdr *ehdr = (Elf32_Ehdr *)header;

         if (header[EI_DATA] == ELFDATA2LSB) {
            if (ehdr->e_machine == EM_386 &&
                (ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV ||
                 ehdr->e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
               platform = "x86-linux";
            }
            else 
            if (ehdr->e_machine == EM_ARM &&
                (ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV ||
                 ehdr->e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
               platform = "arm-linux";
            }
            else
            if (ehdr->e_machine == EM_MIPS &&
                (ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV ||
                 ehdr->e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
               platform = "mips32-linux";
            }
         }
         else if (header[EI_DATA] == ELFDATA2MSB) {
            if (ehdr->e_machine == EM_PPC &&
                (ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV ||
                 ehdr->e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
               platform = "ppc32-linux";
            }
            else 
            if (ehdr->e_machine == EM_MIPS &&
                (ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV ||
                 ehdr->e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
               platform = "mips32-linux";
            }
         }

      } else if (n_bytes >= sizeof(Elf64_Ehdr) && header[EI_CLASS] == ELFCLASS64) {
         const Elf64_Ehdr *ehdr = (Elf64_Ehdr *)header;

         if (header[EI_DATA] == ELFDATA2LSB) {
            if (ehdr->e_machine == EM_X86_64 &&
                (ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV ||
                 ehdr->e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
               platform = "amd64-linux";
            }
         } else if (header[EI_DATA] == ELFDATA2MSB) {
#           if !defined(VGPV_arm_linux_android) && !defined(VGPV_x86_linux_android)
            if (ehdr->e_machine == EM_PPC64 &&
                (ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV ||
                 ehdr->e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
               platform = "ppc64-linux";
            } 
            else 
            if (ehdr->e_machine == EM_S390 &&
                (ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV ||
                 ehdr->e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
               platform = "s390x-linux";
            }
#           endif
         }
      }
   }

   VG_(debugLog)(2, "launcher", "selected platform '%s'\n",
                 platform ? platform : "unknown");

   return platform;
}
Ejemplo n.º 4
0
/* Examine the client and work out which platform it is for */
static const char *select_platform(const char *clientname)
{
   int fd;
   unsigned char *header;
   const char *platform = NULL;
   long pagesize = sysconf(_SC_PAGESIZE);

   if (strchr(clientname, '/') == NULL)
      clientname = find_client(clientname);

   if ((fd = open(clientname, O_RDONLY)) < 0)
      return NULL;
   //   barf("open(%s): %s", clientname, strerror(errno));

   if ((header = mmap(NULL, pagesize, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0)) == MAP_FAILED)
      return NULL;
   //   barf("mmap(%s): %s", clientname, strerror(errno));

   close(fd);

   if (header[0] == '#' && header[1] == '!') {
      char *interp = (char *)header + 2;
      char *interpend;

      while (*interp == ' ' || *interp == '\t')
         interp++;

      for (interpend = interp; !isspace(*interpend); interpend++)
         ;

      *interpend = '\0';

      platform = select_platform(interp);
   } else if (memcmp(header, ELFMAG, SELFMAG) == 0 &&
              header[EI_CLASS] == ELFCLASS32 &&
              header[EI_DATA] == ELFDATA2LSB) {
      const Elf32_Ehdr *ehdr = (Elf32_Ehdr *)header;

      if (ehdr->e_machine == EM_386 &&
          ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV) {
#ifdef NetBSD 
	      platform ="x86-netbsdelf2";
#else
         platform = "x86-linux";
#endif
      } else if (ehdr->e_machine == EM_PPC &&
                 ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV) {
         platform = "ppc32-linux";
      }
   } else if (memcmp(header, ELFMAG, SELFMAG) == 0 &&
              header[EI_CLASS] == ELFCLASS64 &&
              header[EI_DATA] == ELFDATA2LSB) {
      const Elf64_Ehdr *ehdr = (Elf64_Ehdr *)header;

      if (ehdr->e_machine == EM_X86_64 &&
          ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV) {
         platform = "amd64-linux";
      }
   }

   munmap(header, pagesize);

   return platform;
}