td_ta_map_lwp2thr.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* Which thread is running on an lwp?
  2. Copyright (C) 1999, 2001, 2002 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1999.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "thread_dbP.h"
  17. #include <linuxthreads/internals.h>
  18. td_err_e
  19. td_ta_map_lwp2thr (const td_thragent_t *ta, lwpid_t lwpid, td_thrhandle_t *th)
  20. {
  21. int pthread_threads_max = ta->pthread_threads_max;
  22. size_t sizeof_descr = ta->sizeof_descr;
  23. struct pthread_handle_struct phc[pthread_threads_max];
  24. size_t cnt;
  25. #ifdef ALL_THREADS_STOPPED
  26. int num;
  27. #else
  28. # define num 1
  29. #endif
  30. LOG ("td_ta_map_lwp2thr");
  31. /* Test whether the TA parameter is ok. */
  32. if (! ta_ok (ta))
  33. return TD_BADTA;
  34. /* Read all the descriptors. */
  35. if (ps_pdread (ta->ph, ta->handles, phc,
  36. sizeof (struct pthread_handle_struct) * pthread_threads_max)
  37. != PS_OK)
  38. return TD_ERR; /* XXX Other error value? */
  39. #ifdef ALL_THREADS_STOPPED
  40. /* Read the number of currently active threads. */
  41. if (ps_pdread (ta->ph, ta->pthread_handles_num, &num, sizeof (int)) != PS_OK)
  42. return TD_ERR; /* XXX Other error value? */
  43. #endif
  44. /* Get the entries one after the other and find out whether the ID
  45. matches. */
  46. for (cnt = 0; cnt < pthread_threads_max && num > 0; ++cnt)
  47. if (phc[cnt].h_descr != NULL)
  48. {
  49. struct _pthread_descr_struct pds;
  50. #ifdef ALL_THREADS_STOPPED
  51. /* First count this active thread. */
  52. --num;
  53. #endif
  54. if (ps_pdread (ta->ph, phc[cnt].h_descr, &pds, sizeof_descr) != PS_OK)
  55. return TD_ERR; /* XXX Other error value? */
  56. if ((pds.p_pid ?: ps_getpid (ta->ph)) == lwpid)
  57. {
  58. /* Found it. Now fill in the `td_thrhandle_t' object. */
  59. th->th_ta_p = (td_thragent_t *) ta;
  60. th->th_unique = phc[cnt].h_descr;
  61. return TD_OK;
  62. }
  63. }
  64. else if (cnt == 0)
  65. {
  66. /* The initial thread always exists. But it might not yet be
  67. initialized. Construct a value. */
  68. th->th_ta_p = (td_thragent_t *) ta;
  69. th->th_unique = NULL;
  70. return TD_OK;
  71. }
  72. return TD_NOLWP;
  73. }