remove da log printing from libdathread.c
[platform/core/system/swap-probe.git] / probe_thread / libdathread.c
1 /*
2  *  DA probe
3  *
4  * Copyright 2000 - 2011 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  *
12  * This library is free software; you can redistribute it and/or modify it under
13  * the terms of the GNU Lesser General Public License as published by the
14  * Free Software Foundation; either version 2.1 of the License, or (at your option)
15  * any later version.
16  *
17  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
18  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
20  * License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this library; if not, write to the Free Software Foundation, Inc., 51
24  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25  *
26  * Contributors:
27  * - S-Core Co., Ltd
28  *
29  */
30
31 #include <pthread.h>
32 #include <errno.h>
33 #include "daprobe.h"
34 #include "probeinfo.h"
35 #include "dahelper.h"
36 #include "da_thread.h"
37
38 #include "binproto.h"
39
40 typedef struct thread_routine_call_t {
41         void *(*thread_routine)(void *);
42         void *argument;
43 } thread_routine_call;
44
45 static enum DaOptions _sopt = OPT_THREAD;
46
47 // called when pthread_exit, pthread_cancel is called
48 void _da_cleanup_handler(void *data)
49 {
50         pthread_t pSelf;
51
52         probeInfo_t     probeInfo;
53
54         PRE_UNCONDITIONAL_BLOCK_BEGIN();
55
56         pSelf = pthread_self();
57         
58         PREPARE_LOCAL_BUF();
59         PACK_COMMON_BEGIN(MSG_PROBE_THREAD, LC_THREAD, "p", data);
60         PACK_COMMON_END(0, 0, 1);
61         PACK_THREAD(pSelf, THREAD_PTHREAD, THREAD_API_INTERNAL_STOP);
62         FLUSH_LOCAL_BUF();
63         
64         PRE_UNCONDITIONAL_BLOCK_END();
65
66         return;
67 }
68
69 void *_da_ThreadProc(void *params)
70 {
71         void *ret;
72         thread_routine_call *ptrc;
73         ptrc = (thread_routine_call *) params;
74         pthread_t pSelf;
75
76         probeInfo_t     probeInfo;
77
78         PRE_UNCONDITIONAL_BLOCK_BEGIN();
79
80         pSelf = pthread_self();
81         
82         PREPARE_LOCAL_BUF();
83         PACK_COMMON_BEGIN(MSG_PROBE_THREAD, LC_THREAD, "p", params);
84         PACK_COMMON_END(0, 0, 1);
85         PACK_THREAD(pSelf, THREAD_PTHREAD, THREAD_API_INTERNAL_START);
86         FLUSH_LOCAL_BUF();
87         
88         PRE_UNCONDITIONAL_BLOCK_END();
89
90         pthread_cleanup_push(_da_cleanup_handler, NULL);
91         // call user-defined thread routine
92         ret = ptrc->thread_routine(ptrc->argument);
93         pthread_cleanup_pop(0);
94
95         PRE_UNCONDITIONAL_BLOCK_BEGIN();
96
97         pSelf = pthread_self();
98         
99         PREPARE_LOCAL_BUF();
100         PACK_COMMON_BEGIN(MSG_PROBE_THREAD, LC_THREAD, "p", params);
101         PACK_COMMON_END(ret, 0, 1);
102         PACK_THREAD(pSelf, THREAD_PTHREAD, THREAD_API_INTERNAL_STOP);
103         FLUSH_LOCAL_BUF();
104         
105         PRE_UNCONDITIONAL_BLOCK_END();
106         
107         free(ptrc);
108         return ret;
109 }
110
111 int pthread_create(pthread_t *thread, const pthread_attr_t *attr, 
112                 void *(*start_routine) (void*), void *arg)
113 {
114         static int (*pthread_createp)(pthread_t *thread,
115                         const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);
116
117         BEFORE_ORIGINAL_THREAD(pthread_create, LIBPTHREAD);
118
119         if(blockresult)
120         {
121                 probeBlockStart();      
122                 thread_routine_call *ptrc = 
123                         (thread_routine_call *) malloc(sizeof(thread_routine_call));
124                 ptrc->thread_routine = start_routine;
125                 ptrc->argument = arg;
126                 probeBlockEnd();
127
128                 ret = pthread_createp(thread, attr, _da_ThreadProc, (void *) ptrc);
129         }
130         else // when pthread_create is called inside probe so (ex. custom chart, sampling thread)
131         {
132                 ret = pthread_createp(thread, attr, start_routine, arg);
133         }
134
135         AFTER_PACK_ORIGINAL_THREAD(ret, *thread, THREAD_API_START, 
136                         "pppp", thread, attr, start_routine, arg);
137
138         return ret;
139 }
140
141 int pthread_join(pthread_t thread, void **retval)
142 {
143         static int (*pthread_joinp)(pthread_t thread, void **retval);
144
145         DECLARE_VARIABLE_STANDARD;
146         GET_REAL_FUNC(pthread_join, LIBPTHREAD);
147
148         PRE_PROBEBLOCK_BEGIN();
149         
150         PREPARE_LOCAL_BUF();
151         PACK_COMMON_BEGIN(MSG_PROBE_THREAD, LC_THREAD, "xp", thread, retval);
152         PACK_COMMON_END(0, 0, blockresult);
153         PACK_THREAD(thread, THREAD_PTHREAD, THREAD_API_WAIT_START);
154         FLUSH_LOCAL_BUF();
155         
156         PRE_PROBEBLOCK_END();
157
158         ret = pthread_joinp(thread, retval);
159
160         // send WAIT_END log
161         newerrno = errno;
162         if(postBlockBegin(blockresult)) {
163                 setProbePoint(&probeInfo);
164
165                 PREPARE_LOCAL_BUF();
166                 PACK_COMMON_BEGIN(MSG_PROBE_THREAD, LC_THREAD, "xp", thread, retval);
167                 PACK_COMMON_END(ret, errno, blockresult);
168                 PACK_THREAD(thread, THREAD_PTHREAD, THREAD_API_WAIT_END);
169                 FLUSH_LOCAL_BUF();
170         
171                 postBlockEnd();
172         }
173         
174         return ret;     
175 }
176
177 void pthread_exit(void *retval)
178 {
179         pthread_t pSelf;
180         static void (*pthread_exitp)(void *retval) __attribute__((noreturn));
181
182         DECLARE_VARIABLE_STANDARD;
183         GET_REAL_FUNC(pthread_exit, LIBPTHREAD);
184
185         PRE_PROBEBLOCK_BEGIN();
186         newerrno = 0;
187         pSelf = pthread_self();
188         
189         PREPARE_LOCAL_BUF();
190         PACK_COMMON_BEGIN(MSG_PROBE_THREAD, LC_THREAD, "p", retval);
191         PACK_COMMON_END(0, 0, blockresult);
192         PACK_THREAD(pSelf, THREAD_PTHREAD, THREAD_API_EXIT);
193         FLUSH_LOCAL_BUF();
194         
195         PRE_PROBEBLOCK_END();
196
197         pthread_exitp(retval);
198 }
199
200 int pthread_cancel(pthread_t thread)
201 {
202         static int (*pthread_cancelp)(pthread_t thread);
203
204         BEFORE_ORIGINAL_THREAD(pthread_cancel, LIBPTHREAD);
205
206         ret = pthread_cancelp(thread);
207
208         AFTER_PACK_ORIGINAL_THREAD(ret, thread, THREAD_API_STOP, "x", thread);
209
210         return ret;
211 }
212
213 int pthread_detach(pthread_t thread)
214 {
215         static int (*pthread_detachp)(pthread_t thread);
216
217         BEFORE_ORIGINAL_THREAD(pthread_detach, LIBPTHREAD);
218
219         ret = pthread_detachp(thread);
220
221         AFTER_PACK_ORIGINAL_THREAD(ret, thread, THREAD_API_OTHER, "x", thread);
222
223         return ret;
224 }
225
226 pthread_t pthread_self(void)
227 {
228         pthread_t ret_pthr;
229         static pthread_t (*pthread_selfp)(void);
230
231         BEFORE_ORIGINAL_THREAD(pthread_self, LIBPTHREAD);
232
233         ret_pthr = pthread_selfp();
234         
235         newerrno = errno;
236         if(postBlockBegin(blockresult)) {
237
238                 AFTER_PACK_ORIGINAL_THREAD(ret_pthr, ret_pthr, THREAD_API_OTHER, "", 0);
239
240                 postBlockEnd();
241         }
242
243         return ret_pthr;
244 }
245
246 int pthread_equal(pthread_t t1, pthread_t t2)
247 {
248         static int (*pthread_equalp)(pthread_t t1, pthread_t t2);
249
250         BEFORE_ORIGINAL_THREAD(pthread_equal, LIBPTHREAD);
251
252         ret = pthread_equalp(t1, t2);
253
254         AFTER_PACK_ORIGINAL_THREAD(ret, t1, THREAD_API_OTHER, "xx", t1, t2);
255
256         return ret;
257 }
258
259 int pthread_setcancelstate(int state, int *oldstate)
260 {
261         pthread_t pSelf;
262         static int (*pthread_setcancelstatep)(int state, int *oldstate);
263
264         BEFORE_ORIGINAL_THREAD(pthread_setcancelstate, LIBPTHREAD);
265
266         pSelf = pthread_self();
267         ret = pthread_setcancelstatep(state, oldstate);
268
269         AFTER_PACK_ORIGINAL_THREAD(ret, pSelf, THREAD_API_OTHER, 
270                         "dp", state, oldstate);
271
272         return ret;
273 }
274
275 int pthread_setcanceltype(int type, int *oldtype)
276 {
277         pthread_t pSelf;
278         static int (*pthread_setcanceltypep)(int type, int *oldtype);
279
280         BEFORE_ORIGINAL_THREAD(pthread_setcanceltype, LIBPTHREAD);
281
282         pSelf = pthread_self();
283         ret = pthread_setcanceltypep(type, oldtype);
284
285         AFTER_PACK_ORIGINAL_THREAD(ret, pSelf, THREAD_API_OTHER, 
286                         "dp", type, oldtype);
287
288         return ret;
289 }
290
291 int pthread_attr_init(pthread_attr_t *attr)
292 {
293         pthread_t thread = 0;
294         static int (*pthread_attr_initp)(pthread_attr_t *attr);
295
296         BEFORE_ORIGINAL_THREAD(pthread_attr_init, LIBPTHREAD);
297
298         ret = pthread_attr_initp(attr);
299
300         AFTER_PACK_ORIGINAL_THREAD(ret, thread, THREAD_API_OTHER, "p", attr);
301
302         return ret;
303 }
304
305 int pthread_attr_destroy(pthread_attr_t *attr) 
306 {
307         pthread_t thread = 0;
308         static int (*pthread_attr_destroyp)(pthread_attr_t *attr);
309
310         BEFORE_ORIGINAL_THREAD(pthread_attr_destroy, LIBPTHREAD);
311
312         ret = pthread_attr_destroyp(attr);
313
314         AFTER_PACK_ORIGINAL_THREAD(ret, thread, THREAD_API_OTHER, "p", attr);
315
316         return ret;
317 }
318
319 int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
320 {
321         pthread_t thread = 0;
322         static int (*pthread_attr_getdetachstatep)(const pthread_attr_t *attr,
323                         int *detachstate);
324
325         BEFORE_ORIGINAL_THREAD(pthread_attr_getdetachstate, LIBPTHREAD);
326
327         ret = pthread_attr_getdetachstatep(attr, detachstate);
328
329         AFTER_PACK_ORIGINAL_THREAD(ret, thread, THREAD_API_OTHER, 
330                         "pp", attr, detachstate);
331
332         return ret;
333 }
334
335 int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
336 {
337         pthread_t thread = 0;
338         static int (*pthread_attr_setdetachstatep)(pthread_attr_t *attr, 
339                         int detachstate);
340
341         BEFORE_ORIGINAL_THREAD(pthread_attr_setdetachstate, LIBPTHREAD);
342
343         ret = pthread_attr_setdetachstatep(attr, detachstate);
344
345         AFTER_PACK_ORIGINAL_THREAD(ret, thread, THREAD_API_OTHER, 
346                         "pd", attr, detachstate);
347
348         return ret;
349 }
350
351 int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
352 {
353         pthread_t thread = 0;
354         static int (*pthread_attr_getstacksizep)(const pthread_attr_t *attr,
355                         size_t *stacksize);
356
357         BEFORE_ORIGINAL_THREAD(pthread_attr_getstacksize, LIBPTHREAD);
358
359         ret = pthread_attr_getstacksizep(attr, stacksize);
360
361         AFTER_PACK_ORIGINAL_THREAD(ret, thread, THREAD_API_OTHER, 
362                         "pp", attr, stacksize);
363
364         return ret;
365 }
366
367 int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
368 {
369         pthread_t thread = 0;
370         static int (*pthread_attr_setstacksizep)(pthread_attr_t *attr, 
371                         size_t stacksize);
372
373         BEFORE_ORIGINAL_THREAD(pthread_attr_setstacksize, LIBPTHREAD);
374
375         ret = pthread_attr_setstacksizep(attr, stacksize);
376
377         AFTER_PACK_ORIGINAL_THREAD(ret, thread, THREAD_API_OTHER, 
378                         "px", attr, stacksize);
379
380         return ret;
381 }
382
383 int pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr)
384 {
385         pthread_t thread = 0;
386         static int (*pthread_attr_getstackaddrp)(const pthread_attr_t *attr,
387                         void **stackaddr);
388
389         BEFORE_ORIGINAL_THREAD(pthread_attr_getstackaddr, LIBPTHREAD);
390
391         ret = pthread_attr_getstackaddrp(attr, stackaddr);
392
393         AFTER_PACK_ORIGINAL_THREAD(ret, thread, THREAD_API_OTHER, 
394                         "pp", attr, stackaddr);
395
396         return ret;
397 }
398
399 int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr)
400 {
401         pthread_t thread = 0;
402         static int (*pthread_attr_setstackaddrp)(pthread_attr_t *attr, 
403                         void *stackaddr);
404
405         BEFORE_ORIGINAL_THREAD(pthread_attr_setstackaddr, LIBPTHREAD);
406
407         ret = pthread_attr_setstackaddrp(attr, stackaddr);
408
409         AFTER_PACK_ORIGINAL_THREAD(ret, thread, THREAD_API_OTHER, 
410                         "pp", attr, stackaddr);
411
412         return ret;
413 }
414
415 int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inheritsched)
416 {
417         pthread_t thread = 0;
418         static int (*pthread_attr_getinheritschedp)(const pthread_attr_t *attr,
419                         int *inheritsched);
420
421         BEFORE_ORIGINAL_THREAD(pthread_attr_getinheritsched, LIBPTHREAD);
422
423         ret = pthread_attr_getinheritschedp(attr, inheritsched);
424
425         AFTER_PACK_ORIGINAL_THREAD(ret, thread, THREAD_API_OTHER, 
426                         "pp", attr, inheritsched);
427
428         return ret;
429 }
430
431 int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched)
432 {
433         pthread_t thread = 0;
434         static int (*pthread_attr_setinheritschedp)(pthread_attr_t *attr, 
435                         int inheritsched);
436
437         BEFORE_ORIGINAL_THREAD(pthread_attr_setinheritsched, LIBPTHREAD);
438
439         ret = pthread_attr_setinheritschedp(attr, inheritsched);
440
441         AFTER_PACK_ORIGINAL_THREAD(ret, thread, THREAD_API_OTHER, 
442                         "pd", attr, inheritsched);
443
444         return ret;
445 }
446
447 int pthread_attr_getschedparam(const pthread_attr_t *attr,
448                 struct sched_param *param)
449 {
450         pthread_t thread = 0;
451         static int (*pthread_attr_getschedparamp)(const pthread_attr_t *attr,
452                         struct sched_param *param);
453
454         BEFORE_ORIGINAL_THREAD(pthread_attr_getschedparam, LIBPTHREAD);
455
456         ret = pthread_attr_getschedparamp(attr, param);
457
458         AFTER_PACK_ORIGINAL_THREAD(ret, thread, THREAD_API_OTHER, 
459                         "pp", attr, param);
460
461         return ret;
462 }
463
464 int pthread_attr_setschedparam(pthread_attr_t *attr, 
465                 const struct sched_param *param)
466 {
467         pthread_t thread = 0;
468         static int (*pthread_attr_setschedparamp)(pthread_attr_t *attr,
469                         const struct sched_param *param);
470
471         BEFORE_ORIGINAL_THREAD(pthread_attr_setschedparam, LIBPTHREAD);
472
473         ret = pthread_attr_setschedparamp(attr, param);
474
475         AFTER_PACK_ORIGINAL_THREAD(ret, thread, THREAD_API_OTHER, 
476                         "pp", attr, param);
477
478         return ret;
479 }
480
481 int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
482 {
483         pthread_t thread = 0;
484         static int (*pthread_attr_getschedpolicyp)(const pthread_attr_t *attr,
485                         int *policy);
486
487         BEFORE_ORIGINAL_THREAD(pthread_attr_getschedpolicy, LIBPTHREAD);
488
489         ret = pthread_attr_getschedpolicyp(attr, policy);
490
491         AFTER_PACK_ORIGINAL_THREAD(ret, thread, THREAD_API_OTHER, 
492                         "pp", attr, policy);
493
494         return ret;
495 }
496
497 int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
498 {
499         pthread_t thread = 0;
500         static int (*pthread_attr_setschedpolicyp)(pthread_attr_t *attr, 
501                         int policy);
502
503         BEFORE_ORIGINAL_THREAD(pthread_attr_setschedpolicy, LIBPTHREAD);
504
505         ret = pthread_attr_setschedpolicyp(attr, policy);
506
507         AFTER_PACK_ORIGINAL_THREAD(ret, thread, THREAD_API_OTHER, 
508                         "pd", attr, policy);
509
510         return ret;
511 }
512
513 int pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize)
514 {
515         pthread_t thread = 0;
516         static int (*pthread_attr_getguardsizep)(const pthread_attr_t *attr,
517                         size_t *guardsize);
518
519         BEFORE_ORIGINAL_THREAD(pthread_attr_getguardsize, LIBPTHREAD);
520
521         ret = pthread_attr_getguardsizep(attr, guardsize);
522
523         AFTER_PACK_ORIGINAL_THREAD(ret, thread, THREAD_API_OTHER, 
524                         "pp", attr, guardsize);
525
526         return ret;
527 }
528
529 int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
530 {
531         pthread_t thread = 0;
532         static int (*pthread_attr_setguardsizep)(pthread_attr_t *attr, 
533                         size_t guardsize);
534
535         BEFORE_ORIGINAL_THREAD(pthread_attr_setguardsize, LIBPTHREAD);
536
537         ret = pthread_attr_setguardsizep(attr, guardsize);
538
539         AFTER_PACK_ORIGINAL_THREAD(ret, thread, THREAD_API_OTHER, 
540                         "px", attr, guardsize);
541
542         return ret;
543 }
544
545 int pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope)
546 {
547         pthread_t thread = 0;
548         static int (*pthread_attr_getscopep)(const pthread_attr_t *attr,
549                         int *contentionscope);
550
551         BEFORE_ORIGINAL_THREAD(pthread_attr_getscope, LIBPTHREAD);
552
553         ret = pthread_attr_getscopep(attr, contentionscope);
554
555         AFTER_PACK_ORIGINAL_THREAD(ret, thread, THREAD_API_OTHER, 
556                         "pp", attr, contentionscope);
557
558         return ret;
559 }
560
561 int pthread_attr_setscope(pthread_attr_t *attr, int contentionscope)
562 {
563         pthread_t thread = 0;
564         static int (*pthread_attr_setscopep)(pthread_attr_t *attr, 
565                         int contentionscope);
566
567         BEFORE_ORIGINAL_THREAD(pthread_attr_setscope, LIBPTHREAD);
568
569         ret = pthread_attr_setscopep(attr, contentionscope);
570
571         AFTER_PACK_ORIGINAL_THREAD(ret, thread, THREAD_API_OTHER, 
572                         "pd", attr, contentionscope);
573
574         return ret;
575 }
576
577 int pthread_attr_getstack(const pthread_attr_t *attr, 
578                 void **stackaddr, size_t *stacksize)
579 {
580         pthread_t thread = 0;
581         static int (*pthread_attr_getstackp)(const pthread_attr_t *attr,
582                         void **stackaddr, size_t *stacksize);
583
584         BEFORE_ORIGINAL_THREAD(pthread_attr_getstack, LIBPTHREAD);
585
586         ret = pthread_attr_getstackp(attr, stackaddr, stacksize);
587
588         AFTER_PACK_ORIGINAL_THREAD(ret, thread, THREAD_API_OTHER, 
589                         "ppp", attr, stackaddr, stacksize);
590
591         return ret;
592 }
593
594 int pthread_attr_setstack(pthread_attr_t *attr,
595                 void *stackaddr, size_t stacksize)
596 {
597         pthread_t thread = 0;
598         static int (*pthread_attr_setstackp)(pthread_attr_t *attr,
599                         void *stackaddr, size_t stacksize);
600
601         BEFORE_ORIGINAL_THREAD(pthread_attr_setstack, LIBPTHREAD);
602
603         ret = pthread_attr_setstackp(attr, stackaddr, stacksize);
604
605         AFTER_PACK_ORIGINAL_THREAD(ret, thread, THREAD_API_OTHER, 
606                         "ppx", attr, stackaddr, stacksize);
607
608         return ret;
609 }
610
611 /*
612 void pthread_testcancel(void);
613
614 int pthread_key_create(pthread_key_t *key, void (*destructor)(void*));
615 int pthread_key_delete(pthread_key_t key);
616
617 int pthread_getconcurrency(void);
618 int pthread_setconcurrency(int new_level);
619 int pthread_getcpuclockid(pthread_t thread_id, clockid_t *clock_id);
620 int pthread_getschedparam(pthread_t thread, int *policy,
621                 struct sched_param *param);
622 int pthread_setschedparam(pthread_t thread, int policy,
623                 const struct sched_param *param);
624 int pthread_setschedprio(pthread_t thread, int prio);
625 void *pthread_getspecific(pthread_key_t key);
626 int pthread_setspecific(pthread_key_t key, const void *value);
627
628 int pthread_once(pthread_once_t *once_control, void (*init_routine)(void));
629
630 int pthread_atfork(void (*prepare)(void), void (*parent)(void),
631                 void (*child)(void));
632 */
633