Back to index

glibc  2.9
td_ta_thr_iter.c
Go to the documentation of this file.
00001 /* Iterate over a process's threads.
00002    Copyright (C) 1999,2000,2001,2002,2003 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1999.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include "thread_dbP.h"
00022 #include <linuxthreads/internals.h>
00023 #include <alloca.h>
00024 
00025 static int
00026 handle_descr (const td_thragent_t *ta, td_thr_iter_f *callback,
00027              void *cbdata_p, td_thr_state_e state, int ti_pri,
00028              size_t cnt, pthread_descr descr)
00029 {
00030   struct _pthread_descr_struct pds;
00031   size_t sizeof_descr = ta->sizeof_descr;
00032   td_thrhandle_t th;
00033 
00034   if (descr == NULL)
00035     {
00036       /* No descriptor (yet).  */
00037       if (cnt == 0)
00038        {
00039          /* This is the main thread.  Create a fake descriptor.  */
00040          memset (&pds, '\0', sizeof (pds));
00041 
00042          /* Empty thread descriptor the thread library would create.  */
00043 #if !defined USE_TLS || !TLS_DTV_AT_TP
00044          pds.p_header.data.self = &pds;
00045 #endif
00046          pds.p_nextlive = pds.p_prevlive = &pds;
00047          pds.p_tid = PTHREAD_THREADS_MAX;
00048          /* The init code also sets up p_lock, p_errnop, p_herrnop, and
00049             p_userstack but this should not be necessary here.  */
00050 
00051          th.th_ta_p = (td_thragent_t *) ta;
00052          th.th_unique = NULL;
00053          if (callback (&th, cbdata_p) != 0)
00054            return TD_DBERR;
00055 
00056          /* All done successfully.  */
00057          return TD_OK;
00058        }
00059       else if (cnt == 1)
00060        /* The manager is not yet started.  No big deal.  */
00061        return TD_OK;
00062       else
00063        /* For every other thread this should not happen.  */
00064        return TD_ERR;
00065     }
00066 
00067   if (ps_pdread (ta->ph, descr, &pds, sizeof_descr) != PS_OK)
00068     return TD_ERR;   /* XXX Other error value?  */
00069 
00070   /* The manager thread must be handled special.  The descriptor
00071      exists but the thread only gets created when the first
00072      `pthread_create' call is issued.  A clear indication that this
00073      happened is when the p_pid field is non-zero.  */
00074   if (cnt == 1 && pds.p_pid == 0)
00075     return TD_OK;
00076 
00077   /* Now test whether this thread matches the specified
00078      conditions.  */
00079 
00080   /* Only if the priority level is as high or higher.  */
00081   if (pds.p_priority < ti_pri)
00082     return TD_OK;
00083 
00084   /* Test the state.
00085      XXX This is incomplete.  */
00086   if (state != TD_THR_ANY_STATE)
00087     return TD_OK;
00088 
00089   /* XXX For now we ignore threads which are not running anymore.
00090      The reason is that gdb tries to get the registers and fails.
00091      In future we should have a special mode of the thread library
00092      in which we keep the process around until the actual join
00093      operation happened.  */
00094   if (pds.p_exited != 0)
00095     return TD_OK;
00096 
00097   /* Yep, it matches.  Call the callback function.  */
00098   th.th_ta_p = (td_thragent_t *) ta;
00099   th.th_unique = descr;
00100   if (callback (&th, cbdata_p) != 0)
00101     return TD_DBERR;
00102 
00103   /* All done successfully.  */
00104   return TD_OK;
00105 }
00106 
00107 
00108 td_err_e
00109 td_ta_thr_iter (const td_thragent_t *ta, td_thr_iter_f *callback,
00110               void *cbdata_p, td_thr_state_e state, int ti_pri,
00111               sigset_t *ti_sigmask_p, unsigned int ti_user_flags)
00112 {
00113   int pthread_threads_max;
00114   struct pthread_handle_struct *phc;
00115   td_err_e result = TD_OK;
00116   int cnt;
00117 #ifdef ALL_THREADS_STOPPED
00118   int num;
00119 #else
00120 # define num 1
00121 #endif
00122 
00123   LOG ("td_ta_thr_iter");
00124 
00125   /* Test whether the TA parameter is ok.  */
00126   if (! ta_ok (ta))
00127     return TD_BADTA;
00128 
00129   pthread_threads_max = ta->pthread_threads_max;
00130   phc = (struct pthread_handle_struct *) alloca (sizeof (phc[0])
00131                                            * pthread_threads_max);
00132 
00133   /* First read only the main thread and manager thread information.  */
00134   if (ps_pdread (ta->ph, ta->handles, phc,
00135                sizeof (struct pthread_handle_struct) * 2) != PS_OK)
00136     return TD_ERR;   /* XXX Other error value?  */
00137 
00138   /* Now handle these descriptors.  */
00139   result = handle_descr (ta, callback, cbdata_p, state, ti_pri, 0,
00140                       phc[0].h_descr);
00141   if (result != TD_OK)
00142     return result;
00143   result = handle_descr (ta, callback, cbdata_p, state, ti_pri, 1,
00144                       phc[1].h_descr);
00145   if (result != TD_OK)
00146     return result;
00147 
00148   /* Read all the descriptors.  */
00149   if (ps_pdread (ta->ph, ta->handles + 2, &phc[2],
00150                (sizeof (struct pthread_handle_struct)
00151                 * (pthread_threads_max - 2))) != PS_OK)
00152     return TD_ERR;   /* XXX Other error value?  */
00153 
00154 #ifdef ALL_THREADS_STOPPED
00155   /* Read the number of currently active threads.  */
00156   if (ps_pdread (ta->ph, ta->pthread_handles_num, &num, sizeof (int)) != PS_OK)
00157     return TD_ERR;   /* XXX Other error value?  */
00158 #endif
00159 
00160   /* Now get all descriptors, one after the other.  */
00161   for (cnt = 2; cnt < pthread_threads_max && num > 0; ++cnt)
00162     if (phc[cnt].h_descr != NULL)
00163       {
00164 #ifdef ALL_THREADS_STOPPED
00165        /* First count this active thread.  */
00166        --num;
00167 #endif
00168 
00169        result = handle_descr (ta, callback, cbdata_p, state, ti_pri, cnt,
00170                             phc[cnt].h_descr);
00171        if (result != TD_OK)
00172          break;
00173       }
00174 
00175   return result;
00176 }