report-wait.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* Report on what a thread in our task is waiting for.
  2. Copyright (C) 1996-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <hurd.h>
  16. #include <hurd/signal.h>
  17. #include <hurd/fd.h>
  18. #include <string.h>
  19. #include <assert.h>
  20. #include <hurd/msg_server.h>
  21. #include <thread_state.h>
  22. #include <intr-msg.h>
  23. static char *
  24. describe_number (char *description, const char *flavor, long int i, size_t size)
  25. {
  26. unsigned long int j;
  27. char *limit = description + size;
  28. char *p = flavor == NULL ? description : __stpncpy (description, flavor, size);
  29. char *end;
  30. if (p == limit)
  31. return p;
  32. /* Handle sign. */
  33. if (i < 0)
  34. {
  35. i = -i;
  36. *p++ = '-';
  37. }
  38. if (p == limit)
  39. return p;
  40. /* Allocate space for the number at the end of DESCRIPTION. */
  41. for (j = i; j >= 10; j /= 10)
  42. p++;
  43. end = p + 1;
  44. if (end < limit)
  45. *end = '\0';
  46. else
  47. end = limit;
  48. do
  49. {
  50. if (p < limit)
  51. *p = '0' + i % 10;
  52. p--;
  53. i /= 10;
  54. } while (i != 0);
  55. return end;
  56. }
  57. static char *
  58. describe_port (char *description, mach_port_t port, size_t size)
  59. {
  60. int i;
  61. if (port == MACH_PORT_NULL)
  62. return __stpncpy (description, "(null)", size);
  63. if (port == MACH_PORT_DEAD)
  64. return __stpncpy (description, "(dead)", size);
  65. if (port == __mach_task_self ())
  66. return __stpncpy (description, "task-self", size);
  67. for (i = 0; i < _hurd_nports; ++i)
  68. if (port == _hurd_ports[i].port)
  69. return describe_number (description, "init#", i, size);
  70. if (_hurd_init_dtable)
  71. {
  72. for (i = 0; i < _hurd_init_dtablesize; ++i)
  73. if (port == _hurd_init_dtable[i])
  74. return describe_number (description, "fd#", i, size);
  75. }
  76. if (_hurd_dtable)
  77. {
  78. for (i = 0; i < _hurd_dtablesize; ++i)
  79. if (_hurd_dtable[i] == NULL)
  80. continue;
  81. else if (port == _hurd_dtable[i]->port.port)
  82. return describe_number (description, "fd#", i, size);
  83. else if (port == _hurd_dtable[i]->ctty.port)
  84. return describe_number (description, "bgfd#", i, size);
  85. }
  86. return describe_number (description, "port#", port, size);
  87. }
  88. /* We want _HURD_ITIMER_THREAD, but don't want to link in the itimer code
  89. unnecessarily. */
  90. extern thread_t _hurd_itimer_thread; /* XXX */
  91. weak_extern (_hurd_itimer_thread)
  92. kern_return_t
  93. _S_msg_report_wait (mach_port_t msgport, thread_t thread,
  94. string_t description, mach_msg_id_t *msgid)
  95. {
  96. char *limit = description + 1024; /* XXX */
  97. char *cur = description;
  98. *msgid = 0;
  99. if (thread == _hurd_msgport_thread)
  100. /* Cute. */
  101. cur = __stpncpy (cur, "msgport", limit - cur);
  102. else if (&_hurd_itimer_thread && thread == _hurd_itimer_thread)
  103. cur = __stpncpy (cur, "itimer", limit - cur);
  104. else
  105. {
  106. /* Make sure this is really one of our threads. */
  107. struct hurd_sigstate *ss;
  108. __mutex_lock (&_hurd_siglock);
  109. for (ss = _hurd_sigstates; ss != NULL; ss = ss->next)
  110. if (ss->thread == thread)
  111. break;
  112. __mutex_unlock (&_hurd_siglock);
  113. if (ss == NULL)
  114. /* To hell with you. */
  115. return EINVAL;
  116. if (ss->suspended != MACH_PORT_NULL)
  117. cur = __stpncpy (cur, "sigsuspend", limit - cur);
  118. else
  119. {
  120. /* Examine the thread's state to see if it is blocked in an RPC. */
  121. struct machine_thread_state state;
  122. mach_msg_type_number_t count = MACHINE_THREAD_STATE_COUNT;
  123. error_t err;
  124. err = __thread_get_state (thread, MACHINE_THREAD_STATE_FLAVOR,
  125. (natural_t *) &state, &count);
  126. if (err)
  127. return err;
  128. assert (count == MACHINE_THREAD_STATE_COUNT);
  129. if (SYSCALL_EXAMINE (&state, msgid))
  130. {
  131. mach_port_t send_port, rcv_port;
  132. mach_msg_option_t option;
  133. mach_msg_timeout_t timeout;
  134. /* Blocked in a system call. */
  135. if (*msgid == -25
  136. /* mach_msg system call. Examine its parameters. */
  137. && MSG_EXAMINE (&state, msgid, &rcv_port, &send_port,
  138. &option, &timeout) == 0)
  139. {
  140. if (send_port != MACH_PORT_NULL && *msgid != 0)
  141. {
  142. /* For the normal case of RPCs, we consider the
  143. destination port to be the interesting thing
  144. whether we are in fact sending or receiving at the
  145. moment. That tells us who we are waiting for the
  146. reply from. */
  147. if (send_port == ss->intr_port)
  148. {
  149. /* This is a Hurd interruptible RPC.
  150. Mark it by surrounding the port description
  151. string with [...] brackets. */
  152. if (cur < limit)
  153. *cur++ = '[';
  154. cur = describe_port (cur, send_port, limit - cur);
  155. if (cur < limit)
  156. *cur++ = ']';
  157. }
  158. else
  159. cur = describe_port (cur, send_port, limit - cur);
  160. }
  161. else if (rcv_port != MACH_PORT_NULL)
  162. {
  163. /* This system call had no send port, but had a
  164. receive port. The msgid we extracted is then just
  165. some garbage or perhaps the msgid of the last
  166. message this thread received, but it's not a
  167. helpful thing to return. */
  168. cur = describe_port (cur, rcv_port, limit - cur);
  169. cur = __stpncpy (cur, ":rcv", limit - cur);
  170. *msgid = 0;
  171. }
  172. else if ((option & (MACH_RCV_MSG|MACH_RCV_TIMEOUT))
  173. == (MACH_RCV_MSG|MACH_RCV_TIMEOUT))
  174. {
  175. /* A receive with no valid port can be used for a
  176. pure timeout. Report the timeout value (counted
  177. in milliseconds); note this is the original total
  178. time, not the time remaining. */
  179. cur = describe_number (cur, 0, timeout, limit - cur);
  180. cur = __stpncpy (cur, "ms", limit - cur);
  181. *msgid = 0;
  182. }
  183. else
  184. {
  185. cur = __stpncpy (cur, "mach_msg", limit - cur);
  186. *msgid = 0;
  187. }
  188. }
  189. else /* Some other system call. */
  190. {
  191. cur = describe_number (cur, "syscall#", *msgid, limit - cur);
  192. *msgid = 0;
  193. }
  194. }
  195. }
  196. }
  197. __mach_port_deallocate (__mach_task_self (), thread);
  198. if (cur == limit)
  199. return ENOMEM;
  200. *cur = '\0';
  201. return 0;
  202. }
  203. kern_return_t
  204. _S_msg_describe_ports (mach_port_t msgport, mach_port_t refport,
  205. const mach_port_t *ports, mach_msg_type_number_t nports,
  206. char **desc, mach_msg_type_number_t *desclen)
  207. {
  208. char *p, *end;
  209. if (__USEPORT (AUTH, msgport != port))
  210. return EPERM;
  211. end = *desc + *desclen;
  212. p = *desc;
  213. while (nports-- > 0)
  214. {
  215. char this[200];
  216. describe_port (this, *ports++, sizeof this);
  217. p = __stpncpy (p, this, end - p);
  218. if (p == end && p[-1] != '\0')
  219. return ENOMEM;
  220. }
  221. *desclen = p - *desc;
  222. return 0;
  223. }