dispatcher.c 43 KB

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