Ejemplo n.º 1
0
/*
 * output the code to set env VAR to VALUE
 */
static	void    set_variable(const char *var, const char *value)
{
  char	comm[1024];
  
  if (value == NULL || *value == '\0') {
    (void)loc_snprintf(comm, sizeof(comm), "unset %s\n", var);
  }
  else if (bourne_b) {
    (void)loc_snprintf(comm, sizeof(comm), "%s=%s\nexport %s\n",
		       var, value, var);
  }
  else if (rcshell_b) {
    (void)loc_snprintf(comm, sizeof(comm), "%s='%s'\n", var, value);
  }
  else if (gdb_b) {
    (void)loc_snprintf(comm, sizeof(comm), "set env %s %s\n", var, value);
  }
  else {
    (void)loc_snprintf(comm, sizeof(comm), "setenv %s %s\n", var, value);
  }
  
  if (make_changes_b) {
    (void)printf("%s", comm);
  }
  if ((! make_changes_b) || verbose_b) {
    (void)fprintf(stderr, "Outputed:\n");
    (void)fprintf(stderr, "%s", comm);
  }
}
Ejemplo n.º 2
0
/*
 * char *_dmalloc_ptimeval
 *
 * DESCRIPTION:
 *
 * Print the time into local buffer.
 *
 * RETURNS:
 *
 * Poiner to the buf argument.
 *
 * ARGUMENTS:
 *
 * timeval_p -> Pointer to a time value.
 *
 * buf -> Internal buffer into which we are writing the time.
 *
 * buf_size -> Size of the buffer.
 *
 * elapsed_b -> Set to 1 to dump the elapsed instead of global time.
 */
char	*_dmalloc_ptimeval(const TIMEVAL_TYPE *timeval_p, char *buf,
			   const int buf_size, const int elapsed_b)
{
  unsigned long	hrs, mins, secs, usecs;
  
  secs = timeval_p->tv_sec;
  usecs = timeval_p->tv_usec;
  
  if (elapsed_b) {
    if (usecs >= _dmalloc_start.tv_usec) {
      usecs -= _dmalloc_start.tv_usec;
    }
    else {
      usecs = _dmalloc_start.tv_usec - usecs;
      secs--;
    }
    secs -= _dmalloc_start.tv_sec;
    
    hrs = secs / SECS_IN_HOUR;
    mins = (secs / SECS_IN_MIN) % MINS_IN_HOUR;
    secs %= SECS_IN_MIN;
    
    (void)loc_snprintf(buf, buf_size, "%lu:%02lu:%02lu.%06lu",
		       hrs, mins, secs, usecs);
  }
  else {
    (void)loc_snprintf(buf, buf_size, "%lu.%06lu",
		       secs, usecs);
  }
  
  return buf;
}
Ejemplo n.º 3
0
/*
 * void _dmalloc_die
 *
 * DESCRIPTION:
 *
 * Kill the program because of an internal malloc error.
 *
 * RETURNS:
 *
 * None.
 *
 * ARGUMENTS:
 *
 * silent_b -> Set to 1 to not drop log entries.
 */
void	_dmalloc_die(const int silent_b)
{
  char	*stop_str;
  int	len;
  
  if (! silent_b) {
    if (BIT_IS_SET(_dmalloc_flags, DEBUG_ERROR_ABORT)) {
      stop_str = "dumping";
    }
    else {
      stop_str = "halting";
    }
    
    /* print a message that we are going down */
    len = loc_snprintf(error_str, sizeof(error_str),
		       "debug-malloc library: %s program, fatal error\r\n",
		       stop_str);
    (void)write(STDERR, error_str, len);
    if (dmalloc_errno != ERROR_NONE) {
      len = loc_snprintf(error_str, sizeof(error_str),
			 "   Error: %s (err %d)\r\n",
			 dmalloc_strerror(dmalloc_errno), dmalloc_errno);
      (void)write(STDERR, error_str, len);
    }
  }
  
  /*
   * set this in case the following generates a recursive call for
   * some dumb reason
   */
  _dmalloc_aborting_b = 1;
  
  /* do I need to drop core? */
  if (BIT_IS_SET(_dmalloc_flags, DEBUG_ERROR_ABORT)
      || BIT_IS_SET(_dmalloc_flags, DEBUG_ERROR_DUMP)) {
#if USE_ABORT
    abort();
#else
#ifdef KILL_PROCESS
    KILL_PROCESS;
#endif
#endif
  }
  
  /*
   * NOTE: this should not be exit() because fclose will free, etc
   */
  _exit(1);
}
Ejemplo n.º 4
0
/*
 * char *_dmalloc_ptime
 *
 * DESCRIPTION:
 *
 * Print the time into local buffer.
 *
 * RETURNS:
 *
 * Poiner to the buf argument.
 *
 * ARGUMENTS:
 *
 * time_p -> Pointer to a time value.
 *
 * buf -> Internal buffer into which we are writing the time.
 *
 * buf_size -> Size of the buffer.
 *
 * elapsed_b -> Set to 1 to dump the elapsed instead of global time.
 */
