tools/trace-malloc/lib/nsTraceMalloc.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial