1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/tools/trace-malloc/lib/nsTraceMalloc.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,226 @@ 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 +#ifndef nsTraceMalloc_h___ 1.10 +#define nsTraceMalloc_h___ 1.11 + 1.12 +#include <stdint.h> 1.13 +#include <stdio.h> /* for FILE */ 1.14 +#include "prtypes.h" 1.15 + 1.16 +#ifdef XP_WIN 1.17 +#define setlinebuf(stream) setvbuf((stream), (char *)NULL, _IOLBF, 0) 1.18 +#endif 1.19 + 1.20 +#ifdef __cplusplus 1.21 +extern "C" { 1.22 +#endif 1.23 + 1.24 +/** 1.25 + * Magic "number" at start of a trace-malloc log file. Inspired by the PNG 1.26 + * magic string, which inspired XPCOM's typelib (.xpt) file magic. See the 1.27 + * NS_TraceMallocStartup comment (below) for magic number differences in log 1.28 + * file structure. 1.29 + */ 1.30 +#define NS_TRACE_MALLOC_MAGIC "XPCOM\nTMLog08\r\n\032" 1.31 +#define NS_TRACE_MALLOC_MAGIC_SIZE 16 1.32 + 1.33 +/** 1.34 + * Trace-malloc stats, traced via the 'Z' event at the end of a log file. 1.35 + */ 1.36 +typedef struct nsTMStats { 1.37 + uint32_t calltree_maxstack; 1.38 + uint32_t calltree_maxdepth; 1.39 + uint32_t calltree_parents; 1.40 + uint32_t calltree_maxkids; 1.41 + uint32_t calltree_kidhits; 1.42 + uint32_t calltree_kidmisses; 1.43 + uint32_t calltree_kidsteps; 1.44 + uint32_t callsite_recurrences; 1.45 + uint32_t backtrace_calls; 1.46 + uint32_t backtrace_failures; 1.47 + uint32_t btmalloc_failures; 1.48 + uint32_t dladdr_failures; 1.49 + uint32_t malloc_calls; 1.50 + uint32_t malloc_failures; 1.51 + uint32_t calloc_calls; 1.52 + uint32_t calloc_failures; 1.53 + uint32_t realloc_calls; 1.54 + uint32_t realloc_failures; 1.55 + uint32_t free_calls; 1.56 + uint32_t null_free_calls; 1.57 +} nsTMStats; 1.58 + 1.59 +#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} 1.60 + 1.61 +/** 1.62 + * Call NS_TraceMallocStartup with a valid file descriptor to enable logging 1.63 + * of compressed malloc traces, including callsite chains. Integers may be 1.64 + * unsigned serial numbers, sizes, or offsets, and require at most 32 bits. 1.65 + * They're encoded as follows: 1.66 + * 0-127 0xxxxxxx (binary, one byte) 1.67 + * 128-16383 10xxxxxx xxxxxxxx 1.68 + * 16384-0x1fffff 110xxxxx xxxxxxxx xxxxxxxx 1.69 + * 0x200000-0xfffffff 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx 1.70 + * 0x10000000-0xffffffff 11110000 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx 1.71 + * Strings are NUL-terminated ASCII. 1.72 + * 1.73 + * Event Operands (magic TMLog01) 1.74 + * 'L' library serial, shared object filename string 1.75 + * 'N' method serial, library serial, demangled name string 1.76 + * 'S' site serial, parent serial, method serial, calling pc offset 1.77 + * 'M' site serial, malloc size 1.78 + * 'C' site serial, calloc size 1.79 + * 'R' site serial, realloc oldsize, realloc size 1.80 + * 'F' site serial, free size 1.81 + * 1.82 + * Event Operands (magic TMLog02) 1.83 + * 'Z' serialized struct tmstats (20 unsigned integers), 1.84 + * maxkids parent callsite serial, 1.85 + * maxstack top callsite serial 1.86 + * 1.87 + * Event Operands (magic TMLog03) 1.88 + * 'T' seconds, microseconds, caption 1.89 + * 1.90 + * Event Operands (magic TMLog04) 1.91 + * 'R' site serial, realloc size, old site serial, realloc oldsize 1.92 + * 1.93 + * Event Operands (magic TMLog05) 1.94 + * 'M' site serial, address, malloc size 1.95 + * 'C' site serial, address, calloc size 1.96 + * 'R' site serial, address, realloc size, old site serial, 1.97 + * old address, old size 1.98 + * 'F' site serial, address, free size 1.99 + * 1.100 + * Event Operands (magic TMLog06) 1.101 + * 'M' site serial, interval time (end), address, malloc size 1.102 + * 'C' site serial, interval time (end), address, calloc size 1.103 + * 'R' site serial, interval time (end), address, realloc size, 1.104 + * old site serial, old address, old size 1.105 + * 'F' site serial, interval time (end), address, free size 1.106 + * 1.107 + * Event Operands (magic TMLog07) 1.108 + * 'M' site serial, interval time (start), duration, address, malloc size 1.109 + * 'C' site serial, interval time (start), duration, address, calloc size 1.110 + * 'R' site serial, interval time (start), duration, address, realloc size, 1.111 + * old site serial, old address, old size 1.112 + * 'F' site serial, interval time (start), duration, address, free size 1.113 + * 1.114 + * Event Operands (magic TMLog08) 1.115 + * 'G' filename serial, source filename string. 1.116 + * 'N' method serial, library serial, source filename serial, 1.117 + * source file linenumber, demangled name string 1.118 + * 1.119 + * See tools/trace-malloc/bloatblame.c for an example log-file reader. 1.120 + */ 1.121 +#define TM_EVENT_LIBRARY 'L' 1.122 +#define TM_EVENT_FILENAME 'G' 1.123 +#define TM_EVENT_METHOD 'N' 1.124 +#define TM_EVENT_CALLSITE 'S' 1.125 +#define TM_EVENT_MALLOC 'M' 1.126 +#define TM_EVENT_CALLOC 'C' 1.127 +#define TM_EVENT_REALLOC 'R' 1.128 +#define TM_EVENT_FREE 'F' 1.129 +#define TM_EVENT_STATS 'Z' 1.130 +#define TM_EVENT_TIMESTAMP 'T' 1.131 + 1.132 +PR_EXTERN(void) NS_TraceMallocStartup(int logfd); 1.133 + 1.134 +/** 1.135 + * Initialize malloc tracing, using the ``standard'' startup arguments. 1.136 + */ 1.137 +PR_EXTERN(int) NS_TraceMallocStartupArgs(int argc, char* argv[]); 1.138 + 1.139 +/** 1.140 + * Return PR_TRUE iff |NS_TraceMallocStartup[Args]| has been successfully called. 1.141 + */ 1.142 +PR_EXTERN(PRBool) NS_TraceMallocHasStarted(void); 1.143 + 1.144 +/** 1.145 + * Stop all malloc tracing, flushing any buffered events to the logfile. 1.146 + */ 1.147 +PR_EXTERN(void) NS_TraceMallocShutdown(void); 1.148 + 1.149 +/** 1.150 + * Disable malloc tracing. 1.151 + */ 1.152 +PR_EXTERN(void) NS_TraceMallocDisable(void); 1.153 + 1.154 +/** 1.155 + * Enable malloc tracing. 1.156 + */ 1.157 +PR_EXTERN(void) NS_TraceMallocEnable(void); 1.158 + 1.159 +/** 1.160 + * Change the log file descriptor, flushing any buffered output to the old 1.161 + * fd, and writing NS_TRACE_MALLOC_MAGIC to the new file if it is zero length. 1.162 + * Return the old fd, so the caller can swap open fds. Return -2 on failure, 1.163 + * which means malloc failure. 1.164 + */ 1.165 +PR_EXTERN(int) NS_TraceMallocChangeLogFD(int fd); 1.166 + 1.167 +/** 1.168 + * Close the file descriptor fd and forget any bookkeeping associated with it. 1.169 + * Do nothing if fd is -1. 1.170 + */ 1.171 +PR_EXTERN(void) NS_TraceMallocCloseLogFD(int fd); 1.172 + 1.173 +/** 1.174 + * Emit a timestamp event with the given caption to the current log file. 1.175 + */ 1.176 +PR_EXTERN(void) NS_TraceMallocLogTimestamp(const char *caption); 1.177 + 1.178 +/** 1.179 + * Walk the stack, dumping frames in standard form to ofp. If skip is 0, 1.180 + * exclude the frames for NS_TraceStack and anything it calls to do the walk. 1.181 + * If skip is less than 0, include -skip such frames. If skip is positive, 1.182 + * exclude that many frames leading to the call to NS_TraceStack. 1.183 + */ 1.184 +PR_EXTERN(void) 1.185 +NS_TraceStack(int skip, FILE *ofp); 1.186 + 1.187 +/** 1.188 + * Dump a human-readable listing of current allocations and their compressed 1.189 + * stack backtraces to the file named by pathname. Beware this file may have 1.190 + * very long lines. 1.191 + * 1.192 + * Return -1 on error with errno set by the system, 0 on success. 1.193 + */ 1.194 +PR_EXTERN(int) 1.195 +NS_TraceMallocDumpAllocations(const char *pathname); 1.196 + 1.197 +/** 1.198 + * Flush all logfile buffers. 1.199 + */ 1.200 +PR_EXTERN(void) 1.201 +NS_TraceMallocFlushLogfiles(void); 1.202 + 1.203 +/** 1.204 + * Track all realloc and free calls operating on a given allocation. 1.205 + */ 1.206 +PR_EXTERN(void) 1.207 +NS_TrackAllocation(void* ptr, FILE *ofp); 1.208 + 1.209 +/* opaque type for API */ 1.210 +typedef struct nsTMStackTraceIDStruct *nsTMStackTraceID; 1.211 + 1.212 +/** 1.213 + * Get an identifier for the stack trace of the current thread (to this 1.214 + * function's callsite) that can be used to print that stack trace later. 1.215 + */ 1.216 +PR_EXTERN(nsTMStackTraceID) 1.217 +NS_TraceMallocGetStackTrace(void); 1.218 + 1.219 +/** 1.220 + * Print the stack trace identified. 1.221 + */ 1.222 +PR_EXTERN(void) 1.223 +NS_TraceMallocPrintStackTrace(FILE *ofp, nsTMStackTraceID id); 1.224 + 1.225 +#ifdef __cplusplus 1.226 +} 1.227 +#endif 1.228 + 1.229 +#endif /* nsTraceMalloc_h___ */