Back to index

glibc  2.9
td_ta_event_getmsg.c
Go to the documentation of this file.
00001 /* Retrieve event.
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 <stddef.h>
00022 #include <string.h>
00023 
00024 #include "thread_dbP.h"
00025 #include <linuxthreads/internals.h>
00026 
00027 
00028 td_err_e
00029 td_ta_event_getmsg (const td_thragent_t *ta, td_event_msg_t *msg)
00030 {
00031   /* XXX I cannot think of another way but using a static variable.  */
00032   static td_thrhandle_t th;
00033   td_eventbuf_t event;
00034   psaddr_t addr;
00035 
00036   LOG ("td_ta_event_getmsg");
00037 
00038   /* Test whether the TA parameter is ok.  */
00039   if (! ta_ok (ta))
00040     return TD_BADTA;
00041 
00042   /* Get the pointer to the thread descriptor with the last event.  */
00043   if (ps_pdread (ta->ph, ta->pthread_last_event,
00044                &addr, sizeof (void *)) != PS_OK)
00045     return TD_ERR;   /* XXX Other error value?  */
00046 
00047   /* If the pointer is NULL no event occurred.  */
00048   if (addr == 0)
00049     return TD_NOMSG;
00050 
00051   /* Read the even structure from the target.  */
00052   if (ps_pdread (ta->ph,
00053                ((char *) addr
00054                 + offsetof (struct _pthread_descr_struct, p_eventbuf)),
00055                &event, sizeof (td_eventbuf_t)) != PS_OK)
00056     return TD_ERR;   /* XXX Other error value?  */
00057 
00058   /* Check whether an event occurred.  */
00059   if (event.eventnum == TD_EVENT_NONE)
00060     {
00061       /* Oh well, this means the last event was already read.  So
00062         we have to look for any other event.  */
00063       struct pthread_handle_struct handles[ta->pthread_threads_max];
00064       int num;
00065       int i;
00066 
00067       /* Read the number of currently active threads.  */
00068       if (ps_pdread (ta->ph, ta->pthread_handles_num, &num, sizeof (int))
00069          != PS_OK)
00070        return TD_ERR;       /* XXX Other error value?  */
00071 
00072       /* Now read the handles.  */
00073       if (ps_pdread (ta->ph, ta->handles, handles,
00074                    ta->pthread_threads_max * sizeof (handles[0])) != PS_OK)
00075        return TD_ERR;       /* XXX Other error value?  */
00076 
00077       for (i = 0; i < ta->pthread_threads_max && num > 0; ++i)
00078        {
00079          if (handles[i].h_descr == NULL)
00080            /* No entry here.  */
00081            continue;
00082 
00083          /* First count this active thread.  */
00084          --num;
00085 
00086          if (handles[i].h_descr == addr)
00087            /* We already handled this.  */
00088            continue;
00089 
00090          /* Read the event data for this thread.  */
00091          if (ps_pdread (ta->ph,
00092                       ((char *) handles[i].h_descr
00093                        + offsetof (struct _pthread_descr_struct,
00094                                   p_eventbuf)),
00095                       &event, sizeof (td_eventbuf_t)) != PS_OK)
00096            return TD_ERR;
00097 
00098          if (event.eventnum != TD_EVENT_NONE)
00099            {
00100              /* We found a thread with an unreported event.  */
00101              addr = handles[i].h_descr;
00102              break;
00103            }
00104        }
00105 
00106       /* If we haven't found any other event signal this to the user.  */
00107       if (event.eventnum == TD_EVENT_NONE)
00108        return TD_NOMSG;
00109     }
00110 
00111   /* Generate the thread descriptor.  */
00112   th.th_ta_p = (td_thragent_t *) ta;
00113   th.th_unique = addr;
00114 
00115   /* Fill the user's data structure.  */
00116   msg->event = event.eventnum;
00117   msg->th_p = &th;
00118   msg->msg.data = (uintptr_t) event.eventdata;
00119 
00120   /* And clear the event message in the target.  */
00121   memset (&event, '\0', sizeof (td_eventbuf_t));
00122   if (ps_pdwrite (ta->ph,
00123                 ((char *) addr
00124                  + offsetof (struct _pthread_descr_struct, p_eventbuf)),
00125                 &event, sizeof (td_eventbuf_t)) != PS_OK)
00126     return TD_ERR;   /* XXX Other error value?  */
00127 
00128   return TD_OK;
00129 }