tizen 2.3 release
[framework/system/swap-probe.git] / probe_thread / libdasync.c
1 /*
2  *  DA probe
3  *
4  * Copyright 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 <pthread.h>
34 #include <time.h>
35 #include <errno.h>
36 #include "daprobe.h"
37 #include "probeinfo.h"
38 #include "dahelper.h"
39 #include "da_sync.h"
40
41 #include "binproto.h"
42 #include "real_functions.h"
43
44 static enum DaOptions _sopt = OPT_THREAD;
45
46 int pthread_mutex_init(pthread_mutex_t *mutex,
47                 const pthread_mutexattr_t *attr) {
48         static int (*pthread_mutex_initp)(pthread_mutex_t *mutex,
49                         const pthread_mutexattr_t *attr);
50
51         BEFORE_ORIGINAL_SYNC(pthread_mutex_init, LIBPTHREAD);
52
53         ret = pthread_mutex_initp(mutex, attr);
54
55         AFTER_PACK_ORIGINAL_SYNC(API_ID_pthread_mutex_init,
56                                  'd', ret, mutex, SYNC_PTHREAD_MUTEX,
57                                  SYNC_API_OTHER, "pp",
58                                  voidp_to_uint64(mutex), voidp_to_uint64(attr));
59
60         return ret;
61 }
62
63 int pthread_mutex_destroy(pthread_mutex_t *mutex) {
64         static int (*pthread_mutex_destroyp)(pthread_mutex_t *mutex);
65
66         BEFORE_ORIGINAL_SYNC(pthread_mutex_destroy, LIBPTHREAD);
67
68         ret = pthread_mutex_destroyp(mutex);
69
70         AFTER_PACK_ORIGINAL_SYNC(API_ID_pthread_mutex_destroy,
71                                  'd', ret, mutex, SYNC_PTHREAD_MUTEX,
72                                  SYNC_API_OTHER, "p", voidp_to_uint64(mutex));
73
74         return ret;
75 }
76
77 int real_pthread_mutex_lock(pthread_mutex_t *mutex) {
78         static int (*pthread_mutex_lockp)(pthread_mutex_t *mutex);
79
80         GET_REAL_FUNC(pthread_mutex_lock, LIBPTHREAD);
81
82         return pthread_mutex_lockp(mutex);
83 }
84
85 int pthread_mutex_lock(pthread_mutex_t *mutex) {
86         static int (*pthread_mutex_lockp)(pthread_mutex_t *mutex);
87
88         DECLARE_VARIABLE_STANDARD;
89         GET_REAL_FUNC(pthread_mutex_lock, LIBPTHREAD);
90
91         PRE_PROBEBLOCK_BEGIN();
92
93         PREPARE_LOCAL_BUF();
94         PACK_COMMON_BEGIN(MSG_PROBE_SYNC,
95                           API_ID_pthread_mutex_lock,
96                           "p", voidp_to_uint64(mutex));
97         PACK_COMMON_END('d', 0, 0, blockresult);
98         PACK_SYNC(mutex, SYNC_PTHREAD_MUTEX, SYNC_API_ACQUIRE_WAIT_START);
99         FLUSH_LOCAL_BUF();
100
101         PRE_PROBEBLOCK_END();
102
103         ret = pthread_mutex_lockp(mutex);
104
105         // send WAIT_END log
106         newerrno = errno;
107         if(postBlockBegin(blockresult)) {
108                 setProbePoint(&probeInfo);
109
110                 PREPARE_LOCAL_BUF();
111                 PACK_COMMON_BEGIN(MSG_PROBE_SYNC,
112                                   API_ID_pthread_mutex_lock,
113                                   "p", voidp_to_uint64(mutex));
114                 PACK_COMMON_END('d', ret, errno, blockresult);
115                 PACK_SYNC(mutex, SYNC_PTHREAD_MUTEX, SYNC_API_ACQUIRE_WAIT_END);
116                 FLUSH_LOCAL_BUF();
117
118                 postBlockEnd();
119         }
120         errno = (newerrno != 0) ? newerrno : olderrno;
121
122         return ret;
123 }
124
125 int pthread_mutex_timedlock(pthread_mutex_t *mutex,
126                 const struct timespec *abs_timeout) {
127         static int (*pthread_mutex_timedlockp)(pthread_mutex_t *mutex,
128                         const struct timespec *abs_timeout);
129
130         DECLARE_VARIABLE_STANDARD;
131         GET_REAL_FUNC(pthread_mutex_timedlock, LIBPTHREAD);
132
133         PRE_PROBEBLOCK_BEGIN();
134
135         PREPARE_LOCAL_BUF();
136         PACK_COMMON_BEGIN(MSG_PROBE_SYNC,
137                           API_ID_pthread_mutex_timedlock,
138                           "pp", voidp_to_uint64(mutex),
139                           voidp_to_uint64(abs_timeout));
140         PACK_COMMON_END('d', 0, 0, blockresult);
141         PACK_SYNC(mutex, SYNC_PTHREAD_MUTEX, SYNC_API_ACQUIRE_WAIT_START);
142         FLUSH_LOCAL_BUF();
143
144         PRE_PROBEBLOCK_END();
145
146         ret = pthread_mutex_timedlockp(mutex, abs_timeout);
147
148         // send WAIT_END log
149         newerrno = errno;
150         if(postBlockBegin(blockresult)) {
151                 setProbePoint(&probeInfo);
152
153                 PREPARE_LOCAL_BUF();
154                 PACK_COMMON_BEGIN(MSG_PROBE_SYNC,
155                                   API_ID_pthread_mutex_timedlock,
156                                   "pp", voidp_to_uint64(mutex),
157                                   voidp_to_uint64(abs_timeout));
158                 PACK_COMMON_END('d', ret, errno, blockresult);
159                 PACK_SYNC(mutex, SYNC_PTHREAD_MUTEX, SYNC_API_ACQUIRE_WAIT_END);
160                 FLUSH_LOCAL_BUF();
161
162                 postBlockEnd();
163         }
164
165     errno = (newerrno != 0) ? newerrno : olderrno;
166
167         return ret;
168 }
169
170 int pthread_mutex_trylock(pthread_mutex_t *mutex) {
171         static int (*pthread_mutex_trylockp)(pthread_mutex_t *mutex);
172
173         BEFORE_ORIGINAL_SYNC(pthread_mutex_trylock, LIBPTHREAD);
174
175         ret = pthread_mutex_trylockp(mutex);
176
177         AFTER_PACK_ORIGINAL_SYNC(API_ID_pthread_mutex_trylock,
178                                  'd', ret, mutex, SYNC_PTHREAD_MUTEX,
179                                  SYNC_API_TRY_ACQUIRE, "p",
180                                  voidp_to_uint64(mutex));
181
182         return ret;
183 }
184
185 int real_pthread_mutex_unlock(pthread_mutex_t *mutex) {
186         static int (*pthread_mutex_unlockp)(pthread_mutex_t *mutex);
187
188         GET_REAL_FUNC(pthread_mutex_unlock, LIBPTHREAD);
189
190         return pthread_mutex_unlockp(mutex);
191 }
192
193 int pthread_mutex_unlock(pthread_mutex_t *mutex) {
194         static int (*pthread_mutex_unlockp)(pthread_mutex_t *mutex);
195
196         BEFORE_ORIGINAL_SYNC(pthread_mutex_unlock, LIBPTHREAD);
197
198         ret = pthread_mutex_unlockp(mutex);
199
200         AFTER_PACK_ORIGINAL_SYNC(API_ID_pthread_mutex_unlock,
201                                  'd', ret, mutex, SYNC_PTHREAD_MUTEX,
202                                  SYNC_API_RELEASE, "p",
203                                  voidp_to_uint64(mutex));
204
205         return ret;
206 }
207
208 int pthread_mutexattr_init(pthread_mutexattr_t *attr) {
209         static int (*pthread_mutexattr_initp)(pthread_mutexattr_t *attr);
210
211         BEFORE_ORIGINAL_SYNC(pthread_mutexattr_init, LIBPTHREAD);
212
213         ret = pthread_mutexattr_initp(attr);
214
215         AFTER_PACK_ORIGINAL_SYNC(API_ID_pthread_mutexattr_init,
216                                  'd', ret, 0, SYNC_PTHREAD_MUTEX,
217                                  SYNC_API_OTHER, "p",
218                                  voidp_to_uint64(attr));
219
220         return ret;
221 }
222
223 int pthread_mutexattr_destroy(pthread_mutexattr_t *attr) {
224         static int (*pthread_mutexattr_destroyp)(pthread_mutexattr_t *attr);
225
226         BEFORE_ORIGINAL_SYNC(pthread_mutexattr_destroy, LIBPTHREAD);
227
228         ret = pthread_mutexattr_destroyp(attr);
229
230         AFTER_PACK_ORIGINAL_SYNC(API_ID_pthread_mutexattr_destroy,
231                                  'd', ret, 0, SYNC_PTHREAD_MUTEX,
232                                  SYNC_API_OTHER, "p",
233                                  voidp_to_uint64(attr));
234
235         return ret;
236 }
237
238 int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *attr,
239                 int *prioceiling) {
240         static int (*pthread_mutexattr_getprioceilingp)(
241                         const pthread_mutexattr_t *attr, int *prioceiling);
242
243         BEFORE_ORIGINAL_SYNC(pthread_mutexattr_getprioceiling, LIBPTHREAD);
244
245         ret = pthread_mutexattr_getprioceilingp(attr, prioceiling);
246
247         AFTER_PACK_ORIGINAL_SYNC(API_ID_pthread_mutexattr_getprioceiling,
248                                  'd', ret, 0, SYNC_PTHREAD_MUTEX,
249                                  SYNC_API_OTHER, "pp",
250                                  voidp_to_uint64(attr),
251                                  voidp_to_uint64(prioceiling));
252
253         return ret;
254 }
255
256 int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attr,
257                 int prioceiling) {
258         static int (*pthread_mutexattr_setprioceilingp)(
259                         pthread_mutexattr_t *attr, int prioceiling);
260
261         BEFORE_ORIGINAL_SYNC(pthread_mutexattr_setprioceiling, LIBPTHREAD);
262
263         ret = pthread_mutexattr_setprioceilingp(attr, prioceiling);
264
265         AFTER_PACK_ORIGINAL_SYNC(API_ID_pthread_mutexattr_setprioceiling,
266                                  'd', ret, 0, SYNC_PTHREAD_MUTEX,
267                                  SYNC_API_OTHER, "pd", voidp_to_uint64(attr),
268                                  prioceiling);
269
270         return ret;
271 }
272
273 int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *attr,
274                 int *protocol) {
275         static int (*pthread_mutexattr_getprotocolp)(
276                         const pthread_mutexattr_t *attr, int *protocol);
277
278         BEFORE_ORIGINAL_SYNC(pthread_mutexattr_getprotocol, LIBPTHREAD);
279
280         ret = pthread_mutexattr_getprotocolp(attr, protocol);
281
282         AFTER_PACK_ORIGINAL_SYNC(API_ID_pthread_mutexattr_getprotocol,
283                                  'd', ret, 0, SYNC_PTHREAD_MUTEX,
284                                  SYNC_API_OTHER, "pp",
285                                  voidp_to_uint64(attr),
286                                  voidp_to_uint64(protocol));
287
288         return ret;
289 }
290
291 int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr,
292                 int protocol) {
293         static int (*pthread_mutexattr_setprotocolp)(
294                         pthread_mutexattr_t *attr, int protocol);
295
296         BEFORE_ORIGINAL_SYNC(pthread_mutexattr_setprotocol, LIBPTHREAD);
297
298         ret = pthread_mutexattr_setprotocolp(attr, protocol);
299
300         AFTER_PACK_ORIGINAL_SYNC(API_ID_pthread_mutexattr_setprotocol,
301                                  'd', ret, 0, SYNC_PTHREAD_MUTEX,
302                                  SYNC_API_OTHER, "pd", voidp_to_uint64(attr),
303                                  protocol);
304
305         return ret;
306 }
307
308 int pthread_mutexattr_getpshared(const pthread_mutexattr_t *attr,
309                 int *pshared) {
310         static int (*pthread_mutexattr_getpsharedp)(
311                         const pthread_mutexattr_t *attr, int *pshared);
312
313         BEFORE_ORIGINAL_SYNC(pthread_mutexattr_getpshared, LIBPTHREAD);
314
315         ret = pthread_mutexattr_getpsharedp(attr, pshared);
316
317         AFTER_PACK_ORIGINAL_SYNC(API_ID_pthread_mutexattr_getpshared,
318                                  'd', ret, 0, SYNC_PTHREAD_MUTEX,
319                                  SYNC_API_OTHER, "pp",
320                                  voidp_to_uint64(attr),
321                                  voidp_to_uint64(pshared));
322
323         return ret;
324 }
325
326 int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr,
327                 int pshared) {
328         static int (*pthread_mutexattr_setpsharedp)(
329                         pthread_mutexattr_t *attr, int pshared);
330
331         BEFORE_ORIGINAL_SYNC(pthread_mutexattr_setpshared, LIBPTHREAD);
332
333         ret = pthread_mutexattr_setpsharedp(attr, pshared);
334
335         AFTER_PACK_ORIGINAL_SYNC(API_ID_pthread_mutexattr_setpshared,
336                                  'd', ret, 0, SYNC_PTHREAD_MUTEX,
337                                  SYNC_API_OTHER, "pd", voidp_to_uint64(attr),
338                                  pshared);
339
340         return ret;
341 }
342
343 int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type) {
344         static int (*pthread_mutexattr_gettypep)(
345                         const pthread_mutexattr_t *attr, int *type);
346
347         BEFORE_ORIGINAL_SYNC(pthread_mutexattr_gettype, LIBPTHREAD);
348
349         ret = pthread_mutexattr_gettypep(attr, type);
350
351         AFTER_PACK_ORIGINAL_SYNC(API_ID_pthread_mutexattr_gettype,
352                                  'd', ret, 0, SYNC_PTHREAD_MUTEX,
353                                  SYNC_API_OTHER, "pp",
354                                  voidp_to_uint64(attr),
355                                  voidp_to_uint64(type));
356
357         return ret;
358 }
359
360 int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) {
361         static int (*pthread_mutexattr_settypep)(
362                         pthread_mutexattr_t *attr, int type);
363
364         BEFORE_ORIGINAL_SYNC(pthread_mutexattr_settype, LIBPTHREAD);
365
366         ret = pthread_mutexattr_settypep(attr, type);
367
368         AFTER_PACK_ORIGINAL_SYNC(API_ID_pthread_mutexattr_settype,
369                                  'd', ret, 0, SYNC_PTHREAD_MUTEX,
370                                  SYNC_API_OTHER, "pd",
371                                  voidp_to_uint64(attr),
372                                  type);
373
374         return ret;
375 }
376
377 /*
378 int pthread_mutex_getprioceiling(const pthread_mutex_t *mutex,
379                 int *prioceiling);
380 int pthread_mutex_setprioceiling(pthread_mutex_t *mutex,
381                 int prioceiling, int *old_ceiling);
382 */
383
384 int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr) {
385         static int (*pthread_cond_initp)(pthread_cond_t *cond,
386                         const pthread_condattr_t *attr);
387
388         BEFORE_ORIGINAL_SYNC(pthread_cond_init, LIBPTHREAD);
389
390         ret = pthread_cond_initp(cond, attr);
391
392         AFTER_PACK_ORIGINAL_SYNC(API_ID_pthread_cond_init,
393                                  'd', ret, cond, SYNC_PTHREAD_COND_VARIABLE,
394                                  SYNC_API_OTHER, "pp",
395                                  voidp_to_uint64(cond),
396                                  voidp_to_uint64(attr));
397
398         return ret;
399 }
400
401 int pthread_cond_destroy(pthread_cond_t *cond) {
402         static int (*pthread_cond_destroyp)(pthread_cond_t *cond);
403
404         BEFORE_ORIGINAL_SYNC(pthread_cond_destroy, LIBPTHREAD);
405
406         ret = pthread_cond_destroyp(cond);
407
408         AFTER_PACK_ORIGINAL_SYNC(API_ID_pthread_cond_destroy,
409                                  'd', ret, cond, SYNC_PTHREAD_COND_VARIABLE,
410                                  SYNC_API_OTHER, "p",
411                                  voidp_to_uint64(cond));
412
413         return ret;
414 }
415
416 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
417         static int (*pthread_cond_waitp)(pthread_cond_t *cond,
418                         pthread_mutex_t *mutex);
419
420         DECLARE_VARIABLE_STANDARD;
421         GET_REAL_FUNC(pthread_cond_wait, LIBPTHREAD);
422
423         PRE_PROBEBLOCK_BEGIN();
424         // send WAIT_START log
425
426         PREPARE_LOCAL_BUF();
427         PACK_COMMON_BEGIN(MSG_PROBE_SYNC,
428                           API_ID_pthread_cond_wait,
429                           "pp",
430                           voidp_to_uint64(cond),
431                           voidp_to_uint64(mutex));
432         PACK_COMMON_END('d', 0, 0, blockresult);
433         PACK_SYNC(cond, SYNC_PTHREAD_COND_VARIABLE, SYNC_API_COND_WAIT_START);
434         FLUSH_LOCAL_BUF();
435
436         PRE_PROBEBLOCK_END();
437
438         ret = pthread_cond_waitp(cond, mutex);
439
440         // send WAIT_END log
441         newerrno = errno;
442         if(postBlockBegin(blockresult)) {
443                 setProbePoint(&probeInfo);
444
445                 PREPARE_LOCAL_BUF();
446                 PACK_COMMON_BEGIN(MSG_PROBE_SYNC,
447                                   API_ID_pthread_cond_wait,
448                                   "pp",
449                                   voidp_to_uint64(cond),
450                                   voidp_to_uint64(mutex));
451                 PACK_COMMON_END('d', ret, errno, blockresult);
452                 PACK_SYNC(cond, SYNC_PTHREAD_COND_VARIABLE, SYNC_API_COND_WAIT_END);
453                 FLUSH_LOCAL_BUF();
454
455                 postBlockEnd();
456         }
457     errno = (newerrno != 0) ? newerrno : olderrno;
458
459         return ret;
460 }
461
462 int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
463                 const struct timespec *abstime) {
464         static int (*pthread_cond_timedwaitp)(pthread_cond_t *cond,
465                                 pthread_mutex_t *mutex, const struct timespec *abstime);
466
467         DECLARE_VARIABLE_STANDARD;
468         GET_REAL_FUNC(pthread_cond_timedwait, LIBPTHREAD);
469
470         PRE_PROBEBLOCK_BEGIN();
471         // send WAIT_START log
472
473         PREPARE_LOCAL_BUF();
474         PACK_COMMON_BEGIN(MSG_PROBE_SYNC,
475                           API_ID_pthread_cond_timedwait,
476                           "ppp",
477                           voidp_to_uint64(cond),
478                           voidp_to_uint64(mutex),
479                           voidp_to_uint64(abstime));
480         PACK_COMMON_END('d', 0, 0, blockresult);
481         PACK_SYNC(cond, SYNC_PTHREAD_COND_VARIABLE, SYNC_API_COND_WAIT_START);
482         FLUSH_LOCAL_BUF();
483
484         PRE_PROBEBLOCK_END();
485
486         ret = pthread_cond_timedwaitp(cond, mutex, abstime);
487
488         // send WAIT_END log
489         newerrno = errno;
490         if(postBlockBegin(blockresult)) {
491                 setProbePoint(&probeInfo);
492
493                 PREPARE_LOCAL_BUF();
494                 PACK_COMMON_BEGIN(MSG_PROBE_SYNC,
495                                   API_ID_pthread_cond_timedwait,
496                                   "ppp",
497                                   voidp_to_uint64(cond),
498                                   voidp_to_uint64(mutex),
499                                   voidp_to_uint64(abstime));
500                 PACK_COMMON_END('d', ret, errno, blockresult);
501                 PACK_SYNC(cond, SYNC_PTHREAD_COND_VARIABLE, SYNC_API_COND_WAIT_END);
502                 FLUSH_LOCAL_BUF();
503
504                 postBlockEnd();
505         }
506
507     errno = (newerrno != 0) ? newerrno : olderrno;
508
509         return ret;
510 }
511
512 int pthread_cond_signal(pthread_cond_t *cond) {
513         static int (*pthread_cond_signalp)(pthread_cond_t *cond);
514
515         BEFORE_ORIGINAL_SYNC(pthread_cond_signal, LIBPTHREAD);
516
517         ret = pthread_cond_signalp(cond);
518
519         AFTER_PACK_ORIGINAL_SYNC(API_ID_pthread_cond_signal,
520                                  'd', ret, cond, SYNC_PTHREAD_COND_VARIABLE,
521                                  SYNC_API_NOTIFY, "p", voidp_to_uint64(cond));
522
523         return ret;
524 }
525
526 int pthread_cond_broadcast(pthread_cond_t *cond) {
527         static int (*pthread_cond_broadcastp)(pthread_cond_t *cond);
528
529         BEFORE_ORIGINAL_SYNC(pthread_cond_broadcast, LIBPTHREAD);
530
531         ret = pthread_cond_broadcastp(cond);
532
533         AFTER_PACK_ORIGINAL_SYNC(API_ID_pthread_cond_broadcast,
534                                  'd', ret, cond, SYNC_PTHREAD_COND_VARIABLE,
535                                  SYNC_API_NOTIFY_ALL, "p", voidp_to_uint64(cond));
536
537         return ret;
538 }