⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dmalloc.c

📁 测试内存泄露工具
💻 C
📖 第 1 页 / 共 2 页
字号:
      }      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;}/* * 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);    }  }}/* * dump the current settings of the malloc variables */static	void	dump_current(void){  char		*log_path, *loc_start_file, token[64];  const char	*env_str;  DMALLOC_PNT	addr;  unsigned long	inter, limit_val, loc_start_size, loc_start_iter;  long		addr_count;  int		lock_on, loc_start_line;  unsigned int	flags;    /* get the options flag */  env_str = getenv(OPTIONS_ENVIRON);  if (env_str == NULL) {    env_str = "";  }  _dmalloc_environ_process(env_str, &addr, &addr_count, &flags,			   &inter, &lock_on, &log_path,			   &loc_start_file, &loc_start_line, &loc_start_iter,			   &loc_start_size, &limit_val);    if (flags == 0) {    (void)fprintf(stderr, "Debug-Flags  not-set\n");  }  else {    (void)find_tag(flags, NULL, token, sizeof(token));    (void)fprintf(stderr, "Debug-Flags %#x (%u) (%s)\n",		  flags, flags, token);    if (verbose_b) {      dump_debug(flags);    }  }    if (addr == NULL) {    (void)fprintf(stderr, "Address      not-set\n");  }  else {    if (addr_count == 0) {      (void)fprintf(stderr, "Address      %#lx\n", (long)addr);    }    else {      (void)fprintf(stderr, "Address      %#lx, count = %ld\n",		    (long)addr, addr_count);    }  }    if (inter == 0) {    (void)fprintf(stderr, "Interval     not-set\n");  }  else {    (void)fprintf(stderr, "Interval     %lu\n", inter);  }    if (lock_on == 0) {    (void)fprintf(stderr, "Lock-On      not-set\n");  }  else {    (void)fprintf(stderr, "Lock-On      %d\n", lock_on);  }    if (log_path == NULL) {    (void)fprintf(stderr, "Logpath      not-set\n");  }  else {    (void)fprintf(stderr, "Logpath      '%s'\n", log_path);  }    if (limit_val == 0) {    (void)fprintf(stderr, "Mem-Limit    not-set\n");  }  else {    (void)fprintf(stderr, "Mem-Limit    %lu\n", limit_val);  }    if (loc_start_file != NULL) {    (void)fprintf(stderr, "Start-File   '%s', line = %d\n",		  loc_start_file, loc_start_line);  }  else if (loc_start_iter > 0) {    (void)fprintf(stderr, "Start-Count  %lu\n", loc_start_iter);  }  else if (loc_start_size > 0) {    (void)fprintf(stderr, "Start-Size   %lu\n", loc_start_size);  }  else {    (void)fprintf(stderr, "Start        not-set\n");  }    (void)fprintf(stderr, "\n");  (void)fprintf(stderr, "Debug Malloc Utility: http://dmalloc.com/\n");  (void)fprintf(stderr,		"  For a list of the command-line options enter: %s --usage\n",		argv_argv[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);  }}/* * Returns the string for ERROR_NUM. */static	char	*local_strerror(const int error_num){  error_str_t	*err_p;    for (err_p = error_list; err_p->es_error != 0; err_p++) {    if (err_p->es_error == error_num) {      return err_p->es_string;    }  }    return INVALID_ERROR;}/* * static void header * * DESCRIPTION: * * Print out a little usage header to the user. * * RETURNS: * * None. * * ARGUMENTS: * * None. */static	void	header(void){  (void)fprintf(stderr,		"Debug Malloc Utility: http://dmalloc.com/\n");  (void)fprintf(stderr,		"  This utility helps set the Debug Malloc environment variables.\n");}int	main(int argc, char **argv){  char		buf[1024];  int		set_b = 0;  char		*log_path, *loc_start_file;  const char	*env_str;  DMALLOC_PNT	addr;  unsigned long	inter, limit_val, loc_start_size, loc_start_iter;  long		addr_count;  int		lock_on;  int		loc_start_line;  unsigned int	flags;    argv_help_string = "Sets dmalloc library env variables.  Also try --usage.";  argv_version_string = dmalloc_version;    argv_process(args, argc, argv);    if (help_b) {    header();    (void)fprintf(stderr,		  "  For a list of the command-line options enter: %s --usage\n",		  argv_argv[0]);    exit(0);  }  if (usage_b) {    header();    argv_usage(args, ARGV_USAGE_ALL);    exit(0);  }    if (very_verbose_b) {    verbose_b = 1;  }    /* try to figure out the shell we are using */  if ((! bourne_b) && (! cshell_b) && (! gdb_b) && (! rcshell_b)) {    choose_shell();  }    /* get the current debug information from the env variable */  env_str = getenv(OPTIONS_ENVIRON);  if (env_str == NULL) {    env_str = "";  }  _dmalloc_environ_process(env_str, &addr, &addr_count, &flags, &inter,			   &lock_on, &log_path, &loc_start_file,			   &loc_start_line, &loc_start_iter, &loc_start_size,			   &limit_val);    /*   * So, if a tag was specified on the command line then we set the   * debug from it.  If it was not then we see if the debug flags were   * set as a hex value from the -d.  If this was not used then take   * the current value.   */  if (tag == NULL) {    if (argv_was_used(args, DEBUG_ARG)) {      set_b = 1;      /* should we clear the rest? */      if (remove_auto_b && (! keep_b)) {	clear_b = 1;      }    }    else {      debug = flags;    }  }  else {    if (argv_was_used(args, DEBUG_ARG)) {      (void)fprintf(stderr, "%s: warning -d ignored, processing tag '%s'\n",		    argv_program, tag);    }    set_b = 1;    debug = find_tag(0L, tag, NULL, 0);    /* should we clear the rest? */    if (remove_auto_b && (! keep_b)) {      clear_b = 1;    }  }    if (plus.aa_entry_n > 0) {    int		plus_c;    for (plus_c = 0; plus_c < plus.aa_entry_n; plus_c++) {      BIT_SET(debug, token_to_value(ARGV_ARRAY_ENTRY(plus, char *, plus_c)));      set_b = 1;    }  }    if (minus.aa_entry_n > 0) {    int		minus_c;    for (minus_c = 0; minus_c < minus.aa_entry_n; minus_c++) {      BIT_CLEAR(debug,		token_to_value(ARGV_ARRAY_ENTRY(minus, char *, minus_c)));      set_b = 1;    }  }    if (address != NULL) {    _dmalloc_address_break(address, &addr, &addr_count);    set_b = 1;  }  else if (clear_b) {    addr = NULL;  }    if (argv_was_used(args, INTERVAL_ARG)) {    inter = interval;    set_b = 1;  }  else if (clear_b) {    inter = 0;  }    /*   * NOTE: this should be after the debug setting which this tests.   */  if (argv_was_used(args, THREAD_LOCK_ON_ARG)) {    lock_on = thread_lock_on;    set_b = 1;  }  else if (clear_b) {    lock_on = 0;  }    if (logpath != NULL) {    log_path = logpath;    set_b = 1;  }  else if (clear_b) {    log_path = NULL;  }    if (start_file != NULL) {    _dmalloc_start_break(start_file, &loc_start_file, &loc_start_line,			 &loc_start_iter, &loc_start_size);    set_b = 1;  }  else if (start_iter > 0) {    loc_start_file = NULL;    loc_start_line = 0;    loc_start_iter = start_iter;    loc_start_size = 0;    set_b = 1;  }  else if (start_size > 0) {    loc_start_file = NULL;    loc_start_line = 0;    loc_start_iter = 0;    loc_start_size = start_size;    set_b = 1;  }  else if (clear_b) {    loc_start_file = NULL;    loc_start_line = 0;    loc_start_iter = 0;    loc_start_size = 0;  }    if (argv_was_used(args, LIMIT_ARG)) {    limit_val = limit_arg;    set_b = 1;  }    if (errno_to_print > 0) {    (void)fprintf(stderr, "%s: dmalloc_errno value '%d' = \n",		  argv_program, errno_to_print);    (void)fprintf(stderr, "   '%s'\n", local_strerror(errno_to_print));  }    if (list_tags_b) {    list_tags();  }    if (debug_tokens_b) {    attr_t		*attr_p;    unsigned int	left = 0x7fffffff;        (void)fprintf(stderr, "Debug Tokens:\n");    for (attr_p = attributes; attr_p->at_string != NULL; attr_p++) {      /* skip any disabled tokens */      if (attr_p->at_value == 0 && strcmp(attr_p->at_string, "none") != 0) {	continue;      }      if (attr_p->at_value != 0 && (! BIT_IS_SET(left, attr_p->at_value))) {	/* skip any tokens we've seen before */	continue;      }      if (very_verbose_b) {	(void)fprintf(stderr, "%s -- %s (%#lx)\n",		      attr_p->at_string, attr_p->at_desc, attr_p->at_value);      }      else if (verbose_b) {	(void)fprintf(stderr, "%s -- %s\n",		      attr_p->at_string, attr_p->at_desc);      }      else {	(void)fprintf(stderr, "%s\n", attr_p->at_string);      }      BIT_CLEAR(left, attr_p->at_value);    }  }    if (clear_b || set_b) {    _dmalloc_environ_set(buf, sizeof(buf), long_tokens_b, addr, addr_count,			 debug, inter, lock_on, log_path, loc_start_file,			 loc_start_line, loc_start_iter, loc_start_size,			 limit_val);    set_variable(OPTIONS_ENVIRON, buf);  }  else if (errno_to_print == 0	   && (! list_tags_b)	   && (! debug_tokens_b)) {    dump_current();  }    argv_cleanup(args);    exit(0);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -