[PROTO] Merge swap_draft <- tizen_2.2 as generic commit
[platform/core/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 && getTraceState() == 0)
62         {
63                 add_memory_hash(pret, size);
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", size);
72         PACK_COMMON_END(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 && getTraceState() == 0)
95         {
96                 add_memory_hash(pret, size);
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", size);
105         PACK_COMMON_END(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
119         GET_REAL_FUNCP_RTLD_NEXT_CPP(_ZdlPv, deletep);
120
121         bfiltering = INTERNAL_DELETE_FILTERING;
122         PRE_PROBEBLOCK();
123
124         if(ptr != NULL && getTraceState() == 0)
125         {
126                 del_memory_hash(ptr);
127         }
128
129         deletep(ptr);
130
131         POST_PACK_PROBEBLOCK_BEGIN();
132         
133         PREPARE_LOCAL_BUF();
134         PACK_COMMON_BEGIN(MSG_PROBE_MEMORY,
135                           API_ID_void_operator_delete_void__ptr__throw__,
136                           "p", ptr);
137         PACK_COMMON_END(0, newerrno, blockresult);
138         PACK_MEMORY(0, MEMORY_API_FREE, ptr);
139         FLUSH_LOCAL_BUF();
140         
141         POST_PACK_PROBEBLOCK_END();
142 }
143
144 void operator delete[](void *ptr) throw()
145 {
146         static void (*deletep)(void *ptr);
147         DECLARE_VARIABLE_STANDARD;
148
149         GET_REAL_FUNCP_RTLD_NEXT_CPP(_ZdaPv, deletep);
150
151         bfiltering = INTERNAL_DELETE_FILTERING;
152         PRE_PROBEBLOCK();
153
154         if(ptr != NULL && getTraceState() == 0)
155         {
156                 del_memory_hash(ptr);
157         }
158
159         deletep(ptr);
160
161         POST_PACK_PROBEBLOCK_BEGIN();
162         
163         PREPARE_LOCAL_BUF();
164         PACK_COMMON_BEGIN(MSG_PROBE_MEMORY,
165                           API_ID_void_operator_delete___void__ptr__throw__,
166                           "p", ptr);
167         PACK_COMMON_END(0, newerrno, blockresult);
168         PACK_MEMORY(0, MEMORY_API_FREE, ptr);
169         FLUSH_LOCAL_BUF();
170         
171         POST_PACK_PROBEBLOCK_END();
172 }
173
174 void *operator new(std::size_t size, const std::nothrow_t& nothrow) throw()
175 {
176         static void*(*newp)(std::size_t size, const std::nothrow_t& nothrow);
177         DECLARE_VARIABLE_STANDARD;
178         void *pret;
179
180         GET_REAL_FUNCP_RTLD_NEXT_CPP(_ZnwjRKSt9nothrow_t, newp);
181
182         bfiltering = INTERNAL_NEW_FILTERING;
183         PRE_PROBEBLOCK();
184
185         pret = newp(size, nothrow);
186
187         if(pret != NULL && getTraceState() == 0)
188         {
189                 add_memory_hash(pret, size);
190         }
191
192         POST_PACK_PROBEBLOCK_BEGIN();
193         
194         PREPARE_LOCAL_BUF();
195         PACK_COMMON_BEGIN(MSG_PROBE_MEMORY,
196                           API_ID_void__operator_new_std__size_t_size__const_std__nothrow_t__nothrow__throw__,
197                           "xp", size, &nothrow);
198         PACK_COMMON_END(pret, newerrno, blockresult);
199         PACK_MEMORY(size, MEMORY_API_ALLOC, pret);
200         FLUSH_LOCAL_BUF();
201         
202         POST_PACK_PROBEBLOCK_END();
203
204         return pret;
205 }
206
207 void *operator new[](std::size_t size, const std::nothrow_t& nothrow) throw()
208 {
209         static void*(*newp)(std::size_t size, const std::nothrow_t& nothrow);
210         DECLARE_VARIABLE_STANDARD;
211         void *pret;
212
213         GET_REAL_FUNCP_RTLD_NEXT_CPP(_ZnajRKSt9nothrow_t, newp);
214
215         bfiltering = INTERNAL_NEW_FILTERING;
216         PRE_PROBEBLOCK();
217
218         pret = newp(size, nothrow);
219
220         if(pret != NULL && getTraceState() == 0)
221         {
222                 add_memory_hash(pret, size);
223         }
224
225         POST_PACK_PROBEBLOCK_BEGIN();
226         
227         PREPARE_LOCAL_BUF();
228         PACK_COMMON_BEGIN(MSG_PROBE_MEMORY,
229                           API_ID_void__operator_new___std__size_t_size__const_std__nothrow_t__nothrow__throw__,
230                           "xp", size, &nothrow);
231         PACK_COMMON_END(pret, newerrno, blockresult);
232         PACK_MEMORY(size, MEMORY_API_ALLOC, pret);
233         FLUSH_LOCAL_BUF();
234         
235         POST_PACK_PROBEBLOCK_END();
236
237         return pret;
238 }
239
240 void operator delete(void *ptr, const std::nothrow_t& nothrow) throw()
241 {
242         static void (*deletep)(void *ptr, const std::nothrow_t& nothrow);
243         DECLARE_VARIABLE_STANDARD;
244
245         GET_REAL_FUNCP_RTLD_NEXT_CPP(_ZdlPvRKSt9nothrow_t, deletep);
246
247         bfiltering = INTERNAL_DELETE_FILTERING;
248         PRE_PROBEBLOCK();
249
250         if(ptr != NULL && getTraceState() == 0)
251         {
252                 del_memory_hash(ptr);
253         }
254
255         deletep(ptr, nothrow);
256
257         POST_PACK_PROBEBLOCK_BEGIN();
258         
259         PREPARE_LOCAL_BUF();
260         PACK_COMMON_BEGIN(MSG_PROBE_MEMORY,
261                           API_ID_void_operator_delete_void__ptr__const_std__nothrow_t__nothrow__throw__,
262                           "pp", ptr, &nothrow);
263         PACK_COMMON_END(0, newerrno, blockresult);
264         PACK_MEMORY(0, MEMORY_API_FREE, ptr);
265         FLUSH_LOCAL_BUF();
266         
267         POST_PACK_PROBEBLOCK_END();
268 }
269
270 void operator delete[](void *ptr, const std::nothrow_t& nothrow) throw()
271 {
272         static void (*deletep)(void *ptr, const std::nothrow_t& nothrow);
273         DECLARE_VARIABLE_STANDARD;
274
275         GET_REAL_FUNCP_RTLD_NEXT_CPP(_ZdaPvRKSt9nothrow_t, deletep);
276
277         bfiltering = INTERNAL_DELETE_FILTERING;
278         PRE_PROBEBLOCK();
279
280         if(ptr != NULL && getTraceState() == 0)
281         {
282                 del_memory_hash(ptr);
283         }
284
285         deletep(ptr, nothrow);
286
287         POST_PACK_PROBEBLOCK_BEGIN();
288                 
289         PREPARE_LOCAL_BUF();
290         PACK_COMMON_BEGIN(MSG_PROBE_MEMORY,
291                           API_ID_void_operator_delete___void__ptr__const_std__nothrow_t__nothrow__throw__,
292                           "pp", ptr, &nothrow);
293         PACK_COMMON_END(0, newerrno, blockresult);
294         PACK_MEMORY(0, MEMORY_API_FREE, ptr);
295         FLUSH_LOCAL_BUF();
296         
297         POST_PACK_PROBEBLOCK_END();
298 }
299