char	*_dmalloc_ptime(const TIME_TYPE *time_p, char *buf, const int buf_size,
			const int elapsed_b)
{
  unsigned long	hrs, mins, secs;
  
  secs = *time_p;
  
  if (elapsed_b) {
    secs -= _dmalloc_start;
    
    hrs = secs / SECS_IN_HOUR;
    mins = (secs / SECS_IN_MIN) % MINS_IN_HOUR;
    secs %= SECS_IN_MIN;
    
    (void)loc_snprintf(buf, buf_size, "%lu:%02lu:%02lu", hrs, mins, secs);
  }
  else {
    (void)loc_snprintf(buf, buf_size, "%lu", secs);
  }
  
  return buf;
}
Ejemplo n.º 5
0
/*
 * Read in a rc file from PATH and process it looking for the
 * specified DEBUG_VALUE or TAG_FIND token.  It passes back the
 * returned debug value in DEBUG_P.  Passes back the matching TOKEN of
 * TOKEN_SIZE.
 *
 * Returns FILE_NOT_FOUND, FILE_FOUND, or TOKEN_FOUND.
 */
static	int	read_rc_file(const char *path, const long debug_value,
			     const char *tag_find, long *debug_p,
			     char *token, const int token_size)
{
  FILE	*infile;
  int	found_b = 0;
  char	next_token[64];
  long	new_debug;
  
  /* open the path */
  infile = fopen(path, "r");
  if (infile == NULL) {
    return FILE_NOT_FOUND;
  }
  
  /* run through the tokens, looking for a match */
  while (read_next_token(infile, &new_debug, next_token,
			 sizeof(next_token)) == 1) {
    
    /* are we looking for a tag? */
    if (tag_find != NULL && strcmp(tag_find, next_token) == 0) {
      found_b = 1;
      break;
    }
    
    /* are we looking for a debug-value? */
    if (debug_value > 0 && debug_value == new_debug) {
      found_b = 1;
      break;
    }
  }
  
  (void)fclose(infile);
  
  SET_POINTER(debug_p, new_debug);
  if (token != NULL) {
    (void)loc_snprintf(token, token_size, "config file token: %s",
		       next_token);
  }
  
  if (found_b) {
    return TOKEN_FOUND;
  }
  else {
    return FILE_FOUND;
  }
}
Ejemplo n.º 6
0
/*
 * Set dmalloc environ variable(s) with the values (maybe SHORT debug
 * info) into BUF.
 */
