1 /* Iterate over a process's thread-specific data.
2    Copyright (C) 1999-2022 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, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <stdint.h>
20 #include "thread_dbP.h"
21 #include <alloca.h>
22 
23 td_err_e
td_ta_tsd_iter(const td_thragent_t * ta_arg,td_key_iter_f * callback,void * cbdata_p)24 td_ta_tsd_iter (const td_thragent_t *ta_arg, td_key_iter_f *callback,
25 		void *cbdata_p)
26 {
27   td_thragent_t *const ta = (td_thragent_t *) ta_arg;
28   td_err_e err;
29   void *keys;
30   size_t keys_nb, keys_elemsize;
31   psaddr_t addr;
32   uint32_t idx;
33 
34   LOG ("td_ta_tsd_iter");
35 
36   /* Test whether the TA parameter is ok.  */
37   if (! ta_ok (ta))
38     return TD_BADTA;
39 
40   /* This makes sure we have the size information on hand.  */
41   addr = 0;
42   err = _td_locate_field (ta,
43 			  ta->ta_var___pthread_keys, SYM_DESC___pthread_keys,
44 			  (psaddr_t) 0 + 1, &addr);
45   if (err != TD_OK)
46     return err;
47 
48   /* Now copy in the entire array of key descriptors.  */
49   keys_elemsize = (addr - (psaddr_t) 0) / 8;
50   keys_nb = keys_elemsize * DB_DESC_NELEM (ta->ta_var___pthread_keys);
51   keys = __alloca (keys_nb);
52   err = DB_GET_SYMBOL (addr, ta, __pthread_keys);
53   if (err != TD_OK)
54     return err;
55   if (ps_pdread (ta->ph, addr, keys, keys_nb) != PS_OK)
56     return TD_ERR;
57 
58   /* Now get all descriptors, one after the other.  */
59   for (idx = 0; idx < DB_DESC_NELEM (ta->ta_var___pthread_keys); ++idx)
60     {
61       psaddr_t seq, destr;
62       err = DB_GET_FIELD_LOCAL (seq, ta, keys, pthread_key_struct, seq, 0);
63       if (err != TD_OK)
64 	return err;
65       if (((uintptr_t) seq) & 1)
66 	{
67 	  err = DB_GET_FIELD_LOCAL (destr, ta, keys, pthread_key_struct,
68 				    destr, 0);
69 	  if (err != TD_OK)
70 	    return err;
71 	  /* Return with an error if the callback returns a nonzero value.  */
72 	  if (callback ((thread_key_t) idx, destr, cbdata_p) != 0)
73 	    return TD_DBERR;
74 	}
75       /* Advance to the next element in the copied array.  */
76       keys += keys_elemsize;
77     }
78 
79   return TD_OK;
80 }
81