td_ta_event_getmsg.c 3.9 KB

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