Mirror of strace – the linux syscall tracer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wait.c 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
  3. * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
  4. * Copyright (c) 1993-1996 Rick Sladkey <jrs@world.std.com>
  5. * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
  6. * Copyright (c) 2002-2004 Roland McGrath <roland@redhat.com>
  7. * Copyright (c) 2004 Ulrich Drepper <drepper@redhat.com>
  8. * Copyright (c) 2009-2013 Denys Vlasenko <dvlasenk@redhat.com>
  9. * Copyright (c) 2014-2015 Dmitry V. Levin <ldv@altlinux.org>
  10. * Copyright (c) 2014-2018 The strace developers.
  11. * All rights reserved.
  12. *
  13. * SPDX-License-Identifier: LGPL-2.1-or-later
  14. */
  15. #include "defs.h"
  16. #include "ptrace.h"
  17. #include "wait.h"
  18. #include "xlat/wait4_options.h"
  19. #include "xlat/ptrace_events.h"
  20. static int
  21. printstatus(int status)
  22. {
  23. int exited = 0;
  24. /*
  25. * Here is a tricky presentation problem. This solution
  26. * is still not entirely satisfactory but since there
  27. * are no wait status constructors it will have to do.
  28. */
  29. if (WIFSTOPPED(status)) {
  30. int sig = WSTOPSIG(status);
  31. tprintf("[{WIFSTOPPED(s) && WSTOPSIG(s) == %s%s}",
  32. sprintsigname(sig & 0x7f),
  33. sig & 0x80 ? " | 0x80" : "");
  34. status &= ~W_STOPCODE(sig);
  35. } else if (WIFSIGNALED(status)) {
  36. tprintf("[{WIFSIGNALED(s) && WTERMSIG(s) == %s%s}",
  37. sprintsigname(WTERMSIG(status)),
  38. WCOREDUMP(status) ? " && WCOREDUMP(s)" : "");
  39. status &= ~(W_EXITCODE(0, WTERMSIG(status)) | WCOREFLAG);
  40. } else if (WIFEXITED(status)) {
  41. tprintf("[{WIFEXITED(s) && WEXITSTATUS(s) == %d}",
  42. WEXITSTATUS(status));
  43. exited = 1;
  44. status &= ~W_EXITCODE(WEXITSTATUS(status), 0);
  45. }
  46. #ifdef WIFCONTINUED
  47. else if (WIFCONTINUED(status)) {
  48. tprints("[{WIFCONTINUED(s)}");
  49. status &= ~W_CONTINUED;
  50. }
  51. #endif
  52. else {
  53. tprintf("[%#x]", status);
  54. return 0;
  55. }
  56. if (status) {
  57. unsigned int event = (unsigned int) status >> 16;
  58. if (event) {
  59. tprints(" | ");
  60. printxval(ptrace_events, event, "PTRACE_EVENT_???");
  61. tprints(" << 16");
  62. status &= 0xffff;
  63. }
  64. if (status)
  65. tprintf(" | %#x", status);
  66. }
  67. tprints("]");
  68. return exited;
  69. }
  70. static int
  71. printwaitn(struct tcb *const tcp,
  72. void (*const print_rusage)(struct tcb *, kernel_ulong_t))
  73. {
  74. if (entering(tcp)) {
  75. /* On Linux, kernel-side pid_t is typedef'ed to int
  76. * on all arches. Also, glibc-2.8 truncates wait3 and wait4
  77. * pid argument to int on 64bit arches, producing,
  78. * for example, wait4(4294967295, ...) instead of -1
  79. * in strace. We have to use int here, not long.
  80. */
  81. int pid = tcp->u_arg[0];
  82. tprintf("%d, ", pid);
  83. } else {
  84. int status;
  85. /* status */
  86. if (tcp->u_rval == 0)
  87. printaddr(tcp->u_arg[1]);
  88. else if (!umove_or_printaddr(tcp, tcp->u_arg[1], &status))
  89. printstatus(status);
  90. /* options */
  91. tprints(", ");
  92. printflags(wait4_options, tcp->u_arg[2], "W???");
  93. if (print_rusage) {
  94. /* usage */
  95. tprints(", ");
  96. if (tcp->u_rval > 0)
  97. print_rusage(tcp, tcp->u_arg[3]);
  98. else
  99. printaddr(tcp->u_arg[3]);
  100. }
  101. }
  102. return 0;
  103. }
  104. SYS_FUNC(waitpid)
  105. {
  106. return printwaitn(tcp, NULL);
  107. }
  108. SYS_FUNC(wait4)
  109. {
  110. return printwaitn(tcp, printrusage);
  111. }
  112. #ifdef ALPHA
  113. SYS_FUNC(osf_wait4)
  114. {
  115. return printwaitn(tcp, printrusage32);
  116. }
  117. #endif
  118. #include "xlat/waitid_types.h"
  119. SYS_FUNC(waitid)
  120. {
  121. if (entering(tcp)) {
  122. printxval(waitid_types, tcp->u_arg[0], "P_???");
  123. int pid = tcp->u_arg[1];
  124. tprintf(", %d, ", pid);
  125. } else {
  126. /* siginfo */
  127. printsiginfo_at(tcp, tcp->u_arg[2]);
  128. /* options */
  129. tprints(", ");
  130. printflags(wait4_options, tcp->u_arg[3], "W???");
  131. /* usage */
  132. tprints(", ");
  133. printrusage(tcp, tcp->u_arg[4]);
  134. }
  135. return 0;
  136. }