fix issues with gdb 8.0
[uclibc-ng.git] / libpthread / nptl_db / td_ta_map_lwp2thr.c
blob6b4382fbd74925734cf051d59d4ef0feb219ce73
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.
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"
20 #include <stdlib.h>
21 #include <byteswap.h>
22 #include <sys/procfs.h>
25 td_err_e
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;
30 ps_err_e err;
31 td_err_e terr;
32 prgregset_t regs;
33 psaddr_t addr;
35 LOG ("td_ta_map_lwp2thr");
37 /* Test whether the TA parameter is ok. */
38 if (! ta_ok (ta))
39 return TD_BADTA;
41 if (ta->ta_howto == ta_howto_unknown)
43 /* We need to read in from the inferior the instructions what to do. */
44 psaddr_t howto;
46 err = td_lookup (ta->ph, SYM_TH_UNIQUE_CONST_THREAD_AREA, &howto);
47 if (err == PS_OK)
49 err = ps_pdread (ta->ph, howto,
50 &ta->ta_howto_data.const_thread_area,
51 sizeof ta->ta_howto_data.const_thread_area);
52 if (err != PS_OK)
53 return TD_ERR;
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);
59 else
61 switch (sizeof (regs[0]))
63 case 8:
64 err = td_lookup (ta->ph, SYM_TH_UNIQUE_REGISTER64, &howto);
65 if (err == PS_OK)
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,
71 &howto);
72 if (err == PS_OK)
73 ta->ta_howto = ta_howto_reg_thread_area;
75 break;
77 case 4:
78 err = td_lookup (ta->ph, SYM_TH_UNIQUE_REGISTER32, &howto);
79 if (err == PS_OK)
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,
85 &howto);
86 if (err == PS_OK)
87 ta->ta_howto = ta_howto_reg_thread_area;
89 break;
91 default:
92 abort ();
93 return TD_DBERR;
96 if (err != PS_OK)
97 return TD_DBERR;
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);
102 if (err != PS_OK)
103 return TD_ERR;
104 if (DB_DESC_SIZE (ta->ta_howto_data.reg) == 0)
105 return TD_DBERR;
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 default:
121 return TD_DBERR;
123 case ta_howto_reg:
124 /* On most machines, we are just looking at a register. */
125 if (ps_lgetregs (ta->ph, lwpid, regs) != PS_OK)
126 return TD_ERR;
127 terr = _td_fetch_value_local (ta, ta->ta_howto_data.reg, -1,
128 0, regs, &addr);
129 if (terr != TD_OK)
130 return terr;
132 /* In this descriptor the nelem word is overloaded as the bias. */
133 addr += (int32_t) DB_DESC_NELEM (ta->ta_howto_data.reg);
134 th->th_unique = addr;
135 break;
137 case ta_howto_const_thread_area:
138 /* Some hosts don't have this call and this case won't be used. */
139 # pragma weak ps_get_thread_area
140 if (&ps_get_thread_area == NULL)
141 return TD_NOCAPAB;
143 /* A la x86-64, there is a magic index for get_thread_area. */
144 if (ps_get_thread_area (ta->ph, lwpid,
145 ta->ta_howto_data.const_thread_area,
146 &th->th_unique) != PS_OK)
147 return TD_ERR; /* XXX Other error value? */
148 break;
150 case ta_howto_reg_thread_area:
151 if (&ps_get_thread_area == NULL)
152 return TD_NOCAPAB;
154 /* A la i386, a register holds the index for get_thread_area. */
155 if (ps_lgetregs (ta->ph, lwpid, regs) != PS_OK)
156 return TD_ERR;
157 terr = _td_fetch_value_local (ta, ta->ta_howto_data.reg_thread_area,
158 -1, 0, regs, &addr);
159 if (terr != TD_OK)
160 return terr;
161 /* In this descriptor the nelem word is overloaded as scale factor. */
162 if (ps_get_thread_area
163 (ta->ph, lwpid,
164 ((addr - (psaddr_t) 0)
165 >> DB_DESC_NELEM (ta->ta_howto_data.reg_thread_area)),
166 &th->th_unique) != PS_OK)
167 return TD_ERR; /* XXX Other error value? */
168 break;
171 /* Found it. Now complete the `td_thrhandle_t' object. */
172 th->th_ta_p = ta;
174 return TD_OK;
177 td_err_e
178 td_ta_map_lwp2thr (const td_thragent_t *ta_arg,
179 lwpid_t lwpid, td_thrhandle_t *th)
181 td_thragent_t *const ta = (td_thragent_t *) ta_arg;
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. */
189 psaddr_t list;
190 td_err_e err = DB_GET_SYMBOL (list, ta, __stack_user);
191 if (err != TD_OK)
192 return err;
194 err = DB_GET_FIELD (list, ta, list, list_t, next, 0);
195 if (err != TD_OK)
196 return err;
198 if (list == 0)
200 if (ps_getpid (ta->ph) != lwpid)
201 return TD_ERR;
202 th->th_ta_p = ta;
203 th->th_unique = 0;
204 return TD_OK;
207 return __td_ta_lookup_th_unique (ta_arg, lwpid, th);