Remove unused variable and field
[official-gcc.git] / libbacktrace / btest.c
blobcc647b8d81c115edda53959afc89b28d1d0dca31
1 /* btest.c -- Test for libbacktrace library
2 Copyright (C) 2012-2013 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Google.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
9 (1) Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 (2) Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the
15 distribution.
17 (3) The name of the author may not be used to
18 endorse or promote products derived from this software without
19 specific prior written permission.
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE. */
33 /* This program tests the externally visible interfaces of the
34 libbacktrace library. */
36 #include <assert.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
41 #include "filenames.h"
43 #include "backtrace.h"
44 #include "backtrace-supported.h"
46 /* Portable attribute syntax. Actually some of these tests probably
47 won't work if the attributes are not recognized. */
49 #ifndef GCC_VERSION
50 # define GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)
51 #endif
53 #if (GCC_VERSION < 2007)
54 # define __attribute__(x)
55 #endif
57 #ifndef ATTRIBUTE_UNUSED
58 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
59 #endif
61 /* Used to collect backtrace info. */
63 struct info
65 char *filename;
66 int lineno;
67 char *function;
70 /* Passed to backtrace callback function. */
72 struct bdata
74 struct info *all;
75 size_t index;
76 size_t max;
77 int failed;
80 /* Passed to backtrace_simple callback function. */
82 struct sdata
84 uintptr_t *addrs;
85 size_t index;
86 size_t max;
87 int failed;
90 /* Passed to backtrace_syminfo callback function. */
92 struct symdata
94 const char *name;
95 uintptr_t val;
96 int failed;
99 /* The backtrace state. */
101 static void *state;
103 /* The number of failures. */
105 static int failures;
107 /* Return the base name in a path. */
109 static const char *
110 base (const char *p)
112 const char *last;
113 const char *s;
115 last = NULL;
116 for (s = p; *s != '\0'; ++s)
118 if (IS_DIR_SEPARATOR (*s))
119 last = s + 1;
121 return last != NULL ? last : p;
124 /* Check an entry in a struct info array. */
126 static void
127 check (const char *name, int index, const struct info *all, int want_lineno,
128 const char *want_function, int *failed)
130 if (*failed)
131 return;
132 if (strcmp (base (all[index].filename), "btest.c") != 0)
134 fprintf (stderr, "%s: [%d]: got %s expected test.c\n", name, index,
135 all[index].filename);
136 *failed = 1;
138 if (all[index].lineno != want_lineno)
140 fprintf (stderr, "%s: [%d]: got %d expected %d\n", name, index,
141 all[index].lineno, want_lineno);
142 *failed = 1;
144 if (strcmp (all[index].function, want_function) != 0)
146 fprintf (stderr, "%s: [%d]: got %s expected %s\n", name, index,
147 all[index].function, want_function);
148 *failed = 1;
152 /* The backtrace callback function. */
154 static int
155 callback_one (void *vdata, uintptr_t pc ATTRIBUTE_UNUSED,
156 const char *filename, int lineno, const char *function)
158 struct bdata *data = (struct bdata *) vdata;
159 struct info *p;
161 if (data->index >= data->max)
163 fprintf (stderr, "callback_one: callback called too many times\n");
164 data->failed = 1;
165 return 1;
168 p = &data->all[data->index];
169 if (filename == NULL)
170 p->filename = NULL;
171 else
173 p->filename = strdup (filename);
174 assert (p->filename != NULL);
176 p->lineno = lineno;
177 if (function == NULL)
178 p->function = NULL;
179 else
181 p->function = strdup (function);
182 assert (p->function != NULL);
184 ++data->index;
186 return 0;
189 /* An error callback passed to backtrace. */
191 static void
192 error_callback_one (void *vdata, const char *msg, int errnum)
194 struct bdata *data = (struct bdata *) vdata;
196 fprintf (stderr, "%s", msg);
197 if (errnum > 0)
198 fprintf (stderr, ": %s", strerror (errnum));
199 fprintf (stderr, "\n");
200 data->failed = 1;
203 /* The backtrace_simple callback function. */
205 static int
206 callback_two (void *vdata, uintptr_t pc)
208 struct sdata *data = (struct sdata *) vdata;
210 if (data->index >= data->max)
212 fprintf (stderr, "callback_two: callback called too many times\n");
213 data->failed = 1;
214 return 1;
217 data->addrs[data->index] = pc;
218 ++data->index;
220 return 0;
223 /* An error callback passed to backtrace_simple. */
225 static void
226 error_callback_two (void *vdata, const char *msg, int errnum)
228 struct sdata *data = (struct sdata *) vdata;
230 fprintf (stderr, "%s", msg);
231 if (errnum > 0)
232 fprintf (stderr, ": %s", strerror (errnum));
233 fprintf (stderr, "\n");
234 data->failed = 1;
237 /* The backtrace_syminfo callback function. */
239 static void
240 callback_three (void *vdata, uintptr_t pc ATTRIBUTE_UNUSED,
241 const char *symname, uintptr_t symval)
243 struct symdata *data = (struct symdata *) vdata;
245 if (symname == NULL)
246 data->name = NULL;
247 else
249 data->name = strdup (symname);
250 assert (data->name != NULL);
252 data->val = symval;
255 /* The backtrace_syminfo error callback function. */
257 static void
258 error_callback_three (void *vdata, const char *msg, int errnum)
260 struct symdata *data = (struct symdata *) vdata;
262 fprintf (stderr, "%s", msg);
263 if (errnum > 0)
264 fprintf (stderr, ": %s", strerror (errnum));
265 fprintf (stderr, "\n");
266 data->failed = 1;
269 /* Test the backtrace function with non-inlined functions. */
271 static int test1 (void) __attribute__ ((noinline, unused));
272 static int f2 (int) __attribute__ ((noinline));
273 static int f3 (int, int) __attribute__ ((noinline));
275 static int
276 test1 (void)
278 /* Returning a value here and elsewhere avoids a tailcall which
279 would mess up the backtrace. */
280 return f2 (__LINE__) + 1;
283 static int
284 f2 (int f1line)
286 return f3 (f1line, __LINE__) + 2;
289 static int
290 f3 (int f1line, int f2line)
292 struct info all[20];
293 struct bdata data;
294 int f3line;
295 int i;
297 data.all = &all[0];
298 data.index = 0;
299 data.max = 20;
300 data.failed = 0;
302 f3line = __LINE__ + 1;
303 i = backtrace_full (state, 0, callback_one, error_callback_one, &data);
305 if (i != 0)
307 fprintf (stderr, "test1: unexpected return value %d\n", i);
308 data.failed = 1;
311 check ("test1", 0, all, f3line, "f3", &data.failed);
312 check ("test1", 1, all, f2line, "f2", &data.failed);
313 check ("test1", 2, all, f1line, "test1", &data.failed);
315 printf ("%s: backtrace_full noinline\n", data.failed ? "FAIL" : "PASS");
317 if (data.failed)
318 ++failures;
320 return failures;
323 /* Test the backtrace function with inlined functions. */
325 static inline int test2 (void) __attribute__ ((always_inline, unused));
326 static inline int f12 (int) __attribute__ ((always_inline));
327 static inline int f13 (int, int) __attribute__ ((always_inline));
329 static inline int
330 test2 (void)
332 return f12 (__LINE__) + 1;
335 static inline int
336 f12 (int f1line)
338 return f13 (f1line, __LINE__) + 2;
341 static inline int
342 f13 (int f1line, int f2line)
344 struct info all[20];
345 struct bdata data;
346 int f3line;
347 int i;
349 data.all = &all[0];
350 data.index = 0;
351 data.max = 20;
352 data.failed = 0;
354 f3line = __LINE__ + 1;
355 i = backtrace_full (state, 0, callback_one, error_callback_one, &data);
357 if (i != 0)
359 fprintf (stderr, "test2: unexpected return value %d\n", i);
360 data.failed = 1;
363 check ("test2", 0, all, f3line, "f13", &data.failed);
364 check ("test2", 1, all, f2line, "f12", &data.failed);
365 check ("test2", 2, all, f1line, "test2", &data.failed);
367 printf ("%s: backtrace_full inline\n", data.failed ? "FAIL" : "PASS");
369 if (data.failed)
370 ++failures;
372 return failures;
375 /* Test the backtrace_simple function with non-inlined functions. */
377 static int test3 (void) __attribute__ ((noinline, unused));
378 static int f22 (int) __attribute__ ((noinline));
379 static int f23 (int, int) __attribute__ ((noinline));
381 static int
382 test3 (void)
384 return f22 (__LINE__) + 1;
387 static int
388 f22 (int f1line)
390 return f23 (f1line, __LINE__) + 2;
393 static int
394 f23 (int f1line, int f2line)
396 uintptr_t addrs[20];
397 struct sdata data;
398 int f3line;
399 int i;
401 data.addrs = &addrs[0];
402 data.index = 0;
403 data.max = 20;
404 data.failed = 0;
406 f3line = __LINE__ + 1;
407 i = backtrace_simple (state, 0, callback_two, error_callback_two, &data);
409 if (i != 0)
411 fprintf (stderr, "test3: unexpected return value %d\n", i);
412 data.failed = 1;
415 if (!data.failed)
417 struct info all[20];
418 struct bdata bdata;
419 int j;
421 bdata.all = &all[0];
422 bdata.index = 0;
423 bdata.max = 20;
424 bdata.failed = 0;
426 for (j = 0; j < 3; ++j)
428 i = backtrace_pcinfo (state, addrs[j], callback_one,
429 error_callback_one, &bdata);
430 if (i != 0)
432 fprintf (stderr,
433 ("test3: unexpected return value "
434 "from backtrace_pcinfo %d\n"),
436 bdata.failed = 1;
438 if (!bdata.failed && bdata.index != (size_t) (j + 1))
440 fprintf (stderr,
441 ("wrong number of calls from backtrace_pcinfo "
442 "got %u expected %d\n"),
443 (unsigned int) bdata.index, j + 1);
444 bdata.failed = 1;
448 check ("test3", 0, all, f3line, "f23", &bdata.failed);
449 check ("test3", 1, all, f2line, "f22", &bdata.failed);
450 check ("test3", 2, all, f1line, "test3", &bdata.failed);
452 if (bdata.failed)
453 data.failed = 1;
455 for (j = 0; j < 3; ++j)
457 struct symdata symdata;
459 symdata.name = NULL;
460 symdata.val = 0;
461 symdata.failed = 0;
463 i = backtrace_syminfo (state, addrs[j], callback_three,
464 error_callback_three, &symdata);
465 if (i == 0)
467 fprintf (stderr,
468 ("test3: [%d]: unexpected return value "
469 "from backtrace_syminfo %d\n"),
470 j, i);
471 symdata.failed = 1;
474 if (!symdata.failed)
476 const char *expected;
478 switch (j)
480 case 0:
481 expected = "f23";
482 break;
483 case 1:
484 expected = "f22";
485 break;
486 case 2:
487 expected = "test3";
488 break;
489 default:
490 assert (0);
493 if (symdata.name == NULL)
495 fprintf (stderr, "test3: [%d]: NULL syminfo name\n", j);
496 symdata.failed = 1;
498 /* Use strncmp, not strcmp, because GCC might create a
499 clone. */
500 else if (strncmp (symdata.name, expected, strlen (expected))
501 != 0)
503 fprintf (stderr,
504 ("test3: [%d]: unexpected syminfo name "
505 "got %s expected %s\n"),
506 j, symdata.name, expected);
507 symdata.failed = 1;
511 if (symdata.failed)
512 data.failed = 1;
516 printf ("%s: backtrace_simple noinline\n", data.failed ? "FAIL" : "PASS");
518 if (data.failed)
519 ++failures;
521 return failures;
524 /* Test the backtrace_simple function with inlined functions. */
526 static inline int test4 (void) __attribute__ ((always_inline, unused));
527 static inline int f32 (int) __attribute__ ((always_inline));
528 static inline int f33 (int, int) __attribute__ ((always_inline));
530 static inline int
531 test4 (void)
533 return f32 (__LINE__) + 1;
536 static inline int
537 f32 (int f1line)
539 return f33 (f1line, __LINE__) + 2;
542 static inline int
543 f33 (int f1line, int f2line)
545 uintptr_t addrs[20];
546 struct sdata data;
547 int f3line;
548 int i;
550 data.addrs = &addrs[0];
551 data.index = 0;
552 data.max = 20;
553 data.failed = 0;
555 f3line = __LINE__ + 1;
556 i = backtrace_simple (state, 0, callback_two, error_callback_two, &data);
558 if (i != 0)
560 fprintf (stderr, "test3: unexpected return value %d\n", i);
561 data.failed = 1;
564 if (!data.failed)
566 struct info all[20];
567 struct bdata bdata;
569 bdata.all = &all[0];
570 bdata.index = 0;
571 bdata.max = 20;
572 bdata.failed = 0;
574 i = backtrace_pcinfo (state, addrs[0], callback_one, error_callback_one,
575 &bdata);
576 if (i != 0)
578 fprintf (stderr,
579 ("test4: unexpected return value "
580 "from backtrace_pcinfo %d\n"),
582 bdata.failed = 1;
585 check ("test4", 0, all, f3line, "f33", &bdata.failed);
586 check ("test4", 1, all, f2line, "f32", &bdata.failed);
587 check ("test4", 2, all, f1line, "test4", &bdata.failed);
589 if (bdata.failed)
590 data.failed = 1;
593 printf ("%s: backtrace_simple inline\n", data.failed ? "FAIL" : "PASS");
595 if (data.failed)
596 ++failures;
598 return failures;
601 static void
602 error_callback_create (void *data ATTRIBUTE_UNUSED, const char *msg,
603 int errnum)
605 fprintf (stderr, "%s", msg);
606 if (errnum > 0)
607 fprintf (stderr, ": %s", strerror (errnum));
608 fprintf (stderr, "\n");
609 exit (EXIT_FAILURE);
612 /* Run all the tests. */
615 main (int argc ATTRIBUTE_UNUSED, char **argv)
617 state = backtrace_create_state (argv[0], BACKTRACE_SUPPORTS_THREADS,
618 error_callback_create, NULL);
620 #if BACKTRACE_SUPPORTED
621 test1 ();
622 test2 ();
623 test3 ();
624 test4 ();
625 #endif
627 exit (failures ? EXIT_FAILURE : EXIT_SUCCESS);