tst-backtrace3.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* Test backtrace and backtrace_symbols for recursive calls.
  2. Copyright (C) 2010-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 3 * fn, and do_test. */
  22. #define NUM_FUNCTIONS 4
  23. NO_INLINE int
  24. fn (int c)
  25. {
  26. void *addresses[NUM_FUNCTIONS];
  27. char **symbols;
  28. int n;
  29. int i;
  30. if (c > 0)
  31. {
  32. fn (c - 1);
  33. return x;
  34. }
  35. /* Get the backtrace addresses. */
  36. n = backtrace (addresses, sizeof (addresses) / sizeof (addresses[0]));
  37. printf ("Obtained backtrace with %d functions\n", n);
  38. /* Check that there are at least four functions. */
  39. if (n < NUM_FUNCTIONS)
  40. {
  41. FAIL ();
  42. return 1;
  43. }
  44. /* Convert them to symbols. */
  45. symbols = backtrace_symbols (addresses, n);
  46. /* Check that symbols were obtained. */
  47. if (symbols == NULL)
  48. {
  49. FAIL ();
  50. return 1;
  51. }
  52. for (i = 0; i < n; ++i)
  53. printf ("Function %d: %s\n", i, symbols[i]);
  54. /* Check that the function names obtained are accurate. */
  55. for (i = 0; i < n - 1; ++i)
  56. if (!match (symbols[i], "fn"))
  57. {
  58. FAIL ();
  59. return 1;
  60. }
  61. /* Symbol names are not available for static functions, so we do not
  62. check do_test. */
  63. return x;
  64. }
  65. NO_INLINE int
  66. do_test (void)
  67. {
  68. fn (2);
  69. return ret;
  70. }
  71. #include <support/test-driver.c>