tst-backtrace4.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* Test backtrace and backtrace_symbols for signal frames.
  2. Copyright (C) 2011-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 <execinfo.h>
  16. #include <search.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <sys/types.h>
  21. #include <signal.h>
  22. #include <unistd.h>
  23. #include "tst-backtrace.h"
  24. /* The backtrace should include at least handle_signal, a signal
  25. trampoline, 3 * fn, and do_test. */
  26. #define NUM_FUNCTIONS 6
  27. volatile int sig_handled = 0;
  28. void
  29. handle_signal (int signum)
  30. {
  31. void *addresses[NUM_FUNCTIONS];
  32. char **symbols;
  33. int n;
  34. int i;
  35. sig_handled = 1;
  36. /* Get the backtrace addresses. */
  37. n = backtrace (addresses, sizeof (addresses) / sizeof (addresses[0]));
  38. printf ("Obtained backtrace with %d functions (want at least %d)\n",
  39. n, NUM_FUNCTIONS);
  40. /* Check that there are at least NUM_FUNCTIONS functions. */
  41. if (n < NUM_FUNCTIONS)
  42. {
  43. FAIL ();
  44. /* Only return if we got no symbols at all. The partial output is
  45. still useful for debugging failures. */
  46. if (n <= 0)
  47. return;
  48. }
  49. /* Convert them to symbols. */
  50. symbols = backtrace_symbols (addresses, n);
  51. /* Check that symbols were obtained. */
  52. if (symbols == NULL)
  53. {
  54. FAIL ();
  55. return;
  56. }
  57. for (i = 0; i < n; ++i)
  58. printf ("Function %d: %s\n", i, symbols[i]);
  59. /* Check that the function names obtained are accurate. */
  60. if (!match (symbols[0], "handle_signal"))
  61. FAIL ();
  62. /* Do not check name for signal trampoline. */
  63. for (i = 2; i < n - 1; i++)
  64. if (!match (symbols[i], "fn"))
  65. {
  66. FAIL ();
  67. return;
  68. }
  69. /* Symbol names are not available for static functions, so we do not
  70. check do_test. */
  71. }
  72. NO_INLINE int
  73. fn (int c)
  74. {
  75. pid_t parent_pid, child_pid;
  76. if (c > 0)
  77. {
  78. fn (c - 1);
  79. return x;
  80. }
  81. signal (SIGUSR1, handle_signal);
  82. parent_pid = getpid ();
  83. child_pid = fork ();
  84. if (child_pid == (pid_t) -1)
  85. abort ();
  86. else if (child_pid == 0)
  87. {
  88. sleep (1);
  89. kill (parent_pid, SIGUSR1);
  90. _exit (0);
  91. }
  92. /* In the parent. */
  93. while (sig_handled == 0)
  94. ;
  95. return 0;
  96. }
  97. NO_INLINE int
  98. do_test (void)
  99. {
  100. fn (2);
  101. return ret;
  102. }
  103. #include <support/test-driver.c>