* td_ta_event_getmsg.c (td_ta_event_getmsg): Write the NEXT pointer
[platform/upstream/glibc.git] / nptl_db / td_ta_event_getmsg.c
1 /* Retrieve event.
2    Copyright (C) 1999, 2001, 2002 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@redhat.com>, 1999.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #include <stddef.h>
22 #include <string.h>
23
24 #include "thread_dbP.h"
25
26
27 td_err_e
28 td_ta_event_getmsg (const td_thragent_t *ta, td_event_msg_t *msg)
29 {
30   /* XXX I cannot think of another way but using a static variable.  */
31   /* XXX Use at least __thread once it is possible.  */
32   static td_thrhandle_t th;
33
34   LOG ("td_ta_event_getmsg");
35
36   /* Test whether the TA parameter is ok.  */
37   if (! ta_ok (ta))
38     return TD_BADTA;
39
40   /* Get the pointer to the thread descriptor with the last event.  */
41   psaddr_t addr;
42   if (ps_pdread (ta->ph, ta->pthread_last_event,
43                  &addr, sizeof (struct pthread *)) != PS_OK)
44     return TD_ERR;      /* XXX Other error value?  */
45
46   if (addr == 0)
47     /* Nothing waiting.  */
48     return TD_NOMSG;
49
50   /* Read the event structure from the target.  */
51   td_eventbuf_t event;
52   if (ps_pdread (ta->ph, (char *) addr + offsetof (struct pthread, eventbuf),
53                  &event, sizeof (td_eventbuf_t)) != PS_OK)
54     return TD_ERR;      /* XXX Other error value?  */
55
56   /* If the structure is on the list there better be an event recorded.  */
57   assert (event.eventnum != TD_EVENT_NONE);
58
59   /* Generate the thread descriptor.  */
60   th.th_ta_p = (td_thragent_t *) ta;
61   th.th_unique = addr;
62
63   /* Fill the user's data structure.  */
64   msg->event = event.eventnum;
65   msg->th_p = &th;
66   msg->msg.data = (uintptr_t) event.eventdata;
67
68   /* Get the pointer to the next descriptor with an event.  */
69   psaddr_t next;
70   if (ps_pdread (ta->ph, (char *) addr + offsetof (struct pthread, nextevent),
71                  &next, sizeof (struct pthread *)) != PS_OK)
72     return TD_ERR;      /* XXX Other error value?  */
73
74   /* Store the pointer in the list head variable.  */
75   if (ps_pdwrite (ta->ph, ta->pthread_last_event,
76                   &next, sizeof (struct pthread *)) != PS_OK)
77     return TD_ERR;      /* XXX Other error value?  */
78
79   /* Clear the next pointer in the current descriptor.  */
80   next = NULL;
81   if (ps_pdwrite (ta->ph, (char *) addr + offsetof (struct pthread, nextevent),
82                   &next, sizeof (struct pthread *)) != PS_OK)
83     return TD_ERR;      /* XXX Other error value?  */
84
85   return TD_OK;
86 }