1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/tools/trace-malloc/allocation-stacks.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,92 @@ 1.4 +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 +#include <stdio.h> 1.10 +#include <stdlib.h> 1.11 +#include <string.h> 1.12 +#include <errno.h> 1.13 +#ifdef HAVE_GETOPT_H 1.14 +#include <getopt.h> 1.15 +#else 1.16 +extern int getopt(int argc, char *const *argv, const char *shortopts); 1.17 +extern char *optarg; 1.18 +extern int optind; 1.19 +#ifdef XP_WIN32 1.20 +int optind=1; 1.21 +#endif 1.22 +#endif 1.23 +#include <time.h> 1.24 +#include "nsTraceMalloc.h" 1.25 +#include "tmreader.h" 1.26 + 1.27 +static char *program; 1.28 + 1.29 +static void my_tmevent_handler(tmreader *tmr, tmevent *event) 1.30 +{ 1.31 + tmcallsite *callsite; 1.32 + 1.33 + switch (event->type) { 1.34 + case TM_EVENT_REALLOC: 1.35 + case TM_EVENT_MALLOC: 1.36 + case TM_EVENT_CALLOC: 1.37 + for (callsite = tmreader_callsite(tmr, event->serial); 1.38 + callsite != &tmr->calltree_root; callsite = callsite->parent) { 1.39 + fprintf(stdout, "%s +%08X (%s:%d)\n", 1.40 + (const char*)callsite->method->graphnode.entry.value, 1.41 + callsite->offset, 1.42 + callsite->method->sourcefile, 1.43 + callsite->method->linenumber); 1.44 + } 1.45 + fprintf(stdout, "\n"); 1.46 + } 1.47 +} 1.48 + 1.49 +int main(int argc, char **argv) 1.50 +{ 1.51 + int i, j, rv; 1.52 + tmreader *tmr; 1.53 + FILE *fp; 1.54 + time_t start; 1.55 + 1.56 + program = *argv; 1.57 + 1.58 + tmr = tmreader_new(program, NULL); 1.59 + if (!tmr) { 1.60 + perror(program); 1.61 + exit(1); 1.62 + } 1.63 + 1.64 + start = time(NULL); 1.65 + fprintf(stdout, "%s starting at %s", program, ctime(&start)); 1.66 + fflush(stdout); 1.67 + 1.68 + argc -= optind; 1.69 + argv += optind; 1.70 + if (argc == 0) { 1.71 + if (tmreader_eventloop(tmr, "-", my_tmevent_handler) <= 0) 1.72 + exit(1); 1.73 + } else { 1.74 + for (i = j = 0; i < argc; i++) { 1.75 + fp = fopen(argv[i], "r"); 1.76 + if (!fp) { 1.77 + fprintf(stderr, "%s: can't open %s: %s\n", 1.78 + program, argv[i], strerror(errno)); 1.79 + exit(1); 1.80 + } 1.81 + rv = tmreader_eventloop(tmr, argv[i], my_tmevent_handler); 1.82 + if (rv < 0) 1.83 + exit(1); 1.84 + if (rv > 0) 1.85 + j++; 1.86 + fclose(fp); 1.87 + } 1.88 + if (j == 0) 1.89 + exit(1); 1.90 + } 1.91 + 1.92 + tmreader_destroy(tmr); 1.93 + 1.94 + exit(0); 1.95 +}