void	_dmalloc_environ_set(char *buf, const int buf_size,
			     const int long_tokens_b,
			     const DMALLOC_PNT address,
			     const unsigned long addr_count,
			     const unsigned int debug,
			     const unsigned long interval, const int lock_on,
			     const char *logpath, const char *start_file_p,
			     const int start_line,
			     const unsigned long start_iter,
			     const unsigned long start_size,
			     const unsigned long limit_val)
{
  char	*buf_p = buf, *bounds_p = buf + buf_size;
  
  if (debug > 0) {
    if (long_tokens_b) {
      attr_t	*attr_p;
      
      for (attr_p = attributes; attr_p->at_string != NULL; attr_p++) {
	if (debug & attr_p->at_value) {
	  buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "%s,",
				attr_p->at_string);
	}
      }
    }
    else {
      buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "%s%c%#x,",
			    DEBUG_LABEL, ASSIGNMENT_CHAR, debug);
    }
  }
  if (address != NULL) {
    if (addr_count > 0) {
      buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "%s%c%#lx:%lu,",
			    ADDRESS_LABEL, ASSIGNMENT_CHAR, (long)address,
			    addr_count);
    }
    else {
      buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "%s%c%#lx,",
			    ADDRESS_LABEL, ASSIGNMENT_CHAR, (long)address);
    }
  }
  if (interval > 0) {
    buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "%s%c%lu,",
			  INTERVAL_LABEL, ASSIGNMENT_CHAR, interval);
  }
  if (lock_on > 0) {
    buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "%s%c%d,",
			  LOCK_ON_LABEL, ASSIGNMENT_CHAR, lock_on);
  }
  if (logpath != NULL) {
    buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "%s%c%s,",
			  LOGFILE_LABEL, ASSIGNMENT_CHAR, logpath);
  }
  if (start_file_p != NULL) {
    if (start_line > 0) {
      buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "%s%c%s:%d,",
			    START_LABEL, ASSIGNMENT_CHAR, start_file_p,
			    start_line);
    }
    else {
      buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "%s%c%s,",
			    START_LABEL, ASSIGNMENT_CHAR, start_file_p);
    }
  }
  else if (start_iter > 0) {
    /* NOTE: there is an 'c' (for count) before the iter variable here */
    buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "%s%cc%lu,",
			  START_LABEL, ASSIGNMENT_CHAR, start_iter);
  }
  else if (start_size > 0) {
    /* NOTE: there is an 's' before the size variable here */
    buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "%s%cs%lu,",
			  START_LABEL, ASSIGNMENT_CHAR, start_size);
  }
  if (limit_val > 0) {
    buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "%s%c%lu,",
			  LIMIT_LABEL, ASSIGNMENT_CHAR, limit_val);
  }
  
  /* cut off the last comma */
  if (buf_p > buf) {
    buf_p--;
  }
  
  *buf_p = '\0';
}
Ejemplo n.º 7
0
/*
 * static void *heap_extend
 *
 * DESCRIPTION:
 *
 * Get more bytes from the system functions.
 *
 * RETURNS:
 *
 * Success - Valid pointer.
 *
 * Failure - NULL
 *
 * ARGUMENTS:
 *
 * incr -> Number of bytes we need.
 */
static	void	*heap_extend(const int incr)
{
  void	*ret = SBRK_ERROR;
  char	*high;
  
#if INTERNAL_MEMORY_SPACE
  {
    static char	block_o_bytes[INTERNAL_MEMORY_SPACE];
    static char *bounds_p = block_o_bytes + sizeof(block_o_bytes);
    static char *block_p = block_o_bytes;
    
    if (block_p + incr >= bounds_p) {
      ret = SBRK_ERROR;
    }
    else {
      ret = block_p;
      block_p += incr;
    }
  }
#else
#if HAVE_MMAP && USE_MMAP
#if MAP_ANON
  /* if we have and can use mmap, then do so */
  ret = mmap(0L, incr, PROT_READ | PROT_WRITE | PROT_EXEC,
	     MAP_PRIVATE | MAP_ANON, -1 /* no fd */, 0 /* no offset */);
#else
#endif
  if (ret == MAP_FAILED) {
    ret = SBRK_ERROR;
  }
#else
#if HAVE_SBRK
  ret = sbrk(incr);
#endif /* if HAVE_SBRK */
#endif /* if not HAVE_MMAP && USE_MMAP */
#endif /* if not INTERNAL_MEMORY_SPACE */
  
  if (ret == SBRK_ERROR) {
    if (BIT_IS_SET(_dmalloc_flags, DEBUG_CATCH_NULL)) {
      char	str[128];
      int	len;
      len = loc_snprintf(str, sizeof(str),
			 "\r\ndmalloc: critical error: could not extend heap %u more bytes\r\n", incr);
      (void)write(STDERR, str, len);
      _dmalloc_die(0);
    }
    dmalloc_errno = ERROR_ALLOC_FAILED;
    dmalloc_error("heap_extend");
  }
  
  if (_dmalloc_heap_low == NULL || (char *)ret < (char *)_dmalloc_heap_low) {
    _dmalloc_heap_low = ret;
  }
  high = (char *)ret + incr;
  if (high > (char *)_dmalloc_heap_high) {
    _dmalloc_heap_high = high;
  }
  
  if (BIT_IS_SET(_dmalloc_flags, DEBUG_LOG_ADMIN)) {
    dmalloc_message("extended heap space by %d bytes [%#lx, %#lx]",
		    incr, (unsigned long)_dmalloc_heap_low,
		    (unsigned long)_dmalloc_heap_high);
  }
  
  return ret;
}
Ejemplo n.º 8
0
/*
 * List the tags that in the files.
 */
