testlib.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* testlib.c -- test functions for libbacktrace library
  2. Copyright (C) 2012-2022 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. #include <assert.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include "filenames.h"
  32. #include "backtrace.h"
  33. #include "testlib.h"
  34. /* The backtrace state. */
  35. void *state;
  36. /* The number of failures. */
  37. int failures;
  38. /* Return the base name in a path. */
  39. const char *
  40. base (const char *p)
  41. {
  42. const char *last;
  43. const char *s;
  44. last = NULL;
  45. for (s = p; *s != '\0'; ++s)
  46. {
  47. if (IS_DIR_SEPARATOR (*s))
  48. last = s + 1;
  49. }
  50. return last != NULL ? last : p;
  51. }
  52. /* Check an entry in a struct info array. */
  53. void
  54. check (const char *name, int index, const struct info *all, int want_lineno,
  55. const char *want_function, const char *want_file, int *failed)
  56. {
  57. if (*failed)
  58. return;
  59. if (all[index].filename == NULL || all[index].function == NULL)
  60. {
  61. fprintf (stderr, "%s: [%d]: missing file name or function name\n",
  62. name, index);
  63. *failed = 1;
  64. return;
  65. }
  66. if (strcmp (base (all[index].filename), want_file) != 0)
  67. {
  68. fprintf (stderr, "%s: [%d]: got %s expected %s\n", name, index,
  69. all[index].filename, want_file);
  70. *failed = 1;
  71. }
  72. if (all[index].lineno != want_lineno)
  73. {
  74. fprintf (stderr, "%s: [%d]: got %d expected %d\n", name, index,
  75. all[index].lineno, want_lineno);
  76. *failed = 1;
  77. }
  78. if (strcmp (all[index].function, want_function) != 0)
  79. {
  80. fprintf (stderr, "%s: [%d]: got %s expected %s\n", name, index,
  81. all[index].function, want_function);
  82. *failed = 1;
  83. }
  84. }
  85. /* The backtrace callback function. */
  86. int
  87. callback_one (void *vdata, uintptr_t pc ATTRIBUTE_UNUSED,
  88. const char *filename, int lineno, const char *function)
  89. {
  90. struct bdata *data = (struct bdata *) vdata;
  91. struct info *p;
  92. if (data->index >= data->max)
  93. {
  94. fprintf (stderr, "callback_one: callback called too many times\n");
  95. data->failed = 1;
  96. return 1;
  97. }
  98. p = &data->all[data->index];
  99. if (filename == NULL)
  100. p->filename = NULL;
  101. else
  102. {
  103. p->filename = strdup (filename);
  104. assert (p->filename != NULL);
  105. }
  106. p->lineno = lineno;
  107. if (function == NULL)
  108. p->function = NULL;
  109. else
  110. {
  111. p->function = strdup (function);
  112. assert (p->function != NULL);
  113. }
  114. ++data->index;
  115. return 0;
  116. }
  117. /* An error callback passed to backtrace. */
  118. void
  119. error_callback_one (void *vdata, const char *msg, int errnum)
  120. {
  121. struct bdata *data = (struct bdata *) vdata;
  122. fprintf (stderr, "%s", msg);
  123. if (errnum > 0)
  124. fprintf (stderr, ": %s", strerror (errnum));
  125. fprintf (stderr, "\n");
  126. data->failed = 1;
  127. }
  128. /* The backtrace_simple callback function. */
  129. int
  130. callback_two (void *vdata, uintptr_t pc)
  131. {
  132. struct sdata *data = (struct sdata *) vdata;
  133. if (data->index >= data->max)
  134. {
  135. fprintf (stderr, "callback_two: callback called too many times\n");
  136. data->failed = 1;
  137. return 1;
  138. }
  139. data->addrs[data->index] = pc;
  140. ++data->index;
  141. return 0;
  142. }
  143. /* An error callback passed to backtrace_simple. */
  144. void
  145. error_callback_two (void *vdata, const char *msg, int errnum)
  146. {
  147. struct sdata *data = (struct sdata *) vdata;
  148. fprintf (stderr, "%s", msg);
  149. if (errnum > 0)
  150. fprintf (stderr, ": %s", strerror (errnum));
  151. fprintf (stderr, "\n");
  152. data->failed = 1;
  153. }
  154. /* The backtrace_syminfo callback function. */
  155. void
  156. callback_three (void *vdata, uintptr_t pc ATTRIBUTE_UNUSED,
  157. const char *symname, uintptr_t symval,
  158. uintptr_t symsize)
  159. {
  160. struct symdata *data = (struct symdata *) vdata;
  161. if (symname == NULL)
  162. data->name = NULL;
  163. else
  164. {
  165. data->name = strdup (symname);
  166. assert (data->name != NULL);
  167. }
  168. data->val = symval;
  169. data->size = symsize;
  170. }
  171. /* The backtrace_syminfo error callback function. */
  172. void
  173. error_callback_three (void *vdata, const char *msg, int errnum)
  174. {
  175. struct symdata *data = (struct symdata *) vdata;
  176. fprintf (stderr, "%s", msg);
  177. if (errnum > 0)
  178. fprintf (stderr, ": %s", strerror (errnum));
  179. fprintf (stderr, "\n");
  180. data->failed = 1;
  181. }
  182. /* The backtrace_create_state error callback function. */
  183. void
  184. error_callback_create (void *data ATTRIBUTE_UNUSED, const char *msg,
  185. int errnum)
  186. {
  187. fprintf (stderr, "%s", msg);
  188. if (errnum > 0)
  189. fprintf (stderr, ": %s", strerror (errnum));
  190. fprintf (stderr, "\n");
  191. exit (EXIT_FAILURE);
  192. }