Logo Search packages:      
Sourcecode: eresi version File versions  Download package

int traces_run ( elfshobj_t file,
char **  argv,
int  argc 
)

Run a trace session (used in etrace)

Parameters:
file target
argv argument list
argc argument counter

Definition at line 1155 of file trace.c.

References buf, elfsh_get_hdr(), elfsh_get_objtype(), s_obj::name, and revm_output().

Referenced by cmd_tracerun().

{
  char            *home;
  char            *filename;
  size_t    len;
  u_int           index;
  char            *path;
  char            *av[argc+2];
  int       ret;
  struct timeval now, after;
  double    difftime;

  PROFILER_IN(__FILE__, __FUNCTION__, __LINE__);

  /* We use etrace */
  if (world.state.revm_mode == REVM_STATE_TRACER)
    {
      argc = 0;
      if (last_arguments)
      {
        while (last_arguments[argc] != NULL)
          argc++;
        argv = last_arguments;
      }
      else
      {
        XALLOC(__FILE__, __FUNCTION__, __LINE__, argv, sizeof(char*), -1);
        argv[0] = NULL;
      }
    }

  if (argc < 0 || !argv)
    PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__,
             "Invalid parameters", -1); 

  if (!(elfsh_get_objtype(elfsh_get_hdr(file)) == ET_EXEC))
    PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__,
             "We need a ET_EXEC binary", -1);

  home = getenv("HOME");

  if (!home)
    PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__,
             "Cannot find home directory", -1);
  
  snprintf(buf, BUFSIZ - 1, "%s/%s", home, REVM_TRACE_REP);

  /* Create temp directory if needed */
  mkdir(buf, 0700);

  /* We search the filename (we have a path) */
  len = strlen(file->name);
  filename = file->name;

  for (index = len; index > 0; index--)
    if (file->name[index] == '/')
      break;
  
  if (INTERVAL(0, index+1, len))
    filename = file->name + index + 1;

  /* Full path */
  snprintf(buf, BUFSIZ - 1, "%s/%s/%s", home, REVM_TRACE_REP, filename);
  path = strdup(buf);

  /* Pre register working objects for ELFsh */
  elfsh_register_working_objects(&world.curjob->loaded,
                         &world.shared_hash);

  if (elfsh_save_obj(file, path) < 0)
    PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__,
               "Failed to save object", -1);

  av[0] = path;
  for (index = 0; index < argc; index++)
    av[index+1] = argv[index];
  av[index+1] = NULL;

  gettimeofday(&now, NULL);

  /* Print start */
  revm_output("Start execution of ");
  for (index = 0; index < argc+1; index++)
    {
      snprintf(buf, BUFSIZ - 1, "%s ", av[index]);
      revm_output(buf);
    }
  revm_output("\n");

  /* Start with arguments */
  if (!fork())
    ret = execv(av[0], av);
  else
    wait(NULL);

  gettimeofday(&after, NULL);

  difftime = (after.tv_sec - now.tv_sec) + ((double) (after.tv_usec - now.tv_usec)) * 0.000001;

  snprintf(buf, BUFSIZ - 1, "End execution (%.6f sec)\n\n", difftime);
  revm_output(buf);

  XFREE(__FILE__, __FUNCTION__, __LINE__, path);

  PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0);
}


Generated by  Doxygen 1.6.0   Back to index