michael@0: /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: #ifndef nsTraceMalloc_h___ michael@0: #define nsTraceMalloc_h___ michael@0: michael@0: #include michael@0: #include /* for FILE */ michael@0: #include "prtypes.h" michael@0: michael@0: #ifdef XP_WIN michael@0: #define setlinebuf(stream) setvbuf((stream), (char *)NULL, _IOLBF, 0) michael@0: #endif michael@0: michael@0: #ifdef __cplusplus michael@0: extern "C" { michael@0: #endif michael@0: michael@0: /** michael@0: * Magic "number" at start of a trace-malloc log file. Inspired by the PNG michael@0: * magic string, which inspired XPCOM's typelib (.xpt) file magic. See the michael@0: * NS_TraceMallocStartup comment (below) for magic number differences in log michael@0: * file structure. michael@0: */ michael@0: #define NS_TRACE_MALLOC_MAGIC "XPCOM\nTMLog08\r\n\032" michael@0: #define NS_TRACE_MALLOC_MAGIC_SIZE 16 michael@0: michael@0: /** michael@0: * Trace-malloc stats, traced via the 'Z' event at the end of a log file. michael@0: */ michael@0: typedef struct nsTMStats { michael@0: uint32_t calltree_maxstack; michael@0: uint32_t calltree_maxdepth; michael@0: uint32_t calltree_parents; michael@0: uint32_t calltree_maxkids; michael@0: uint32_t calltree_kidhits; michael@0: uint32_t calltree_kidmisses; michael@0: uint32_t calltree_kidsteps; michael@0: uint32_t callsite_recurrences; michael@0: uint32_t backtrace_calls; michael@0: uint32_t backtrace_failures; michael@0: uint32_t btmalloc_failures; michael@0: uint32_t dladdr_failures; michael@0: uint32_t malloc_calls; michael@0: uint32_t malloc_failures; michael@0: uint32_t calloc_calls; michael@0: uint32_t calloc_failures; michael@0: uint32_t realloc_calls; michael@0: uint32_t realloc_failures; michael@0: uint32_t free_calls; michael@0: uint32_t null_free_calls; michael@0: } nsTMStats; michael@0: michael@0: #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: michael@0: /** michael@0: * Call NS_TraceMallocStartup with a valid file descriptor to enable logging michael@0: * of compressed malloc traces, including callsite chains. Integers may be michael@0: * unsigned serial numbers, sizes, or offsets, and require at most 32 bits. michael@0: * They're encoded as follows: michael@0: * 0-127 0xxxxxxx (binary, one byte) michael@0: * 128-16383 10xxxxxx xxxxxxxx michael@0: * 16384-0x1fffff 110xxxxx xxxxxxxx xxxxxxxx michael@0: * 0x200000-0xfffffff 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx michael@0: * 0x10000000-0xffffffff 11110000 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx michael@0: * Strings are NUL-terminated ASCII. michael@0: * michael@0: * Event Operands (magic TMLog01) michael@0: * 'L' library serial, shared object filename string michael@0: * 'N' method serial, library serial, demangled name string michael@0: * 'S' site serial, parent serial, method serial, calling pc offset michael@0: * 'M' site serial, malloc size michael@0: * 'C' site serial, calloc size michael@0: * 'R' site serial, realloc oldsize, realloc size michael@0: * 'F' site serial, free size michael@0: * michael@0: * Event Operands (magic TMLog02) michael@0: * 'Z' serialized struct tmstats (20 unsigned integers), michael@0: * maxkids parent callsite serial, michael@0: * maxstack top callsite serial michael@0: * michael@0: * Event Operands (magic TMLog03) michael@0: * 'T' seconds, microseconds, caption michael@0: * michael@0: * Event Operands (magic TMLog04) michael@0: * 'R' site serial, realloc size, old site serial, realloc oldsize michael@0: * michael@0: * Event Operands (magic TMLog05) michael@0: * 'M' site serial, address, malloc size michael@0: * 'C' site serial, address, calloc size michael@0: * 'R' site serial, address, realloc size, old site serial, michael@0: * old address, old size michael@0: * 'F' site serial, address, free size michael@0: * michael@0: * Event Operands (magic TMLog06) michael@0: * 'M' site serial, interval time (end), address, malloc size michael@0: * 'C' site serial, interval time (end), address, calloc size michael@0: * 'R' site serial, interval time (end), address, realloc size, michael@0: * old site serial, old address, old size michael@0: * 'F' site serial, interval time (end), address, free size michael@0: * michael@0: * Event Operands (magic TMLog07) michael@0: * 'M' site serial, interval time (start), duration, address, malloc size michael@0: * 'C' site serial, interval time (start), duration, address, calloc size michael@0: * 'R' site serial, interval time (start), duration, address, realloc size, michael@0: * old site serial, old address, old size michael@0: * 'F' site serial, interval time (start), duration, address, free size michael@0: * michael@0: * Event Operands (magic TMLog08) michael@0: * 'G' filename serial, source filename string. michael@0: * 'N' method serial, library serial, source filename serial, michael@0: * source file linenumber, demangled name string michael@0: * michael@0: * See tools/trace-malloc/bloatblame.c for an example log-file reader. michael@0: */ michael@0: #define TM_EVENT_LIBRARY 'L' michael@0: #define TM_EVENT_FILENAME 'G' michael@0: #define TM_EVENT_METHOD 'N' michael@0: #define TM_EVENT_CALLSITE 'S' michael@0: #define TM_EVENT_MALLOC 'M' michael@0: #define TM_EVENT_CALLOC 'C' michael@0: #define TM_EVENT_REALLOC 'R' michael@0: #define TM_EVENT_FREE 'F' michael@0: #define TM_EVENT_STATS 'Z' michael@0: #define TM_EVENT_TIMESTAMP 'T' michael@0: michael@0: PR_EXTERN(void) NS_TraceMallocStartup(int logfd); michael@0: michael@0: /** michael@0: * Initialize malloc tracing, using the ``standard'' startup arguments. michael@0: */ michael@0: PR_EXTERN(int) NS_TraceMallocStartupArgs(int argc, char* argv[]); michael@0: michael@0: /** michael@0: * Return PR_TRUE iff |NS_TraceMallocStartup[Args]| has been successfully called. michael@0: */ michael@0: PR_EXTERN(PRBool) NS_TraceMallocHasStarted(void); michael@0: michael@0: /** michael@0: * Stop all malloc tracing, flushing any buffered events to the logfile. michael@0: */ michael@0: PR_EXTERN(void) NS_TraceMallocShutdown(void); michael@0: michael@0: /** michael@0: * Disable malloc tracing. michael@0: */ michael@0: PR_EXTERN(void) NS_TraceMallocDisable(void); michael@0: michael@0: /** michael@0: * Enable malloc tracing. michael@0: */ michael@0: PR_EXTERN(void) NS_TraceMallocEnable(void); michael@0: michael@0: /** michael@0: * Change the log file descriptor, flushing any buffered output to the old michael@0: * fd, and writing NS_TRACE_MALLOC_MAGIC to the new file if it is zero length. michael@0: * Return the old fd, so the caller can swap open fds. Return -2 on failure, michael@0: * which means malloc failure. michael@0: */ michael@0: PR_EXTERN(int) NS_TraceMallocChangeLogFD(int fd); michael@0: michael@0: /** michael@0: * Close the file descriptor fd and forget any bookkeeping associated with it. michael@0: * Do nothing if fd is -1. michael@0: */ michael@0: PR_EXTERN(void) NS_TraceMallocCloseLogFD(int fd); michael@0: michael@0: /** michael@0: * Emit a timestamp event with the given caption to the current log file. michael@0: */ michael@0: PR_EXTERN(void) NS_TraceMallocLogTimestamp(const char *caption); michael@0: michael@0: /** michael@0: * Walk the stack, dumping frames in standard form to ofp. If skip is 0, michael@0: * exclude the frames for NS_TraceStack and anything it calls to do the walk. michael@0: * If skip is less than 0, include -skip such frames. If skip is positive, michael@0: * exclude that many frames leading to the call to NS_TraceStack. michael@0: */ michael@0: PR_EXTERN(void) michael@0: NS_TraceStack(int skip, FILE *ofp); michael@0: michael@0: /** michael@0: * Dump a human-readable listing of current allocations and their compressed michael@0: * stack backtraces to the file named by pathname. Beware this file may have michael@0: * very long lines. michael@0: * michael@0: * Return -1 on error with errno set by the system, 0 on success. michael@0: */ michael@0: PR_EXTERN(int) michael@0: NS_TraceMallocDumpAllocations(const char *pathname); michael@0: michael@0: /** michael@0: * Flush all logfile buffers. michael@0: */ michael@0: PR_EXTERN(void) michael@0: NS_TraceMallocFlushLogfiles(void); michael@0: michael@0: /** michael@0: * Track all realloc and free calls operating on a given allocation. michael@0: */ michael@0: PR_EXTERN(void) michael@0: NS_TrackAllocation(void* ptr, FILE *ofp); michael@0: michael@0: /* opaque type for API */ michael@0: typedef struct nsTMStackTraceIDStruct *nsTMStackTraceID; michael@0: michael@0: /** michael@0: * Get an identifier for the stack trace of the current thread (to this michael@0: * function's callsite) that can be used to print that stack trace later. michael@0: */ michael@0: PR_EXTERN(nsTMStackTraceID) michael@0: NS_TraceMallocGetStackTrace(void); michael@0: michael@0: /** michael@0: * Print the stack trace identified. michael@0: */ michael@0: PR_EXTERN(void) michael@0: NS_TraceMallocPrintStackTrace(FILE *ofp, nsTMStackTraceID id); michael@0: michael@0: #ifdef __cplusplus michael@0: } michael@0: #endif michael@0: michael@0: #endif /* nsTraceMalloc_h___ */