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