btest.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /* btest.c -- Test 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. /* This program tests the externally visible interfaces of the
  28. libbacktrace library. */
  29. #include <assert.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <unistd.h>
  34. #include <sys/stat.h>
  35. #include "filenames.h"
  36. #include "backtrace.h"
  37. #include "backtrace-supported.h"
  38. #include "testlib.h"
  39. /* Test the backtrace function with non-inlined functions. */
  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. static int
  44. test1 (void)
  45. {
  46. /* Returning a value here and elsewhere avoids a tailcall which
  47. would mess up the backtrace. */
  48. return f2 (__LINE__) + 1;
  49. }
  50. static int
  51. f2 (int f1line)
  52. {
  53. return f3 (f1line, __LINE__) + 2;
  54. }
  55. static int
  56. f3 (int f1line, int f2line)
  57. {
  58. struct info all[20];
  59. struct bdata data;
  60. int f3line;
  61. int i;
  62. data.all = &all[0];
  63. data.index = 0;
  64. data.max = 20;
  65. data.failed = 0;
  66. f3line = __LINE__ + 1;
  67. i = backtrace_full (state, 0, callback_one, error_callback_one, &data);
  68. if (i != 0)
  69. {
  70. fprintf (stderr, "test1: unexpected return value %d\n", i);
  71. data.failed = 1;
  72. }
  73. if (data.index < 3)
  74. {
  75. fprintf (stderr,
  76. "test1: not enough frames; got %zu, expected at least 3\n",
  77. data.index);
  78. data.failed = 1;
  79. }
  80. check ("test1", 0, all, f3line, "f3", "btest.c", &data.failed);
  81. check ("test1", 1, all, f2line, "f2", "btest.c", &data.failed);
  82. check ("test1", 2, all, f1line, "test1", "btest.c", &data.failed);
  83. printf ("%s: backtrace_full noinline\n", data.failed ? "FAIL" : "PASS");
  84. if (data.failed)
  85. ++failures;
  86. return failures;
  87. }
  88. /* Test the backtrace function with inlined functions. */
  89. static inline int test2 (void) __attribute__ ((always_inline, unused));
  90. static inline int f12 (int) __attribute__ ((always_inline));
  91. static inline int f13 (int, int) __attribute__ ((always_inline));
  92. static inline int
  93. test2 (void)
  94. {
  95. return f12 (__LINE__) + 1;
  96. }
  97. static inline int
  98. f12 (int f1line)
  99. {
  100. return f13 (f1line, __LINE__) + 2;
  101. }
  102. static inline int
  103. f13 (int f1line, int f2line)
  104. {
  105. struct info all[20];
  106. struct bdata data;
  107. int f3line;
  108. int i;
  109. data.all = &all[0];
  110. data.index = 0;
  111. data.max = 20;
  112. data.failed = 0;
  113. f3line = __LINE__ + 1;
  114. i = backtrace_full (state, 0, callback_one, error_callback_one, &data);
  115. if (i != 0)
  116. {
  117. fprintf (stderr, "test2: unexpected return value %d\n", i);
  118. data.failed = 1;
  119. }
  120. check ("test2", 0, all, f3line, "f13", "btest.c", &data.failed);
  121. check ("test2", 1, all, f2line, "f12", "btest.c", &data.failed);
  122. check ("test2", 2, all, f1line, "test2", "btest.c", &data.failed);
  123. printf ("%s: backtrace_full inline\n", data.failed ? "FAIL" : "PASS");
  124. if (data.failed)
  125. ++failures;
  126. return failures;
  127. }
  128. /* Test the backtrace_simple function with non-inlined functions. */
  129. static int test3 (void) __attribute__ ((noinline, noclone, unused));
  130. static int f22 (int) __attribute__ ((noinline, noclone));
  131. static int f23 (int, int) __attribute__ ((noinline, noclone));
  132. static int
  133. test3 (void)
  134. {
  135. return f22 (__LINE__) + 1;
  136. }
  137. static int
  138. f22 (int f1line)
  139. {
  140. return f23 (f1line, __LINE__) + 2;
  141. }
  142. static int
  143. f23 (int f1line, int f2line)
  144. {
  145. uintptr_t addrs[20];
  146. struct sdata data;
  147. int f3line;
  148. int i;
  149. data.addrs = &addrs[0];
  150. data.index = 0;
  151. data.max = 20;
  152. data.failed = 0;
  153. f3line = __LINE__ + 1;
  154. i = backtrace_simple (state, 0, callback_two, error_callback_two, &data);
  155. if (i != 0)
  156. {
  157. fprintf (stderr, "test3: unexpected return value %d\n", i);
  158. data.failed = 1;
  159. }
  160. if (!data.failed)
  161. {
  162. struct info all[20];
  163. struct bdata bdata;
  164. int j;
  165. bdata.all = &all[0];
  166. bdata.index = 0;
  167. bdata.max = 20;
  168. bdata.failed = 0;
  169. for (j = 0; j < 3; ++j)
  170. {
  171. i = backtrace_pcinfo (state, addrs[j], callback_one,
  172. error_callback_one, &bdata);
  173. if (i != 0)
  174. {
  175. fprintf (stderr,
  176. ("test3: unexpected return value "
  177. "from backtrace_pcinfo %d\n"),
  178. i);
  179. bdata.failed = 1;
  180. }
  181. if (!bdata.failed && bdata.index != (size_t) (j + 1))
  182. {
  183. fprintf (stderr,
  184. ("wrong number of calls from backtrace_pcinfo "
  185. "got %u expected %d\n"),
  186. (unsigned int) bdata.index, j + 1);
  187. bdata.failed = 1;
  188. }
  189. }
  190. check ("test3", 0, all, f3line, "f23", "btest.c", &bdata.failed);
  191. check ("test3", 1, all, f2line, "f22", "btest.c", &bdata.failed);
  192. check ("test3", 2, all, f1line, "test3", "btest.c", &bdata.failed);
  193. if (bdata.failed)
  194. data.failed = 1;
  195. for (j = 0; j < 3; ++j)
  196. {
  197. struct symdata symdata;
  198. symdata.name = NULL;
  199. symdata.val = 0;
  200. symdata.size = 0;
  201. symdata.failed = 0;
  202. i = backtrace_syminfo (state, addrs[j], callback_three,
  203. error_callback_three, &symdata);
  204. if (i == 0)
  205. {
  206. fprintf (stderr,
  207. ("test3: [%d]: unexpected return value "
  208. "from backtrace_syminfo %d\n"),
  209. j, i);
  210. symdata.failed = 1;
  211. }
  212. if (!symdata.failed)
  213. {
  214. const char *expected;
  215. switch (j)
  216. {
  217. case 0:
  218. expected = "f23";
  219. break;
  220. case 1:
  221. expected = "f22";
  222. break;
  223. case 2:
  224. expected = "test3";
  225. break;
  226. default:
  227. assert (0);
  228. }
  229. if (symdata.name == NULL)
  230. {
  231. fprintf (stderr, "test3: [%d]: NULL syminfo name\n", j);
  232. symdata.failed = 1;
  233. }
  234. /* Use strncmp, not strcmp, because GCC might create a
  235. clone. */
  236. else if (strncmp (symdata.name, expected, strlen (expected))
  237. != 0)
  238. {
  239. fprintf (stderr,
  240. ("test3: [%d]: unexpected syminfo name "
  241. "got %s expected %s\n"),
  242. j, symdata.name, expected);
  243. symdata.failed = 1;
  244. }
  245. }
  246. if (symdata.failed)
  247. data.failed = 1;
  248. }
  249. }
  250. printf ("%s: backtrace_simple noinline\n", data.failed ? "FAIL" : "PASS");
  251. if (data.failed)
  252. ++failures;
  253. return failures;
  254. }
  255. /* Test the backtrace_simple function with inlined functions. */
  256. static inline int test4 (void) __attribute__ ((always_inline, unused));
  257. static inline int f32 (int) __attribute__ ((always_inline));
  258. static inline int f33 (int, int) __attribute__ ((always_inline));
  259. static inline int
  260. test4 (void)
  261. {
  262. return f32 (__LINE__) + 1;
  263. }
  264. static inline int
  265. f32 (int f1line)
  266. {
  267. return f33 (f1line, __LINE__) + 2;
  268. }
  269. static inline int
  270. f33 (int f1line, int f2line)
  271. {
  272. uintptr_t addrs[20];
  273. struct sdata data;
  274. int f3line;
  275. int i;
  276. data.addrs = &addrs[0];
  277. data.index = 0;
  278. data.max = 20;
  279. data.failed = 0;
  280. f3line = __LINE__ + 1;
  281. i = backtrace_simple (state, 0, callback_two, error_callback_two, &data);
  282. if (i != 0)
  283. {
  284. fprintf (stderr, "test3: unexpected return value %d\n", i);
  285. data.failed = 1;
  286. }
  287. if (!data.failed)
  288. {
  289. struct info all[20];
  290. struct bdata bdata;
  291. bdata.all = &all[0];
  292. bdata.index = 0;
  293. bdata.max = 20;
  294. bdata.failed = 0;
  295. i = backtrace_pcinfo (state, addrs[0], callback_one, error_callback_one,
  296. &bdata);
  297. if (i != 0)
  298. {
  299. fprintf (stderr,
  300. ("test4: unexpected return value "
  301. "from backtrace_pcinfo %d\n"),
  302. i);
  303. bdata.failed = 1;
  304. }
  305. check ("test4", 0, all, f3line, "f33", "btest.c", &bdata.failed);
  306. check ("test4", 1, all, f2line, "f32", "btest.c", &bdata.failed);
  307. check ("test4", 2, all, f1line, "test4", "btest.c", &bdata.failed);
  308. if (bdata.failed)
  309. data.failed = 1;
  310. }
  311. printf ("%s: backtrace_simple inline\n", data.failed ? "FAIL" : "PASS");
  312. if (data.failed)
  313. ++failures;
  314. return failures;
  315. }
  316. static int test5 (void) __attribute__ ((unused));
  317. int global = 1;
  318. static int
  319. test5 (void)
  320. {
  321. struct symdata symdata;
  322. int i;
  323. uintptr_t addr = (uintptr_t) &global;
  324. if (sizeof (global) > 1)
  325. addr += 1;
  326. symdata.name = NULL;
  327. symdata.val = 0;
  328. symdata.size = 0;
  329. symdata.failed = 0;
  330. i = backtrace_syminfo (state, addr, callback_three,
  331. error_callback_three, &symdata);
  332. if (i == 0)
  333. {
  334. fprintf (stderr,
  335. "test5: unexpected return value from backtrace_syminfo %d\n",
  336. i);
  337. symdata.failed = 1;
  338. }
  339. if (!symdata.failed)
  340. {
  341. if (symdata.name == NULL)
  342. {
  343. fprintf (stderr, "test5: NULL syminfo name\n");
  344. symdata.failed = 1;
  345. }
  346. else if (!(strncmp (symdata.name, "global", 6) == 0
  347. && (symdata.name[6] == '\0'|| symdata.name[6] == '.')))
  348. {
  349. fprintf (stderr,
  350. "test5: unexpected syminfo name got %s expected %s\n",
  351. symdata.name, "global");
  352. symdata.failed = 1;
  353. }
  354. else if (symdata.val != (uintptr_t) &global)
  355. {
  356. fprintf (stderr,
  357. "test5: unexpected syminfo value got %lx expected %lx\n",
  358. (unsigned long) symdata.val,
  359. (unsigned long) (uintptr_t) &global);
  360. symdata.failed = 1;
  361. }
  362. else if (symdata.size != sizeof (global))
  363. {
  364. fprintf (stderr,
  365. "test5: unexpected syminfo size got %lx expected %lx\n",
  366. (unsigned long) symdata.size,
  367. (unsigned long) sizeof (global));
  368. symdata.failed = 1;
  369. }
  370. }
  371. printf ("%s: backtrace_syminfo variable\n",
  372. symdata.failed ? "FAIL" : "PASS");
  373. if (symdata.failed)
  374. ++failures;
  375. return failures;
  376. }
  377. #define MIN_DESCRIPTOR 3
  378. #define MAX_DESCRIPTOR 10
  379. static int fstat_status[MAX_DESCRIPTOR];
  380. /* Check files that are available. */
  381. static void
  382. check_available_files (void)
  383. {
  384. struct stat s;
  385. for (unsigned i = MIN_DESCRIPTOR; i < MAX_DESCRIPTOR; i++)
  386. fstat_status[i] = fstat (i, &s);
  387. }
  388. /* Check that are no files left open. */
  389. static void
  390. check_open_files (void)
  391. {
  392. for (unsigned i = MIN_DESCRIPTOR; i < MAX_DESCRIPTOR; i++)
  393. {
  394. if (fstat_status[i] != 0 && close (i) == 0)
  395. {
  396. fprintf (stderr,
  397. "ERROR: descriptor %d still open after tests complete\n",
  398. i);
  399. ++failures;
  400. }
  401. }
  402. }
  403. /* Run all the tests. */
  404. int
  405. main (int argc ATTRIBUTE_UNUSED, char **argv)
  406. {
  407. check_available_files ();
  408. state = backtrace_create_state (argv[0], BACKTRACE_SUPPORTS_THREADS,
  409. error_callback_create, NULL);
  410. #if BACKTRACE_SUPPORTED
  411. test1 ();
  412. test2 ();
  413. test3 ();
  414. test4 ();
  415. #if BACKTRACE_SUPPORTS_DATA
  416. test5 ();
  417. #endif
  418. #endif
  419. check_open_files ();
  420. exit (failures ? EXIT_FAILURE : EXIT_SUCCESS);
  421. }