1 /* Retrieve event.
2    Copyright (C) 1999-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <stddef.h>
20 #include <string.h>
21 
22 #include "thread_dbP.h"
23 
24 
25 td_err_e
td_ta_event_getmsg(const td_thragent_t * ta_arg,td_event_msg_t * msg)26 td_ta_event_getmsg (const td_thragent_t *ta_arg, td_event_msg_t *msg)
27 {
28   td_thragent_t *const ta = (td_thragent_t *) ta_arg;
29   td_err_e err;
30   psaddr_t eventbuf, eventnum, eventdata;
31   psaddr_t thp, next;
32   void *copy;
33 
34   /* XXX I cannot think of another way but using a static variable.  */
35   /* XXX Use at least __thread once it is possible.  */
36   static td_thrhandle_t th;
37 
38   LOG ("td_thr_event_getmsg");
39 
40   /* Test whether the TA parameter is ok.  */
41   if (! ta_ok (ta))
42     return TD_BADTA;
43 
44   /* Get the pointer to the thread descriptor with the last event.  */
45   err = DB_GET_VALUE (thp, ta, __nptl_last_event, 0);
46   if (err != TD_OK)
47     return err;
48 
49   if (thp == 0)
50     /* Nothing waiting.  */
51     return TD_NOMSG;
52 
53   /* Copy the event message buffer in from the inferior.  */
54   err = DB_GET_FIELD_ADDRESS (eventbuf, ta, thp, pthread, eventbuf, 0);
55   if (err == TD_OK)
56     err = DB_GET_STRUCT (copy, ta, eventbuf, td_eventbuf_t);
57   if (err != TD_OK)
58     return err;
59 
60   /* Read the event details from the target thread.  */
61   err = DB_GET_FIELD_LOCAL (eventnum, ta, copy, td_eventbuf_t, eventnum, 0);
62   if (err != TD_OK)
63     return err;
64   /* If the structure is on the list there better be an event recorded.  */
65   if ((int) (uintptr_t) eventnum == TD_EVENT_NONE)
66     return TD_DBERR;
67 
68   /* Fill the user's data structure.  */
69   err = DB_GET_FIELD_LOCAL (eventdata, ta, copy, td_eventbuf_t, eventdata, 0);
70   if (err != TD_OK)
71     return err;
72 
73   /* Generate the thread descriptor.  */
74   th.th_ta_p = (td_thragent_t *) ta;
75   th.th_unique = thp;
76 
77   /* Fill the user's data structure.  */
78   msg->msg.data = (uintptr_t) eventdata;
79   msg->event = (uintptr_t) eventnum;
80   msg->th_p = &th;
81 
82   /* And clear the event message in the target.  */
83   memset (copy, 0, ta->ta_sizeof_td_eventbuf_t);
84   err = DB_PUT_STRUCT (ta, eventbuf, td_eventbuf_t, copy);
85   if (err != TD_OK)
86     return err;
87 
88   /* Get the pointer to the next descriptor with an event.  */
89   err = DB_GET_FIELD (next, ta, thp, pthread, nextevent, 0);
90   if (err != TD_OK)
91     return err;
92 
93   /* Store the pointer in the list head variable.  */
94   err = DB_PUT_VALUE (ta, __nptl_last_event, 0, next);
95   if (err != TD_OK)
96     return err;
97 
98   if (next != 0)
99     /* Clear the next pointer in the current descriptor.  */
100     err = DB_PUT_FIELD (ta, thp, pthread, nextevent, 0, 0);
101 
102   return err;
103 }
104