(root)/
glibc-2.38/
nptl_db/
thread_dbP.h
       1  /* Private header for thread debug library
       2     Copyright (C) 2003-2023 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  #ifndef _THREAD_DBP_H
      20  #define _THREAD_DBP_H	1
      21  
      22  #include <stdbool.h>
      23  #include <stdint.h>
      24  #include <string.h>
      25  #include <stdlib.h>
      26  #include <unistd.h>
      27  #include <assert.h>
      28  #include "proc_service.h"
      29  #include "thread_db.h"
      30  #include <pthreadP.h>  	/* This is for *_BITMASK only.  */
      31  #include <list.h>
      32  #include <gnu/lib-names.h>
      33  #include <libc-diag.h>
      34  
      35  /* Indices for the symbol names.  */
      36  enum
      37    {
      38  # define DB_STRUCT(type)		SYM_SIZEOF_##type,
      39  # define DB_STRUCT_FIELD(type, field)	SYM_##type##_FIELD_##field,
      40  # define DB_STRUCT_FLEXIBLE_ARRAY(type, field) DB_STRUCT_FIELD (type, field)
      41  # define DB_SYMBOL(name)		SYM_##name,
      42  # define DB_FUNCTION(name)		SYM_##name,
      43  # define DB_VARIABLE(name)		SYM_##name, SYM_DESC_##name,
      44  # include "structs.def"
      45  # undef DB_STRUCT
      46  # undef DB_STRUCT_FIELD
      47  # undef DB_STRUCT_FLEXIBLE_ARRAY
      48  # undef DB_SYMBOL
      49  # undef DB_FUNCTION
      50  # undef DB_VARIABLE
      51  
      52      SYM_TH_UNIQUE_CONST_THREAD_AREA,
      53      SYM_TH_UNIQUE_REGISTER64,
      54      SYM_TH_UNIQUE_REGISTER32,
      55      SYM_TH_UNIQUE_REGISTER64_THREAD_AREA,
      56      SYM_TH_UNIQUE_REGISTER32_THREAD_AREA,
      57  
      58      SYM_NUM_MESSAGES
      59    };
      60  
      61  
      62  /* Comment out the following for less verbose output.  */
      63  #ifndef NDEBUG
      64  # define LOG(c) \
      65    if (__td_debug) \
      66      assert (write (2, c "\n", strlen (c "\n")) == strlen (c "\n"))
      67  extern int __td_debug attribute_hidden;
      68  #else
      69  # define LOG(c)
      70  #endif
      71  
      72  
      73  #define DB_DESC_SIZE(desc)	((desc)[0])
      74  #define DB_DESC_NELEM(desc)	((desc)[1])
      75  #define DB_DESC_OFFSET(desc)	((desc)[2])
      76  #define DB_SIZEOF_DESC		(3 * sizeof (uint32_t))
      77  #define DB_DEFINE_DESC(name, size, nelem, offset) \
      78    const uint32_t name[3] = { (size), (nelem), (offset) }
      79  typedef uint32_t db_desc_t[3];
      80  
      81  
      82  /* Handle for a process.  This type is opaque.  */
      83  struct td_thragent
      84  {
      85    /* Chain on the list of all agent structures.  */
      86    list_t list;
      87  
      88    /* Delivered by the debugger and we have to pass it back in the
      89       proc callbacks.  */
      90    struct ps_prochandle *ph;
      91  
      92    /* Cached values read from the inferior.  */
      93  # define DB_STRUCT(type) \
      94    uint32_t ta_sizeof_##type;
      95  # define DB_STRUCT_FIELD(type, field) \
      96    db_desc_t ta_field_##type##_##field;
      97  # define DB_STRUCT_FLEXIBLE_ARRAY(type, field) DB_STRUCT_FIELD (type, field)
      98  # define DB_SYMBOL(name) \
      99    psaddr_t ta_addr_##name;
     100  # define DB_FUNCTION(name) \
     101    psaddr_t ta_addr_##name;
     102  # define DB_VARIABLE(name) \
     103    psaddr_t ta_addr_##name; \
     104    db_desc_t ta_var_##name;
     105  # include "structs.def"
     106  # undef DB_STRUCT
     107  # undef DB_STRUCT_FIELD
     108  # undef DB_STRUCT_FLEXIBLE_ARRAY
     109  # undef DB_FUNCTION
     110  # undef DB_SYMBOL
     111  # undef DB_VARIABLE
     112  
     113    psaddr_t ta_addr__rtld_global;
     114  
     115    /* The method of locating a thread's th_unique value.  */
     116    enum
     117      {
     118        ta_howto_unknown,
     119        ta_howto_reg,
     120        ta_howto_reg_thread_area,
     121        ta_howto_const_thread_area
     122      } ta_howto;
     123    union
     124    {
     125      uint32_t const_thread_area;	/* Constant argument to ps_get_thread_area.  */
     126      /* These are as if the descriptor of the field in prregset_t,
     127         but DB_DESC_NELEM is overloaded as follows: */
     128      db_desc_t reg;		/* Signed bias applied to register value.  */
     129      db_desc_t reg_thread_area;	/* Bits to scale down register value.  */
     130    } ta_howto_data;
     131  };
     132  
     133  
     134  /* List of all known descriptors.  */
     135  extern list_t __td_agent_list attribute_hidden;
     136  
     137  
     138  /* Function used to test for correct thread agent pointer.  */
     139  static inline bool
     140  ta_ok (const td_thragent_t *ta)
     141  {
     142    list_t *runp;
     143  
     144    list_for_each (runp, &__td_agent_list)
     145      if (list_entry (runp, td_thragent_t, list) == ta)
     146        return true;
     147  
     148    return false;
     149  }
     150  
     151  
     152  /* Internal wrappers around ps_pglobal_lookup.  */
     153  extern ps_err_e td_mod_lookup (struct ps_prochandle *ps, const char *modname,
     154  			       int idx, psaddr_t *sym_addr) attribute_hidden;
     155  #define td_lookup(ps, idx, sym_addr) \
     156    td_mod_lookup ((ps), LIBPTHREAD_SO, (idx), (sym_addr))
     157  
     158  
     159  /* Store in psaddr_t VAR the address of inferior's symbol NAME.  */
     160  #define DB_GET_SYMBOL(var, ta, name)					      \
     161    (((ta)->ta_addr_##name == 0						      \
     162      && td_lookup ((ta)->ph, SYM_##name, &(ta)->ta_addr_##name) != PS_OK)      \
     163     ? TD_ERR : ((var) = (ta)->ta_addr_##name, TD_OK))
     164  
     165  /* Store in psaddr_t VAR the value of ((TYPE) PTR)->FIELD[IDX] in the inferior.
     166     A target field smaller than psaddr_t is zero-extended.  */
     167  #define DB_GET_FIELD(var, ta, ptr, type, field, idx) \
     168    _td_fetch_value ((ta), (ta)->ta_field_##type##_##field, \
     169  		   SYM_##type##_FIELD_##field, \
     170  		   (psaddr_t) 0 + (idx), (ptr), &(var))
     171  
     172  /* With GCC 5.3 when compiling with -Os the compiler emits a warning
     173     that slot may be used uninitialized.  This is never the case since
     174     the dynamic loader initializes the slotinfo list and
     175     dtv_slotinfo_list will point slot at the first entry.  Therefore
     176     when DB_GET_FIELD_ADDRESS is called with a slot for ptr, the slot is
     177     always initialized.  */
     178  DIAG_PUSH_NEEDS_COMMENT;
     179  DIAG_IGNORE_Os_NEEDS_COMMENT (5, "-Wmaybe-uninitialized");
     180  #define DB_GET_FIELD_ADDRESS(var, ta, ptr, type, field, idx) \
     181    ((var) = (ptr), _td_locate_field ((ta), (ta)->ta_field_##type##_##field, \
     182  				    SYM_##type##_FIELD_##field, \
     183  				    (psaddr_t) 0 + (idx), &(var)))
     184  DIAG_POP_NEEDS_COMMENT;
     185  
     186  extern td_err_e _td_locate_field (td_thragent_t *ta,
     187  				  db_desc_t desc, int descriptor_name,
     188  				  psaddr_t idx,
     189  				  psaddr_t *address) attribute_hidden;
     190  
     191  
     192  /* Like DB_GET_FIELD, but PTR is a local pointer to a structure that
     193     has already been copied in from the inferior.  */
     194  #define DB_GET_FIELD_LOCAL(var, ta, ptr, type, field, idx) \
     195    _td_fetch_value_local ((ta), (ta)->ta_field_##type##_##field, \
     196  		         SYM_##type##_FIELD_##field, \
     197  			 (psaddr_t) 0 + (idx), (ptr), &(var))
     198  
     199  /* Store in psaddr_t VAR the value of variable NAME[IDX] in the inferior.
     200     A target value smaller than psaddr_t is zero-extended.  */
     201  #define DB_GET_VALUE(var, ta, name, idx)				      \
     202    (((ta)->ta_addr_##name == 0						      \
     203      && td_lookup ((ta)->ph, SYM_##name, &(ta)->ta_addr_##name) != PS_OK)      \
     204     ? TD_ERR								      \
     205     : _td_fetch_value ((ta), (ta)->ta_var_##name, SYM_DESC_##name, 	      \
     206  		      (psaddr_t) 0 + (idx), (ta)->ta_addr_##name, &(var)))
     207  
     208  /* Helper functions for those.  */
     209  extern td_err_e _td_fetch_value (td_thragent_t *ta,
     210  				 db_desc_t field, int descriptor_name,
     211  				 psaddr_t idx, psaddr_t address,
     212  				 psaddr_t *result) attribute_hidden;
     213  extern td_err_e _td_fetch_value_local (td_thragent_t *ta,
     214  				       db_desc_t field,
     215  				       int descriptor_name,
     216  				       psaddr_t idx, void *address,
     217  				       psaddr_t *result) attribute_hidden;
     218  
     219  /* Store psaddr_t VALUE in ((TYPE) PTR)->FIELD[IDX] in the inferior.
     220     A target field smaller than psaddr_t is zero-extended.  */
     221  #define DB_PUT_FIELD(ta, ptr, type, field, idx, value) \
     222    _td_store_value ((ta), (ta)->ta_field_##type##_##field, \
     223  		   SYM_##type##_FIELD_##field, \
     224  		   (psaddr_t) 0 + (idx), (ptr), (value))
     225  
     226  #define DB_PUT_FIELD_LOCAL(ta, ptr, type, field, idx, value) \
     227    _td_store_value_local ((ta), (ta)->ta_field_##type##_##field, \
     228  			 SYM_##type##_FIELD_##field, \
     229  			 (psaddr_t) 0 + (idx), (ptr), (value))
     230  
     231  /* Store psaddr_t VALUE in variable NAME[IDX] in the inferior.
     232     A target field smaller than psaddr_t is zero-extended.  */
     233  #define DB_PUT_VALUE(ta, name, idx, value)				      \
     234    (((ta)->ta_addr_##name == 0						      \
     235      && td_lookup ((ta)->ph, SYM_##name, &(ta)->ta_addr_##name) != PS_OK)      \
     236     ? TD_ERR								      \
     237     : _td_store_value ((ta), (ta)->ta_var_##name, SYM_DESC_##name, 	      \
     238  		      (psaddr_t) 0 + (idx), (ta)->ta_addr_##name, (value)))
     239  
     240  /* Helper functions for those.  */
     241  extern td_err_e _td_store_value (td_thragent_t *ta,
     242  				 db_desc_t field, int descriptor_name,
     243  				 psaddr_t idx, psaddr_t address,
     244  				 psaddr_t value) attribute_hidden;
     245  extern td_err_e _td_store_value_local (td_thragent_t *ta,
     246  				       db_desc_t field, int descriptor_name,
     247  				       psaddr_t idx, void *address,
     248  				       psaddr_t value) attribute_hidden;
     249  
     250  #define DB_GET_STRUCT(var, ta, ptr, type)				      \
     251    ({ td_err_e _err = TD_OK;						      \
     252       if ((ta)->ta_sizeof_##type == 0)					      \
     253         _err = _td_check_sizeof ((ta), &(ta)->ta_sizeof_##type,		      \
     254  				      SYM_SIZEOF_##type);		      \
     255       if (_err == TD_OK)							      \
     256         _err = ps_pdread ((ta)->ph, (ptr),				      \
     257  			 (var) = __alloca ((ta)->ta_sizeof_##type),	      \
     258  			 (ta)->ta_sizeof_##type)			      \
     259  	 == PS_OK ? TD_OK : TD_ERR;					      \
     260       else								      \
     261         (var) = NULL; 							      \
     262       _err;								      \
     263    })
     264  #define DB_PUT_STRUCT(ta, ptr, type, copy)				      \
     265    ({ assert ((ta)->ta_sizeof_##type != 0);				      \
     266       ps_pdwrite ((ta)->ph, (ptr), (copy), (ta)->ta_sizeof_##type)	      \
     267         == PS_OK ? TD_OK : TD_ERR;					      \
     268    })
     269  
     270  extern td_err_e _td_check_sizeof (td_thragent_t *ta, uint32_t *sizep,
     271  				  int sizep_name) attribute_hidden;
     272  
     273  extern td_err_e __td_ta_lookup_th_unique (const td_thragent_t *ta,
     274  					  lwpid_t lwpid, td_thrhandle_t *th);
     275  
     276  /* Try to initialize TA->ta_addr__rtld_global.  Return true on
     277     success, false on failure (which may be cached).  */
     278  bool __td_ta_rtld_global (td_thragent_t *ta) attribute_hidden;
     279  
     280  /* Obtain the address of the list_t fields _dl_stack_user and
     281     _dl_stack_used in _rtld_global, or fall back to the global
     282     variables of the same name (to support statically linked
     283     programs).  */
     284  td_err_e __td_ta_stack_user (td_thragent_t *ta, psaddr_t *plist)
     285    attribute_hidden;
     286  td_err_e __td_ta_stack_used (td_thragent_t *ta, psaddr_t *plist)
     287    attribute_hidden;
     288  
     289  #endif /* thread_dbP.h */