1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
|
/* Copyright (C) 2021-2024 Free Software Foundation, Inc.
Contributed by Oracle.
This file is part of GNU Binutils.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, 51 Franklin Street - Fifth Floor, Boston,
MA 02110-1301, USA. */
/*
* Central SIGPROF dispatcher to various module event handlers
* (REALPROF profile, HWC check, overview sample, manual sample)
*/
#include "config.h"
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <sys/syscall.h>
#include <time.h>
#include <signal.h>
#include "gp-defs.h"
#include "gp-experiment.h"
#include "collector.h"
#include "collector_module.h"
#include "tsd.h"
#include "hwcdrv.h"
#include "memmgr.h"
static void collector_sigprof_dispatcher (int, siginfo_t*, void*);
static int init_interposition_intf ();
static int collector_timer_create (timer_t * ptimerid);
static int collector_timer_settime (int period, timer_t timerid);
static int collector_timer_gettime (timer_t timerid);
static volatile int collector_sigprof_entries = 0; /* counter for SIGPROF signals in DISPATCH_TST mode */
static timer_t collector_master_thread_timerid = NULL;
static collector_mutex_t collector_clone_libc_lock = COLLECTOR_MUTEX_INITIALIZER;
static unsigned dispatcher_key = COLLECTOR_TSD_INVALID_KEY;
static int (*__real_clone) (int (*fn)(void *), void *child_stack, int flags,
void *arg, ...) = NULL;
static int (*__real_timer_create) (clockid_t clockid,
struct sigevent *sevp, timer_t *timerid) = NULL;
static int (*__real_timer_settime) (timer_t timerid, int flags,
const struct itimerspec *new_value,
struct itimerspec *old_value) = NULL;
static int (*__real_timer_delete) (timer_t timerid) = NULL;
static int (*__real_timer_gettime) (timer_t timerid,
struct itimerspec *curr_value) = NULL;
static int (*__real_pthread_create_2_34) (pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg) = NULL;
static int (*__real_pthread_create_2_17) (pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg) = NULL;
static int (*__real_pthread_create_2_2_5) (pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg) = NULL;
static int (*__real_pthread_create_2_1) (pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg) = NULL;
static int (*__real_pthread_create_2_0) (pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg) = NULL;
static int (*__real_timer_create_2_34) (clockid_t clockid,
struct sigevent *sevp, timer_t *timerid) = NULL;
static int (*__real_timer_create_2_17) (clockid_t clockid,
struct sigevent *sevp, timer_t *timerid) = NULL;
static int (*__real_timer_create_2_3_3) (clockid_t clockid,
struct sigevent *sevp, timer_t *timerid) = NULL;
static int (*__real_timer_create_2_2_5) (clockid_t clockid,
struct sigevent *sevp, timer_t *timerid) = NULL;
static int (*__real_timer_create_2_2) (clockid_t clockid,
struct sigevent *sevp, timer_t *timerid) = NULL;
int (*__real_pthread_sigmask_2_32) (int, const sigset_t *, sigset_t *) = NULL;
int (*__real_pthread_sigmask_2_17) (int, const sigset_t *, sigset_t *) = NULL;
int (*__real_pthread_sigmask_2_2_5) (int, const sigset_t *, sigset_t *) = NULL;
int (*__real_pthread_sigmask_2_0) (int, const sigset_t *, sigset_t *) = NULL;
/* Original SIGPROF handler which will be replaced with the dispatcher. Used
* to properly interact with libaio, which uses SIGPROF as its SIGAIOCANCEL. */
static struct sigaction original_sigprof_handler;
enum
{
DISPATCH_NYI = -1, /* dispatcher not yet installed */
DISPATCH_OFF = 0, /* dispatcher installed, but disabled */
DISPATCH_ON = 1, /* dispatcher installed, and enabled */
DISPATCH_TST = 2 /* dispatcher installed, and enabled in testing mode */
};
static struct sigaction sigaction_0;
static int dispatch_mode = DISPATCH_NYI; /* controls SIGPROF dispatching */
static int itimer_period_requested = 0; /* dispatcher itimer period */
static int itimer_period_actual = 0; /* actual dispatcher itimer period */
static int (*__real_sigaction) (int signum, const struct sigaction *act,
struct sigaction *oldact) = NULL;
static int (*__real_setitimer) (int which, const struct itimerval *new_value,
struct itimerval *old_value) = NULL;
static int (*__real_libc_setitimer) (int which,
const struct itimerval *new_value, struct itimerval *old_value) = NULL;
static int (*__real_sigprocmask) (int how, const sigset_t *set,
sigset_t *oldset) = NULL;
static int (*__real_thr_sigsetmask) (int how, const sigset_t *iset,
sigset_t *oset) = NULL;
static int (*__real_pthread_sigmask) (int how, const sigset_t *set,
sigset_t *oldset) = NULL;
static int (*__real_pthread_create) (pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg) = NULL;
/*
* void collector_sigprof_dispatcher()
*
* Common SIGPROF event handler which dispatches events to appropriate
* module handlers, if they are active for this collection and due.
* Dispatch sequence, logic and handlers currently hardcoded in dispatcher.
*/
static void
collector_sigprof_dispatcher (int sig, siginfo_t *info, void *context)
{
if (info == NULL || (info->si_code <= 0 && info->si_code != SI_TIMER))
{
TprintfT (DBG_LT2, "collector_sigprof_dispatcher signal for %p\n",
original_sigprof_handler.sa_handler);
/* pass signal to previous handler */
/* watch for recursion, SIG_IGN, and SIG_DFL */
if (original_sigprof_handler.sa_handler == SIG_DFL)
__collector_SIGDFL_handler (SIGPROF);
else if (original_sigprof_handler.sa_handler != SIG_IGN &&
original_sigprof_handler.sa_sigaction != &collector_sigprof_dispatcher)
{
(original_sigprof_handler.sa_sigaction)(sig, info, context);
TprintfT (DBG_LT2, "collector_sigprof_dispatcher handled\n");
}
}
else if (dispatch_mode == DISPATCH_ON)
{
#if ARCH(SPARC)
ucontext_t uctxmem;
ucontext_t *uctx = &uctxmem;
uctx->uc_link = NULL;
/* 23340823 signal handler third argument should point to a ucontext_t */
/* Convert sigcontext to ucontext_t on sparc-Linux */
struct sigcontext *sctx = (struct sigcontext*) context;
#if WSIZE(32)
uctx->uc_mcontext.gregs[REG_PC] = sctx->si_regs.pc;
__collector_memcpy (&uctx->uc_mcontext.gregs[3],
sctx->si_regs.u_regs,
sizeof (sctx->si_regs.u_regs));
#else
uctx->uc_mcontext.mc_gregs[MC_PC] = sctx->sigc_regs.tpc;
__collector_memcpy (&uctx->uc_mcontext.mc_gregs[3],
sctx->sigc_regs.u_regs,
sizeof (sctx->sigc_regs.u_regs));
#endif /* WSIZE() */
#else /* not sparc-Linux */
ucontext_t *uctx = (ucontext_t*) context;
#endif /* ARCH() */
TprintfT (DBG_LT3, "collector_sigprof_dispatcher dispatching signal\n");
/* XXXX the order of these checks/activities may need adjustment */
/* XXXX should also check (first) for a "cached" manual sample */
/* HWC check for each LWP: required even if collection is paused */
/* This should be first, otherwise it's likely to find the counters
* stopped due to an event/overflow during some of the other activities.
*/
/* XXXX HWC check performed every time (skipping if HWC profiling inactive)
* to avoid complexity of maintaining separate check times for each LWP
*/
__collector_ext_hwc_check (info, uctx);
/* XXXX if sigemtpending, should perhaps skip __collector_ext_usage_sample
* (and get it next time through)
*/
/* check for experiment past delay start */
if (__collector_delay_start != 0)
{
hrtime_t now = __collector_gethrtime ();
if (__collector_delay_start < now)
{
TprintfT (0, "__collector_ext_usage_sample: now (%lld) > delay_start (%lld)\n",
(now - __collector_start_time), (__collector_delay_start - __collector_start_time));
/* resume the data collection */
__collector_delay_start = 0;
__collector_resume ();
/* don't take a periodic sample, just let the resume sample cover it */
if (__collector_sample_period != 0)
{
/* this update should only be done for periodic samples */
while (__collector_next_sample < now)
__collector_next_sample += ((hrtime_t) NANOSEC) * __collector_sample_period;
}
/* return; */
}
}
/* check for periodic sampling */
if (__collector_gethrtime () > __collector_next_sample)
__collector_ext_usage_sample (PERIOD_SMPL, "periodic");
/* check for experiment past termination time */
if (__collector_exp_active && __collector_terminate_time != 0)
{
hrtime_t now = __collector_gethrtime ();
if (__collector_terminate_time < now)
{
TprintfT (0, "__collector_ext_usage_sample: now (%lld) > terminate_time (%lld); closing experiment\n",
(now - __collector_start_time), (__collector_terminate_time - __collector_start_time));
/* close the experiment */
__collector_close_experiment ();
}
}
/* call the code to process the profile data, and generate the packet */
/* (must always be called, otherwise profile data must be aggregated,
* but can be left till last, as already have the required data)
*/
__collector_ext_profile_handler (info, uctx);
}
else if (dispatch_mode == DISPATCH_TST)
{
collector_sigprof_entries++;
return;
}
}
/*
* __collector_sigprof_install
*/
int
__collector_sigprof_install ()
{
TprintfT (DBG_LT2, "__collector_sigprof_install\n");
struct sigaction oact;
if (__collector_sigaction (SIGPROF, NULL, &oact) != 0)
return COL_ERROR_DISPINIT;
if (oact.sa_sigaction == collector_sigprof_dispatcher)
/* signal handler is already in place; we are probably in a fork-child */
TprintfT (DBG_LT1, "dispatcher: __collector_ext_dispatcher_install() collector_sigprof_dispatcher already installed\n");
else
{
struct sigaction c_act = sigaction_0;
sigemptyset (&c_act.sa_mask);
sigaddset (&c_act.sa_mask, HWCFUNCS_SIGNAL); /* block SIGEMT delivery in handler */
c_act.sa_sigaction = collector_sigprof_dispatcher;
c_act.sa_flags = SA_RESTART | SA_SIGINFO;
if (__collector_sigaction (SIGPROF, &c_act, &original_sigprof_handler))
return COL_ERROR_DISPINIT;
}
dispatch_mode = DISPATCH_OFF; /* don't dispatch yet */
TprintfT (DBG_LT2, "__collector_sigprof_install done\n");
return COL_ERROR_NONE;
}
/*
* void __collector_ext_dispatcher_tsd_create_key()
*
* create tsd key for dispatcher
*/
void
__collector_ext_dispatcher_tsd_create_key ()
{
dispatcher_key = __collector_tsd_create_key (sizeof (timer_t), NULL, NULL);
}
/*
* int __collector_ext_dispatcher_install()
*
* installs a common handler/dispatcher (and itimer) for SIGPROF events
*/
int
__collector_ext_dispatcher_install ()
{
int timer_period;
TprintfT (DBG_LT2, "__collector_ext_dispatcher_install\n");
/* check period set for interval timer, which will be used as the basis
* for all timed activities: if not set, no role for SIGPROF dispatcher
*/
if (itimer_period_requested <= 0)
{
TprintfT (DBG_LT1, "No interval timer set: skipping dispatcher install!\n");
return COL_ERROR_NONE; /* no itimer/dispatcher required */
}
/* check for an existing interval timer */
if (collector_master_thread_timerid == NULL)
if (collector_timer_create (&collector_master_thread_timerid) < 0)
return COL_ERROR_ITMRINIT;
timer_t *timeridptr = __collector_tsd_get_by_key (dispatcher_key);
if (timeridptr != NULL)
*timeridptr = collector_master_thread_timerid; // store for per thread timer stop/start
TprintfT (DBG_LT3, "__collector_ext_dispatcher_install: collector_master_thread_timerid=%p\n",
collector_master_thread_timerid);
timer_period = collector_timer_gettime (collector_master_thread_timerid);
if (timer_period > 0)
{
TprintfT (DBG_LT1, "Overriding app-set interval timer with period %d\n", timer_period);
(void) __collector_log_write ("<event kind=\"%s\" id=\"%d\">%d->%d</event>\n",
SP_JCMD_CWARN, COL_WARN_ITMRPOVR, timer_period, itimer_period_requested);
}
/* install the interval timer used for all timed activities */
if (collector_timer_settime (itimer_period_requested, collector_master_thread_timerid) < 0)
return COL_ERROR_ITMRINIT;
TprintfT (DBG_LT2, "__collector_ext_dispatcher_install done\n");
dispatch_mode = DISPATCH_ON; /* activate SIGPROF dispatch to event handlers */
return COL_ERROR_NONE;
}
int
__collector_sigaction (int sig, const struct sigaction *nact, struct sigaction *oact)
{
TprintfT (DBG_LT1, "__collector_sigaction: %d, %p\n", sig, nact ? nact->sa_sigaction : NULL);
if (NULL_PTR (sigaction))
init_interposition_intf ();
/* Whether we change the signal handler in the kernel
* or not make sure the real sigaction is aware about
* our new handler (6227565)
*/
return CALL_REAL (sigaction)(sig, nact, oact);
}
/*
* We have special dispatchers for SIGPROF and HWCFUNCS_SIGNAL to
* decide whether the signal was intended for us or for the user.
* One special case is SIGDFL, in which case we don't have a
* user-function address to call. If the user did indeed set
* default disposition for one of these signals and sent that
* signal, we honor that action, even though it will lead to
* termination.
*/
void
__collector_SIGDFL_handler (int sig)
{
/* remove our dispatcher, replacing it with the default disposition */
struct sigaction act = sigaction_0;
act.sa_handler = SIG_DFL;
if (__collector_sigaction (sig, &act, NULL))
{
/* XXXXXX what are we supposed to do here? we're committing suicide anyhow */
}
/* resend the signal we intercepted earlier */
// XXXX Bug 18177509 - additional sigprof signal kills target program
kill (getpid (), sig);
}
/*
* suspend/resume timer per thread
*/
void
__collector_ext_dispatcher_thread_timer_suspend ()
{
timer_t * timeridptr = __collector_tsd_get_by_key (dispatcher_key);
if (timeridptr != NULL && *timeridptr != NULL)
(void) collector_timer_settime (0, *timeridptr);
return;
}
int
__collector_ext_dispatcher_thread_timer_resume ()
{
timer_t * timeridptr = __collector_tsd_get_by_key (dispatcher_key);
if (timeridptr == NULL)
return -1;
if (*timeridptr == NULL)
{ // timer id not initialized yet
TprintfT (DBG_LT2, "__collector_ext_dispatcher_thread_timer_resume: timer not initialized yet, create it\n");
if (collector_timer_create (timeridptr) == -1)
{
TprintfT (0, "__collector_ext_dispatcher_thread_timer_resume(): WARNING: No timer created\n");
return -1;
}
}
return collector_timer_settime (itimer_period_requested, *timeridptr);
}
void
__collector_ext_dispatcher_suspend ()
{
TprintfT (DBG_LT2, "__collector_ext_dispatcher_suspend\n");
if (dispatch_mode == DISPATCH_NYI)
{
TprintfT (0, "__collector_ext_dispatcher_suspend(): WARNING: No dispatcher installed\n");
return;
}
/* disable SIGPROF dispatching */
dispatch_mode = DISPATCH_OFF;
/* disable the interval timer; ignore any failures */
__collector_ext_dispatcher_thread_timer_suspend ();
return;
}
void
__collector_ext_dispatcher_restart ()
{
TprintfT (DBG_LT2, "__collector_ext_dispatcher_restart(ip=%d)\n", itimer_period_requested);
if (dispatch_mode == DISPATCH_NYI)
{
TprintfT (0, "__collector_ext_dispatcher_restart(): WARNING: No dispatcher installed\n");
return;
}
/* restart the interval timer used for all timed activities */
if (__collector_ext_dispatcher_thread_timer_resume () == 0)
dispatch_mode = DISPATCH_ON; /* re-activate SIGPROF dispatch to handlers */
return;
}
/*
* void __collector_ext_dispatcher_deinstall()
*
* If installed, disables SIGPROF dispatch and interval timer.
* Includes checks for last SIGPROF dispatch time, interval timer period,
* and currently installed SIGPROF handler, with appropriate warnings logged.
* The dispatcher remains installed to handle pending collector SIGPROFs and
* forward non-collector SIGPROFs to the application's handler(s).
* If the decision is ever made actually to deinstall the dispatcher,
* consider bug 4183714 and what to do about any possible pending
* SIGPROFs.
*/
void
__collector_ext_dispatcher_deinstall ()
{
TprintfT (DBG_LT1, "__collector_ext_dispatcher_deinstall()\n");
if (dispatch_mode == DISPATCH_NYI)
{
TprintfT (0, "__collector_ext_dispatcher_deinstall(): WARNING: No dispatcher installed\n");
return;
}
dispatch_mode = DISPATCH_OFF; /* disable SIGPROF dispatching */
/* verify that interval timer is still installed with expected period */
int timer_period = collector_timer_gettime (collector_master_thread_timerid);
if (timer_period != itimer_period_actual)
{
TprintfT (DBG_LT2, "dispatcher: Collector interval timer period changed %d -> %d\n",
itimer_period_actual, timer_period);
if ((itimer_period_actual >= (timer_period + timer_period / 10)) ||
(itimer_period_actual <= (timer_period - timer_period / 10)))
__collector_log_write ("<event kind=\"%s\" id=\"%d\">%d -> %d</event>\n",
SP_JCMD_CWARN, COL_WARN_ITMRREP,
itimer_period_actual, timer_period);
else
__collector_log_write ("<event kind=\"%s\" id=\"%d\">%d -> %d</event>\n",
SP_JCMD_COMMENT, COL_WARN_PROFRND,
itimer_period_actual, timer_period);
}
/* Verify that SIGPROF dispatcher is still installed.
* (still required with sigaction interposition and management,
* since interposition is not done for attach experiments)
*/
struct sigaction curr;
if (__collector_sigaction (SIGPROF, NULL, &curr) == -1)
TprintfT (0, "ERROR: dispatcher sigaction check failed: errno=%d\n", errno);
else if (curr.sa_sigaction != collector_sigprof_dispatcher)
{
TprintfT (0, "ERROR: collector dispatcher replaced by %p!\n", curr.sa_handler);
(void) __collector_log_write ("<event kind=\"%s\" id=\"%d\">%p</event>\n",
SP_JCMD_CWARN, COL_WARN_SIGPROF, curr.sa_handler);
}
else
TprintfT (DBG_LT2, "collector dispatcher integrity verified!\n");
/* disable the interval timer; ignore any failures */
if (collector_master_thread_timerid != NULL)
{
(void) CALL_REAL (timer_delete)(collector_master_thread_timerid);
collector_master_thread_timerid = NULL;
}
dispatcher_key = COLLECTOR_TSD_INVALID_KEY;
itimer_period_requested = 0;
itimer_period_actual = 0;
}
/*
* void __collector_ext_dispatcher_fork_child_cleanup()
*
* delete timer, clear timer interval
*/
void
__collector_ext_dispatcher_fork_child_cleanup ()
{
if (collector_master_thread_timerid != NULL)
{
(void) CALL_REAL (timer_delete)(collector_master_thread_timerid);
collector_master_thread_timerid = NULL;
}
__collector_mutex_init (&collector_clone_libc_lock);
dispatcher_key = COLLECTOR_TSD_INVALID_KEY;
itimer_period_requested = 0;
itimer_period_actual = 0;
}
/*
* int __collector_ext_itimer_set (int rperiod)
*
* set itimer period, if not yet set to a positive number of microseconds,
* (after rounding to sys_resolution if necessary) and return its value
*/
int
__collector_ext_itimer_set (int rperiod)
{
int period;
/* if rperiod is negative, force setting */
if (rperiod < 0)
{
itimer_period_actual = 0;
period = -rperiod;
}
else
period = rperiod;
// ignore SIGPROF while testing itimer interval setting
int saved = dispatch_mode;
dispatch_mode = DISPATCH_OFF;
if (collector_timer_create (&collector_master_thread_timerid) == -1)
{
TprintfT (0, "__collector_ext_itimer_set(): WARNING: No timer created\n");
return itimer_period_actual;
}
if (collector_timer_settime (period, collector_master_thread_timerid) == 0)
{
itimer_period_actual = collector_timer_gettime (collector_master_thread_timerid);
(void) collector_timer_settime (0, collector_master_thread_timerid); /* XXXX unset for now */
itimer_period_requested = period;
if (itimer_period_requested != itimer_period_actual)
{
TprintfT (DBG_LT2, " itimer period %d adjusted to %d\n",
itimer_period_requested, itimer_period_actual);
// (void) __collector_log_write("<event kind=\"%s\" id=\"%d\">%d -> %d</event>\n",
// SP_JCMD_CWARN, COL_WARN_PROFRND, itimer_period_requested, itimer_period_actual);
}
else
TprintfT (DBG_LT2, " itimer period %d accepted\n", period);
}
// restore dispatching SIGPROF handler
dispatch_mode = saved;
TprintfT (0, "__collector_ext_itimer_set(%d), requested=%d, actual=%d)\n",
rperiod, itimer_period_requested, itimer_period_actual);
return (itimer_period_actual);
}
static int
collector_timer_gettime (timer_t timerid)
{
int timer_period;
struct itimerspec itimer;
if (timerid == NULL)
return (0); // timer was not initialized
if (CALL_REAL (timer_gettime)(timerid, &itimer) == -1)
{
/* this should never reasonably fail, so not worth logging */
TprintfT (DBG_LT1, "WARNING: timer_gettime failed: errno=%d\n", errno);
return (-1);
}
timer_period = ((itimer.it_interval.tv_sec * NANOSEC) +
itimer.it_interval.tv_nsec) / 1000;
TprintfT (DBG_LT2, "collector_timer_gettime (period=%d)\n", timer_period);
return (timer_period);
}
static int
collector_timer_create (timer_t * ptimerid)
{
struct sigevent sigev;
if (NULL_PTR (timer_create))
init_interposition_intf ();
TprintfT (DBG_LT2, "collector_timer_settime(): timer_create is %p\n", __real_timer_create);
sigev.sigev_notify = SIGEV_THREAD_ID | SIGEV_SIGNAL;
sigev.sigev_signo = SIGPROF;
sigev.sigev_value.sival_ptr = ptimerid;
#if !defined(__MUSL_LIBC)
sigev._sigev_un._tid = __collector_gettid ();
#endif
if (CALL_REAL (timer_create)(CLOCK_THREAD_CPUTIME_ID, &sigev, ptimerid) == -1)
{
TprintfT (DBG_LT2, "collector_timer_settime() failed! errno=%d\n", errno);
return -1;
}
return 0;
}
static int
collector_timer_settime (int period, timer_t timerid)
{
struct itimerspec itimer;
if (NULL_PTR (timer_settime))
init_interposition_intf ();
TprintfT (DBG_LT2, "collector_timer_settime(period=%d)\n", period);
time_t NPM = 1000;
itimer.it_interval.tv_sec = NPM * period / NANOSEC;
itimer.it_interval.tv_nsec = (NPM * period) % NANOSEC;
itimer.it_value = itimer.it_interval;
if (CALL_REAL (timer_settime)(timerid, 0, &itimer, NULL) == -1)
{
TprintfT (DBG_LT2, "collector_timer_settime(%d) failed! errno=%d\n", period, errno);
return -1;
}
return 0;
}
static void
protect_profiling_signals (sigset_t* lset)
{
static unsigned int protected_sigprof = 0;
static unsigned int protected_sigemt = 0;
// T1 relies on thread signal masking, so best not to mess with it:
// T1 users have already been warned about the dangers of its use
if (__collector_libthread_T1)
return;
if (sigismember (lset, SIGPROF) && (dispatch_mode == DISPATCH_ON))
{
TprintfT (0, "WARNING: ignoring %s block while profiling\n", "SIGPROF");
if (protected_sigprof == 0)
__collector_log_write ("<event kind=\"%s\" id=\"%d\">%s</event>\n",
SP_JCMD_CWARN, COL_WARN_SIGMASK, "SIGPROF");
sigdelset (lset, SIGPROF);
protected_sigprof++;
}
if (sigismember (lset, HWCFUNCS_SIGNAL) && __collector_ext_hwc_active ())
{
TprintfT (0, "WARNING: ignoring %s block while profiling\n", "SIGEMT");
if (protected_sigemt == 0)
__collector_log_write ("<event kind=\"%s\" id=\"%d\">%s</event>\n",
SP_JCMD_CWARN, COL_WARN_SIGMASK, HWCFUNCS_SIGNAL_STRING);
sigdelset (lset, HWCFUNCS_SIGNAL);
protected_sigemt++;
}
}
static int
init_interposition_intf ()
{
if (__collector_dlsym_guard)
return 1;
void *dlflag;
/* Linux requires RTLD_LAZY, Solaris can do just RTLD_NOLOAD */
void *handle = dlopen (SYS_LIBC_NAME, RTLD_LAZY | RTLD_NOLOAD);
__real_setitimer = dlsym (RTLD_NEXT, "setitimer");
if (__real_setitimer == NULL)
{
__real_setitimer = dlsym (RTLD_DEFAULT, "setitimer");
if (__real_setitimer == NULL)
{
TprintfT (DBG_LT2, "init_interposition_intf() setitimer not found\n");
return 1;
}
dlflag = RTLD_DEFAULT;
}
else
dlflag = RTLD_NEXT;
TprintfT (DBG_LT2, "init_interposition_intf() using RTLD_%s\n",
(dlflag == RTLD_DEFAULT) ? "DEFAULT" : "NEXT");
__real_sigaction = dlsym (dlflag, "sigaction");
/* also explicitly get libc.so/setitimer (as a backup) */
__real_libc_setitimer = dlsym (handle, "setitimer");
__real_sigprocmask = dlsym (dlflag, "sigprocmask");
__real_thr_sigsetmask = dlsym (dlflag, "thr_sigsetmask");
__real_pthread_sigmask_2_32 = dlvsym (dlflag, "pthread_sigmask", "GLIBC_2.32");
__real_pthread_sigmask_2_17 = dlvsym (dlflag, "pthread_sigmask", "GLIBC_2.17");
__real_pthread_sigmask_2_2_5 = dlvsym (dlflag, "pthread_sigmask", "GLIBC_2.2.5");
__real_pthread_sigmask_2_0 = dlvsym (dlflag, "pthread_sigmask", "GLIBC_2.0");
if (__real_pthread_sigmask_2_32)
__real_pthread_sigmask = __real_pthread_sigmask_2_32;
else if (__real_pthread_sigmask_2_17)
__real_pthread_sigmask = __real_pthread_sigmask_2_17;
else if (__real_pthread_sigmask_2_2_5)
__real_pthread_sigmask = __real_pthread_sigmask_2_2_5;
else if (__real_pthread_sigmask_2_0)
__real_pthread_sigmask = __real_pthread_sigmask_2_0;
else
__real_pthread_sigmask = dlsym (dlflag, "pthread_sigmask");
__real_pthread_create_2_34 = dlvsym (dlflag, "pthread_create", "GLIBC_2.34");
__real_pthread_create_2_17 = dlvsym (dlflag, "pthread_create", "GLIBC_2.17");
__real_pthread_create_2_2_5 = dlvsym (dlflag, "pthread_create", "GLIBC_2.2.5");
__real_pthread_create_2_1 = dlvsym (dlflag, "pthread_create", "GLIBC_2.1");
__real_pthread_create_2_0 = dlvsym (dlflag, "pthread_create", "GLIBC_2.0");
if (__real_pthread_create_2_34)
__real_pthread_create = __real_pthread_create_2_34;
else if (__real_pthread_create_2_17)
__real_pthread_create = __real_pthread_create_2_17;
else if (__real_pthread_create_2_2_5)
__real_pthread_create = __real_pthread_create_2_2_5;
else if (__real_pthread_create_2_1)
__real_pthread_create = __real_pthread_create_2_1;
else if (__real_pthread_create_2_0)
__real_pthread_create = __real_pthread_create_2_0;
else
__real_pthread_create = dlsym (dlflag, "pthread_create");
__real_timer_create_2_34 = dlvsym (dlflag, "timer_create", "GLIBC_2.34");
__real_timer_create_2_17 = dlvsym (dlflag, "timer_create", "GLIBC_2.17");
__real_timer_create_2_3_3 = dlvsym (dlflag, "timer_create", "GLIBC_2.3.3");
__real_timer_create_2_2_5 = dlvsym (dlflag, "timer_create", "GLIBC_2.2.5");
__real_timer_create_2_2 = dlvsym (dlflag, "timer_create", "GLIBC_2.2");
if (__real_timer_create_2_34)
__real_timer_create = __real_timer_create_2_34;
else if (__real_timer_create_2_17)
__real_timer_create = __real_timer_create_2_17;
else if (__real_timer_create_2_3_3)
__real_timer_create = __real_timer_create_2_3_3;
else if (__real_timer_create_2_2_5)
__real_timer_create = __real_timer_create_2_2_5;
else if (__real_timer_create_2_2)
__real_timer_create = __real_timer_create_2_2;
else
__real_timer_create = dlsym (dlflag, "timer_create");
void *t;
if ((t = dlvsym (dlflag, "timer_settime", "GLIBC_2.34")) != NULL)
__real_timer_settime = t;
else if ((t = dlvsym (dlflag, "timer_settime", "GLIBC_2.17")) != NULL)
__real_timer_settime = t;
else if ((t = dlvsym (dlflag, "timer_settime", "GLIBC_2.3.3")) != NULL)
__real_timer_settime = t;
else if ((t = dlvsym (dlflag, "timer_settime", "GLIBC_2.2.5")) != NULL)
__real_timer_settime = t;
else if ((t = dlvsym (dlflag, "timer_settime", "GLIBC_2.0")) != NULL)
__real_timer_settime = t;
else
__real_timer_settime = dlsym (dlflag, "timer_settime");
if ((t = dlvsym (dlflag, "timer_delete", "GLIBC_2.34")) != NULL)
__real_timer_delete = t;
else if ((t = dlvsym (dlflag, "timer_delete", "GLIBC_2.17")) != NULL)
__real_timer_delete = t;
else if ((t = dlvsym (dlflag, "timer_delete", "GLIBC_2.3.3")) != NULL)
__real_timer_delete = t;
else if ((t = dlvsym (dlflag, "timer_delete", "GLIBC_2.2.5")) != NULL)
__real_timer_delete = t;
else if ((t = dlvsym (dlflag, "timer_delete", "GLIBC_2.2")) != NULL)
__real_timer_delete = t;
else
__real_timer_delete = dlsym (dlflag, "timer_delete");
if ((t = dlvsym (dlflag, "timer_gettime", "GLIBC_2.34")) != NULL)
__real_timer_gettime = t;
else if ((t = dlvsym (dlflag, "timer_gettime", "GLIBC_2.17")) != NULL)
__real_timer_gettime = t;
else if ((t = dlvsym (dlflag, "timer_gettime", "GLIBC_2.3.3")) != NULL)
__real_timer_gettime = t;
else if ((t = dlvsym (dlflag, "timer_gettime", "GLIBC_2.2.5")) != NULL)
__real_timer_gettime = t;
else if ((t = dlvsym (dlflag, "timer_gettime", "GLIBC_2.0")) != NULL)
__real_timer_gettime = t;
else
__real_timer_gettime = dlsym (dlflag, "timer_gettime");
__real_clone = dlsym (dlflag, "clone");
#define PR_FUNC(f) TprintfT (DBG_LT2, " dispetcher.c: " #f ": @%p\n", f)
PR_FUNC (__real_clone);
PR_FUNC (__real_libc_setitimer);
PR_FUNC (__real_pthread_create);
PR_FUNC (__real_pthread_create_2_0);
PR_FUNC (__real_pthread_create_2_1);
PR_FUNC (__real_pthread_create_2_17);
PR_FUNC (__real_pthread_create_2_2_5);
PR_FUNC (__real_pthread_create_2_34);
PR_FUNC (__real_pthread_sigmask);
PR_FUNC (__real_pthread_sigmask_2_0);
PR_FUNC (__real_pthread_sigmask_2_2_5);
PR_FUNC (__real_pthread_sigmask_2_17);
PR_FUNC (__real_pthread_sigmask_2_32);
PR_FUNC (__real_setitimer);
PR_FUNC (__real_sigaction);
PR_FUNC (__real_sigprocmask);
PR_FUNC (__real_thr_sigsetmask);
PR_FUNC (__real_timer_create);
PR_FUNC (__real_timer_create_2_17);
PR_FUNC (__real_timer_create_2_2);
PR_FUNC (__real_timer_create_2_2_5);
PR_FUNC (__real_timer_create_2_3_3);
PR_FUNC (__real_timer_create_2_34);
PR_FUNC (__real_timer_delete);
PR_FUNC (__real_timer_gettime);
PR_FUNC (__real_timer_settime);
return 0;
}
/*------------------------------------------------------------- sigaction */
/* NB: need a global interposing function called "sigaction" */
int
sigaction (int sig, const struct sigaction *nact, struct sigaction *oact)
{
int ret = 0;
int err = 0;
if (NULL_PTR (sigaction))
err = init_interposition_intf ();
if (err)
return -1;
TprintfT (DBG_LT3, "sigaction(sig=%02d, nact=%p) interposing\n", sig, nact);
if (sig == SIGPROF && dispatch_mode != DISPATCH_NYI)
{
if (oact != NULL)
{
oact->sa_handler = original_sigprof_handler.sa_handler;
oact->sa_mask = original_sigprof_handler.sa_mask;
oact->sa_flags = original_sigprof_handler.sa_flags;
}
if (nact != NULL)
{
original_sigprof_handler.sa_handler = nact->sa_handler;
original_sigprof_handler.sa_mask = nact->sa_mask;
original_sigprof_handler.sa_flags = nact->sa_flags;
TprintfT (DBG_LT1, "dispatcher: new sigaction(sig=%02d) set\n", sig);
}
}
else if (sig == HWCFUNCS_SIGNAL)
ret = collector_sigemt_sigaction (nact, oact);
else
{
if (sig != SIGCHLD || collector_sigchld_sigaction (nact, oact))
ret = CALL_REAL (sigaction)(sig, nact, oact);
TprintfT (DBG_LT3, "Real sigaction(sig=%02d) returned %d (oact=%p)\n",
sig, ret, oact);
/* but check for other important signals */
/* check for sample and pause/resume signals; give warning once, if need be */
if ((sig == __collector_sample_sig) && (__collector_sample_sig_warn == 0))
{
/* give user a warning */
(void) __collector_log_write ("<event kind=\"%s\" id=\"%d\">%d</event>\n",
SP_JCMD_CWARN, COL_WARN_SAMPSIGUSED, __collector_sample_sig);
__collector_sample_sig_warn = 1;
}
if ((sig == __collector_pause_sig) && (__collector_pause_sig_warn == 0))
{
/* give user a warning */
(void) __collector_log_write ("<event kind=\"%s\" id=\"%d\">%d</event>\n",
SP_JCMD_CWARN, COL_WARN_PAUSESIGUSED, __collector_pause_sig);
__collector_pause_sig_warn = 1;
}
}
TprintfT (DBG_LT3, "sigaction() returning %d (oact=%p)\n", ret, oact);
return ret;
}
/*
* In addition to interposing on sigaction(), should we also interpose
* on other important signal functions like signal() or sigset()?
* - On Solaris, those other functions apparently call sigaction().
* So, we only have to interpose on it.
* - On Linux, we should perhaps interpose on these other functions,
* but they are less portable than sigaction() and deprecated or even obsolete.
* So, we interpose, but don't overly worry about doing a good job.
*/
sighandler_t
signal (int sig, sighandler_t handler)
{
struct sigaction nact;
struct sigaction oact;
TprintfT (DBG_LT3, "signal(sig=%02d, handler=%p) interposing\n", sig, handler);
sigemptyset (&nact.sa_mask);
nact.sa_handler = handler;
nact.sa_flags = SA_RESTART;
if (sigaction (sig, &nact, &oact))
return SIG_ERR;
TprintfT (DBG_LT3, "signal() returning %p\n", oact.sa_handler);
return oact.sa_handler;
}
sighandler_t
sigset (int sig, sighandler_t handler)
{
TprintfT (DBG_LT3, "sigset(sig=%02d, handler=%p) interposing\n", sig, handler);
return signal (sig, handler);
}
/*------------------------------------------------------------- timer_create */
// map interposed symbol versions
static int
gprofng_timer_create (int (real_func) (clockid_t, struct sigevent *, timer_t *),
clockid_t clockid,
struct sigevent *sevp, timer_t *timerid)
{
// collector reserves SIGPROF
if (sevp == NULL || sevp->sigev_notify != SIGEV_SIGNAL ||
sevp->sigev_signo != SIGPROF)
{
int ret = real_func (clockid, sevp, timerid);
TprintfT (DBG_LT2, "timer_create @%p (%d) ret=%d\n", real_func,
(int) clockid, ret);
return ret;
}
/* log that application's timer_create request is overridden */
(void) __collector_log_write ("<event kind=\"%s\" id=\"%d\">%d</event>\n",
SP_JCMD_CWARN, COL_WARN_ITMROVR, -1);
errno = EBUSY;
TprintfT (DBG_LT2, "timer_create @%p (%d) ret=%d\n", real_func,
(int) clockid, -1); \
return -1;
}
#define DCL_TIMER_CREATE(dcl_f) \
int dcl_f (clockid_t clockid, struct sigevent *sevp, timer_t *timerid) \
{ \
if (__real_timer_create == NULL) \
init_interposition_intf (); \
return gprofng_timer_create (__real_timer_create, clockid, sevp, timerid); \
}
DCL_FUNC_VER (DCL_TIMER_CREATE, timer_create_2_34, timer_create@GLIBC_2.34)
DCL_FUNC_VER (DCL_TIMER_CREATE, timer_create_2_17, timer_create@GLIBC_2.17)
DCL_FUNC_VER (DCL_TIMER_CREATE, timer_create_2_3_3, timer_create@GLIBC_2.3.3)
DCL_FUNC_VER (DCL_TIMER_CREATE, timer_create_2_2_5, timer_create@GLIBC_2.2.5)
DCL_FUNC_VER (DCL_TIMER_CREATE, timer_create_2_2, timer_create@GLIBC_2.2)
DCL_TIMER_CREATE (timer_create)
/*------------------------------------------------------------- setitimer */
int
_setitimer (int which, const struct itimerval *nval,
struct itimerval *oval)
{
int ret;
int period;
if (NULL_PTR (setitimer))
init_interposition_intf ();
if (nval == NULL)
period = -1;
else
period = (nval->it_interval.tv_sec * MICROSEC) +
nval->it_interval.tv_usec;
TprintfT (DBG_LT1, "setitimer(which=%d,nval=%dus) interposing\n", which, period);
/* collector reserves ITIMER_REALPROF for its own use, and ITIMER_PROF
* uses the same signal (SIGPROF) so it must also be reserved
*/
if (((which != ITIMER_REALPROF) && (which != ITIMER_PROF)) || (nval == NULL))
{
ret = CALL_REAL (setitimer)(which, nval, oval);
if (oval == NULL)
period = -1;
else
period = (oval->it_interval.tv_sec * MICROSEC) +
oval->it_interval.tv_usec;
TprintfT (DBG_LT2, "Real setitimer(%d) returned %d (oval=%dus)\n",
which, ret, period);
return ret;
}
/* log that application's setitimer request is overridden */
(void) __collector_log_write ("<event kind=\"%s\" id=\"%d\">%d</event>\n",
SP_JCMD_CWARN, COL_WARN_ITMROVR, period);
if (oval == NULL)
period = -1;
else
{
getitimer (which, oval); /* return current itimer setting */
period = (oval->it_interval.tv_sec * MICROSEC) +
oval->it_interval.tv_usec;
}
ret = -1;
errno = EBUSY;
TprintfT (DBG_LT2, "setitimer() returning %d (oval=%dus)\n", ret, period);
return ret;
}
/*--------------------------------------------------------------- sigprocmask */
int
__collector_sigprocmask (int how, const sigset_t* iset, sigset_t* oset)
{
int err = 0;
if (NULL_PTR (sigprocmask))
err = init_interposition_intf ();
if (err)
return -1;
TprintfT (DBG_LT2, "__collector_sigprocmask(%d) interposing\n", how);
sigset_t lsigset;
sigset_t* lset = NULL;
if (iset)
{
lsigset = *iset;
lset = &lsigset;
if ((how == SIG_BLOCK) || (how == SIG_SETMASK))
protect_profiling_signals (lset);
}
int ret = CALL_REAL (sigprocmask)(how, lset, oset);
TprintfT (DBG_LT2, "__collector_sigprocmask(%d) returning %d\n", how, ret);
return ret;
}
/*------------------------------------------------------------ thr_sigsetmask */
int
__collector_thr_sigsetmask (int how, const sigset_t* iset, sigset_t* oset)
{
if (NULL_PTR (thr_sigsetmask))
init_interposition_intf ();
TprintfT (DBG_LT1, "__collector_thr_sigsetmask(%d) interposing\n", how);
sigset_t lsigset;
sigset_t* lset = NULL;
if (iset)
{
lsigset = *iset;
lset = &lsigset;
if ((how == SIG_BLOCK) || (how == SIG_SETMASK))
protect_profiling_signals (lset);
}
int ret = CALL_REAL (thr_sigsetmask)(how, lset, oset);
TprintfT (DBG_LT1, "__collector_thr_sigsetmask(%d) returning %d\n", how, ret);
return ret;
}
/*----------------------------------------------------------- pthread_sigmask */
// map interposed symbol versions
static int
gprofng_pthread_sigmask (int (real_func) (int, const sigset_t *, sigset_t*),
int how, const sigset_t *iset, sigset_t* oset)
{
sigset_t lsigset;
sigset_t* lset = NULL;
if (iset)
{
lsigset = *iset;
lset = &lsigset;
if (how == SIG_BLOCK || how == SIG_SETMASK)
protect_profiling_signals (lset);
}
int ret = (real_func) (how, lset, oset);
TprintfT (DBG_LT1, "real_pthread_sigmask @%p (%d) ret=%d\n",
real_func, how, ret);
return ret;
}
#define DCL_PTHREAD_SIGMASK(dcl_f) \
int dcl_f (int how, const sigset_t *iset, sigset_t* oset) \
{ \
if (__real_pthread_sigmask == NULL) \
init_interposition_intf (); \
return gprofng_pthread_sigmask (__real_pthread_sigmask, how, iset, oset); \
}
DCL_FUNC_VER (DCL_PTHREAD_SIGMASK, pthread_sigmask_2_32, pthread_sigmask@GLIBC_2.32)
DCL_FUNC_VER (DCL_PTHREAD_SIGMASK, pthread_sigmask_2_17, pthread_sigmask@GLIBC_2.17)
DCL_FUNC_VER (DCL_PTHREAD_SIGMASK, pthread_sigmask_2_2_5, pthread_sigmask@GLIBC_2.2.5)
DCL_FUNC_VER (DCL_PTHREAD_SIGMASK, pthread_sigmask_2_0, pthread_sigmask@GLIBC_2.0)
DCL_PTHREAD_SIGMASK (pthread_sigmask)
/*----------------------------------------------------------- pthread_create */
typedef struct _CollectorArgs
{
void *(*func)(void*);
void *arg;
void *stack;
int isPthread;
} CollectorArgs;
static void *
collector_root (void *cargs)
{
/* save the real arguments and free cargs */
void *(*func)(void*) = ((CollectorArgs*) cargs)->func;
void *arg = ((CollectorArgs*) cargs)->arg;
void *stack = ((CollectorArgs*) cargs)->stack;
int isPthread = ((CollectorArgs*) cargs)->isPthread;
__collector_freeCSize (__collector_heap, cargs, sizeof (CollectorArgs));
/* initialize tsd for this thread */
if (__collector_tsd_allocate () == 0)
/* init tsd for unwind, called right after __collector_tsd_allocate()*/
__collector_ext_unwind_key_init (isPthread, stack);
if (!isPthread)
__collector_mutex_lock (&collector_clone_libc_lock);
/* set the profile timer */
timer_t *timeridptr = __collector_tsd_get_by_key (dispatcher_key);
timer_t timerid = NULL;
if (timeridptr != NULL)
{
collector_timer_create (timeridptr);
if (*timeridptr != NULL)
collector_timer_settime (itimer_period_requested, *timeridptr);
timerid = *timeridptr;
}
int hwc_rc = __collector_ext_hwc_lwp_init ();
if (!isPthread)
__collector_mutex_unlock (&collector_clone_libc_lock);
/* call the real function */
void *ret = func (arg);
if (!isPthread)
__collector_mutex_lock (&collector_clone_libc_lock);
if (timerid != NULL)
CALL_REAL (timer_delete)(timerid);
if (!hwc_rc)
/* pthread_kill not handled here */
__collector_ext_hwc_lwp_fini ();
if (!isPthread)
__collector_mutex_unlock (&collector_clone_libc_lock);
/* if we have this chance, release tsd */
__collector_tsd_release ();
return ret;
}
// map interposed symbol versions
static int
gprofng_pthread_create (int (real_func) (pthread_t *, const pthread_attr_t *,
void *(*)(void *), void *),
pthread_t *thread, const pthread_attr_t *attr,
void *(*func)(void*), void *arg)
{
TprintfT (DBG_LTT, "gprofng_pthread_create @%p\n", real_func);
if (dispatch_mode != DISPATCH_ON)
return (real_func) (thread, attr, func, arg);
CollectorArgs *cargs = __collector_allocCSize (__collector_heap,
sizeof (CollectorArgs), 1);
if (cargs == NULL)
return (real_func) (thread, attr, func, arg);
cargs->func = func;
cargs->arg = arg;
cargs->stack = NULL;
cargs->isPthread = 1;
int ret = (real_func) (thread, attr, &collector_root, cargs);
if (ret)
__collector_freeCSize (__collector_heap, cargs, sizeof (CollectorArgs));
TprintfT (DBG_LT1, "gprofng_pthread_create @%p returns %d\n", real_func, ret);
return ret;
}
#define DCL_PTHREAD_CREATE(dcl_f) \
int dcl_f (pthread_t *thread, const pthread_attr_t *attr, \
void *(*func)(void*), void *arg) \
{ \
if (__real_pthread_create == NULL) \
init_interposition_intf (); \
return gprofng_pthread_create (__real_pthread_create, thread, attr, func, arg); \
}
DCL_FUNC_VER (DCL_PTHREAD_CREATE, pthread_create_2_34, pthread_create@GLIBC_2.34)
DCL_FUNC_VER (DCL_PTHREAD_CREATE, pthread_create_2_17, pthread_create@GLIBC_2.17)
DCL_FUNC_VER (DCL_PTHREAD_CREATE, pthread_create_2_2_5, pthread_create@GLIBC_2.2.5)
DCL_FUNC_VER (DCL_PTHREAD_CREATE, pthread_create_2_1, pthread_create@GLIBC_2.1)
DCL_FUNC_VER (DCL_PTHREAD_CREATE, pthread_create_2_0, pthread_create@GLIBC_2.0)
DCL_PTHREAD_CREATE (pthread_create)
int
__collector_ext_clone_pthread (int (*fn)(void *), void *child_stack, int flags, void *arg,
va_list va /* pid_t *ptid, struct user_desc *tls, pid_t *" ctid" */)
{
if (NULL_PTR (clone))
init_interposition_intf ();
TprintfT (0, "clone thread interposing\n");
pid_t * ptid = NULL;
struct user_desc * tls = NULL;
pid_t * ctid = NULL;
int num_args = 0;
if (flags & (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID))
{
ptid = va_arg (va, pid_t *);
tls = va_arg (va, struct user_desc*);
ctid = va_arg (va, pid_t *);
num_args = 3;
}
else if (flags & CLONE_SETTLS)
{
ptid = va_arg (va, pid_t *);
tls = va_arg (va, struct user_desc*);
num_args = 2;
}
else if (flags & CLONE_PARENT_SETTID)
{
ptid = va_arg (va, pid_t *);
num_args = 1;
}
int ret = 0;
if (dispatch_mode != DISPATCH_ON)
{
switch (num_args)
{
case 3:
ret = CALL_REAL (clone)(fn, child_stack, flags, arg, ptid, tls, ctid);
break;
case 2:
ret = CALL_REAL (clone)(fn, child_stack, flags, arg, ptid, tls);
break;
case 1:
ret = CALL_REAL (clone)(fn, child_stack, flags, arg, ptid);
break;
default:
ret = CALL_REAL (clone)(fn, child_stack, flags, arg);
break;
}
return ret;
}
CollectorArgs *cargs = __collector_allocCSize (__collector_heap, sizeof (CollectorArgs), 1);
if (cargs == NULL)
{
switch (num_args)
{
case 3:
ret = CALL_REAL (clone)(fn, child_stack, flags, arg, ptid, tls, ctid);
break;
case 2:
ret = CALL_REAL (clone)(fn, child_stack, flags, arg, ptid, tls);
break;
case 1:
ret = CALL_REAL (clone)(fn, child_stack, flags, arg, ptid);
break;
default:
ret = CALL_REAL (clone)(fn, child_stack, flags, arg);
break;
}
return ret;
}
cargs->func = (void *(*)(void*))fn;
cargs->arg = arg;
cargs->stack = child_stack;
cargs->isPthread = 0;
switch (num_args)
{
case 3:
ret = CALL_REAL (clone)((int(*)(void*))collector_root, child_stack, flags, cargs, ptid, tls, ctid);
break;
case 2:
ret = CALL_REAL (clone)((int(*)(void*))collector_root, child_stack, flags, cargs, ptid, tls);
break;
case 1:
ret = CALL_REAL (clone)((int(*)(void*))collector_root, child_stack, flags, cargs, ptid);
break;
default:
ret = CALL_REAL (clone)((int(*)(void*))collector_root, child_stack, flags, cargs);
break;
}
if (ret < 0)
__collector_freeCSize (__collector_heap, cargs, sizeof (CollectorArgs));
TprintfT (DBG_LT1, "clone thread returning %d\n", ret);
return ret;
}
// weak symbols:
int sigprocmask (int, const sigset_t*, sigset_t*) __attribute__ ((weak, alias ("__collector_sigprocmask")));
int thr_sigsetmask (int, const sigset_t*, sigset_t*) __attribute__ ((weak, alias ("__collector_thr_sigsetmask")));
__typeof(setitimer) setitimer __attribute__ ((weak, alias ("_setitimer")));
|