mtest.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /* mtest.c -- Minidebug test for libbacktrace library
  2. Copyright (C) 2020-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. /* This program tests using libbacktrace with a program that uses the
  28. minidebuginfo format in a .gnu_debugdata section. See
  29. https://sourceware.org/gdb/current/onlinedocs/gdb/MiniDebugInfo.html
  30. for a bit more information about minidebuginfo. What is relevant
  31. for libbacktrace is that we have just a symbol table, with no debug
  32. info, so we should be able to do a function backtrace, but we can't
  33. do a file/line backtrace. */
  34. #include <assert.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include "backtrace.h"
  38. #include "backtrace-supported.h"
  39. #include "testlib.h"
  40. static int test1 (void) __attribute__ ((noinline, noclone, unused));
  41. static int f2 (int) __attribute__ ((noinline, noclone));
  42. static int f3 (int, int) __attribute__ ((noinline, noclone));
  43. /* Collected PC values. */
  44. static uintptr_t addrs[20];
  45. /* The backtrace callback function. This is like callback_one in
  46. testlib.c, but it saves the PC also. */
  47. static int
  48. callback_mtest (void *vdata, uintptr_t pc, const char *filename, int lineno,
  49. const char *function)
  50. {
  51. struct bdata *data = (struct bdata *) vdata;
  52. if (data->index >= sizeof addrs / sizeof addrs[0])
  53. {
  54. fprintf (stderr, "callback_mtest: callback called too many times\n");
  55. data->failed = 1;
  56. return 1;
  57. }
  58. addrs[data->index] = pc;
  59. return callback_one (vdata, pc, filename, lineno, function);
  60. }
  61. /* Test the backtrace function with non-inlined functions. (We don't
  62. test with inlined functions because they won't work with minidebug
  63. anyhow.) */
  64. static int
  65. test1 (void)
  66. {
  67. /* Returning a value here and elsewhere avoids a tailcall which
  68. would mess up the backtrace. */
  69. return f2 (__LINE__) + 1;
  70. }
  71. static int
  72. f2 (int f1line)
  73. {
  74. return f3 (f1line, __LINE__) + 2;
  75. }
  76. static int
  77. f3 (int f1line __attribute__ ((unused)), int f2line __attribute__ ((unused)))
  78. {
  79. struct info all[20];
  80. struct bdata data;
  81. int i;
  82. size_t j;
  83. data.all = &all[0];
  84. data.index = 0;
  85. data.max = 20;
  86. data.failed = 0;
  87. i = backtrace_full (state, 0, callback_mtest, error_callback_one, &data);
  88. if (i != 0)
  89. {
  90. fprintf (stderr, "test1: unexpected return value %d\n", i);
  91. data.failed = 1;
  92. }
  93. if (data.index < 3)
  94. {
  95. fprintf (stderr,
  96. "test1: not enough frames; got %zu, expected at least 3\n",
  97. data.index);
  98. data.failed = 1;
  99. }
  100. /* When using minidebug we don't expect the function name here. */
  101. for (j = 0; j < 3 && j < data.index; j++)
  102. {
  103. if (all[j].function == NULL)
  104. {
  105. struct symdata symdata;
  106. symdata.name = NULL;
  107. symdata.val = 0;
  108. symdata.size = 0;
  109. symdata.failed = 0;
  110. i = backtrace_syminfo (state, addrs[j], callback_three,
  111. error_callback_three, &symdata);
  112. if (i == 0)
  113. {
  114. fprintf (stderr,
  115. ("test1: [%zu], unexpected return value from "
  116. "backtrace_syminfo %d\n"),
  117. j, i);
  118. data.failed = 1;
  119. }
  120. else if (symdata.name == NULL)
  121. {
  122. fprintf (stderr, "test1: [%zu]: syminfo did not find name\n", j);
  123. data.failed = 1;
  124. }
  125. else
  126. all[j].function = strdup (symdata.name);
  127. }
  128. }
  129. if (data.index > 0)
  130. {
  131. if (all[0].function == NULL)
  132. {
  133. fprintf (stderr, "test1: [0]: missing function name\n");
  134. data.failed = 1;
  135. }
  136. else if (strcmp (all[0].function, "f3") != 0)
  137. {
  138. fprintf (stderr, "test1: [0]: got %s expected %s\n",
  139. all[0].function, "f3");
  140. data.failed = 1;
  141. }
  142. }
  143. if (data.index > 1)
  144. {
  145. if (all[1].function == NULL)
  146. {
  147. fprintf (stderr, "test1: [1]: missing function name\n");
  148. data.failed = 1;
  149. }
  150. else if (strcmp (all[1].function, "f2") != 0)
  151. {
  152. fprintf (stderr, "test1: [1]: got %s expected %s\n",
  153. all[0].function, "f2");
  154. data.failed = 1;
  155. }
  156. }
  157. if (data.index > 2)
  158. {
  159. if (all[2].function == NULL)
  160. {
  161. fprintf (stderr, "test1: [2]: missing function name\n");
  162. data.failed = 1;
  163. }
  164. else if (strcmp (all[2].function, "test1") != 0)
  165. {
  166. fprintf (stderr, "test1: [2]: got %s expected %s\n",
  167. all[0].function, "test1");
  168. data.failed = 1;
  169. }
  170. }
  171. printf ("%s: backtrace_full noinline\n", data.failed ? "FAIL" : "PASS");
  172. if (data.failed)
  173. ++failures;
  174. return failures;
  175. }
  176. /* Test the backtrace_simple function with non-inlined functions. */
  177. static int test3 (void) __attribute__ ((noinline, noclone, unused));
  178. static int f22 (int) __attribute__ ((noinline, noclone));
  179. static int f23 (int, int) __attribute__ ((noinline, noclone));
  180. static int
  181. test3 (void)
  182. {
  183. return f22 (__LINE__) + 1;
  184. }
  185. static int
  186. f22 (int f1line)
  187. {
  188. return f23 (f1line, __LINE__) + 2;
  189. }
  190. static int
  191. f23 (int f1line __attribute__ ((unused)), int f2line __attribute__ ((unused)))
  192. {
  193. uintptr_t addrs[20];
  194. struct sdata data;
  195. int i;
  196. data.addrs = &addrs[0];
  197. data.index = 0;
  198. data.max = 20;
  199. data.failed = 0;
  200. i = backtrace_simple (state, 0, callback_two, error_callback_two, &data);
  201. if (i != 0)
  202. {
  203. fprintf (stderr, "test3: unexpected return value %d\n", i);
  204. data.failed = 1;
  205. }
  206. if (!data.failed)
  207. {
  208. int j;
  209. for (j = 0; j < 3; ++j)
  210. {
  211. struct symdata symdata;
  212. symdata.name = NULL;
  213. symdata.val = 0;
  214. symdata.size = 0;
  215. symdata.failed = 0;
  216. i = backtrace_syminfo (state, addrs[j], callback_three,
  217. error_callback_three, &symdata);
  218. if (i == 0)
  219. {
  220. fprintf (stderr,
  221. ("test3: [%d]: unexpected return value "
  222. "from backtrace_syminfo %d\n"),
  223. j, i);
  224. symdata.failed = 1;
  225. }
  226. if (!symdata.failed)
  227. {
  228. const char *expected;
  229. switch (j)
  230. {
  231. case 0:
  232. expected = "f23";
  233. break;
  234. case 1:
  235. expected = "f22";
  236. break;
  237. case 2:
  238. expected = "test3";
  239. break;
  240. default:
  241. assert (0);
  242. }
  243. if (symdata.name == NULL)
  244. {
  245. fprintf (stderr, "test3: [%d]: NULL syminfo name\n", j);
  246. symdata.failed = 1;
  247. }
  248. /* Use strncmp, not strcmp, because GCC might create a
  249. clone. */
  250. else if (strncmp (symdata.name, expected, strlen (expected))
  251. != 0)
  252. {
  253. fprintf (stderr,
  254. ("test3: [%d]: unexpected syminfo name "
  255. "got %s expected %s\n"),
  256. j, symdata.name, expected);
  257. symdata.failed = 1;
  258. }
  259. }
  260. if (symdata.failed)
  261. data.failed = 1;
  262. }
  263. }
  264. printf ("%s: backtrace_simple noinline\n", data.failed ? "FAIL" : "PASS");
  265. if (data.failed)
  266. ++failures;
  267. return failures;
  268. }
  269. int test5 (void) __attribute__ ((unused));
  270. int global = 1;
  271. int
  272. test5 (void)
  273. {
  274. struct symdata symdata;
  275. int i;
  276. uintptr_t addr = (uintptr_t) &global;
  277. if (sizeof (global) > 1)
  278. addr += 1;
  279. symdata.name = NULL;
  280. symdata.val = 0;
  281. symdata.size = 0;
  282. symdata.failed = 0;
  283. i = backtrace_syminfo (state, addr, callback_three,
  284. error_callback_three, &symdata);
  285. if (i == 0)
  286. {
  287. fprintf (stderr,
  288. "test5: unexpected return value from backtrace_syminfo %d\n",
  289. i);
  290. symdata.failed = 1;
  291. }
  292. if (!symdata.failed)
  293. {
  294. if (symdata.name == NULL)
  295. {
  296. fprintf (stderr, "test5: NULL syminfo name\n");
  297. symdata.failed = 1;
  298. }
  299. else if (!(strncmp (symdata.name, "global", 6) == 0
  300. && (symdata.name[6] == '\0'|| symdata.name[6] == '.')))
  301. {
  302. fprintf (stderr,
  303. "test5: unexpected syminfo name got %s expected %s\n",
  304. symdata.name, "global");
  305. symdata.failed = 1;
  306. }
  307. else if (symdata.val != (uintptr_t) &global)
  308. {
  309. fprintf (stderr,
  310. "test5: unexpected syminfo value got %lx expected %lx\n",
  311. (unsigned long) symdata.val,
  312. (unsigned long) (uintptr_t) &global);
  313. symdata.failed = 1;
  314. }
  315. else if (symdata.size != sizeof (global))
  316. {
  317. fprintf (stderr,
  318. "test5: unexpected syminfo size got %lx expected %lx\n",
  319. (unsigned long) symdata.size,
  320. (unsigned long) sizeof (global));
  321. symdata.failed = 1;
  322. }
  323. }
  324. printf ("%s: backtrace_syminfo variable\n",
  325. symdata.failed ? "FAIL" : "PASS");
  326. if (symdata.failed)
  327. ++failures;
  328. return failures;
  329. }
  330. int
  331. main (int argc ATTRIBUTE_UNUSED, char **argv)
  332. {
  333. state = backtrace_create_state (argv[0], BACKTRACE_SUPPORTS_THREADS,
  334. error_callback_create, NULL);
  335. #if BACKTRACE_SUPPORTED
  336. test1 ();
  337. test3 ();
  338. #if BACKTRACE_SUPPORTS_DATA
  339. test5 ();
  340. #endif
  341. #endif
  342. exit (failures ? EXIT_FAILURE : EXIT_SUCCESS);
  343. }