td_ta_map_lwp2thr.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #include "thread_dbP.h"
  18. #include <linuxthreads/internals.h>
  19. td_err_e
  20. td_ta_map_lwp2thr (const td_thragent_t *ta, lwpid_t lwpid, td_thrhandle_t *th)
  21. {
  22. int pthread_threads_max = ta->pthread_threads_max;
  23. size_t sizeof_descr = ta->sizeof_descr;
  24. struct pthread_handle_struct phc[pthread_threads_max];
  25. size_t cnt;
  26. #ifdef ALL_THREADS_STOPPED
  27. int num;
  28. #else
  29. # define num 1
  30. #endif
  31. LOG ("td_ta_map_lwp2thr");
  32. /* Test whether the TA parameter is ok. */
  33. if (! ta_ok (ta))
  34. return TD_BADTA;
  35. /* Read all the descriptors. */
  36. if (ps_pdread (ta->ph, ta->handles, phc,
  37. sizeof (struct pthread_handle_struct) * pthread_threads_max)
  38. != PS_OK)
  39. return TD_ERR; /* XXX Other error value? */
  40. #ifdef ALL_THREADS_STOPPED
  41. /* Read the number of currently active threads. */
  42. if (ps_pdread (ta->ph, ta->pthread_handles_num, &num, sizeof (int)) != PS_OK)
  43. return TD_ERR; /* XXX Other error value? */
  44. #endif
  45. /* Get the entries one after the other and find out whether the ID
  46. matches. */
  47. for (cnt = 0; cnt < pthread_threads_max && num > 0; ++cnt)
  48. if (phc[cnt].h_descr != NULL)
  49. {
  50. struct _pthread_descr_struct pds;
  51. #ifdef ALL_THREADS_STOPPED
  52. /* First count this active thread. */
  53. --num;
  54. #endif
  55. if (ps_pdread (ta->ph, phc[cnt].h_descr, &pds, sizeof_descr) != PS_OK)
  56. return TD_ERR; /* XXX Other error value? */
  57. if ((pds.p_pid ?: ps_getpid (ta->ph)) == lwpid)
  58. {
  59. /* Found it. Now fill in the `td_thrhandle_t' object. */
  60. th->th_ta_p = (td_thragent_t *) ta;
  61. th->th_unique = phc[cnt].h_descr;
  62. return TD_OK;
  63. }
  64. }
  65. else if (cnt == 0)
  66. {
  67. /* The initial thread always exists. But it might not yet be
  68. initialized. Construct a value. */
  69. th->th_ta_p = (td_thragent_t *) ta;
  70. th->th_unique = NULL;
  71. return TD_OK;
  72. }
  73. return TD_NOLWP;
  74. }