tizen 2.3 release
[framework/system/swap-probe.git] / probe_memory / libdanew.cpp
1 /*
2  *  DA probe
3  *
4  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:
7  *
8  * Woojin Jung <woojin2.jung@samsung.com>
9  * Jaewon Lim <jaewon81.lim@samsung.com>
10  * Juyoung Kim <j0.kim@samsung.com>
11  * Anastasia Lyupa <a.lyupa@samsung.com>
12  *
13  * This library is free software; you can redistribute it and/or modify it under
14  * the terms of the GNU Lesser General Public License as published by the
15  * Free Software Foundation; either version 2.1 of the License, or (at your option)
16  * any later version.
17  *
18  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
19  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
21  * License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public License
24  * along with this library; if not, write to the Free Software Foundation, Inc., 51
25  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26  *
27  * Contributors:
28  * - S-Core Co., Ltd
29  * - Samsung RnD Institute Russia
30  *
31  */
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <dlfcn.h>
37 #include <stdbool.h>
38 #include <errno.h>
39 #include <new>
40 #include "daprobe.h"
41 #include "probeinfo.h"
42 #include "dacollection.h"
43 #include "da_memory.h"
44 #include "binproto.h"
45
46 static enum DaOptions _sopt = OPT_ALLOC;
47
48 void *operator new(std::size_t size) throw (std::bad_alloc)
49 {
50         static void*(*newp)(std::size_t size);
51         DECLARE_VARIABLE_STANDARD;
52         void *pret;
53
54         GET_REAL_FUNCP_RTLD_NEXT_CPP(_Znwj,newp);
55
56         bfiltering = INTERNAL_NEW_FILTERING;
57         PRE_PROBEBLOCK();
58
59         pret = newp(size);
60
61         if(pret != NULL && gProbeBlockCount == 0)
62         {
63                 add_memory_hash(pret, size, MEMTYPE_NEW, blockresult ? MEM_EXTERNAL : MEM_INTERNAL);
64         }
65
66         POST_PACK_PROBEBLOCK_BEGIN();
67
68         PREPARE_LOCAL_BUF();
69         PACK_COMMON_BEGIN(MSG_PROBE_MEMORY,
70                           API_ID_void__operator_new_std__size_t_size__throw__std__bad_alloc_,
71                           "x", (uint64_t)(size));
72         PACK_COMMON_END('p', pret, newerrno, blockresult);
73         PACK_MEMORY(size, MEMORY_API_ALLOC, pret);
74         FLUSH_LOCAL_BUF();
75
76         POST_PACK_PROBEBLOCK_END();
77
78         return pret;
79 }
80
81 void *operator new[](std::size_t size) throw (std::bad_alloc)
82 {
83         static void*(*newp)(std::size_t size);
84         DECLARE_VARIABLE_STANDARD;
85         void *pret;
86
87         GET_REAL_FUNCP_RTLD_NEXT_CPP(_Znaj, newp);
88
89         bfiltering = INTERNAL_NEW_FILTERING;
90         PRE_PROBEBLOCK();
91
92         pret = newp(size);
93
94         if(pret != NULL && gProbeBlockCount == 0)
95         {
96                 add_memory_hash(pret, size, MEMTYPE_NEW, blockresult ? MEM_EXTERNAL : MEM_INTERNAL);
97         }
98
99         POST_PACK_PROBEBLOCK_BEGIN();
100
101         PREPARE_LOCAL_BUF();
102         PACK_COMMON_BEGIN(MSG_PROBE_MEMORY,
103                           API_ID_void__operator_new___std__size_t_size__throw__std__bad_alloc_,
104                           "x", (uint64_t)(size));
105         PACK_COMMON_END('p', pret, newerrno, blockresult);
106         PACK_MEMORY(size, MEMORY_API_ALLOC, pret);
107         FLUSH_LOCAL_BUF();
108
109         POST_PACK_PROBEBLOCK_END();
110
111         return pret;
112 }
113
114 void operator delete(void *ptr) throw()
115 {
116         static void (*deletep)(void *ptr);
117         DECLARE_VARIABLE_STANDARD;
118         unsigned short caller;
119
120         GET_REAL_FUNCP_RTLD_NEXT_CPP(_ZdlPv, deletep);
121
122         bfiltering = INTERNAL_DELETE_FILTERING;
123         PRE_PROBEBLOCK();
124
125         if(ptr != NULL && gProbeBlockCount == 0)
126         {
127                 probeBlockStart();
128                 ret = del_memory_hash(ptr, MEMTYPE_DELETE, &caller);
129                 if(blockresult == 0 && ret == 0 && caller == MEM_EXTERNAL)
130                 {
131                         setProbePoint(&probeInfo);
132                         blockresult = 2;
133                         probingStart();
134                 }
135                 probeBlockEnd();
136         }
137
138         deletep(ptr);
139
140         POST_PACK_PROBEBLOCK_BEGIN();
141
142         PREPARE_LOCAL_BUF();
143         PACK_COMMON_BEGIN(MSG_PROBE_MEMORY,
144                           API_ID_void_operator_delete_void__ptr__throw__,
145                           "p", voidp_to_uint64(ptr));
146         PACK_COMMON_END('v', 0, newerrno, blockresult);
147         PACK_MEMORY(0, MEMORY_API_FREE, ptr);
148         FLUSH_LOCAL_BUF();
149
150         POST_PACK_PROBEBLOCK_END();
151 }
152
153 void operator delete[](void *ptr) throw()
154 {
155         static void (*deletep)(void *ptr);
156         DECLARE_VARIABLE_STANDARD;
157         unsigned short caller;
158
159         GET_REAL_FUNCP_RTLD_NEXT_CPP(_ZdaPv, deletep);
160
161         bfiltering = INTERNAL_DELETE_FILTERING;
162         PRE_PROBEBLOCK();
163
164         if(ptr != NULL && gProbeBlockCount == 0)
165         {
166                 probeBlockStart();
167                 ret = del_memory_hash(ptr, MEMTYPE_DELETE, &caller);
168                 if(blockresult == 0 && ret == 0 && caller == MEM_EXTERNAL)
169                 {
170                         setProbePoint(&probeInfo);
171                         blockresult = 2;
172                         probingStart();
173                 }
174                 probeBlockEnd();
175         }
176
177         deletep(ptr);
178
179         POST_PACK_PROBEBLOCK_BEGIN();
180
181         PREPARE_LOCAL_BUF();
182         PACK_COMMON_BEGIN(MSG_PROBE_MEMORY,
183                           API_ID_void_operator_delete___void__ptr__throw__,
184                           "p", voidp_to_uint64(ptr));
185         PACK_COMMON_END('v', 0, newerrno, blockresult);
186         PACK_MEMORY(0, MEMORY_API_FREE, ptr);
187         FLUSH_LOCAL_BUF();
188
189         POST_PACK_PROBEBLOCK_END();
190 }
191
192 void *operator new(std::size_t size, const std::nothrow_t& nothrow) throw()
193 {
194         static void*(*newp)(std::size_t size, const std::nothrow_t& nothrow);
195         DECLARE_VARIABLE_STANDARD;
196         void *pret;
197
198         GET_REAL_FUNCP_RTLD_NEXT_CPP(_ZnwjRKSt9nothrow_t, newp);
199
200         bfiltering = INTERNAL_NEW_FILTERING;
201         PRE_PROBEBLOCK();
202
203         pret = newp(size, nothrow);
204
205         if(pret != NULL && gProbeBlockCount == 0)
206         {
207                 add_memory_hash(pret, size, MEMTYPE_NEW, blockresult ? MEM_EXTERNAL : MEM_INTERNAL);
208         }
209
210         POST_PACK_PROBEBLOCK_BEGIN();
211
212         PREPARE_LOCAL_BUF();
213         PACK_COMMON_BEGIN(MSG_PROBE_MEMORY,
214                           API_ID_void__operator_new_std__size_t_size__const_std__nothrow_t__nothrow__throw__,
215                           "xp", (uint64_t)(size), voidp_to_uint64(&nothrow));
216         PACK_COMMON_END('p', pret, newerrno, blockresult);
217         PACK_MEMORY(size, MEMORY_API_ALLOC, pret);
218         FLUSH_LOCAL_BUF();
219
220         POST_PACK_PROBEBLOCK_END();
221
222         return pret;
223 }
224
225 void *operator new[](std::size_t size, const std::nothrow_t& nothrow) throw()
226 {
227         static void*(*newp)(std::size_t size, const std::nothrow_t& nothrow);
228         DECLARE_VARIABLE_STANDARD;
229         void *pret;
230
231         GET_REAL_FUNCP_RTLD_NEXT_CPP(_ZnajRKSt9nothrow_t, newp);
232
233         bfiltering = INTERNAL_NEW_FILTERING;
234         PRE_PROBEBLOCK();
235
236         pret = newp(size, nothrow);
237
238         if(pret != NULL && gProbeBlockCount == 0)
239         {
240                 add_memory_hash(pret, size, MEMTYPE_NEW, blockresult ? MEM_EXTERNAL : MEM_INTERNAL);
241         }
242
243         POST_PACK_PROBEBLOCK_BEGIN();
244
245         PREPARE_LOCAL_BUF();
246         PACK_COMMON_BEGIN(MSG_PROBE_MEMORY,
247                           API_ID_void__operator_new___std__size_t_size__const_std__nothrow_t__nothrow__throw__,
248                           "xp", (uint64_t)(size), voidp_to_uint64(&nothrow));
249         PACK_COMMON_END('p', pret, newerrno, blockresult);
250         PACK_MEMORY(size, MEMORY_API_ALLOC, pret);
251         FLUSH_LOCAL_BUF();
252
253         POST_PACK_PROBEBLOCK_END();
254
255         return pret;
256 }
257
258 void operator delete(void *ptr, const std::nothrow_t& nothrow) throw()
259 {
260         static void (*deletep)(void *ptr, const std::nothrow_t& nothrow);
261         DECLARE_VARIABLE_STANDARD;
262         unsigned short caller;
263
264         GET_REAL_FUNCP_RTLD_NEXT_CPP(_ZdlPvRKSt9nothrow_t, deletep);
265
266         bfiltering = INTERNAL_DELETE_FILTERING;
267         PRE_PROBEBLOCK();
268
269         if(ptr != NULL && gProbeBlockCount == 0)
270         {
271                 probeBlockStart();
272                 ret = del_memory_hash(ptr, MEMTYPE_DELETE, &caller);
273                 if(blockresult == 0 && ret == 0 && caller == MEM_EXTERNAL)
274                 {
275                         setProbePoint(&probeInfo);
276                         blockresult = 2;
277                         probingStart();
278                 }
279                 probeBlockEnd();
280         }
281
282         deletep(ptr, nothrow);
283
284         POST_PACK_PROBEBLOCK_BEGIN();
285
286         PREPARE_LOCAL_BUF();
287         PACK_COMMON_BEGIN(MSG_PROBE_MEMORY,
288                           API_ID_void_operator_delete_void__ptr__const_std__nothrow_t__nothrow__throw__,
289                           "pp", voidp_to_uint64(ptr), voidp_to_uint64(&nothrow));
290         PACK_COMMON_END('v', 0, newerrno, blockresult);
291         PACK_MEMORY(0, MEMORY_API_FREE, ptr);
292         FLUSH_LOCAL_BUF();
293
294         POST_PACK_PROBEBLOCK_END();
295 }
296
297 void operator delete[](void *ptr, const std::nothrow_t& nothrow) throw()
298 {
299         static void (*deletep)(void *ptr, const std::nothrow_t& nothrow);
300         DECLARE_VARIABLE_STANDARD;
301         unsigned short caller;
302
303         GET_REAL_FUNCP_RTLD_NEXT_CPP(_ZdaPvRKSt9nothrow_t, deletep);
304
305         bfiltering = INTERNAL_DELETE_FILTERING;
306         PRE_PROBEBLOCK();
307
308         if(ptr != NULL && gProbeBlockCount == 0)
309         {
310                 probeBlockStart();
311                 ret = del_memory_hash(ptr, MEMTYPE_DELETE, &caller);
312                 if(blockresult == 0 && ret == 0 && caller == MEM_EXTERNAL)
313                 {
314                         setProbePoint(&probeInfo);
315                         blockresult = 2;
316                         probingStart();
317                 }
318                 probeBlockEnd();
319         }
320
321         deletep(ptr, nothrow);
322
323         POST_PACK_PROBEBLOCK_BEGIN();
324
325         PREPARE_LOCAL_BUF();
326         PACK_COMMON_BEGIN(MSG_PROBE_MEMORY,
327                           API_ID_void_operator_delete___void__ptr__const_std__nothrow_t__nothrow__throw__,
328                           "pp", voidp_to_uint64(ptr), voidp_to_uint64(&nothrow));
329         PACK_COMMON_END('v', 0, newerrno, blockresult);
330         PACK_MEMORY(0, MEMORY_API_FREE, ptr);
331         FLUSH_LOCAL_BUF();
332
333         POST_PACK_PROBEBLOCK_END();
334 }
335