Commit | Line | Data |
---|---|---|
c39c72ee PMF |
1 | /* Copyright (C) 2009 Pierre-Marc Fournier |
2 | * | |
3 | * This library is free software; you can redistribute it and/or | |
4 | * modify it under the terms of the GNU Lesser General Public | |
5 | * License as published by the Free Software Foundation; either | |
6 | * version 2.1 of the License, or (at your option) any later version. | |
7 | * | |
8 | * This library is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * Lesser General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU Lesser General Public | |
14 | * License along with this library; if not, write to the Free Software | |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
16 | */ | |
17 | ||
e541a28d PMF |
18 | #define _GNU_SOURCE |
19 | #include <dlfcn.h> | |
20 | #include <sys/types.h> | |
21 | #include <stdio.h> | |
22 | ||
fbd8191b PMF |
23 | #include "marker.h" |
24 | ||
c61f7385 PMF |
25 | #if 0 |
26 | INTERCEPT_PROTOTYPE(void, malloc, size_t size) | |
27 | INTERCEPT_TRACE("size %d", size) | |
28 | INTERCEPT_CALL_ARGS(size) | |
29 | INTERCEPT() | |
30 | ||
31 | #define INTERCEPT_FUNC(type, name, args...) \ | |
32 | __I_FUNC_TYPE(type) \ | |
33 | __I_FUNC_NAME(name) \ | |
34 | __I_FUNC_ARGS(args) | |
35 | ||
36 | #define INTERCEPT_TRACE(fmt, args...) \ | |
37 | #define __I_TRACE_FMT fmt \ | |
38 | #define __I_TRACE_ARGS args | |
39 | ||
40 | #define INTERCEPT_CALL_ARGS(args...) \ | |
41 | #define __I_CALL_ARGS args | |
42 | ||
43 | #define INTERCEPT() \ | |
44 | __I_FUNC_TYPE __I_FUNC_NAME(__I_FUNC_ARGS) \ | |
45 | { \ | |
46 | static __I_FUNC_TYPE (*plibc_ ## __I_FUNC_NAME)(args) = NULL; \ | |
47 | \ | |
48 | if(plibc_ ## __I_FUNC_NAME == NULL) { \ | |
49 | plibc_ ## __I_FUNC_NAME = dlsym(RTLD_NEXT, "malloc"); \ | |
50 | if(plibc_ ## __I_FUNC_NAME == NULL) { \ | |
51 | fprintf(stderr, "mallocwrap: unable to find malloc\n"); \ | |
52 | return NULL; \ | |
53 | } \ | |
54 | } \ | |
55 | \ | |
56 | trace_mark(ust, __I_FUNC_NAME, __I_TRACE_FMT, __I_TRACE_ARGS); \ | |
57 | \ | |
58 | return plibc_ ## __I_FUNC_NAME (__I_CALL_ARGS); \ | |
59 | } | |
60 | #endif | |
e541a28d PMF |
61 | |
62 | void *malloc(size_t size) | |
63 | { | |
1c184644 PMF |
64 | static void *(*plibc_malloc)(size_t size) = NULL; |
65 | ||
66 | void *retval; | |
67 | ||
e541a28d PMF |
68 | if(plibc_malloc == NULL) { |
69 | plibc_malloc = dlsym(RTLD_NEXT, "malloc"); | |
70 | if(plibc_malloc == NULL) { | |
71 | fprintf(stderr, "mallocwrap: unable to find malloc\n"); | |
72 | return NULL; | |
73 | } | |
74 | } | |
fbd8191b | 75 | |
1c184644 PMF |
76 | retval = plibc_malloc(size); |
77 | ||
78 | trace_mark(ust, malloc, "size %d ptr %p", (int)size, retval); | |
79 | ||
80 | return retval; | |
81 | } | |
82 | ||
83 | void free(void *ptr) | |
84 | { | |
85 | static void *(*plibc_free)(void *ptr) = NULL; | |
86 | ||
87 | if(plibc_free == NULL) { | |
88 | plibc_free = dlsym(RTLD_NEXT, "free"); | |
89 | if(plibc_free == NULL) { | |
90 | fprintf(stderr, "mallocwrap: unable to find free\n"); | |
eb0f0951 | 91 | return; |
1c184644 PMF |
92 | } |
93 | } | |
94 | ||
95 | trace_mark(ust, free, "%p", ptr); | |
fbd8191b | 96 | |
eb0f0951 | 97 | plibc_free(ptr); |
e541a28d | 98 | } |
fbd8191b PMF |
99 | |
100 | MARKER_LIB |