1 /* Which thread is running on an LWP?
2 Copyright (C) 2003-2014 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_lookup_th_unique (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 if (ta
->ta_howto
== ta_howto_unknown
)
37 /* We need to read in from the inferior the instructions what to do. */
40 err
= td_lookup (ta
->ph
, SYM_TH_UNIQUE_CONST_THREAD_AREA
, &howto
);
43 err
= ps_pdread (ta
->ph
, howto
,
44 &ta
->ta_howto_data
.const_thread_area
,
45 sizeof ta
->ta_howto_data
.const_thread_area
);
48 ta
->ta_howto
= ta_howto_const_thread_area
;
49 if (ta
->ta_howto_data
.const_thread_area
& 0xff000000U
)
50 ta
->ta_howto_data
.const_thread_area
51 = bswap_32 (ta
->ta_howto_data
.const_thread_area
);
55 switch (sizeof (regs
[0]))
58 err
= td_lookup (ta
->ph
, SYM_TH_UNIQUE_REGISTER64
, &howto
);
60 ta
->ta_howto
= ta_howto_reg
;
61 else if (err
== PS_NOSYM
)
63 err
= td_lookup (ta
->ph
,
64 SYM_TH_UNIQUE_REGISTER64_THREAD_AREA
,
67 ta
->ta_howto
= ta_howto_reg_thread_area
;
72 err
= td_lookup (ta
->ph
, SYM_TH_UNIQUE_REGISTER32
, &howto
);
74 ta
->ta_howto
= ta_howto_reg
;
75 else if (err
== PS_NOSYM
)
77 err
= td_lookup (ta
->ph
,
78 SYM_TH_UNIQUE_REGISTER32_THREAD_AREA
,
81 ta
->ta_howto
= ta_howto_reg_thread_area
;
93 /* For either of these methods we read in the same descriptor. */
94 err
= ps_pdread (ta
->ph
, howto
,
95 ta
->ta_howto_data
.reg
, DB_SIZEOF_DESC
);
98 if (DB_DESC_SIZE (ta
->ta_howto_data
.reg
) == 0)
100 if (DB_DESC_SIZE (ta
->ta_howto_data
.reg
) & 0xff000000U
)
102 /* Byte-swap these words, though we leave the size word
103 in native order as the handy way to distinguish. */
104 DB_DESC_OFFSET (ta
->ta_howto_data
.reg
)
105 = bswap_32 (DB_DESC_OFFSET (ta
->ta_howto_data
.reg
));
106 DB_DESC_NELEM (ta
->ta_howto_data
.reg
)
107 = bswap_32 (DB_DESC_NELEM (ta
->ta_howto_data
.reg
));
112 switch (ta
->ta_howto
)
118 /* On most machines, we are just looking at a register. */
119 if (ps_lgetregs (ta
->ph
, lwpid
, regs
) != PS_OK
)
121 terr
= _td_fetch_value_local (ta
, ta
->ta_howto_data
.reg
, -1,
126 /* In this descriptor the nelem word is overloaded as the bias. */
127 addr
+= (int32_t) DB_DESC_NELEM (ta
->ta_howto_data
.reg
);
128 th
->th_unique
= addr
;
131 case ta_howto_const_thread_area
:
132 /* Some hosts don't have this call and this case won't be used. */
133 # pragma weak ps_get_thread_area
134 if (&ps_get_thread_area
== NULL
)
137 /* A la x86-64, there is a magic index for get_thread_area. */
138 if (ps_get_thread_area (ta
->ph
, lwpid
,
139 ta
->ta_howto_data
.const_thread_area
,
140 &th
->th_unique
) != PS_OK
)
141 return TD_ERR
; /* XXX Other error value? */
144 case ta_howto_reg_thread_area
:
145 if (&ps_get_thread_area
== NULL
)
148 /* A la i386, a register holds the index for get_thread_area. */
149 if (ps_lgetregs (ta
->ph
, lwpid
, regs
) != PS_OK
)
151 terr
= _td_fetch_value_local (ta
, ta
->ta_howto_data
.reg_thread_area
,
155 /* In this descriptor the nelem word is overloaded as scale factor. */
156 if (ps_get_thread_area
158 ((addr
- (psaddr_t
) 0)
159 >> DB_DESC_NELEM (ta
->ta_howto_data
.reg_thread_area
)),
160 &th
->th_unique
) != PS_OK
)
161 return TD_ERR
; /* XXX Other error value? */
165 /* Found it. Now complete the `td_thrhandle_t' object. */
172 td_ta_map_lwp2thr (const td_thragent_t
*ta_arg
,
173 lwpid_t lwpid
, td_thrhandle_t
*th
)
175 td_thragent_t
*const ta
= (td_thragent_t
*) ta_arg
;
177 LOG ("td_ta_map_lwp2thr");
179 /* Test whether the TA parameter is ok. */
183 /* We cannot rely on thread registers and such information at all
184 before __pthread_initialize_minimal has gotten far enough. They
185 sometimes contain garbage that would confuse us, left by the kernel
186 at exec. So if it looks like initialization is incomplete, we only
187 fake a special descriptor for the initial thread. */
190 td_err_e err
= DB_GET_SYMBOL (list
, ta
, __stack_user
);
194 err
= DB_GET_FIELD (list
, ta
, list
, list_t
, next
, 0);
200 if (ps_getpid (ta
->ph
) != lwpid
)
207 return __td_ta_lookup_th_unique (ta_arg
, lwpid
, th
);