int tmstats(Options* inOptions) /* ** As quick as possible, load the input file and report stats. */ { int retval = 0; tmreader* tmr = NULL; TMStats stats; memset(&stats, 0, sizeof(stats)); stats.mOptions = inOptions; stats.uMinTicks = 0xFFFFFFFFU; /* ** Need a tmreader. */ tmr = tmreader_new(inOptions->mProgramName, &stats); if(NULL != tmr) { int tmResult = 0; tmResult = tmreader_eventloop(tmr, inOptions->mInputName, tmEventHandler); if(0 == tmResult) { retval = __LINE__; ERROR_REPORT(retval, inOptions->mInputName, "Problem reading trace-malloc data."); } tmreader_destroy(tmr); tmr = NULL; if(0 == retval) { retval = report_stats(inOptions, &stats); } } else { retval = __LINE__; ERROR_REPORT(retval, inOptions->mProgramName, "Unable to obtain tmreader."); } return retval; }
tmreader *tmreader_new(const char *program, void *data) { tmreader *tmr; tmr = calloc(1, sizeof *tmr); if (!tmr) return NULL; tmr->program = program; tmr->data = data; PL_INIT_ARENA_POOL(&tmr->arena, "TMReader", 256*1024); tmr->libraries = PL_NewHashTable(100, hash_serial, PL_CompareValues, PL_CompareStrings, &graphnode_hashallocops, &tmr->arena); tmr->filenames = PL_NewHashTable(100, hash_serial, PL_CompareValues, PL_CompareStrings, &filename_hashallocops, &tmr->arena); tmr->components = PL_NewHashTable(10000, PL_HashString, PL_CompareStrings, PL_CompareValues, &component_hashallocops, &tmr->arena); tmr->methods = PL_NewHashTable(10000, hash_serial, PL_CompareValues, PL_CompareStrings, &method_hashallocops, &tmr->arena); tmr->callsites = PL_NewHashTable(200000, hash_serial, PL_CompareValues, PL_CompareValues, &callsite_hashallocops, &tmr->arena); tmr->calltree_root.entry.value = (void*) strdup("root"); if (!tmr->libraries || !tmr->components || !tmr->methods || !tmr->callsites || !tmr->calltree_root.entry.value || !tmr->filenames) { tmreader_destroy(tmr); return NULL; } return tmr; }
int main(int argc, char **argv) { int i, j, rv; tmreader *tmr; FILE *fp; time_t start; handler_data data; program = *argv; handler_data_init(&data); tmr = tmreader_new(program, &data); if (!tmr) { perror(program); exit(1); } start = time(NULL); fprintf(stdout, "%s starting at %s", program, ctime(&start)); fflush(stdout); argc -= optind; argv += optind; if (argc == 0) { if (tmreader_eventloop(tmr, "-", my_tmevent_handler) <= 0) exit(1); } else { for (i = j = 0; i < argc; i++) { fp = fopen(argv[i], "r"); if (!fp) { fprintf(stderr, "%s: can't open %s: %s\n", program, argv[i], strerror(errno)); exit(1); } rv = tmreader_eventloop(tmr, argv[i], my_tmevent_handler); if (rv < 0) exit(1); if (rv > 0) j++; fclose(fp); } if (j == 0) exit(1); } if (!data.finished) { fprintf(stderr, "%s: log file incomplete\n", program); exit(1); } fprintf(stdout, "Leaks: %u bytes, %u allocations\n" "Maximum Heap Size: %u bytes\n" "%u bytes were allocated in %u allocations.\n", data.current_heapsize, data.current_allocations, data.max_heapsize, data.bytes_allocated, data.total_allocations); if (data.unmatched_frees != 0) fprintf(stdout, "Logged %u free (or realloc) calls for which we missed the " "original malloc.\n", data.unmatched_frees); handler_data_finish(&data); tmreader_destroy(tmr); exit(0); }