tools/trace-malloc/allocation-stacks.c

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     2  *
     3  * This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this
     5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include <stdio.h>
     7 #include <stdlib.h>
     8 #include <string.h>
     9 #include <errno.h>
    10 #ifdef HAVE_GETOPT_H
    11 #include <getopt.h>
    12 #else
    13 extern int  getopt(int argc, char *const *argv, const char *shortopts);
    14 extern char *optarg;
    15 extern int  optind;
    16 #ifdef XP_WIN32
    17 int optind=1;
    18 #endif
    19 #endif
    20 #include <time.h>
    21 #include "nsTraceMalloc.h"
    22 #include "tmreader.h"
    24 static char *program;
    26 static void my_tmevent_handler(tmreader *tmr, tmevent *event)
    27 {
    28     tmcallsite *callsite;
    30     switch (event->type) {
    31       case TM_EVENT_REALLOC:
    32       case TM_EVENT_MALLOC:
    33       case TM_EVENT_CALLOC:
    34         for (callsite = tmreader_callsite(tmr, event->serial);
    35              callsite != &tmr->calltree_root; callsite = callsite->parent) {
    36             fprintf(stdout, "%s +%08X (%s:%d)\n",
    37                     (const char*)callsite->method->graphnode.entry.value,
    38                     callsite->offset,
    39                     callsite->method->sourcefile,
    40                     callsite->method->linenumber);
    41         }
    42         fprintf(stdout, "\n");
    43     }
    44 }
    46 int main(int argc, char **argv)
    47 {
    48     int i, j, rv;
    49     tmreader *tmr;
    50     FILE *fp;
    51     time_t start;
    53     program = *argv;
    55     tmr = tmreader_new(program, NULL);
    56     if (!tmr) {
    57         perror(program);
    58         exit(1);
    59     }
    61     start = time(NULL);
    62     fprintf(stdout, "%s starting at %s", program, ctime(&start));
    63     fflush(stdout);
    65     argc -= optind;
    66     argv += optind;
    67     if (argc == 0) {
    68         if (tmreader_eventloop(tmr, "-", my_tmevent_handler) <= 0)
    69             exit(1);
    70     } else {
    71         for (i = j = 0; i < argc; i++) {
    72             fp = fopen(argv[i], "r");
    73             if (!fp) {
    74                 fprintf(stderr, "%s: can't open %s: %s\n",
    75                         program, argv[i], strerror(errno));
    76                 exit(1);
    77             }
    78             rv = tmreader_eventloop(tmr, argv[i], my_tmevent_handler);
    79             if (rv < 0)
    80                 exit(1);
    81             if (rv > 0)
    82                 j++;
    83             fclose(fp);
    84         }
    85         if (j == 0)
    86             exit(1);
    87     }
    89     tmreader_destroy(tmr);
    91     exit(0);
    92 }

mercurial