summaryrefslogtreecommitdiff
path: root/erts/emulator/sys/common/erl_mtrace_sys_wrap.c
blob: fc871f94f18821f91965266f334f8f30ae42a30a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
 * %CopyrightBegin%
 * 
 * Copyright Ericsson AB 2004-2016. All Rights Reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * %CopyrightEnd%
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif
#include "sys.h"
#include "erl_mtrace.h"

#ifdef ERTS_CAN_TRACK_MALLOC
#if defined(HAVE_END_SYMBOL)
extern char end;
#elif defined(HAVE__END_SYMBOL)
extern char _end;
#endif

static int inited = 0;
static int init(void);

static volatile char *heap_start = NULL;
static volatile char *heap_end = NULL;

#if defined(ERTS___AFTER_MORECORE_HOOK_CAN_TRACK_MALLOC) /* ----------------- */

#ifdef HAVE_MALLOC_H
#  include <malloc.h>
#endif

#undef SBRK_0
#define SBRK_0 sbrk(0)

static void
init_hook(void)
{
    __after_morecore_hook = erts_mtrace_update_heap_size;
    if (inited)
	return;
    heap_end = NULL;
#if defined(HAVE_END_SYMBOL)
    heap_start = &end;
#elif defined(HAVE__END_SYMBOL)
    heap_start = &_end;
#else
    heap_start = SBRK_0;
    if (heap_start == (SBRK_RET_TYPE) -1) {
	heap_start = NULL;
	return;
    }
#endif
    inited = 1;
}

static int
init(void)
{
    init_hook();
    return inited;
}

void (*__malloc_initialize_hook)(void) = init_hook;

#elif defined(ERTS_BRK_WRAPPERS_CAN_TRACK_MALLOC) /* ------------------------ */
#ifdef HAVE_DLFCN_H
#  include <dlfcn.h>
#endif

#undef SBRK_0
#define SBRK_0 (*real_sbrk)(0)

#ifndef HAVE_SBRK
#  error no sbrk()
#endif
#if !defined(HAVE_END_SYMBOL) && !defined(HAVE__END_SYMBOL)
#  error no 'end' nor '_end'
#endif

static void update_heap_size(char *new_end);

#define SBRK_IMPL(RET_TYPE, FUNC, ARG_TYPE)			\
RET_TYPE FUNC (ARG_TYPE);					\
static RET_TYPE (*real_ ## FUNC)(ARG_TYPE) = NULL;		\
RET_TYPE FUNC (ARG_TYPE arg)					\
{								\
    RET_TYPE res;						\
    if (!inited && !init())					\
	return (RET_TYPE) -1;					\
    res = (*real_ ## FUNC)(arg);				\
    if (erts_mtrace_enabled && res != ((RET_TYPE) -1))		\
	update_heap_size((char *) (*real_ ## FUNC)(0));		\
    return res;							\
}

#define BRK_IMPL(RET_TYPE, FUNC, ARG_TYPE)			\
RET_TYPE FUNC (ARG_TYPE);					\
static RET_TYPE (*real_ ## FUNC)(ARG_TYPE) = NULL;		\
RET_TYPE FUNC (ARG_TYPE arg)					\
{								\
    RET_TYPE res;						\
    if (!inited && !init())					\
	return (RET_TYPE) -1;					\
    res = (*real_ ## FUNC)(arg);				\
    if (erts_mtrace_enabled && res != ((RET_TYPE) -1))		\
	update_heap_size((char *) arg);				\
    return res;							\
}

SBRK_IMPL(SBRK_RET_TYPE, sbrk, SBRK_ARG_TYPE)
#ifdef HAVE_BRK
   BRK_IMPL(BRK_RET_TYPE, brk, BRK_ARG_TYPE)
#endif

#ifdef HAVE__SBRK
   SBRK_IMPL(SBRK_RET_TYPE, _sbrk, SBRK_ARG_TYPE)
#endif
#ifdef HAVE__BRK
   BRK_IMPL(BRK_RET_TYPE, _brk, BRK_ARG_TYPE)
#endif

#ifdef HAVE___SBRK
   SBRK_IMPL(SBRK_RET_TYPE, __sbrk, SBRK_ARG_TYPE)
#endif
#ifdef HAVE___BRK
   BRK_IMPL(BRK_RET_TYPE, __brk, BRK_ARG_TYPE)
#endif

static int
init(void)
{
    if (inited)
	return 1;

#define INIT_XBRK_SYM(SYM)			\
do {						\
    if (!real_ ## SYM) {			\
	real_ ## SYM = dlsym(RTLD_NEXT, #SYM);	\
	if (!real_ ## SYM) {			\
	    errno = ENOMEM;			\
	    return 0;				\
	}					\
    }						\
} while (0)

    heap_end = NULL;
#if defined(HAVE_END_SYMBOL)
    heap_start = &end;
#elif defined(HAVE__END_SYMBOL)
    heap_start = &_end;
#endif

    INIT_XBRK_SYM(sbrk);
#ifdef HAVE_BRK
    INIT_XBRK_SYM(brk);
#endif
#ifdef HAVE__SBRK
    INIT_XBRK_SYM(_sbrk);
#endif
#ifdef HAVE__BRK
    INIT_XBRK_SYM(_brk);
#endif
#ifdef HAVE___SBRK
    INIT_XBRK_SYM(__sbrk);
#endif
#ifdef HAVE___BRK
    INIT_XBRK_SYM(__brk);
#endif

    return inited = 1;
#undef INIT_XBRK_SYM
}

#endif /* #elif defined(ERTS_BRK_WRAPPERS_CAN_TRACK_MALLOC) */ /* ----------- */

static void
update_heap_size(char *new_end)
{
    volatile char *new_start, *old_start, *old_end;
    Uint size;

    if (new_end == ((char *) -1))
	return;

    new_start = (old_start = heap_start);
    old_end = heap_end;
    heap_end = new_end;
    if (new_end < old_start || !old_start)
	heap_start = (new_start = new_end);

    size = (Uint) (new_end - new_start);

    if (!old_end) {
	if (size)
	    erts_mtrace_crr_alloc((void *) new_start,
				  ERTS_ALC_A_SYSTEM,
				  ERTS_MTRACE_SEGMENT_ID,
				  size);
	else
	    heap_end = NULL;
    }
    else {
	if (old_end != new_end || old_start != new_start) {

	    if (size)
		erts_mtrace_crr_realloc((void *) new_start,
					ERTS_ALC_A_SYSTEM,
					ERTS_MTRACE_SEGMENT_ID,
					(void *) old_start,
					size);
	    else {
		if (old_start)
		    erts_mtrace_crr_free(ERTS_ALC_A_SYSTEM,
					 ERTS_MTRACE_SEGMENT_ID,
					 (void *) old_start);
		heap_end = NULL;
	    }
	}
    }
}

#endif /* #ifdef ERTS_CAN_TRACK_MALLOC */

void
erts_mtrace_update_heap_size(void)
{
#ifdef ERTS_CAN_TRACK_MALLOC
    if (erts_mtrace_enabled && (inited || init()))
	update_heap_size((char *) SBRK_0);
#endif
}