static	void	list_tags(void)
{
  char		path[1024], *path_p, token[80];
  default_t	*def_p;
  const char	*home_p;
  long		new_debug = 0;
  FILE		*rc_file;
  
  /* do we need to have a home variable? */
  if (inpath == NULL) {
    
    /* first we try to read the RC file from the current directory */
    rc_file = fopen(DEFAULT_CONFIG, "r");
    if (rc_file == NULL) {
      
      /* if no file in current directory, try home directory */
      home_p = getenv(HOME_ENVIRON);
      if (home_p == NULL) {
	(void)fprintf(stderr, "%s: could not find variable '%s'\n",
		      argv_program, HOME_ENVIRON);
	exit(1);
      }
      
      (void)loc_snprintf(path, sizeof(path), "%s/%s", home_p, DEFAULT_CONFIG);
      path_p = path;
      
      rc_file = fopen(path, "r");
      /* we don't check for error right here */
    }
    else {
      path_p = DEFAULT_CONFIG;
    }
  }
  else {
    
    /* open the specified file */
    rc_file = fopen(inpath, "r");
    /* we assume that if the file was specified, it must be there */
    if (rc_file == NULL) {
      (void)fprintf(stderr, "%s: could not read '%s': ",
		    argv_program, inpath);
      perror("");
      exit(1);
    }
    path_p = inpath;
  }
  
  if (rc_file != NULL) {
    (void)fprintf(stderr, "Tags available from '%s':\n", path_p);
    
    while (read_next_token(rc_file, &new_debug, token, sizeof(token)) == 1) {
      if (verbose_b) {
	(void)fprintf(stderr, "%s (%#lx):\n", token, new_debug);
	dump_debug(new_debug);
      }
      else {
	(void)fprintf(stderr, "%s\n", token);
      }
    }
    
    (void)fclose(rc_file);
  }
  
  (void)fprintf(stderr, "\n");
  (void)fprintf(stderr, "Tags available by default:\n");
  
  for (def_p = defaults; def_p->de_string != NULL; def_p++) {
    if (verbose_b) {
      (void)fprintf(stderr, "%s (%#lx):\n",
		    def_p->de_string, def_p->de_flags);
      dump_debug(def_p->de_flags);
    }
    else {
      (void)fprintf(stderr, "%s\n", def_p->de_string);
    }
  }
}
Ejemplo n.º 9
0
/*
 * Process the user configuration looking for the TAG_FIND.  If it is
 * null then look for DEBUG_VALUE in the file and copy the token found
 * into TOKEN of TOKEN_SIZE.  Routine returns the new debug value
 * matching tag.
 */
