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.
7 #ifndef INCLUDE_win32_w32_crtdbg_stacktrace_h__
8 #define INCLUDE_win32_w32_crtdbg_stacktrace_h__
12 #if defined(GIT_MSVC_CRTDBG)
17 #include "git2/errors.h"
20 /* MSVC CRTDBG memory leak reporting.
22 * We DO NOT use the "_CRTDBG_MAP_ALLOC" macro described in the MSVC
23 * documentation because all allocs/frees in libgit2 already go through
24 * the "git__" routines defined in this file. Simply using the normal
25 * reporting mechanism causes all leaks to be attributed to a routine
26 * here in util.h (ie, the actual call to calloc()) rather than the
27 * caller of git__calloc().
29 * Therefore, we declare a set of "git__crtdbg__" routines to replace
30 * the corresponding "git__" routines and re-define the "git__" symbols
31 * as macros. This allows us to get and report the file:line info of
34 * We DO NOT replace the "git__free" routine because it needs to remain
35 * a function pointer because it is used as a function argument when
36 * setting up various structure "destructors".
38 * We also DO NOT use the "_CRTDBG_MAP_ALLOC" macro because it causes
39 * "free" to be remapped to "_free_dbg" and this causes problems for
40 * structures which define a field named "free".
42 * Finally, CRTDBG must be explicitly enabled and configured at program
43 * startup. See tests/main.c for an example.
47 * Initialize our memory leak tracking and de-dup data structures.
48 * This should ONLY be called by git_libgit2_init().
50 void git_win32__crtdbg_stacktrace_init(void);
53 * Shutdown our memory leak tracking and dump summary data.
54 * This should ONLY be called by git_libgit2_shutdown().
56 * We explicitly call _CrtDumpMemoryLeaks() during here so
57 * that we can compute summary data for the leaks. We print
58 * the stacktrace of each unique leak.
60 * This cleanup does not happen if the app calls exit()
61 * without calling the libgit2 shutdown code.
63 * This info we print here is independent of any automatic
64 * reporting during exit() caused by _CRTDBG_LEAK_CHECK_DF.
65 * Set it in your app if you also want traditional reporting.
67 void git_win32__crtdbg_stacktrace_cleanup(void);
72 typedef enum git_win32__crtdbg_stacktrace_options
{
74 * Set checkpoint marker.
76 GIT_WIN32__CRTDBG_STACKTRACE__SET_MARK
= (1 << 0),
79 * Dump leaks since last checkpoint marker.
80 * May not be combined with __LEAKS_TOTAL.
82 * Note that this may generate false positives for global TLS
83 * error state and other global caches that aren't cleaned up
84 * until the thread/process terminates. So when using this
85 * around a region of interest, also check the final (at exit)
86 * dump before digging into leaks reported here.
88 GIT_WIN32__CRTDBG_STACKTRACE__LEAKS_SINCE_MARK
= (1 << 1),
91 * Dump leaks since init. May not be combined
92 * with __LEAKS_SINCE_MARK.
94 GIT_WIN32__CRTDBG_STACKTRACE__LEAKS_TOTAL
= (1 << 2),
97 * Suppress printing during dumps.
98 * Just return leak count.
100 GIT_WIN32__CRTDBG_STACKTRACE__QUIET
= (1 << 3),
102 } git_win32__crtdbg_stacktrace_options
;
105 * Checkpoint memory state and/or dump unique stack traces of
106 * current memory leaks.
108 * @return number of unique leaks (relative to requested starting
111 GIT_EXTERN(int) git_win32__crtdbg_stacktrace__dump(
112 git_win32__crtdbg_stacktrace_options opt
,
116 * Construct stacktrace and append it to the global buffer.
117 * Return pointer to start of this string. On any error or
118 * lack of buffer space, just return the given file buffer
119 * so it will behave as usual.
121 * This should ONLY be called by our internal memory allocations
124 const char *git_win32__crtdbg_stacktrace(int skip
, const char *file
);