Back to index

glibc  2.9
thread_dbP.h
Go to the documentation of this file.
00001 /* Private header for thread debug library
00002    Copyright (C) 2003, 2004, 2007 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #ifndef _THREAD_DBP_H
00021 #define _THREAD_DBP_H       1
00022 
00023 #include <stdbool.h>
00024 #include <stdint.h>
00025 #include <string.h>
00026 #include <stdlib.h>
00027 #include <unistd.h>
00028 #include <assert.h>
00029 #include "proc_service.h"
00030 #include "thread_db.h"
00031 #include "../nptl/pthreadP.h"      /* This is for *_BITMASK only.  */
00032 
00033 /* Indeces for the symbol names.  */
00034 enum
00035   {
00036 # define DB_STRUCT(type)           SYM_SIZEOF_##type,
00037 # define DB_STRUCT_FIELD(type, field)     SYM_##type##_FIELD_##field,
00038 # define DB_SYMBOL(name)           SYM_##name,
00039 # define DB_FUNCTION(name)         SYM_##name,
00040 # define DB_VARIABLE(name)         SYM_##name, SYM_DESC_##name,
00041 # include "structs.def"
00042 # undef DB_STRUCT
00043 # undef DB_STRUCT_FIELD
00044 # undef DB_SYMBOL
00045 # undef DB_FUNCTION
00046 # undef DB_VARIABLE
00047 
00048     SYM_TH_UNIQUE_CONST_THREAD_AREA,
00049     SYM_TH_UNIQUE_REGISTER64,
00050     SYM_TH_UNIQUE_REGISTER32,
00051     SYM_TH_UNIQUE_REGISTER64_THREAD_AREA,
00052     SYM_TH_UNIQUE_REGISTER32_THREAD_AREA,
00053 
00054     SYM_NUM_MESSAGES
00055   };
00056 
00057 
00058 /* Comment out the following for less verbose output.  */
00059 #ifndef NDEBUG
00060 # define LOG(c) if (__td_debug) write (2, c "\n", strlen (c "\n"))
00061 extern int __td_debug attribute_hidden;
00062 #else
00063 # define LOG(c)
00064 #endif
00065 
00066 
00067 #define DB_DESC_SIZE(desc)  ((desc)[0])
00068 #define DB_DESC_NELEM(desc) ((desc)[1])
00069 #define DB_DESC_OFFSET(desc)       ((desc)[2])
00070 #define DB_SIZEOF_DESC             (3 * sizeof (uint32_t))
00071 #define DB_DEFINE_DESC(name, size, nelem, offset) \
00072   const uint32_t name[3] = { (size), (nelem), (offset) }
00073 typedef uint32_t db_desc_t[3];
00074 
00075 
00076 /* Handle for a process.  This type is opaque.  */
00077 struct td_thragent
00078 {
00079   /* Chain on the list of all agent structures.  */
00080   list_t list;
00081 
00082   /* Delivered by the debugger and we have to pass it back in the
00083      proc callbacks.  */
00084   struct ps_prochandle *ph;
00085 
00086   /* Cached values read from the inferior.  */
00087 # define DB_STRUCT(type) \
00088   uint32_t ta_sizeof_##type;
00089 # define DB_STRUCT_FIELD(type, field) \
00090   db_desc_t ta_field_##type##_##field;
00091 # define DB_SYMBOL(name) \
00092   psaddr_t ta_addr_##name;
00093 # define DB_FUNCTION(name) \
00094   psaddr_t ta_addr_##name;
00095 # define DB_VARIABLE(name) \
00096   psaddr_t ta_addr_##name; \
00097   db_desc_t ta_var_##name;
00098 # include "structs.def"
00099 # undef DB_STRUCT
00100 # undef DB_STRUCT_FIELD
00101 # undef DB_FUNCTION
00102 # undef DB_SYMBOL
00103 # undef DB_VARIABLE
00104 
00105   /* The method of locating a thread's th_unique value.  */
00106   enum
00107     {
00108       ta_howto_unknown,
00109       ta_howto_reg,
00110       ta_howto_reg_thread_area,
00111       ta_howto_const_thread_area
00112     } ta_howto;
00113   union
00114   {
00115     uint32_t const_thread_area;    /* Constant argument to ps_get_thread_area.  */
00116     /* These are as if the descriptor of the field in prregset_t,
00117        but DB_DESC_NELEM is overloaded as follows: */
00118     db_desc_t reg;          /* Signed bias applied to register value.  */
00119     db_desc_t reg_thread_area;     /* Bits to scale down register value.  */
00120   } ta_howto_data;
00121 };
00122 
00123 
00124 /* List of all known descriptors.  */
00125 extern list_t __td_agent_list attribute_hidden;
00126 
00127 
00128 /* Function used to test for correct thread agent pointer.  */
00129 static inline bool
00130 ta_ok (const td_thragent_t *ta)
00131 {
00132   list_t *runp;
00133 
00134   list_for_each (runp, &__td_agent_list)
00135     if (list_entry (runp, td_thragent_t, list) == ta)
00136       return true;
00137 
00138   return false;
00139 }
00140 
00141 
00142 /* Internal wrapper around ps_pglobal_lookup.  */
00143 extern ps_err_e td_lookup (struct ps_prochandle *ps,
00144                         int idx, psaddr_t *sym_addr) attribute_hidden;
00145 
00146 
00147 
00148 
00149 /* Store in psaddr_t VAR the address of inferior's symbol NAME.  */
00150 #define DB_GET_SYMBOL(var, ta, name)                                        \
00151   (((ta)->ta_addr_##name == 0                                               \
00152     && td_lookup ((ta)->ph, SYM_##name, &(ta)->ta_addr_##name) != PS_OK)      \
00153    ? TD_ERR : ((var) = (ta)->ta_addr_##name, TD_OK))
00154 
00155 /* Store in psaddr_t VAR the value of ((TYPE) PTR)->FIELD[IDX] in the inferior.
00156    A target field smaller than psaddr_t is zero-extended.  */
00157 #define DB_GET_FIELD(var, ta, ptr, type, field, idx) \
00158   _td_fetch_value ((ta), (ta)->ta_field_##type##_##field, \
00159                  SYM_##type##_FIELD_##field, \
00160                  (psaddr_t) 0 + (idx), (ptr), &(var))
00161 
00162 #define DB_GET_FIELD_ADDRESS(var, ta, ptr, type, field, idx) \
00163   ((var) = (ptr), _td_locate_field ((ta), (ta)->ta_field_##type##_##field, \
00164                                 SYM_##type##_FIELD_##field, \
00165                                 (psaddr_t) 0 + (idx), &(var)))
00166 
00167 extern td_err_e _td_locate_field (td_thragent_t *ta,
00168                               db_desc_t desc, int descriptor_name,
00169                               psaddr_t idx,
00170                               psaddr_t *address) attribute_hidden;
00171 
00172 
00173 /* Like DB_GET_FIELD, but PTR is a local pointer to a structure that
00174    has already been copied in from the inferior.  */
00175 #define DB_GET_FIELD_LOCAL(var, ta, ptr, type, field, idx) \
00176   _td_fetch_value_local ((ta), (ta)->ta_field_##type##_##field, \
00177                        SYM_##type##_FIELD_##field, \
00178                       (psaddr_t) 0 + (idx), (ptr), &(var))
00179 
00180 /* Store in psaddr_t VAR the value of variable NAME[IDX] in the inferior.
00181    A target value smaller than psaddr_t is zero-extended.  */
00182 #define DB_GET_VALUE(var, ta, name, idx)                             \
00183   (((ta)->ta_addr_##name == 0                                               \
00184     && td_lookup ((ta)->ph, SYM_##name, &(ta)->ta_addr_##name) != PS_OK)      \
00185    ? TD_ERR                                                          \
00186    : _td_fetch_value ((ta), (ta)->ta_var_##name, SYM_DESC_##name,           \
00187                     (psaddr_t) 0 + (idx), (ta)->ta_addr_##name, &(var)))
00188 
00189 /* Helper functions for those.  */
00190 extern td_err_e _td_fetch_value (td_thragent_t *ta,
00191                              db_desc_t field, int descriptor_name,
00192                              psaddr_t idx, psaddr_t address,
00193                              psaddr_t *result) attribute_hidden;
00194 extern td_err_e _td_fetch_value_local (td_thragent_t *ta,
00195                                    db_desc_t field,
00196                                    int descriptor_name,
00197                                    psaddr_t idx, void *address,
00198                                    psaddr_t *result) attribute_hidden;
00199 
00200 /* Store psaddr_t VALUE in ((TYPE) PTR)->FIELD[IDX] in the inferior.
00201    A target field smaller than psaddr_t is zero-extended.  */
00202 #define DB_PUT_FIELD(ta, ptr, type, field, idx, value) \
00203   _td_store_value ((ta), (ta)->ta_field_##type##_##field, \
00204                  SYM_##type##_FIELD_##field, \
00205                  (psaddr_t) 0 + (idx), (ptr), (value))
00206 
00207 #define DB_PUT_FIELD_LOCAL(ta, ptr, type, field, idx, value) \
00208   _td_store_value_local ((ta), (ta)->ta_field_##type##_##field, \
00209                       SYM_##type##_FIELD_##field, \
00210                       (psaddr_t) 0 + (idx), (ptr), (value))
00211 
00212 /* Store psaddr_t VALUE in variable NAME[IDX] in the inferior.
00213    A target field smaller than psaddr_t is zero-extended.  */
00214 #define DB_PUT_VALUE(ta, name, idx, value)                                  \
00215   (((ta)->ta_addr_##name == 0                                               \
00216     && td_lookup ((ta)->ph, SYM_##name, &(ta)->ta_addr_##name) != PS_OK)      \
00217    ? TD_ERR                                                          \
00218    : _td_store_value ((ta), (ta)->ta_var_##name, SYM_DESC_##name,           \
00219                     (psaddr_t) 0 + (idx), (ta)->ta_addr_##name, (value)))
00220 
00221 /* Helper functions for those.  */
00222 extern td_err_e _td_store_value (td_thragent_t *ta,
00223                              db_desc_t field, int descriptor_name,
00224                              psaddr_t idx, psaddr_t address,
00225                              psaddr_t value) attribute_hidden;
00226 extern td_err_e _td_store_value_local (td_thragent_t *ta,
00227                                    db_desc_t field, int descriptor_name,
00228                                    psaddr_t idx, void *address,
00229                                    psaddr_t value) attribute_hidden;
00230 
00231 #define DB_GET_STRUCT(var, ta, ptr, type)                            \
00232   ({ td_err_e _err = TD_OK;                                          \
00233      if ((ta)->ta_sizeof_##type == 0)                                       \
00234        _err = _td_check_sizeof ((ta), &(ta)->ta_sizeof_##type,              \
00235                                   SYM_SIZEOF_##type);                \
00236      if (_err == TD_OK)                                                     \
00237        _err = ps_pdread ((ta)->ph, (ptr),                            \
00238                       (var) = __alloca ((ta)->ta_sizeof_##type),            \
00239                       (ta)->ta_sizeof_##type)                        \
00240         == PS_OK ? TD_OK : TD_ERR;                                   \
00241      else                                                            \
00242        (var) = NULL;                                                        \
00243      _err;                                                           \
00244   })
00245 #define DB_PUT_STRUCT(ta, ptr, type, copy)                                  \
00246   ({ assert ((ta)->ta_sizeof_##type != 0);                                  \
00247      ps_pdwrite ((ta)->ph, (ptr), (copy), (ta)->ta_sizeof_##type)           \
00248        == PS_OK ? TD_OK : TD_ERR;                                    \
00249   })
00250 
00251 extern td_err_e _td_check_sizeof (td_thragent_t *ta, uint32_t *sizep,
00252                               int sizep_name) attribute_hidden;
00253 
00254 extern td_err_e __td_ta_lookup_th_unique (const td_thragent_t *ta,
00255                                      lwpid_t lwpid, td_thrhandle_t *th);
00256 
00257 #endif /* thread_dbP.h */