static	long	find_tag(const long debug_value, const char *tag_find,
			 char *token, const int token_size)
{
  char		path[1024], *path_p;
  default_t	*def_p;
  const char	*home_p;
  int		ret;
  long		new_debug = 0;
  
  /* do we need to have a home variable? */
  if (inpath == NULL) {
    
    /* first we try to read the RC file from the current directory */
    ret = read_rc_file(DEFAULT_CONFIG, debug_value, tag_find, &new_debug,
		       token, token_size);
    /* did we find the correct value in the file? */
    if (ret == TOKEN_FOUND) {
      return new_debug;
    }
    
    /* if we did not find the file, check the home directory */
    if (ret == FILE_FOUND) {
      path_p = DEFAULT_CONFIG;
    }
    else {
      /* find our home directory */
      home_p = getenv(HOME_ENVIRON);
      if (home_p == NULL) {
	(void)fprintf(stderr, "%s: could not find variable '%s'\n",
		      argv_program, HOME_ENVIRON);
	exit(1);
      }
      
      (void)loc_snprintf(path, sizeof(path), "%s/%s", home_p, DEFAULT_CONFIG);
      
      /* read in the file from our home directory */
      ret = read_rc_file(path, debug_value, tag_find, &new_debug,
			 token, token_size);
      /* did we find the correct value in the file? */
      if (ret == TOKEN_FOUND) {
	return new_debug;
      }
      if (ret == FILE_FOUND) {
	path_p = path;
      }
      else {
	path_p = NULL;
      }
    }
  }
  else {
    /* read in the specified file */
    ret = read_rc_file(inpath, debug_value, tag_find, &new_debug,
		       token, token_size);
    /* did we find the correct value in the file? */
    if (ret == TOKEN_FOUND) {
      return new_debug;
    }
    /* if the specified was not found, return error */
    if (ret != FILE_FOUND) {
      (void)fprintf(stderr, "%s: could not read '%s': ",
		    argv_program, inpath);
      perror("");
      exit(1);
    }
    path_p = inpath;
  }
  
  /* if tag-find is NULL we assume we are looking for a debug-value */
  if (tag_find == NULL) {
    
    /* now look for the value in the default token list */
    if (token != NULL) {
      for (def_p = defaults; def_p->de_string != NULL; def_p++) {
	if (def_p->de_flags == debug_value) {
	  (void)loc_snprintf(token, token_size, "internal token: %s",
			     def_p->de_string);
	  new_debug = def_p->de_flags;
	  break;
	}
      }
      if (def_p->de_string == NULL) {
	(void)loc_snprintf(token, token_size, "unknown token");
	new_debug = 0;
      }
    }
  }
  else {
    
    /* now look for the token in the default token list */
    for (def_p = defaults; def_p->de_string != NULL; def_p++) {
      if (strcmp(tag_find, def_p->de_string) == 0) {
	new_debug = def_p->de_flags;
	break;
      }
    }
    
    /* did we not find the token? */
    if (def_p->de_string == NULL) {
      if (path_p == NULL) {
	(void)fprintf(stderr, "%s: unknown tag '%s'\n",
		      argv_program, tag_find);
      }
      else {
	(void)fprintf(stderr, "%s: could not find tag '%s' in '%s'\n",
		      argv_program, tag_find, path_p);
      }
      exit(1);
    }
  }
  
  return new_debug;
}
Ejemplo n.º 10
0
/*
 * void _dmalloc_vmessage
 *
 * DESCRIPTION:
 *
 * Message writer with vprintf like arguments which adds a line to the
 * dmalloc logfile.
 *
 * RETURNS:
 *
 * None.
 *
 * ARGUMENTS:
 *
 * format -> Printf-style format statement.
 *
 * args -> Already converted pointer to a stdarg list.
 */
