tst-backtrace2.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Test backtrace and backtrace_symbols.
  2. Copyright (C) 2009-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 "tst-backtrace.h"
  21. /* The backtrace should include at least f1, f2, f3, and do_test. */
  22. #define NUM_FUNCTIONS 4
  23. NO_INLINE void
  24. fn1 (void)
  25. {
  26. void *addresses[NUM_FUNCTIONS];
  27. char **symbols;
  28. int n;
  29. int i;
  30. /* Get the backtrace addresses. */
  31. n = backtrace (addresses, sizeof (addresses) / sizeof (addresses[0]));
  32. printf ("Obtained backtrace with %d functions\n", n);
  33. /* Check that there are at least four functions. */
  34. if (n < NUM_FUNCTIONS)
  35. {
  36. FAIL ();
  37. return;
  38. }
  39. /* Convert them to symbols. */
  40. symbols = backtrace_symbols (addresses, n);
  41. /* Check that symbols were obtained. */
  42. if (symbols == NULL)
  43. {
  44. FAIL ();
  45. return;
  46. }
  47. for (i = 0; i < n; ++i)
  48. printf ("Function %d: %s\n", i, symbols[i]);
  49. /* Check that the function names obtained are accurate. */
  50. if (!match (symbols[0], "fn1"))
  51. {
  52. FAIL ();
  53. return;
  54. }
  55. /* Symbol names are not available for static functions, so we do not
  56. check f2. */
  57. if (!match (symbols[2], "fn3"))
  58. {
  59. FAIL ();
  60. return;
  61. }
  62. /* Symbol names are not available for static functions, so we do not
  63. check do_test. */
  64. }
  65. NO_INLINE int
  66. fn2 (void)
  67. {
  68. fn1 ();
  69. /* Prevent tail calls. */
  70. return x;
  71. }
  72. NO_INLINE int
  73. fn3 (void)
  74. {
  75. fn2();
  76. /* Prevent tail calls. */
  77. return x;
  78. }
  79. NO_INLINE int
  80. do_test (void)
  81. {
  82. /* Test BZ #18084. */
  83. void *buffer[1];
  84. if (backtrace (buffer, 0) != 0)
  85. FAIL ();
  86. fn3 ();
  87. return ret;
  88. }
  89. #include <support/test-driver.c>