int alder_smartall_test() { char *cp, *ra; #ifdef OLD_unix malloc_debug(2); #endif /* Allocate and chain together storage that's subject to the orphaned buffer check. */ ec(malloc(120)); ec(alloc(200)); ec(calloc(10, 4)); ra = alloc(60); strcpy(ra + 8, "Hello, there. This is data."); ra = realloc(ra, 100); ra = realloc(ra, 100); ra = realloc(ra, 2048); ec(realloc(ra, 55)); /* Allocate and chain some storage for which checking is disabled by the sm_static mechanism. */ sm_static(1); ra = malloc(10); ra = alloc(20); ra = calloc(30, sizeof(short)); ra = realloc(ra, 40); sm_static(0); /* Test the "actually" variants. */ ra = actuallymalloc(100); ra = actuallycalloc(10, sizeof(double)); ra = actuallyrealloc(ra, 15 * sizeof(double)); actuallyfree(ra); /* Produce orphan buffer listing. */ sm_dump(0); // sm_dump(Dumparam); /* Release chained buffers. */ while (bchain != NULL) { dc(); } /* Now verify that all have been released. */ #ifdef SMARTALLOC fprintf(stderr, "No orphaned buffer messages should follow this line.\n"); #endif sm_dump(1); return 0; }
void stack_trace() { int status; size_t stack_depth, sz, i; const size_t max_depth = 100; void *stack_addrs[max_depth]; char **stack_strings, *begin, *end, *j, *function, *ret; stack_depth = backtrace(stack_addrs, max_depth); stack_strings = backtrace_symbols(stack_addrs, stack_depth); for (i = 3; i < stack_depth; i++) { sz = 200; /* Just a guess, template names will go much wider */ function = (char *)actuallymalloc(sz); begin = end = 0; /* * Find the parentheses and address offset surrounding the mangled name */ for (j = stack_strings[i]; *j; ++j) { if (*j == '(') { begin = j; } else if (*j == '+') { end = j; } } if (begin && end) { *begin++ = '\0'; *end = '\0'; /* * Found our mangled name, now in [begin, end] */ ret = abi::__cxa_demangle(begin, function, &sz, &status); if (ret) { /* * Return value may be a realloc() of the input */ function = ret; } else { /* * Demangling failed, just pretend it's a C function with no args */ strncpy(function, begin, sz - 3); strcat(function, "()"); function[sz - 1] = '\0'; } Pmsg2(000, " %s:%s\n", stack_strings[i], function); } else { /* didn't find the mangled name, just print the whole line */ Pmsg1(000, " %s\n", stack_strings[i]); } actuallyfree(function); } actuallyfree(stack_strings); /* malloc()ed by backtrace_symbols */ }
/* Generator function for command completion. STATE lets us know whether * to start from scratch; without any state (i.e. STATE == 0), then we * start at the top of the list. */ static char *item_generator(const char *text, int state, const char *item, cpl_item_t type) { static int list_index, len; char *name; /* If this is a new word to complete, initialize now. This includes * saving the length of TEXT for efficiency, and initializing the index * variable to 0. */ if (!state) { list_index = 0; len = strlen(text); switch(type) { case ITEM_ARG: get_items(item); break; case ITEM_HELP: get_arguments(item); break; } } /* Return the next name which partially matches from the command list. */ while (items && list_index < items->list.size()) { name = (char *)items->list[list_index]; list_index++; if (bstrncmp(name, text, len)) { char *ret = (char *) actuallymalloc(strlen(name)+1); strcpy(ret, name); return ret; } } /* If no names matched, then return NULL. */ return ((char *)NULL); }
void stack_trace() { int ret, i; bool demangled_symbol; size_t stack_depth; size_t sz = 200; /* Just a guess, template names will go much wider */ const size_t max_depth = 100; void *stack_addrs[100]; char **stack_strings, *begin, *end, *j, *function; stack_depth = backtrace(stack_addrs, max_depth); stack_strings = backtrace_symbols(stack_addrs, stack_depth); for (i = 1; i < stack_depth; i++) { function = (char *)actuallymalloc(sz); begin = end = 0; /* * Find the single quote and address offset surrounding the mangled name */ for (j = stack_strings[i]; *j; ++j) { if (*j == '\'') { begin = j; } else if (*j == '+') { end = j; } } if (begin && end) { *begin++ = '\0'; *end = '\0'; /* * Found our mangled name, now in [begin, end) */ demangled_symbol = false; while (!demangled_symbol) { ret = cplus_demangle(begin, function, sz); switch (ret) { case DEMANGLE_ENAME: /* * Demangling failed, just pretend it's a C function with no args */ strcat(function, "()"); function[sz - 1] = '\0'; demangled_symbol = true; break; case DEMANGLE_ESPACE: /* * Need more space for demangled function name. */ actuallyfree(function); sz = sz * 2; function = (char *)actuallymalloc(sz); continue; default: demangled_symbol = true; break; } } Pmsg2(000, " %s:%s\n", stack_strings[i], function); } else { /* * Didn't find the mangled name, just print the whole line */ Pmsg1(000, " %s\n", stack_strings[i]); } actuallyfree(function); } actuallyfree(stack_strings); /* malloc()ed by backtrace_symbols */ }