Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | #include <stdio.h> |
michael@0 | 7 | #include <stdlib.h> |
michael@0 | 8 | #include <string.h> |
michael@0 | 9 | #include <errno.h> |
michael@0 | 10 | #ifdef HAVE_GETOPT_H |
michael@0 | 11 | #include <getopt.h> |
michael@0 | 12 | #else |
michael@0 | 13 | extern int getopt(int argc, char *const *argv, const char *shortopts); |
michael@0 | 14 | extern char *optarg; |
michael@0 | 15 | extern int optind; |
michael@0 | 16 | #ifdef XP_WIN32 |
michael@0 | 17 | int optind=1; |
michael@0 | 18 | #endif |
michael@0 | 19 | #endif |
michael@0 | 20 | #include <time.h> |
michael@0 | 21 | #include "nsTraceMalloc.h" |
michael@0 | 22 | #include "tmreader.h" |
michael@0 | 23 | #include "prlog.h" |
michael@0 | 24 | |
michael@0 | 25 | static char *program; |
michael@0 | 26 | |
michael@0 | 27 | typedef struct handler_data { |
michael@0 | 28 | uint32_t current_heapsize; |
michael@0 | 29 | uint32_t max_heapsize; |
michael@0 | 30 | uint32_t bytes_allocated; |
michael@0 | 31 | uint32_t current_allocations; |
michael@0 | 32 | uint32_t total_allocations; |
michael@0 | 33 | uint32_t unmatched_frees; |
michael@0 | 34 | int finished; |
michael@0 | 35 | } handler_data; |
michael@0 | 36 | |
michael@0 | 37 | static void handler_data_init(handler_data *data) |
michael@0 | 38 | { |
michael@0 | 39 | data->current_heapsize = 0; |
michael@0 | 40 | data->max_heapsize = 0; |
michael@0 | 41 | data->bytes_allocated = 0; |
michael@0 | 42 | data->current_allocations = 0; |
michael@0 | 43 | data->total_allocations = 0; |
michael@0 | 44 | data->unmatched_frees = 0; |
michael@0 | 45 | data->finished = 0; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | static void handler_data_finish(handler_data *data) |
michael@0 | 49 | { |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | static void my_tmevent_handler(tmreader *tmr, tmevent *event) |
michael@0 | 53 | { |
michael@0 | 54 | handler_data *data = (handler_data*) tmr->data; |
michael@0 | 55 | |
michael@0 | 56 | switch (event->type) { |
michael@0 | 57 | case TM_EVENT_REALLOC: |
michael@0 | 58 | /* On Windows, original allocation could be before we overrode malloc */ |
michael@0 | 59 | if (event->u.alloc.oldserial != 0) { |
michael@0 | 60 | data->current_heapsize -= event->u.alloc.oldsize; |
michael@0 | 61 | --data->current_allocations; |
michael@0 | 62 | } else { |
michael@0 | 63 | ++data->unmatched_frees; |
michael@0 | 64 | PR_ASSERT(event->u.alloc.oldsize == 0); |
michael@0 | 65 | } |
michael@0 | 66 | /* fall-through intentional */ |
michael@0 | 67 | case TM_EVENT_MALLOC: |
michael@0 | 68 | case TM_EVENT_CALLOC: |
michael@0 | 69 | ++data->current_allocations; |
michael@0 | 70 | ++data->total_allocations; |
michael@0 | 71 | data->bytes_allocated += event->u.alloc.size; |
michael@0 | 72 | data->current_heapsize += event->u.alloc.size; |
michael@0 | 73 | if (data->current_heapsize > data->max_heapsize) |
michael@0 | 74 | data->max_heapsize = data->current_heapsize; |
michael@0 | 75 | break; |
michael@0 | 76 | case TM_EVENT_FREE: |
michael@0 | 77 | /* On Windows, original allocation could be before we overrode malloc */ |
michael@0 | 78 | if (event->serial != 0) { |
michael@0 | 79 | --data->current_allocations; |
michael@0 | 80 | data->current_heapsize -= event->u.alloc.size; |
michael@0 | 81 | } else { |
michael@0 | 82 | ++data->unmatched_frees; |
michael@0 | 83 | PR_ASSERT(event->u.alloc.size == 0); |
michael@0 | 84 | } |
michael@0 | 85 | break; |
michael@0 | 86 | case TM_EVENT_STATS: |
michael@0 | 87 | data->finished = 1; |
michael@0 | 88 | break; |
michael@0 | 89 | } |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | |
michael@0 | 93 | int main(int argc, char **argv) |
michael@0 | 94 | { |
michael@0 | 95 | int i, j, rv; |
michael@0 | 96 | tmreader *tmr; |
michael@0 | 97 | FILE *fp; |
michael@0 | 98 | time_t start; |
michael@0 | 99 | handler_data data; |
michael@0 | 100 | |
michael@0 | 101 | program = *argv; |
michael@0 | 102 | |
michael@0 | 103 | handler_data_init(&data); |
michael@0 | 104 | tmr = tmreader_new(program, &data); |
michael@0 | 105 | if (!tmr) { |
michael@0 | 106 | perror(program); |
michael@0 | 107 | exit(1); |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | start = time(NULL); |
michael@0 | 111 | fprintf(stdout, "%s starting at %s", program, ctime(&start)); |
michael@0 | 112 | fflush(stdout); |
michael@0 | 113 | |
michael@0 | 114 | argc -= optind; |
michael@0 | 115 | argv += optind; |
michael@0 | 116 | if (argc == 0) { |
michael@0 | 117 | if (tmreader_eventloop(tmr, "-", my_tmevent_handler) <= 0) |
michael@0 | 118 | exit(1); |
michael@0 | 119 | } else { |
michael@0 | 120 | for (i = j = 0; i < argc; i++) { |
michael@0 | 121 | fp = fopen(argv[i], "r"); |
michael@0 | 122 | if (!fp) { |
michael@0 | 123 | fprintf(stderr, |
michael@0 | 124 | "TEST-UNEXPECTED-FAIL | leakstats | can't open %s: %s\n", |
michael@0 | 125 | argv[i], strerror(errno)); |
michael@0 | 126 | exit(1); |
michael@0 | 127 | } |
michael@0 | 128 | rv = tmreader_eventloop(tmr, argv[i], my_tmevent_handler); |
michael@0 | 129 | if (rv < 0) |
michael@0 | 130 | exit(1); |
michael@0 | 131 | if (rv > 0) |
michael@0 | 132 | j++; |
michael@0 | 133 | fclose(fp); |
michael@0 | 134 | } |
michael@0 | 135 | if (j == 0) |
michael@0 | 136 | exit(1); |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | if (!data.finished) { |
michael@0 | 140 | fprintf(stderr, "TEST-UNEXPECTED-FAIL | leakstats | log file incomplete\n"); |
michael@0 | 141 | exit(1); |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | fprintf(stdout, |
michael@0 | 145 | "Leaks: %u bytes, %u allocations\n" |
michael@0 | 146 | "Maximum Heap Size: %u bytes\n" |
michael@0 | 147 | "%u bytes were allocated in %u allocations.\n", |
michael@0 | 148 | data.current_heapsize, data.current_allocations, |
michael@0 | 149 | data.max_heapsize, |
michael@0 | 150 | data.bytes_allocated, data.total_allocations); |
michael@0 | 151 | if (data.unmatched_frees != 0) |
michael@0 | 152 | fprintf(stdout, |
michael@0 | 153 | "Logged %u free (or realloc) calls for which we missed the " |
michael@0 | 154 | "original malloc.\n", |
michael@0 | 155 | data.unmatched_frees); |
michael@0 | 156 | |
michael@0 | 157 | handler_data_finish(&data); |
michael@0 | 158 | tmreader_destroy(tmr); |
michael@0 | 159 | |
michael@0 | 160 | exit(0); |
michael@0 | 161 | } |