btest.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. /* btest.c -- Test for libbacktrace library
  2. Copyright (C) 2012-2015 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 "filenames.h"
  34. #include "backtrace.h"
  35. #include "backtrace-supported.h"
  36. /* Portable attribute syntax. Actually some of these tests probably
  37. won't work if the attributes are not recognized. */
  38. #ifndef GCC_VERSION
  39. # define GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)
  40. #endif
  41. #if (GCC_VERSION < 2007)
  42. # define __attribute__(x)
  43. #endif
  44. #ifndef ATTRIBUTE_UNUSED
  45. # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
  46. #endif
  47. /* Used to collect backtrace info. */
  48. struct info
  49. {
  50. char *filename;
  51. int lineno;
  52. char *function;
  53. };
  54. /* Passed to backtrace callback function. */
  55. struct bdata
  56. {
  57. struct info *all;
  58. size_t index;
  59. size_t max;
  60. int failed;
  61. };
  62. /* Passed to backtrace_simple callback function. */
  63. struct sdata
  64. {
  65. uintptr_t *addrs;
  66. size_t index;
  67. size_t max;
  68. int failed;
  69. };
  70. /* Passed to backtrace_syminfo callback function. */
  71. struct symdata
  72. {
  73. const char *name;
  74. uintptr_t val, size;
  75. int failed;
  76. };
  77. /* The backtrace state. */
  78. static void *state;
  79. /* The number of failures. */
  80. static int failures;
  81. /* Return the base name in a path. */
  82. static const char *
  83. base (const char *p)
  84. {
  85. const char *last;
  86. const char *s;
  87. last = NULL;
  88. for (s = p; *s != '\0'; ++s)
  89. {
  90. if (IS_DIR_SEPARATOR (*s))
  91. last = s + 1;
  92. }
  93. return last != NULL ? last : p;
  94. }
  95. /* Check an entry in a struct info array. */
  96. static void
  97. check (const char *name, int index, const struct info *all, int want_lineno,
  98. const char *want_function, int *failed)
  99. {
  100. if (*failed)
  101. return;
  102. if (all[index].filename == NULL || all[index].function == NULL)
  103. {
  104. fprintf (stderr, "%s: [%d]: missing file name or function name\n",
  105. name, index);
  106. *failed = 1;
  107. return;
  108. }
  109. if (strcmp (base (all[index].filename), "btest.c") != 0)
  110. {
  111. fprintf (stderr, "%s: [%d]: got %s expected test.c\n", name, index,
  112. all[index].filename);
  113. *failed = 1;
  114. }
  115. if (all[index].lineno != want_lineno)
  116. {
  117. fprintf (stderr, "%s: [%d]: got %d expected %d\n", name, index,
  118. all[index].lineno, want_lineno);
  119. *failed = 1;
  120. }
  121. if (strcmp (all[index].function, want_function) != 0)
  122. {
  123. fprintf (stderr, "%s: [%d]: got %s expected %s\n", name, index,
  124. all[index].function, want_function);
  125. *failed = 1;
  126. }
  127. }
  128. /* The backtrace callback function. */
  129. static int
  130. callback_one (void *vdata, uintptr_t pc ATTRIBUTE_UNUSED,
  131. const char *filename, int lineno, const char *function)
  132. {
  133. struct bdata *data = (struct bdata *) vdata;
  134. struct info *p;
  135. if (data->index >= data->max)
  136. {
  137. fprintf (stderr, "callback_one: callback called too many times\n");
  138. data->failed = 1;
  139. return 1;
  140. }
  141. p = &data->all[data->index];
  142. if (filename == NULL)
  143. p->filename = NULL;
  144. else
  145. {
  146. p->filename = strdup (filename);
  147. assert (p->filename != NULL);
  148. }
  149. p->lineno = lineno;
  150. if (function == NULL)
  151. p->function = NULL;
  152. else
  153. {
  154. p->function = strdup (function);
  155. assert (p->function != NULL);
  156. }
  157. ++data->index;
  158. return 0;
  159. }
  160. /* An error callback passed to backtrace. */
  161. static void
  162. error_callback_one (void *vdata, const char *msg, int errnum)
  163. {
  164. struct bdata *data = (struct bdata *) vdata;
  165. fprintf (stderr, "%s", msg);
  166. if (errnum > 0)
  167. fprintf (stderr, ": %s", strerror (errnum));
  168. fprintf (stderr, "\n");
  169. data->failed = 1;
  170. }
  171. /* The backtrace_simple callback function. */
  172. static int
  173. callback_two (void *vdata, uintptr_t pc)
  174. {
  175. struct sdata *data = (struct sdata *) vdata;
  176. if (data->index >= data->max)
  177. {
  178. fprintf (stderr, "callback_two: callback called too many times\n");
  179. data->failed = 1;
  180. return 1;
  181. }
  182. data->addrs[data->index] = pc;
  183. ++data->index;
  184. return 0;
  185. }
  186. /* An error callback passed to backtrace_simple. */
  187. static void
  188. error_callback_two (void *vdata, const char *msg, int errnum)
  189. {
  190. struct sdata *data = (struct sdata *) vdata;
  191. fprintf (stderr, "%s", msg);
  192. if (errnum > 0)
  193. fprintf (stderr, ": %s", strerror (errnum));
  194. fprintf (stderr, "\n");
  195. data->failed = 1;
  196. }
  197. /* The backtrace_syminfo callback function. */
  198. static void
  199. callback_three (void *vdata, uintptr_t pc ATTRIBUTE_UNUSED,
  200. const char *symname, uintptr_t symval,
  201. uintptr_t symsize)
  202. {
  203. struct symdata *data = (struct symdata *) vdata;
  204. if (symname == NULL)
  205. data->name = NULL;
  206. else
  207. {
  208. data->name = strdup (symname);
  209. assert (data->name != NULL);
  210. }
  211. data->val = symval;
  212. data->size = symsize;
  213. }
  214. /* The backtrace_syminfo error callback function. */
  215. static void
  216. error_callback_three (void *vdata, const char *msg, int errnum)
  217. {
  218. struct symdata *data = (struct symdata *) vdata;
  219. fprintf (stderr, "%s", msg);
  220. if (errnum > 0)
  221. fprintf (stderr, ": %s", strerror (errnum));
  222. fprintf (stderr, "\n");
  223. data->failed = 1;
  224. }
  225. /* Test the backtrace function with non-inlined functions. */
  226. static int test1 (void) __attribute__ ((noinline, unused));
  227. static int f2 (int) __attribute__ ((noinline));
  228. static int f3 (int, int) __attribute__ ((noinline));
  229. static int
  230. test1 (void)
  231. {
  232. /* Returning a value here and elsewhere avoids a tailcall which
  233. would mess up the backtrace. */
  234. return f2 (__LINE__) + 1;
  235. }
  236. static int
  237. f2 (int f1line)
  238. {
  239. return f3 (f1line, __LINE__) + 2;
  240. }
  241. static int
  242. f3 (int f1line, int f2line)
  243. {
  244. struct info all[20];
  245. struct bdata data;
  246. int f3line;
  247. int i;
  248. data.all = &all[0];
  249. data.index = 0;
  250. data.max = 20;
  251. data.failed = 0;
  252. f3line = __LINE__ + 1;
  253. i = backtrace_full (state, 0, callback_one, error_callback_one, &data);
  254. if (i != 0)
  255. {
  256. fprintf (stderr, "test1: unexpected return value %d\n", i);
  257. data.failed = 1;
  258. }
  259. if (data.index < 3)
  260. {
  261. fprintf (stderr,
  262. "test1: not enough frames; got %zu, expected at least 3\n",
  263. data.index);
  264. data.failed = 1;
  265. }
  266. check ("test1", 0, all, f3line, "f3", &data.failed);
  267. check ("test1", 1, all, f2line, "f2", &data.failed);
  268. check ("test1", 2, all, f1line, "test1", &data.failed);
  269. printf ("%s: backtrace_full noinline\n", data.failed ? "FAIL" : "PASS");
  270. if (data.failed)
  271. ++failures;
  272. return failures;
  273. }
  274. /* Test the backtrace function with inlined functions. */
  275. static inline int test2 (void) __attribute__ ((always_inline, unused));
  276. static inline int f12 (int) __attribute__ ((always_inline));
  277. static inline int f13 (int, int) __attribute__ ((always_inline));
  278. static inline int
  279. test2 (void)
  280. {
  281. return f12 (__LINE__) + 1;
  282. }
  283. static inline int
  284. f12 (int f1line)
  285. {
  286. return f13 (f1line, __LINE__) + 2;
  287. }
  288. static inline int
  289. f13 (int f1line, int f2line)
  290. {
  291. struct info all[20];
  292. struct bdata data;
  293. int f3line;
  294. int i;
  295. data.all = &all[0];
  296. data.index = 0;
  297. data.max = 20;
  298. data.failed = 0;
  299. f3line = __LINE__ + 1;
  300. i = backtrace_full (state, 0, callback_one, error_callback_one, &data);
  301. if (i != 0)
  302. {
  303. fprintf (stderr, "test2: unexpected return value %d\n", i);
  304. data.failed = 1;
  305. }
  306. check ("test2", 0, all, f3line, "f13", &data.failed);
  307. check ("test2", 1, all, f2line, "f12", &data.failed);
  308. check ("test2", 2, all, f1line, "test2", &data.failed);
  309. printf ("%s: backtrace_full inline\n", data.failed ? "FAIL" : "PASS");
  310. if (data.failed)
  311. ++failures;
  312. return failures;
  313. }
  314. /* Test the backtrace_simple function with non-inlined functions. */
  315. static int test3 (void) __attribute__ ((noinline, unused));
  316. static int f22 (int) __attribute__ ((noinline));
  317. static int f23 (int, int) __attribute__ ((noinline));
  318. static int
  319. test3 (void)
  320. {
  321. return f22 (__LINE__) + 1;
  322. }
  323. static int
  324. f22 (int f1line)
  325. {
  326. return f23 (f1line, __LINE__) + 2;
  327. }
  328. static int
  329. f23 (int f1line, int f2line)
  330. {
  331. uintptr_t addrs[20];
  332. struct sdata data;
  333. int f3line;
  334. int i;
  335. data.addrs = &addrs[0];
  336. data.index = 0;
  337. data.max = 20;
  338. data.failed = 0;
  339. f3line = __LINE__ + 1;
  340. i = backtrace_simple (state, 0, callback_two, error_callback_two, &data);
  341. if (i != 0)
  342. {
  343. fprintf (stderr, "test3: unexpected return value %d\n", i);
  344. data.failed = 1;
  345. }
  346. if (!data.failed)
  347. {
  348. struct info all[20];
  349. struct bdata bdata;
  350. int j;
  351. bdata.all = &all[0];
  352. bdata.index = 0;
  353. bdata.max = 20;
  354. bdata.failed = 0;
  355. for (j = 0; j < 3; ++j)
  356. {
  357. i = backtrace_pcinfo (state, addrs[j], callback_one,
  358. error_callback_one, &bdata);
  359. if (i != 0)
  360. {
  361. fprintf (stderr,
  362. ("test3: unexpected return value "
  363. "from backtrace_pcinfo %d\n"),
  364. i);
  365. bdata.failed = 1;
  366. }
  367. if (!bdata.failed && bdata.index != (size_t) (j + 1))
  368. {
  369. fprintf (stderr,
  370. ("wrong number of calls from backtrace_pcinfo "
  371. "got %u expected %d\n"),
  372. (unsigned int) bdata.index, j + 1);
  373. bdata.failed = 1;
  374. }
  375. }
  376. check ("test3", 0, all, f3line, "f23", &bdata.failed);
  377. check ("test3", 1, all, f2line, "f22", &bdata.failed);
  378. check ("test3", 2, all, f1line, "test3", &bdata.failed);
  379. if (bdata.failed)
  380. data.failed = 1;
  381. for (j = 0; j < 3; ++j)
  382. {
  383. struct symdata symdata;
  384. symdata.name = NULL;
  385. symdata.val = 0;
  386. symdata.size = 0;
  387. symdata.failed = 0;
  388. i = backtrace_syminfo (state, addrs[j], callback_three,
  389. error_callback_three, &symdata);
  390. if (i == 0)
  391. {
  392. fprintf (stderr,
  393. ("test3: [%d]: unexpected return value "
  394. "from backtrace_syminfo %d\n"),
  395. j, i);
  396. symdata.failed = 1;
  397. }
  398. if (!symdata.failed)
  399. {
  400. const char *expected;
  401. switch (j)
  402. {
  403. case 0:
  404. expected = "f23";
  405. break;
  406. case 1:
  407. expected = "f22";
  408. break;
  409. case 2:
  410. expected = "test3";
  411. break;
  412. default:
  413. assert (0);
  414. }
  415. if (symdata.name == NULL)
  416. {
  417. fprintf (stderr, "test3: [%d]: NULL syminfo name\n", j);
  418. symdata.failed = 1;
  419. }
  420. /* Use strncmp, not strcmp, because GCC might create a
  421. clone. */
  422. else if (strncmp (symdata.name, expected, strlen (expected))
  423. != 0)
  424. {
  425. fprintf (stderr,
  426. ("test3: [%d]: unexpected syminfo name "
  427. "got %s expected %s\n"),
  428. j, symdata.name, expected);
  429. symdata.failed = 1;
  430. }
  431. }
  432. if (symdata.failed)
  433. data.failed = 1;
  434. }
  435. }
  436. printf ("%s: backtrace_simple noinline\n", data.failed ? "FAIL" : "PASS");
  437. if (data.failed)
  438. ++failures;
  439. return failures;
  440. }
  441. /* Test the backtrace_simple function with inlined functions. */
  442. static inline int test4 (void) __attribute__ ((always_inline, unused));
  443. static inline int f32 (int) __attribute__ ((always_inline));
  444. static inline int f33 (int, int) __attribute__ ((always_inline));
  445. static inline int
  446. test4 (void)
  447. {
  448. return f32 (__LINE__) + 1;
  449. }
  450. static inline int
  451. f32 (int f1line)
  452. {
  453. return f33 (f1line, __LINE__) + 2;
  454. }
  455. static inline int
  456. f33 (int f1line, int f2line)
  457. {
  458. uintptr_t addrs[20];
  459. struct sdata data;
  460. int f3line;
  461. int i;
  462. data.addrs = &addrs[0];
  463. data.index = 0;
  464. data.max = 20;
  465. data.failed = 0;
  466. f3line = __LINE__ + 1;
  467. i = backtrace_simple (state, 0, callback_two, error_callback_two, &data);
  468. if (i != 0)
  469. {
  470. fprintf (stderr, "test3: unexpected return value %d\n", i);
  471. data.failed = 1;
  472. }
  473. if (!data.failed)
  474. {
  475. struct info all[20];
  476. struct bdata bdata;
  477. bdata.all = &all[0];
  478. bdata.index = 0;
  479. bdata.max = 20;
  480. bdata.failed = 0;
  481. i = backtrace_pcinfo (state, addrs[0], callback_one, error_callback_one,
  482. &bdata);
  483. if (i != 0)
  484. {
  485. fprintf (stderr,
  486. ("test4: unexpected return value "
  487. "from backtrace_pcinfo %d\n"),
  488. i);
  489. bdata.failed = 1;
  490. }
  491. check ("test4", 0, all, f3line, "f33", &bdata.failed);
  492. check ("test4", 1, all, f2line, "f32", &bdata.failed);
  493. check ("test4", 2, all, f1line, "test4", &bdata.failed);
  494. if (bdata.failed)
  495. data.failed = 1;
  496. }
  497. printf ("%s: backtrace_simple inline\n", data.failed ? "FAIL" : "PASS");
  498. if (data.failed)
  499. ++failures;
  500. return failures;
  501. }
  502. int global = 1;
  503. static int
  504. test5 (void)
  505. {
  506. struct symdata symdata;
  507. int i;
  508. uintptr_t addr = (uintptr_t) &global;
  509. if (sizeof (global) > 1)
  510. addr += 1;
  511. symdata.name = NULL;
  512. symdata.val = 0;
  513. symdata.size = 0;
  514. symdata.failed = 0;
  515. i = backtrace_syminfo (state, addr, callback_three,
  516. error_callback_three, &symdata);
  517. if (i == 0)
  518. {
  519. fprintf (stderr,
  520. "test5: unexpected return value from backtrace_syminfo %d\n",
  521. i);
  522. symdata.failed = 1;
  523. }
  524. if (!symdata.failed)
  525. {
  526. if (symdata.name == NULL)
  527. {
  528. fprintf (stderr, "test5: NULL syminfo name\n");
  529. symdata.failed = 1;
  530. }
  531. else if (strcmp (symdata.name, "global") != 0)
  532. {
  533. fprintf (stderr,
  534. "test5: unexpected syminfo name got %s expected %s\n",
  535. symdata.name, "global");
  536. symdata.failed = 1;
  537. }
  538. else if (symdata.val != (uintptr_t) &global)
  539. {
  540. fprintf (stderr,
  541. "test5: unexpected syminfo value got %lx expected %lx\n",
  542. (unsigned long) symdata.val,
  543. (unsigned long) (uintptr_t) &global);
  544. symdata.failed = 1;
  545. }
  546. else if (symdata.size != sizeof (global))
  547. {
  548. fprintf (stderr,
  549. "test5: unexpected syminfo size got %lx expected %lx\n",
  550. (unsigned long) symdata.size,
  551. (unsigned long) sizeof (global));
  552. symdata.failed = 1;
  553. }
  554. }
  555. printf ("%s: backtrace_syminfo variable\n",
  556. symdata.failed ? "FAIL" : "PASS");
  557. if (symdata.failed)
  558. ++failures;
  559. return failures;
  560. }
  561. static void
  562. error_callback_create (void *data ATTRIBUTE_UNUSED, const char *msg,
  563. int errnum)
  564. {
  565. fprintf (stderr, "%s", msg);
  566. if (errnum > 0)
  567. fprintf (stderr, ": %s", strerror (errnum));
  568. fprintf (stderr, "\n");
  569. exit (EXIT_FAILURE);
  570. }
  571. /* Run all the tests. */
  572. int
  573. main (int argc ATTRIBUTE_UNUSED, char **argv)
  574. {
  575. state = backtrace_create_state (argv[0], BACKTRACE_SUPPORTS_THREADS,
  576. error_callback_create, NULL);
  577. #if BACKTRACE_SUPPORTED
  578. test1 ();
  579. test2 ();
  580. test3 ();
  581. test4 ();
  582. test5 ();
  583. #endif
  584. exit (failures ? EXIT_FAILURE : EXIT_SUCCESS);
  585. }