michael@0: /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #ifdef HAVE_GETOPT_H michael@0: #include michael@0: #else michael@0: extern int getopt(int argc, char *const *argv, const char *shortopts); michael@0: extern char *optarg; michael@0: extern int optind; michael@0: #ifdef XP_WIN32 michael@0: int optind=1; michael@0: #endif michael@0: #endif michael@0: #include michael@0: #include "nsTraceMalloc.h" michael@0: #include "tmreader.h" michael@0: michael@0: static char *program; michael@0: michael@0: static void my_tmevent_handler(tmreader *tmr, tmevent *event) michael@0: { michael@0: tmcallsite *callsite; michael@0: michael@0: switch (event->type) { michael@0: case TM_EVENT_REALLOC: michael@0: case TM_EVENT_MALLOC: michael@0: case TM_EVENT_CALLOC: michael@0: for (callsite = tmreader_callsite(tmr, event->serial); michael@0: callsite != &tmr->calltree_root; callsite = callsite->parent) { michael@0: fprintf(stdout, "%s +%08X (%s:%d)\n", michael@0: (const char*)callsite->method->graphnode.entry.value, michael@0: callsite->offset, michael@0: callsite->method->sourcefile, michael@0: callsite->method->linenumber); michael@0: } michael@0: fprintf(stdout, "\n"); michael@0: } michael@0: } michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: int i, j, rv; michael@0: tmreader *tmr; michael@0: FILE *fp; michael@0: time_t start; michael@0: michael@0: program = *argv; michael@0: michael@0: tmr = tmreader_new(program, NULL); michael@0: if (!tmr) { michael@0: perror(program); michael@0: exit(1); michael@0: } michael@0: michael@0: start = time(NULL); michael@0: fprintf(stdout, "%s starting at %s", program, ctime(&start)); michael@0: fflush(stdout); michael@0: michael@0: argc -= optind; michael@0: argv += optind; michael@0: if (argc == 0) { michael@0: if (tmreader_eventloop(tmr, "-", my_tmevent_handler) <= 0) michael@0: exit(1); michael@0: } else { michael@0: for (i = j = 0; i < argc; i++) { michael@0: fp = fopen(argv[i], "r"); michael@0: if (!fp) { michael@0: fprintf(stderr, "%s: can't open %s: %s\n", michael@0: program, argv[i], strerror(errno)); michael@0: exit(1); michael@0: } michael@0: rv = tmreader_eventloop(tmr, argv[i], my_tmevent_handler); michael@0: if (rv < 0) michael@0: exit(1); michael@0: if (rv > 0) michael@0: j++; michael@0: fclose(fp); michael@0: } michael@0: if (j == 0) michael@0: exit(1); michael@0: } michael@0: michael@0: tmreader_destroy(tmr); michael@0: michael@0: exit(0); michael@0: }