Back to index

glibc  2.9
td_ta_map_lwp2thr.c
Go to the documentation of this file.
00001 /* Which thread is running on an lwp?
00002    Copyright (C) 1999, 2001, 2002 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 
00024 
00025 td_err_e
00026 td_ta_map_lwp2thr (const td_thragent_t *ta, lwpid_t lwpid, td_thrhandle_t *th)
00027 {
00028   int pthread_threads_max = ta->pthread_threads_max;
00029   size_t sizeof_descr = ta->sizeof_descr;
00030   struct pthread_handle_struct phc[pthread_threads_max];
00031   size_t cnt;
00032 #ifdef ALL_THREADS_STOPPED
00033   int num;
00034 #else
00035 # define num 1
00036 #endif
00037 
00038   LOG ("td_ta_map_lwp2thr");
00039 
00040   /* Test whether the TA parameter is ok.  */
00041   if (! ta_ok (ta))
00042     return TD_BADTA;
00043 
00044   /* Read all the descriptors.  */
00045   if (ps_pdread (ta->ph, ta->handles, phc,
00046                sizeof (struct pthread_handle_struct) * pthread_threads_max)
00047       != PS_OK)
00048     return TD_ERR;   /* XXX Other error value?  */
00049 
00050 #ifdef ALL_THREADS_STOPPED
00051   /* Read the number of currently active threads.  */
00052   if (ps_pdread (ta->ph, ta->pthread_handles_num, &num, sizeof (int)) != PS_OK)
00053     return TD_ERR;   /* XXX Other error value?  */
00054 #endif
00055 
00056   /* Get the entries one after the other and find out whether the ID
00057      matches.  */
00058   for (cnt = 0; cnt < pthread_threads_max && num > 0; ++cnt)
00059     if (phc[cnt].h_descr != NULL)
00060       {
00061        struct _pthread_descr_struct pds;
00062 
00063 #ifdef ALL_THREADS_STOPPED
00064        /* First count this active thread.  */
00065        --num;
00066 #endif
00067 
00068        if (ps_pdread (ta->ph, phc[cnt].h_descr, &pds, sizeof_descr) != PS_OK)
00069          return TD_ERR;     /* XXX Other error value?  */
00070 
00071        if ((pds.p_pid ?: ps_getpid (ta->ph)) == lwpid)
00072          {
00073            /* Found it.  Now fill in the `td_thrhandle_t' object.  */
00074            th->th_ta_p = (td_thragent_t *) ta;
00075            th->th_unique = phc[cnt].h_descr;
00076 
00077            return TD_OK;
00078          }
00079       }
00080     else if (cnt == 0)
00081       {
00082        /* The initial thread always exists.  But it might not yet be
00083           initialized.  Construct a value.  */
00084        th->th_ta_p = (td_thragent_t *) ta;
00085        th->th_unique = NULL;
00086 
00087        return TD_OK;
00088       }
00089 
00090   return TD_NOLWP;
00091 }