tst-backtrace5.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* Test backtrace and backtrace_symbols for signal frames, where a
  2. system call was interrupted by a signal.
  3. Copyright (C) 2011-2026 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  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. <https://www.gnu.org/licenses/>. */
  16. #include <execinfo.h>
  17. #include <search.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <sys/types.h>
  22. #include <signal.h>
  23. #include <unistd.h>
  24. #include "tst-backtrace.h"
  25. #ifndef SIGACTION_FLAGS
  26. # define SIGACTION_FLAGS 0
  27. #endif
  28. /* The backtrace should include at least handle_signal, a signal
  29. trampoline, read, 3 * fn, and do_test. */
  30. #define NUM_FUNCTIONS 7
  31. void
  32. handle_signal (int signum)
  33. {
  34. void *addresses[NUM_FUNCTIONS];
  35. char **symbols;
  36. int n;
  37. int i;
  38. /* Get the backtrace addresses. */
  39. n = backtrace (addresses, sizeof (addresses) / sizeof (addresses[0]));
  40. printf ("Obtained backtrace with %d functions\n", n);
  41. /* Check that there are at least seven functions. */
  42. if (n < NUM_FUNCTIONS)
  43. {
  44. FAIL ();
  45. return;
  46. }
  47. /* Convert them to symbols. */
  48. symbols = backtrace_symbols (addresses, n);
  49. /* Check that symbols were obtained. */
  50. if (symbols == NULL)
  51. {
  52. FAIL ();
  53. return;
  54. }
  55. for (i = 0; i < n; ++i)
  56. printf ("Function %d: %s\n", i, symbols[i]);
  57. /* Check that the function names obtained are accurate. */
  58. if (!match (symbols[0], "handle_signal"))
  59. {
  60. FAIL ();
  61. return;
  62. }
  63. /* Do not check name for signal trampoline or cancellable syscall
  64. wrappers (__syscall_cancel*). */
  65. for (; i < n - 1; i++)
  66. if (match (symbols[i], "read"))
  67. break;
  68. if (i == n - 1)
  69. {
  70. FAIL ();
  71. return;
  72. }
  73. for (; i < n - 1; i++)
  74. if (!match (symbols[i], "fn"))
  75. {
  76. FAIL ();
  77. return;
  78. }
  79. /* Symbol names are not available for static functions, so we do not
  80. check do_test. */
  81. /* Check that backtrace does not return more than what fits in the array
  82. (bug 25423). */
  83. for (int j = 0; j < NUM_FUNCTIONS; j++)
  84. {
  85. n = backtrace (addresses, j);
  86. if (n > j)
  87. {
  88. FAIL ();
  89. return;
  90. }
  91. }
  92. }
  93. NO_INLINE int
  94. fn (int c, int flags)
  95. {
  96. pid_t parent_pid, child_pid;
  97. int pipefd[2];
  98. char r[1];
  99. struct sigaction act;
  100. if (c > 0)
  101. {
  102. fn (c - 1, flags);
  103. return x;
  104. }
  105. memset (&act, 0, sizeof (act));
  106. act.sa_handler = handle_signal;
  107. act.sa_flags = flags;
  108. sigemptyset (&act.sa_mask);
  109. sigaction (SIGUSR1, &act, NULL);
  110. parent_pid = getpid ();
  111. if (pipe (pipefd) == -1)
  112. abort ();
  113. child_pid = fork ();
  114. if (child_pid == (pid_t) -1)
  115. abort ();
  116. else if (child_pid == 0)
  117. {
  118. sleep (1);
  119. kill (parent_pid, SIGUSR1);
  120. _exit (0);
  121. }
  122. /* In the parent. */
  123. read (pipefd[0], r, 1);
  124. return 0;
  125. }
  126. NO_INLINE int
  127. do_test (void)
  128. {
  129. fn (2, SIGACTION_FLAGS);
  130. return ret;
  131. }
  132. #include <support/test-driver.c>