td_ta_event_getmsg.c 3.8 KB

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