ttest.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* ttest.c -- Test for libbacktrace library
  2. Copyright (C) 2017-2021 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor, Google.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. (1) Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. (2) Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. (3) The name of the author may not be used to
  14. endorse or promote products derived from this software without
  15. specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  20. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  25. IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. POSSIBILITY OF SUCH DAMAGE. */
  27. /* Test using the libbacktrace library from multiple threads. */
  28. #include <assert.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <pthread.h>
  33. #include "filenames.h"
  34. #include "backtrace.h"
  35. #include "backtrace-supported.h"
  36. #include "testlib.h"
  37. static int f2 (int) __attribute__ ((noinline));
  38. static int f3 (int, int) __attribute__ ((noinline));
  39. /* Test that a simple backtrace works. This is called via
  40. pthread_create. It returns the number of failures, as void *. */
  41. static void *
  42. test1_thread (void *arg ATTRIBUTE_UNUSED)
  43. {
  44. /* Returning a value here and elsewhere avoids a tailcall which
  45. would mess up the backtrace. */
  46. return (void *) (uintptr_t) (f2 (__LINE__) - 2);
  47. }
  48. static int
  49. f2 (int f1line)
  50. {
  51. return f3 (f1line, __LINE__) + 2;
  52. }
  53. static int
  54. f3 (int f1line, int f2line)
  55. {
  56. struct info all[20];
  57. struct bdata data;
  58. int f3line;
  59. int i;
  60. data.all = &all[0];
  61. data.index = 0;
  62. data.max = 20;
  63. data.failed = 0;
  64. f3line = __LINE__ + 1;
  65. i = backtrace_full (state, 0, callback_one, error_callback_one, &data);
  66. if (i != 0)
  67. {
  68. fprintf (stderr, "test1: unexpected return value %d\n", i);
  69. data.failed = 1;
  70. }
  71. if (data.index < 3)
  72. {
  73. fprintf (stderr,
  74. "test1: not enough frames; got %zu, expected at least 3\n",
  75. data.index);
  76. data.failed = 1;
  77. }
  78. check ("test1", 0, all, f3line, "f3", "ttest.c", &data.failed);
  79. check ("test1", 1, all, f2line, "f2", "ttest.c", &data.failed);
  80. check ("test1", 2, all, f1line, "test1_thread", "ttest.c", &data.failed);
  81. return data.failed;
  82. }
  83. /* Run the test with 10 threads simultaneously. */
  84. #define THREAD_COUNT 10
  85. static void test1 (void) __attribute__ ((unused));
  86. static void
  87. test1 (void)
  88. {
  89. pthread_t atid[THREAD_COUNT];
  90. int i;
  91. int errnum;
  92. int this_fail;
  93. void *ret;
  94. for (i = 0; i < THREAD_COUNT; i++)
  95. {
  96. errnum = pthread_create (&atid[i], NULL, test1_thread, NULL);
  97. if (errnum != 0)
  98. {
  99. fprintf (stderr, "pthread_create %d: %s\n", i, strerror (errnum));
  100. exit (EXIT_FAILURE);
  101. }
  102. }
  103. this_fail = 0;
  104. for (i = 0; i < THREAD_COUNT; i++)
  105. {
  106. errnum = pthread_join (atid[i], &ret);
  107. if (errnum != 0)
  108. {
  109. fprintf (stderr, "pthread_join %d: %s\n", i, strerror (errnum));
  110. exit (EXIT_FAILURE);
  111. }
  112. this_fail += (int) (uintptr_t) ret;
  113. }
  114. printf ("%s: threaded backtrace_full noinline\n", this_fail > 0 ? "FAIL" : "PASS");
  115. failures += this_fail;
  116. }
  117. int
  118. main (int argc ATTRIBUTE_UNUSED, char **argv)
  119. {
  120. state = backtrace_create_state (argv[0], BACKTRACE_SUPPORTS_THREADS,
  121. error_callback_create, NULL);
  122. #if BACKTRACE_SUPPORTED
  123. #if BACKTRACE_SUPPORTS_THREADS
  124. test1 ();
  125. #endif
  126. #endif
  127. exit (failures ? EXIT_FAILURE : EXIT_SUCCESS);
  128. }