void	_dmalloc_vmessage(const char *format, va_list args)
{
  char	*str_p, *bounds_p;
  int	len;
  
  str_p = message_str;
  bounds_p = str_p + sizeof(message_str);
  
  /* no logpath and no print then no workie */
  if (dmalloc_logpath == NULL
      && ! BIT_IS_SET(_dmalloc_flags, DEBUG_PRINT_MESSAGES)) {
    return;
  }
  
#if HAVE_GETPID && LOG_REOPEN
  if (dmalloc_logpath != NULL) {
    char	*log_p;
    
    /*
     * This static pid will be checked to make sure it doesn't change.
     * We make it long in case it's big and we hope it will promote if
     * not.
     */
    static long		current_pid = -1;
    long		new_pid;
    
    new_pid = getpid();
    if (new_pid != current_pid) {
      /* NOTE: we need to do this _before_ the reopen otherwise we recurse */
      current_pid = new_pid;
      
      /* if the new pid doesn't match the old one then reopen it */
      if (current_pid >= 0) {
	
	/* this only works if there is a %p in the logpath */
	for (log_p = dmalloc_logpath; *log_p != '\0'; log_p++) {
	  if (*log_p == '%' && *(log_p + 1) == 'p') {
	    _dmalloc_reopen_log();
	    break;
	  }
	}
      }
    }
  }
#endif
  
  /* do we need to open the logfile? */
  if (dmalloc_logpath != NULL && outfile_fd < 0) {
    _dmalloc_open_log();
  }
  
#if HAVE_TIME
#if LOG_TIME_NUMBER
  {
    long	now;
    now = time(NULL);
    str_p += loc_snprintf(str_p, bounds_p - str_p, "%ld: ", now);
  }
#endif /* LOG_TIME_NUMBER */
#if HAVE_CTIME
#if LOG_CTIME_STRING
  {
    TIME_TYPE	now;
    now = time(NULL);
    str_p += loc_snprintf(str_p, bounds_p - str_p, "%.24s: ", ctime(&now));
  }
#endif /* LOG_CTIME_STRING */
#endif /* HAVE_CTIME */
#endif /* HAVE_TIME */
  
#if LOG_ITERATION
  /* add the iteration number */
  str_p += loc_snprintf(str_p, bounds_p - str_p, "%lu: ", _dmalloc_iter_c);
#endif
#if LOG_PID && HAVE_GETPID
  {
    /* we make it long in case it's big and we hope it will promote if not */
    long	our_pid = getpid();
    
    /* add the pid to the log file */
    str_p += loc_snprintf(str_p, bounds_p - str_p, "p%ld: ", our_pid);
  }
#endif
  
  /*
   * NOTE: the following code, as well as the function definition
   * above, would need to be altered to conform to non-ANSI-C
   * specifications if necessary.
   */
  
  /* write the format + info into str */
  len = loc_vsnprintf(str_p, bounds_p - str_p, format, args);
  
  /* was it an empty format? */
  if (len == 0) {
    return;
  }
  str_p += len;
  
  /* tack on a '\n' if necessary */
  if (*(str_p - 1) != '\n') {
    *str_p++ = '\n';
    *str_p = '\0';
  }
  len = str_p - message_str;
  
  /* do we need to write the message to the logfile */
  if (dmalloc_logpath != NULL) {
    (void)write(outfile_fd, message_str, len);
  }
  
  /* do we need to print the message? */
  if (BIT_IS_SET(_dmalloc_flags, DEBUG_PRINT_MESSAGES)) {
    (void)write(STDERR, message_str, len);
  }
}
Ejemplo n.º 11
0
/*
 * void _dmalloc_open_log
 *
 * DESCRIPTION:
 *
 * Open up our log file and write some version of settings
 * information.
 *
 * RETURNS:
 *
 * None.
 *
 * ARGUMENTS:
 *
 * None.
 */
