tools/trace-malloc/lib/nsTraceMalloc.h

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.

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 #ifndef nsTraceMalloc_h___
michael@0 7 #define nsTraceMalloc_h___
michael@0 8
michael@0 9 #include <stdint.h>
michael@0 10 #include <stdio.h> /* for FILE */
michael@0 11 #include "prtypes.h"
michael@0 12
michael@0 13 #ifdef XP_WIN
michael@0 14 #define setlinebuf(stream) setvbuf((stream), (char *)NULL, _IOLBF, 0)
michael@0 15 #endif
michael@0 16
michael@0 17 #ifdef __cplusplus
michael@0 18 extern "C" {
michael@0 19 #endif
michael@0 20
michael@0 21 /**
michael@0 22 * Magic "number" at start of a trace-malloc log file. Inspired by the PNG
michael@0 23 * magic string, which inspired XPCOM's typelib (.xpt) file magic. See the
michael@0 24 * NS_TraceMallocStartup comment (below) for magic number differences in log
michael@0 25 * file structure.
michael@0 26 */
michael@0 27 #define NS_TRACE_MALLOC_MAGIC "XPCOM\nTMLog08\r\n\032"
michael@0 28 #define NS_TRACE_MALLOC_MAGIC_SIZE 16
michael@0 29
michael@0 30 /**
michael@0 31 * Trace-malloc stats, traced via the 'Z' event at the end of a log file.
michael@0 32 */
michael@0 33 typedef struct nsTMStats {
michael@0 34 uint32_t calltree_maxstack;
michael@0 35 uint32_t calltree_maxdepth;
michael@0 36 uint32_t calltree_parents;
michael@0 37 uint32_t calltree_maxkids;
michael@0 38 uint32_t calltree_kidhits;
michael@0 39 uint32_t calltree_kidmisses;
michael@0 40 uint32_t calltree_kidsteps;
michael@0 41 uint32_t callsite_recurrences;
michael@0 42 uint32_t backtrace_calls;
michael@0 43 uint32_t backtrace_failures;
michael@0 44 uint32_t btmalloc_failures;
michael@0 45 uint32_t dladdr_failures;
michael@0 46 uint32_t malloc_calls;
michael@0 47 uint32_t malloc_failures;
michael@0 48 uint32_t calloc_calls;
michael@0 49 uint32_t calloc_failures;
michael@0 50 uint32_t realloc_calls;
michael@0 51 uint32_t realloc_failures;
michael@0 52 uint32_t free_calls;
michael@0 53 uint32_t null_free_calls;
michael@0 54 } nsTMStats;
michael@0 55
michael@0 56 #define NS_TMSTATS_STATIC_INITIALIZER {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
michael@0 57
michael@0 58 /**
michael@0 59 * Call NS_TraceMallocStartup with a valid file descriptor to enable logging
michael@0 60 * of compressed malloc traces, including callsite chains. Integers may be
michael@0 61 * unsigned serial numbers, sizes, or offsets, and require at most 32 bits.
michael@0 62 * They're encoded as follows:
michael@0 63 * 0-127 0xxxxxxx (binary, one byte)
michael@0 64 * 128-16383 10xxxxxx xxxxxxxx
michael@0 65 * 16384-0x1fffff 110xxxxx xxxxxxxx xxxxxxxx
michael@0 66 * 0x200000-0xfffffff 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
michael@0 67 * 0x10000000-0xffffffff 11110000 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
michael@0 68 * Strings are NUL-terminated ASCII.
michael@0 69 *
michael@0 70 * Event Operands (magic TMLog01)
michael@0 71 * 'L' library serial, shared object filename string
michael@0 72 * 'N' method serial, library serial, demangled name string
michael@0 73 * 'S' site serial, parent serial, method serial, calling pc offset
michael@0 74 * 'M' site serial, malloc size
michael@0 75 * 'C' site serial, calloc size
michael@0 76 * 'R' site serial, realloc oldsize, realloc size
michael@0 77 * 'F' site serial, free size
michael@0 78 *
michael@0 79 * Event Operands (magic TMLog02)
michael@0 80 * 'Z' serialized struct tmstats (20 unsigned integers),
michael@0 81 * maxkids parent callsite serial,
michael@0 82 * maxstack top callsite serial
michael@0 83 *
michael@0 84 * Event Operands (magic TMLog03)
michael@0 85 * 'T' seconds, microseconds, caption
michael@0 86 *
michael@0 87 * Event Operands (magic TMLog04)
michael@0 88 * 'R' site serial, realloc size, old site serial, realloc oldsize
michael@0 89 *
michael@0 90 * Event Operands (magic TMLog05)
michael@0 91 * 'M' site serial, address, malloc size
michael@0 92 * 'C' site serial, address, calloc size
michael@0 93 * 'R' site serial, address, realloc size, old site serial,
michael@0 94 * old address, old size
michael@0 95 * 'F' site serial, address, free size
michael@0 96 *
michael@0 97 * Event Operands (magic TMLog06)
michael@0 98 * 'M' site serial, interval time (end), address, malloc size
michael@0 99 * 'C' site serial, interval time (end), address, calloc size
michael@0 100 * 'R' site serial, interval time (end), address, realloc size,
michael@0 101 * old site serial, old address, old size
michael@0 102 * 'F' site serial, interval time (end), address, free size
michael@0 103 *
michael@0 104 * Event Operands (magic TMLog07)
michael@0 105 * 'M' site serial, interval time (start), duration, address, malloc size
michael@0 106 * 'C' site serial, interval time (start), duration, address, calloc size
michael@0 107 * 'R' site serial, interval time (start), duration, address, realloc size,
michael@0 108 * old site serial, old address, old size
michael@0 109 * 'F' site serial, interval time (start), duration, address, free size
michael@0 110 *
michael@0 111 * Event Operands (magic TMLog08)
michael@0 112 * 'G' filename serial, source filename string.
michael@0 113 * 'N' method serial, library serial, source filename serial,
michael@0 114 * source file linenumber, demangled name string
michael@0 115 *
michael@0 116 * See tools/trace-malloc/bloatblame.c for an example log-file reader.
michael@0 117 */
michael@0 118 #define TM_EVENT_LIBRARY 'L'
michael@0 119 #define TM_EVENT_FILENAME 'G'
michael@0 120 #define TM_EVENT_METHOD 'N'
michael@0 121 #define TM_EVENT_CALLSITE 'S'
michael@0 122 #define TM_EVENT_MALLOC 'M'
michael@0 123 #define TM_EVENT_CALLOC 'C'
michael@0 124 #define TM_EVENT_REALLOC 'R'
michael@0 125 #define TM_EVENT_FREE 'F'
michael@0 126 #define TM_EVENT_STATS 'Z'
michael@0 127 #define TM_EVENT_TIMESTAMP 'T'
michael@0 128
michael@0 129 PR_EXTERN(void) NS_TraceMallocStartup(int logfd);
michael@0 130
michael@0 131 /**
michael@0 132 * Initialize malloc tracing, using the ``standard'' startup arguments.
michael@0 133 */
michael@0 134 PR_EXTERN(int) NS_TraceMallocStartupArgs(int argc, char* argv[]);
michael@0 135
michael@0 136 /**
michael@0 137 * Return PR_TRUE iff |NS_TraceMallocStartup[Args]| has been successfully called.
michael@0 138 */
michael@0 139 PR_EXTERN(PRBool) NS_TraceMallocHasStarted(void);
michael@0 140
michael@0 141 /**
michael@0 142 * Stop all malloc tracing, flushing any buffered events to the logfile.
michael@0 143 */
michael@0 144 PR_EXTERN(void) NS_TraceMallocShutdown(void);
michael@0 145
michael@0 146 /**
michael@0 147 * Disable malloc tracing.
michael@0 148 */
michael@0 149 PR_EXTERN(void) NS_TraceMallocDisable(void);
michael@0 150
michael@0 151 /**
michael@0 152 * Enable malloc tracing.
michael@0 153 */
michael@0 154 PR_EXTERN(void) NS_TraceMallocEnable(void);
michael@0 155
michael@0 156 /**
michael@0 157 * Change the log file descriptor, flushing any buffered output to the old
michael@0 158 * fd, and writing NS_TRACE_MALLOC_MAGIC to the new file if it is zero length.
michael@0 159 * Return the old fd, so the caller can swap open fds. Return -2 on failure,
michael@0 160 * which means malloc failure.
michael@0 161 */
michael@0 162 PR_EXTERN(int) NS_TraceMallocChangeLogFD(int fd);
michael@0 163
michael@0 164 /**
michael@0 165 * Close the file descriptor fd and forget any bookkeeping associated with it.
michael@0 166 * Do nothing if fd is -1.
michael@0 167 */
michael@0 168 PR_EXTERN(void) NS_TraceMallocCloseLogFD(int fd);
michael@0 169
michael@0 170 /**
michael@0 171 * Emit a timestamp event with the given caption to the current log file.
michael@0 172 */
michael@0 173 PR_EXTERN(void) NS_TraceMallocLogTimestamp(const char *caption);
michael@0 174
michael@0 175 /**
michael@0 176 * Walk the stack, dumping frames in standard form to ofp. If skip is 0,
michael@0 177 * exclude the frames for NS_TraceStack and anything it calls to do the walk.
michael@0 178 * If skip is less than 0, include -skip such frames. If skip is positive,
michael@0 179 * exclude that many frames leading to the call to NS_TraceStack.
michael@0 180 */
michael@0 181 PR_EXTERN(void)
michael@0 182 NS_TraceStack(int skip, FILE *ofp);
michael@0 183
michael@0 184 /**
michael@0 185 * Dump a human-readable listing of current allocations and their compressed
michael@0 186 * stack backtraces to the file named by pathname. Beware this file may have
michael@0 187 * very long lines.
michael@0 188 *
michael@0 189 * Return -1 on error with errno set by the system, 0 on success.
michael@0 190 */
michael@0 191 PR_EXTERN(int)
michael@0 192 NS_TraceMallocDumpAllocations(const char *pathname);
michael@0 193
michael@0 194 /**
michael@0 195 * Flush all logfile buffers.
michael@0 196 */
michael@0 197 PR_EXTERN(void)
michael@0 198 NS_TraceMallocFlushLogfiles(void);
michael@0 199
michael@0 200 /**
michael@0 201 * Track all realloc and free calls operating on a given allocation.
michael@0 202 */
michael@0 203 PR_EXTERN(void)
michael@0 204 NS_TrackAllocation(void* ptr, FILE *ofp);
michael@0 205
michael@0 206 /* opaque type for API */
michael@0 207 typedef struct nsTMStackTraceIDStruct *nsTMStackTraceID;
michael@0 208
michael@0 209 /**
michael@0 210 * Get an identifier for the stack trace of the current thread (to this
michael@0 211 * function's callsite) that can be used to print that stack trace later.
michael@0 212 */
michael@0 213 PR_EXTERN(nsTMStackTraceID)
michael@0 214 NS_TraceMallocGetStackTrace(void);
michael@0 215
michael@0 216 /**
michael@0 217 * Print the stack trace identified.
michael@0 218 */
michael@0 219 PR_EXTERN(void)
michael@0 220 NS_TraceMallocPrintStackTrace(FILE *ofp, nsTMStackTraceID id);
michael@0 221
michael@0 222 #ifdef __cplusplus
michael@0 223 }
michael@0 224 #endif
michael@0 225
michael@0 226 #endif /* nsTraceMalloc_h___ */

mercurial