2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
8 #ifndef INCLUDE_win32_leakcheck_h__
9 #define INCLUDE_win32_leakcheck_h__
13 /* Initialize the win32 leak checking system. */
14 int git_win32_leakcheck_global_init(void);
16 #if defined(GIT_WIN32_LEAKCHECK)
21 #include "git2/errors.h"
24 bool git_win32_leakcheck_has_leaks(void);
26 /* Stack frames (for stack tracing, below) */
29 * This type defines a callback to be used to augment a C stacktrace
30 * with "aux" data. This can be used, for example, to allow LibGit2Sharp
31 * (or other interpreted consumer libraries) to give us C# stacktrace
32 * data for the PInvoke.
34 * This callback will be called during crtdbg-instrumented allocs.
36 * @param aux_id [out] A returned "aux_id" representing a unique
37 * (de-duped at the C# layer) stacktrace. "aux_id" 0 is reserved
38 * to mean no aux stacktrace data.
40 typedef void (*git_win32_leakcheck_stack_aux_cb_alloc
)(unsigned int *aux_id
);
43 * This type defines a callback to be used to augment the output of
44 * a stacktrace. This will be used to request the C# layer format
45 * the C# stacktrace associated with "aux_id" into the provided
48 * This callback will be called during leak reporting.
50 * @param aux_id The "aux_id" key associated with a stacktrace.
51 * @param aux_msg A buffer where a formatted message should be written.
52 * @param aux_msg_len The size of the buffer.
54 typedef void (*git_win32_leakcheck_stack_aux_cb_lookup
)(unsigned int aux_id
, char *aux_msg
, size_t aux_msg_len
);
57 * Register an "aux" data provider to augment our C stacktrace data.
59 * This can be used, for example, to allow LibGit2Sharp (or other
60 * interpreted consumer libraries) to give us the C# stacktrace of
63 * If you choose to use this feature, it should be registered during
64 * initialization and not changed for the duration of the process.
66 int git_win32_leakcheck_stack_set_aux_cb(
67 git_win32_leakcheck_stack_aux_cb_alloc cb_alloc
,
68 git_win32_leakcheck_stack_aux_cb_lookup cb_lookup
);
71 * Maximum number of stackframes to record for a
74 #define GIT_WIN32_LEAKCHECK_STACK_MAX_FRAMES 30
77 * Wrapper containing the raw unprocessed stackframe
78 * data for a single stacktrace and any "aux_id".
80 * I put the aux_id first so leaks will be sorted by it.
81 * So, for example, if a specific callstack in C# leaks
82 * a repo handle, all of the pointers within the associated
83 * repo pointer will be grouped together.
87 unsigned int nr_frames
;
88 void *frames
[GIT_WIN32_LEAKCHECK_STACK_MAX_FRAMES
];
89 } git_win32_leakcheck_stack_raw_data
;
92 * Capture raw stack trace data for the current process/thread.
94 * @param skip Number of initial frames to skip. Pass 0 to
95 * begin with the caller of this routine. Pass 1 to begin
96 * with its caller. And so on.
98 int git_win32_leakcheck_stack_capture(git_win32_leakcheck_stack_raw_data
*pdata
, int skip
);
101 * Compare 2 raw stacktraces with the usual -1,0,+1 result.
102 * This includes any "aux_id" values in the comparison, so that
103 * our de-dup is also "aux" context relative.
105 int git_win32_leakcheck_stack_compare(
106 git_win32_leakcheck_stack_raw_data
*d1
,
107 git_win32_leakcheck_stack_raw_data
*d2
);
110 * Format raw stacktrace data into buffer WITHOUT using any mallocs.
112 * @param prefix String written before each frame; defaults to "\t".
113 * @param suffix String written after each frame; defaults to "\n".
115 int git_win32_leakcheck_stack_format(
116 char *pbuf
, size_t buf_len
,
117 const git_win32_leakcheck_stack_raw_data
*pdata
,
118 const char *prefix
, const char *suffix
);
121 * Convenience routine to capture and format stacktrace into
122 * a buffer WITHOUT using any mallocs. This is primarily a
123 * wrapper for testing.
125 * @param skip Number of initial frames to skip. Pass 0 to
126 * begin with the caller of this routine. Pass 1 to begin
127 * with its caller. And so on.
128 * @param prefix String written before each frame; defaults to "\t".
129 * @param suffix String written after each frame; defaults to "\n".
131 int git_win32_leakcheck_stack(
132 char * pbuf
, size_t buf_len
,
134 const char *prefix
, const char *suffix
);
138 /* MSVC CRTDBG memory leak reporting.
140 * We DO NOT use the "_CRTDBG_MAP_ALLOC" macro described in the MSVC
141 * documentation because all allocs/frees in libgit2 already go through
142 * the "git__" routines defined in this file. Simply using the normal
143 * reporting mechanism causes all leaks to be attributed to a routine
144 * here in util.h (ie, the actual call to calloc()) rather than the
145 * caller of git__calloc().
147 * Therefore, we declare a set of "git__crtdbg__" routines to replace
148 * the corresponding "git__" routines and re-define the "git__" symbols
149 * as macros. This allows us to get and report the file:line info of
152 * We DO NOT replace the "git__free" routine because it needs to remain
153 * a function pointer because it is used as a function argument when
154 * setting up various structure "destructors".
156 * We also DO NOT use the "_CRTDBG_MAP_ALLOC" macro because it causes
157 * "free" to be remapped to "_free_dbg" and this causes problems for
158 * structures which define a field named "free".
160 * Finally, CRTDBG must be explicitly enabled and configured at program
161 * startup. See tests/main.c for an example.
165 * Checkpoint options.
167 typedef enum git_win32_leakcheck_stacktrace_options
{
169 * Set checkpoint marker.
171 GIT_WIN32_LEAKCHECK_STACKTRACE_SET_MARK
= (1 << 0),
174 * Dump leaks since last checkpoint marker.
175 * May not be combined with _LEAKS_TOTAL.
177 * Note that this may generate false positives for global TLS
178 * error state and other global caches that aren't cleaned up
179 * until the thread/process terminates. So when using this
180 * around a region of interest, also check the final (at exit)
181 * dump before digging into leaks reported here.
183 GIT_WIN32_LEAKCHECK_STACKTRACE_LEAKS_SINCE_MARK
= (1 << 1),
186 * Dump leaks since init. May not be combined
187 * with _LEAKS_SINCE_MARK.
189 GIT_WIN32_LEAKCHECK_STACKTRACE_LEAKS_TOTAL
= (1 << 2),
192 * Suppress printing during dumps.
193 * Just return leak count.
195 GIT_WIN32_LEAKCHECK_STACKTRACE_QUIET
= (1 << 3),
197 } git_win32_leakcheck_stacktrace_options
;
200 * Checkpoint memory state and/or dump unique stack traces of
201 * current memory leaks.
203 * @return number of unique leaks (relative to requested starting
206 int git_win32_leakcheck_stacktrace_dump(
207 git_win32_leakcheck_stacktrace_options opt
,
211 * Construct stacktrace and append it to the global buffer.
212 * Return pointer to start of this string. On any error or
213 * lack of buffer space, just return the given file buffer
214 * so it will behave as usual.
216 * This should ONLY be called by our internal memory allocations
219 const char *git_win32_leakcheck_stacktrace(int skip
, const char *file
);