1 /* Which thread is running on an LWP?
2 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include "thread_dbP.h"
22 #include <sys/procfs.h>
26 td_ta_map_lwp2thr (const td_thragent_t *ta_arg,
27 lwpid_t lwpid, td_thrhandle_t *th)
29 td_thragent_t *const ta = (td_thragent_t *) ta_arg;
35 LOG ("td_ta_map_lwp2thr");
37 /* Test whether the TA parameter is ok. */
41 if (ta->ta_howto == ta_howto_unknown)
43 /* We need to read in from the inferior the instructions what to do. */
46 err = td_lookup (ta->ph, SYM_TH_UNIQUE_CONST_THREAD_AREA, &howto);
49 err = ps_pdread (ta->ph, howto,
50 &ta->ta_howto_data.const_thread_area,
51 sizeof ta->ta_howto_data.const_thread_area);
54 ta->ta_howto = ta_howto_const_thread_area;
55 if (ta->ta_howto_data.const_thread_area & 0xff000000U)
56 ta->ta_howto_data.const_thread_area
57 = bswap_32 (ta->ta_howto_data.const_thread_area);
61 switch (sizeof (regs[0]))
64 err = td_lookup (ta->ph, SYM_TH_UNIQUE_REGISTER64, &howto);
66 ta->ta_howto = ta_howto_reg;
67 else if (err == PS_NOSYM)
69 err = td_lookup (ta->ph,
70 SYM_TH_UNIQUE_REGISTER64_THREAD_AREA,
73 ta->ta_howto = ta_howto_reg_thread_area;
78 err = td_lookup (ta->ph, SYM_TH_UNIQUE_REGISTER32, &howto);
80 ta->ta_howto = ta_howto_reg;
81 else if (err == PS_NOSYM)
83 err = td_lookup (ta->ph,
84 SYM_TH_UNIQUE_REGISTER32_THREAD_AREA,
87 ta->ta_howto = ta_howto_reg_thread_area;
99 /* For either of these methods we read in the same descriptor. */
100 err = ps_pdread (ta->ph, howto,
101 ta->ta_howto_data.reg, DB_SIZEOF_DESC);
104 if (DB_DESC_SIZE (ta->ta_howto_data.reg) == 0)
106 if (DB_DESC_SIZE (ta->ta_howto_data.reg) & 0xff000000U)
108 /* Byte-swap these words, though we leave the size word
109 in native order as the handy way to distinguish. */
110 DB_DESC_OFFSET (ta->ta_howto_data.reg)
111 = bswap_32 (DB_DESC_OFFSET (ta->ta_howto_data.reg));
112 DB_DESC_NELEM (ta->ta_howto_data.reg)
113 = bswap_32 (DB_DESC_NELEM (ta->ta_howto_data.reg));
118 switch (ta->ta_howto)
120 case ta_howto_unknown:
127 /* On most machines, we are just looking at a register. */
128 if (ps_lgetregs (ta->ph, lwpid, regs) != PS_OK)
130 terr = _td_fetch_value_local (ta, ta->ta_howto_data.reg, -1,
134 /* In this descriptor the nelem word is overloaded as the bias. */
135 addr += (int32_t) DB_DESC_NELEM (ta->ta_howto_data.reg);
136 th->th_unique = addr;
139 case ta_howto_const_thread_area:
140 /* Some hosts don't have this call and this case won't be used. */
141 # pragma weak ps_get_thread_area
142 if (&ps_get_thread_area == NULL)
145 /* A la x86-64, there is a constant magic index for get_thread_area. */
146 if (ps_get_thread_area (ta->ph, lwpid,
147 ta->ta_howto_data.const_thread_area,
148 &th->th_unique) != PS_OK)
149 return TD_ERR; /* XXX Other error value? */
152 case ta_howto_reg_thread_area:
153 if (&ps_get_thread_area == NULL)
156 /* A la i386, there is a register with an index for get_thread_area. */
157 if (ps_lgetregs (ta->ph, lwpid, regs) != PS_OK)
159 terr = _td_fetch_value_local (ta, ta->ta_howto_data.reg_thread_area, -1,
163 /* In this descriptor the nelem word is overloaded as scale factor. */
164 if (ps_get_thread_area
166 ((addr - (psaddr_t) 0)
167 >> DB_DESC_NELEM (ta->ta_howto_data.reg_thread_area)),
168 &th->th_unique) != PS_OK)
169 return TD_ERR; /* XXX Other error value? */
173 /* Found it. Now complete the `td_thrhandle_t' object. */
174 th->th_ta_p = (td_thragent_t *) ta;