void	_dmalloc_open_log(void)
{
  char	log_path[1024];
  int	len;
  
  /* if it's already open or if we don't have a log file configured */
  if (outfile_fd >= 0
      || dmalloc_logpath == NULL) {
    return;
  }
  
  build_logfile_path(log_path, sizeof(log_path));
  
  /* open our logfile */
  outfile_fd = open(log_path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  if (outfile_fd < 0) {
    /* NOTE: we can't use dmalloc_message of course so do it the hardway */
    len = loc_snprintf(error_str, sizeof(error_str),
		       "debug-malloc library: could not open '%s'\r\n",
		       log_path);
    (void)write(STDERR, error_str, len);
    /* disable log_path */
    dmalloc_logpath = NULL;
    return;
  }
  
  /*
   * NOTE: this makes it go recursive here, but it will never enter
   * this section of code.
   */
  
  dmalloc_message("Dmalloc version '%s' from '%s'",
		  dmalloc_version, DMALLOC_HOME);
  dmalloc_message("flags = %#x, logfile '%s'", _dmalloc_flags, log_path);
  dmalloc_message("interval = %lu, addr = %#lx, seen # = %ld, limit = %ld",
		  _dmalloc_check_interval, (unsigned long)_dmalloc_address,
		  _dmalloc_address_seen_n, _dmalloc_memory_limit);
#if LOCK_THREADS
  dmalloc_message("threads enabled, lock-on = %d, lock-init = %d",
		  _dmalloc_lock_on, THREAD_INIT_LOCK);
#endif
    
#if LOG_PNT_TIMEVAL
  {
    char	time_buf[64];
    dmalloc_message("starting time = %s",
		     _dmalloc_ptimeval(&_dmalloc_start, time_buf,
				       sizeof(time_buf), 0));
  }
#else
#if HAVE_TIME /* NOT LOG_PNT_TIME */
  {
    char	time_buf[64];
    dmalloc_message("starting time = %s",
		     _dmalloc_ptime(&_dmalloc_start, time_buf,
				    sizeof(time_buf), 0));
  }
#endif
#endif
  
#if HAVE_GETPID
  {
    /* we make it long in case it's big and we hope it will promote if not */
    long	our_pid = getpid();
    
    dmalloc_message("process pid = %ld", our_pid);
  }
#endif
}
Ejemplo n.º 12
0
/*
 * void _dmalloc_open_log
 *
 * DESCRIPTION:
 *
 * Open up our log file and write some version of settings
 * information.
 *
 * RETURNS:
 *
 * None.
 *
 * ARGUMENTS:
 *
 * None.
 */
static	void	build_logfile_path(char *buf, const int buf_len)
{
  char	*bounds_p;
  char	*path_p, *buf_p, *start_p;
  int	len;
  
  if (dmalloc_logpath == NULL) {
    buf[0] = '\0';
    return;
  }
  
  buf_p = buf;
  bounds_p = buf + buf_len;
  
  start_p = dmalloc_logpath;
  for (path_p = dmalloc_logpath; *path_p != '\0'; path_p++) {
    
    /* if we don't have to do anything special then just continue */
    if (*path_p != '%' || *(path_p + 1) == '\0') {
      if (buf_p < bounds_p) {
	*buf_p++ = *path_p;
      }
      continue;
    }
    
    /* skip over the % */
    path_p++;
    
    /* dump the hostname */
    if (*path_p == 'h') {
#if HAVE_GETHOSTNAME
      char	our_host[128];
      gethostname(our_host, sizeof(our_host));
      buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "%s", our_host);
#else
      buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "not-gethostname");
#endif
    }
    /* dump the thread-id */
    if (*path_p == 'i') {
#if LOG_PNT_THREAD_ID
      char		id_str[256];
      THREAD_TYPE	id;
      
      id = THREAD_GET_ID();
      THREAD_ID_TO_STRING(id_str, sizeof(id_str), id);
      buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "%s", id_str);
#else
      buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "no-thread-id");
#endif
    }
    /* dump the pid -- also support backwards compatibility with %d */
    if (*path_p == 'p' || *path_p == 'd') {
#if HAVE_GETPID
      /* we make it long in case it's big and we hope it will promote if not */
      long	our_pid = getpid();
      buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "%ld", our_pid);
#else
      buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "no-getpid");
#endif
    }
    /* dump the time value */
    if (*path_p == 't') {
#if HAVE_TIME
      /* we make time a long here so it will promote */
      long	now;
      now = time(NULL);
      buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "%ld", now);
#else
      buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "no-time");
#endif
    }
    /* dump the user-id */
    if (*path_p == 'u') {
#if HAVE_GETUID
      /* we make it long in case it's big and we hope it will promote if not */
      long	our_uid = getuid();
      buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "%ld", our_uid);
#else
      buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "no-uid");
#endif
    }
  }
  
  if (buf_p >= bounds_p - 1) {
    /* NOTE: we can't use dmalloc_message of course so do it the hard way */
    len = loc_snprintf(error_str, sizeof(error_str),
		       "debug-malloc library: logfile path too large '%s'\r\n",
		       dmalloc_logpath);
    (void)write(STDERR, error_str, len);
  }
  
  *buf_p = '\0';
}