]> git.proxmox.com Git - libgit2.git/blob - src/win32/w32_crtdbg_stacktrace.h
New upstream version 1.1.0+dfsg.1
[libgit2.git] / src / win32 / w32_crtdbg_stacktrace.h
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
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.
6 */
7 #ifndef INCLUDE_win32_w32_crtdbg_stacktrace_h__
8 #define INCLUDE_win32_w32_crtdbg_stacktrace_h__
9
10 #include "common.h"
11
12 #if defined(GIT_MSVC_CRTDBG)
13
14 #include <stdlib.h>
15 #include <crtdbg.h>
16
17 #include "git2/errors.h"
18 #include "strnlen.h"
19
20 /* MSVC CRTDBG memory leak reporting.
21 *
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().
28 *
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
32 * the real caller.
33 *
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".
37 *
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".
41 *
42 * Finally, CRTDBG must be explicitly enabled and configured at program
43 * startup. See tests/main.c for an example.
44 */
45
46 /**
47 * Initialize our memory leak tracking and de-dup data structures.
48 * This should ONLY be called by git_libgit2_init().
49 */
50 void git_win32__crtdbg_stacktrace_init(void);
51
52 /**
53 * Shutdown our memory leak tracking and dump summary data.
54 * This should ONLY be called by git_libgit2_shutdown().
55 *
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.
59 *
60 * This cleanup does not happen if the app calls exit()
61 * without calling the libgit2 shutdown code.
62 *
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.
66 */
67 void git_win32__crtdbg_stacktrace_cleanup(void);
68
69 /**
70 * Checkpoint options.
71 */
72 typedef enum git_win32__crtdbg_stacktrace_options {
73 /**
74 * Set checkpoint marker.
75 */
76 GIT_WIN32__CRTDBG_STACKTRACE__SET_MARK = (1 << 0),
77
78 /**
79 * Dump leaks since last checkpoint marker.
80 * May not be combined with __LEAKS_TOTAL.
81 *
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.
87 */
88 GIT_WIN32__CRTDBG_STACKTRACE__LEAKS_SINCE_MARK = (1 << 1),
89
90 /**
91 * Dump leaks since init. May not be combined
92 * with __LEAKS_SINCE_MARK.
93 */
94 GIT_WIN32__CRTDBG_STACKTRACE__LEAKS_TOTAL = (1 << 2),
95
96 /**
97 * Suppress printing during dumps.
98 * Just return leak count.
99 */
100 GIT_WIN32__CRTDBG_STACKTRACE__QUIET = (1 << 3),
101
102 } git_win32__crtdbg_stacktrace_options;
103
104 /**
105 * Checkpoint memory state and/or dump unique stack traces of
106 * current memory leaks.
107 *
108 * @return number of unique leaks (relative to requested starting
109 * point) or error.
110 */
111 GIT_EXTERN(int) git_win32__crtdbg_stacktrace__dump(
112 git_win32__crtdbg_stacktrace_options opt,
113 const char *label);
114
115 /**
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.
120 *
121 * This should ONLY be called by our internal memory allocations
122 * routines.
123 */
124 const char *git_win32__crtdbg_stacktrace(int skip, const char *file);
125
126 #endif
127 #endif