td_ta_map_lwp2thr.c 2.6 KB

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