td_ta_event_getmsg.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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@cygnus.com>, 1999.
  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. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #include <stddef.h>
  18. #include <string.h>
  19. #include "thread_dbP.h"
  20. #include "../linuxthreads/internals.h"
  21. td_err_e
  22. td_ta_event_getmsg (const td_thragent_t *ta, td_event_msg_t *msg)
  23. {
  24. /* XXX I cannot think of another way but using a static variable. */
  25. static td_thrhandle_t th;
  26. td_eventbuf_t event;
  27. psaddr_t addr;
  28. LOG ("td_ta_event_getmsg");
  29. /* Test whether the TA parameter is ok. */
  30. if (! ta_ok (ta))
  31. return TD_BADTA;
  32. /* Get the pointer to the thread descriptor with the last event. */
  33. if (ps_pdread (ta->ph, ta->pthread_last_event,
  34. &addr, sizeof (void *)) != PS_OK)
  35. return TD_ERR; /* XXX Other error value? */
  36. /* If the pointer is NULL no event occurred. */
  37. if (addr == 0)
  38. return TD_NOMSG;
  39. /* Read the even structure from the target. */
  40. if (ps_pdread (ta->ph,
  41. ((char *) addr
  42. + offsetof (struct _pthread_descr_struct, p_eventbuf)),
  43. &event, sizeof (td_eventbuf_t)) != PS_OK)
  44. return TD_ERR; /* XXX Other error value? */
  45. /* Check whether an event occurred. */
  46. if (event.eventnum == TD_EVENT_NONE)
  47. {
  48. /* Oh well, this means the last event was already read. So
  49. we have to look for any other event. */
  50. struct pthread_handle_struct handles[ta->pthread_threads_max];
  51. int num;
  52. int i;
  53. /* Read the number of currently active threads. */
  54. if (ps_pdread (ta->ph, ta->pthread_handles_num, &num, sizeof (int))
  55. != PS_OK)
  56. return TD_ERR; /* XXX Other error value? */
  57. /* Now read the handles. */
  58. if (ps_pdread (ta->ph, ta->handles, handles,
  59. ta->pthread_threads_max * sizeof (handles[0])) != PS_OK)
  60. return TD_ERR; /* XXX Other error value? */
  61. for (i = 0; i < ta->pthread_threads_max && num > 0; ++i)
  62. {
  63. if (handles[i].h_descr == NULL)
  64. /* No entry here. */
  65. continue;
  66. /* First count this active thread. */
  67. --num;
  68. if (handles[i].h_descr == addr)
  69. /* We already handled this. */
  70. continue;
  71. /* Read the event data for this thread. */
  72. if (ps_pdread (ta->ph,
  73. ((char *) handles[i].h_descr
  74. + offsetof (struct _pthread_descr_struct,
  75. p_eventbuf)),
  76. &event, sizeof (td_eventbuf_t)) != PS_OK)
  77. return TD_ERR;
  78. if (event.eventnum != TD_EVENT_NONE)
  79. {
  80. /* We found a thread with an unreported event. */
  81. addr = handles[i].h_descr;
  82. break;
  83. }
  84. }
  85. /* If we haven't found any other event signal this to the user. */
  86. if (event.eventnum == TD_EVENT_NONE)
  87. return TD_NOMSG;
  88. }
  89. /* Generate the thread descriptor. */
  90. th.th_ta_p = (td_thragent_t *) ta;
  91. th.th_unique = addr;
  92. /* Fill the user's data structure. */
  93. msg->event = event.eventnum;
  94. msg->th_p = &th;
  95. msg->msg.data = (uintptr_t) event.eventdata;
  96. /* And clear the event message in the target. */
  97. memset (&event, '\0', sizeof (td_eventbuf_t));
  98. if (ps_pdwrite (ta->ph,
  99. ((char *) addr
  100. + offsetof (struct _pthread_descr_struct, p_eventbuf)),
  101. &event, sizeof (td_eventbuf_t)) != PS_OK)
  102. return TD_ERR; /* XXX Other error value? */
  103. return TD_OK;
  104. }