1/*
2 *  OpenVPN -- An application to securely tunnel IP networks
3 *             over a single UDP port, with support for SSL/TLS-based
4 *             session authentication and key exchange,
5 *             packet encryption, packet authentication, and
6 *             packet compression.
7 *
8 *  Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net>
9 *
10 *  This program is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 2
12 *  as published by the Free Software Foundation.
13 *
14 *  This program is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this program (see the file COPYING included with this
21 *  distribution); if not, write to the Free Software Foundation, Inc.,
22 *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 */
24
25#ifndef MEMDBG_H
26#define MEMDBG_H
27
28/*
29 * Valgrind debugging support.
30 *
31 * Valgrind is a great tool for debugging memory issues,
32 * though it seems to generate a lot of warnings in OpenSSL
33 * about uninitialized data. To silence these warnings,
34 * I've put together a suppressions file
35 * in debug/valgrind-suppress.
36 *
37 * Also, grep for VALGRIND_MAKE_READABLE in the OpenVPN source.
38 * Because valgrind thinks that some of the data passed from
39 * OpenSSL back to OpenVPN is tainted due to being sourced
40 * from uninitialized data, we need to untaint it before use --
41 * otherwise we will get a lot of useless warnings.
42 *
43 *   valgrind --tool=memcheck --error-limit=no --suppressions=debug/valgrind-suppress --gen-suppressions=yes ./openvpn ...
44 */
45
46#ifdef USE_VALGRIND
47
48#include "valgrind/memcheck.h"
49
50#define VALGRIND_MAKE_READABLE(addr, len)
51
52#else
53
54#define VALGRIND_MAKE_READABLE(addr, len)
55
56#endif
57
58#ifdef DMALLOC /* see ./configure options to enable */
59
60/*
61 * See ./configure options to enable dmalloc
62 * support for memory leak checking.
63 *
64 * The dmalloc package can be downloaded from:
65 *
66 *     http://dmalloc.com/
67 *
68 * When dmalloc is installed and enabled,
69 * use this command prior to running openvpn:
70 *
71 *    dmalloc -l dlog -i 100 low -p log-unknown
72 *
73 * Also, put this in your .bashrc file:
74 *
75 *    function dmalloc { eval `command dmalloc -b $*`; }
76 *
77 * Or take a more low-level approach:
78 *
79 *    export DMALLOC_OPTIONS="debug=0x4e48503,inter=100,log=dlog"
80 *
81 *  NOTE: When building dmalloc you need to add something
82 *  like this to dmalloc's settings.h -- it will allocate a static
83 *  buffer to be used as the malloc arena:
84 *
85 *  #define INTERNAL_MEMORY_SPACE (1024 * 1024 * 50)
86 */
87
88#include "dmalloc.h"
89
90#define openvpn_dmalloc(file, line, size) dmalloc_malloc((file), (line), (size), DMALLOC_FUNC_MALLOC, 0, 0)
91
92/*
93 * This #define will put the line number of the log
94 * file position where leaked memory was allocated instead
95 * of the source code file and line number.  Make sure
96 * to increase the size of dmalloc's info tables,
97 * (MEMORY_TABLE_SIZE in settings.h)
98 * otherwise it might get overwhelmed by the large
99 * number of unique file/line combinations.
100 */
101#if 0
102#undef malloc
103#define malloc(size) openvpn_dmalloc("logfile", x_msg_line_num, (size))
104#endif
105
106#endif /* DMALLOC */
107
108/*
109 * Force buffers to be zeroed after allocation.
110 * For debugging only.
111 */
112/*#define ZERO_BUFFER_ON_ALLOC*/
113
114#endif /* MEMDBG_H */
115