td_ta_map_lwp2thr.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* Which thread is running on an LWP?
  2. Copyright (C) 2003, 2004, 2007 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library 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 GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "thread_dbP.h"
  16. #include <stdlib.h>
  17. #include <byteswap.h>
  18. #include <sys/procfs.h>
  19. td_err_e
  20. __td_ta_lookup_th_unique (const td_thragent_t *ta_arg,
  21. lwpid_t lwpid, td_thrhandle_t *th)
  22. {
  23. td_thragent_t *const ta = (td_thragent_t *) ta_arg;
  24. ps_err_e err;
  25. td_err_e terr;
  26. prgregset_t regs;
  27. psaddr_t addr;
  28. LOG ("td_ta_map_lwp2thr");
  29. /* Test whether the TA parameter is ok. */
  30. if (! ta_ok (ta))
  31. return TD_BADTA;
  32. if (ta->ta_howto == ta_howto_unknown)
  33. {
  34. /* We need to read in from the inferior the instructions what to do. */
  35. psaddr_t howto;
  36. err = td_lookup (ta->ph, SYM_TH_UNIQUE_CONST_THREAD_AREA, &howto);
  37. if (err == PS_OK)
  38. {
  39. err = ps_pdread (ta->ph, howto,
  40. &ta->ta_howto_data.const_thread_area,
  41. sizeof ta->ta_howto_data.const_thread_area);
  42. if (err != PS_OK)
  43. return TD_ERR;
  44. ta->ta_howto = ta_howto_const_thread_area;
  45. if (ta->ta_howto_data.const_thread_area & 0xff000000U)
  46. ta->ta_howto_data.const_thread_area
  47. = bswap_32 (ta->ta_howto_data.const_thread_area);
  48. }
  49. else
  50. {
  51. switch (sizeof (regs[0]))
  52. {
  53. case 8:
  54. err = td_lookup (ta->ph, SYM_TH_UNIQUE_REGISTER64, &howto);
  55. if (err == PS_OK)
  56. ta->ta_howto = ta_howto_reg;
  57. else if (err == PS_NOSYM)
  58. {
  59. err = td_lookup (ta->ph,
  60. SYM_TH_UNIQUE_REGISTER64_THREAD_AREA,
  61. &howto);
  62. if (err == PS_OK)
  63. ta->ta_howto = ta_howto_reg_thread_area;
  64. }
  65. break;
  66. case 4:
  67. err = td_lookup (ta->ph, SYM_TH_UNIQUE_REGISTER32, &howto);
  68. if (err == PS_OK)
  69. ta->ta_howto = ta_howto_reg;
  70. else if (err == PS_NOSYM)
  71. {
  72. err = td_lookup (ta->ph,
  73. SYM_TH_UNIQUE_REGISTER32_THREAD_AREA,
  74. &howto);
  75. if (err == PS_OK)
  76. ta->ta_howto = ta_howto_reg_thread_area;
  77. }
  78. break;
  79. default:
  80. abort ();
  81. return TD_DBERR;
  82. }
  83. if (err != PS_OK)
  84. return TD_DBERR;
  85. /* For either of these methods we read in the same descriptor. */
  86. err = ps_pdread (ta->ph, howto,
  87. ta->ta_howto_data.reg, DB_SIZEOF_DESC);
  88. if (err != PS_OK)
  89. return TD_ERR;
  90. if (DB_DESC_SIZE (ta->ta_howto_data.reg) == 0)
  91. return TD_DBERR;
  92. if (DB_DESC_SIZE (ta->ta_howto_data.reg) & 0xff000000U)
  93. {
  94. /* Byte-swap these words, though we leave the size word
  95. in native order as the handy way to distinguish. */
  96. DB_DESC_OFFSET (ta->ta_howto_data.reg)
  97. = bswap_32 (DB_DESC_OFFSET (ta->ta_howto_data.reg));
  98. DB_DESC_NELEM (ta->ta_howto_data.reg)
  99. = bswap_32 (DB_DESC_NELEM (ta->ta_howto_data.reg));
  100. }
  101. }
  102. }
  103. switch (ta->ta_howto)
  104. {
  105. default:
  106. return TD_DBERR;
  107. case ta_howto_reg:
  108. /* On most machines, we are just looking at a register. */
  109. if (ps_lgetregs (ta->ph, lwpid, regs) != PS_OK)
  110. return TD_ERR;
  111. terr = _td_fetch_value_local (ta, ta->ta_howto_data.reg, -1,
  112. 0, regs, &addr);
  113. if (terr != TD_OK)
  114. return terr;
  115. /* In this descriptor the nelem word is overloaded as the bias. */
  116. addr += (int32_t) DB_DESC_NELEM (ta->ta_howto_data.reg);
  117. th->th_unique = addr;
  118. break;
  119. case ta_howto_const_thread_area:
  120. /* Some hosts don't have this call and this case won't be used. */
  121. # pragma weak ps_get_thread_area
  122. if (&ps_get_thread_area == NULL)
  123. return TD_NOCAPAB;
  124. /* A la x86-64, there is a magic index for get_thread_area. */
  125. if (ps_get_thread_area (ta->ph, lwpid,
  126. ta->ta_howto_data.const_thread_area,
  127. &th->th_unique) != PS_OK)
  128. return TD_ERR; /* XXX Other error value? */
  129. break;
  130. case ta_howto_reg_thread_area:
  131. if (&ps_get_thread_area == NULL)
  132. return TD_NOCAPAB;
  133. /* A la i386, a register holds the index for get_thread_area. */
  134. if (ps_lgetregs (ta->ph, lwpid, regs) != PS_OK)
  135. return TD_ERR;
  136. terr = _td_fetch_value_local (ta, ta->ta_howto_data.reg_thread_area,
  137. -1, 0, regs, &addr);
  138. if (terr != TD_OK)
  139. return terr;
  140. /* In this descriptor the nelem word is overloaded as scale factor. */
  141. if (ps_get_thread_area
  142. (ta->ph, lwpid,
  143. ((addr - (psaddr_t) 0)
  144. >> DB_DESC_NELEM (ta->ta_howto_data.reg_thread_area)),
  145. &th->th_unique) != PS_OK)
  146. return TD_ERR; /* XXX Other error value? */
  147. break;
  148. }
  149. /* Found it. Now complete the `td_thrhandle_t' object. */
  150. th->th_ta_p = ta;
  151. return TD_OK;
  152. }
  153. td_err_e
  154. td_ta_map_lwp2thr (const td_thragent_t *ta_arg,
  155. lwpid_t lwpid, td_thrhandle_t *th)
  156. {
  157. td_thragent_t *const ta = (td_thragent_t *) ta_arg;
  158. /* We cannot rely on thread registers and such information at all
  159. before __pthread_initialize_minimal has gotten far enough. They
  160. sometimes contain garbage that would confuse us, left by the kernel
  161. at exec. So if it looks like initialization is incomplete, we only
  162. fake a special descriptor for the initial thread. */
  163. psaddr_t list;
  164. td_err_e err = DB_GET_SYMBOL (list, ta, __stack_user);
  165. if (err != TD_OK)
  166. return err;
  167. err = DB_GET_FIELD (list, ta, list, list_t, next, 0);
  168. if (err != TD_OK)
  169. return err;
  170. if (list == 0)
  171. {
  172. if (ps_getpid (ta->ph) != lwpid)
  173. return TD_ERR;
  174. th->th_ta_p = ta;
  175. th->th_unique = 0;
  176. return TD_OK;
  177. }
  178. return __td_ta_lookup_th_unique (ta_arg, lwpid, th);
  179. }