hppa: Fix LO_SUM DLTIND14R address support in PRINT_OPERAND_ADDRESS
[official-gcc.git] / libbacktrace / btest.c
blobd9fc372d33d966caf20a384a9a0eb46c0dcbeee0
1 /* btest.c -- Test for libbacktrace library
2 Copyright (C) 2012-2024 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>
40 #include <unistd.h>
41 #include <sys/stat.h>
43 #include "filenames.h"
45 #include "backtrace.h"
46 #include "backtrace-supported.h"
48 #include "testlib.h"
50 /* Test the backtrace function with non-inlined functions. */
52 static int test1 (void) __attribute__ ((noinline, noclone, unused));
53 static int f2 (int) __attribute__ ((noinline, noclone));
54 static int f3 (int, int) __attribute__ ((noinline, noclone));
56 static int
57 test1 (void)
59 /* Returning a value here and elsewhere avoids a tailcall which
60 would mess up the backtrace. */
61 return f2 (__LINE__) + 1;
64 static int
65 f2 (int f1line)
67 return f3 (f1line, __LINE__) + 2;
70 static int
71 f3 (int f1line, int f2line)
73 struct info all[20];
74 struct bdata data;
75 int f3line;
76 int i;
78 data.all = &all[0];
79 data.index = 0;
80 data.max = 20;
81 data.failed = 0;
83 f3line = __LINE__ + 1;
84 i = backtrace_full (state, 0, callback_one, error_callback_one, &data);
86 if (i != 0)
88 fprintf (stderr, "test1: unexpected return value %d\n", i);
89 data.failed = 1;
92 if (data.index < 3)
94 fprintf (stderr,
95 "test1: not enough frames; got %zu, expected at least 3\n",
96 data.index);
97 data.failed = 1;
100 check ("test1", 0, all, f3line, "f3", "btest.c", &data.failed);
101 check ("test1", 1, all, f2line, "f2", "btest.c", &data.failed);
102 check ("test1", 2, all, f1line, "test1", "btest.c", &data.failed);
104 printf ("%s: backtrace_full noinline\n", data.failed ? "FAIL" : "PASS");
106 if (data.failed)
107 ++failures;
109 return failures;
112 /* Test the backtrace function with inlined functions. */
114 static inline int test2 (void) __attribute__ ((always_inline, unused));
115 static inline int f12 (int) __attribute__ ((always_inline));
116 static inline int f13 (int, int) __attribute__ ((always_inline));
118 static inline int
119 test2 (void)
121 return f12 (__LINE__) + 1;
124 static inline int
125 f12 (int f1line)
127 return f13 (f1line, __LINE__) + 2;
130 static inline int
131 f13 (int f1line, int f2line)
133 struct info all[20];
134 struct bdata data;
135 int f3line;
136 int i;
138 data.all = &all[0];
139 data.index = 0;
140 data.max = 20;
141 data.failed = 0;
143 f3line = __LINE__ + 1;
144 i = backtrace_full (state, 0, callback_one, error_callback_one, &data);
146 if (i != 0)
148 fprintf (stderr, "test2: unexpected return value %d\n", i);
149 data.failed = 1;
152 check ("test2", 0, all, f3line, "f13", "btest.c", &data.failed);
153 check ("test2", 1, all, f2line, "f12", "btest.c", &data.failed);
154 check ("test2", 2, all, f1line, "test2", "btest.c", &data.failed);
156 printf ("%s: backtrace_full inline\n", data.failed ? "FAIL" : "PASS");
158 if (data.failed)
159 ++failures;
161 return failures;
164 /* Test the backtrace_simple function with non-inlined functions. */
166 static int test3 (void) __attribute__ ((noinline, noclone, unused));
167 static int f22 (int) __attribute__ ((noinline, noclone));
168 static int f23 (int, int) __attribute__ ((noinline, noclone));
170 static int
171 test3 (void)
173 return f22 (__LINE__) + 1;
176 static int
177 f22 (int f1line)
179 return f23 (f1line, __LINE__) + 2;
182 static int
183 f23 (int f1line, int f2line)
185 uintptr_t addrs[20];
186 struct sdata data;
187 int f3line;
188 int i;
190 data.addrs = &addrs[0];
191 data.index = 0;
192 data.max = 20;
193 data.failed = 0;
195 f3line = __LINE__ + 1;
196 i = backtrace_simple (state, 0, callback_two, error_callback_two, &data);
198 if (i != 0)
200 fprintf (stderr, "test3: unexpected return value %d\n", i);
201 data.failed = 1;
204 if (!data.failed)
206 struct info all[20];
207 struct bdata bdata;
208 int j;
210 bdata.all = &all[0];
211 bdata.index = 0;
212 bdata.max = 20;
213 bdata.failed = 0;
215 for (j = 0; j < 3; ++j)
217 i = backtrace_pcinfo (state, addrs[j], callback_one,
218 error_callback_one, &bdata);
219 if (i != 0)
221 fprintf (stderr,
222 ("test3: unexpected return value "
223 "from backtrace_pcinfo %d\n"),
225 bdata.failed = 1;
227 if (!bdata.failed && bdata.index != (size_t) (j + 1))
229 fprintf (stderr,
230 ("wrong number of calls from backtrace_pcinfo "
231 "got %u expected %d\n"),
232 (unsigned int) bdata.index, j + 1);
233 bdata.failed = 1;
237 check ("test3", 0, all, f3line, "f23", "btest.c", &bdata.failed);
238 check ("test3", 1, all, f2line, "f22", "btest.c", &bdata.failed);
239 check ("test3", 2, all, f1line, "test3", "btest.c", &bdata.failed);
241 if (bdata.failed)
242 data.failed = 1;
244 for (j = 0; j < 3; ++j)
246 struct symdata symdata;
248 symdata.name = NULL;
249 symdata.val = 0;
250 symdata.size = 0;
251 symdata.failed = 0;
253 i = backtrace_syminfo (state, addrs[j], callback_three,
254 error_callback_three, &symdata);
255 if (i == 0)
257 fprintf (stderr,
258 ("test3: [%d]: unexpected return value "
259 "from backtrace_syminfo %d\n"),
260 j, i);
261 symdata.failed = 1;
264 if (!symdata.failed)
266 const char *expected;
268 switch (j)
270 case 0:
271 expected = "f23";
272 break;
273 case 1:
274 expected = "f22";
275 break;
276 case 2:
277 expected = "test3";
278 break;
279 default:
280 assert (0);
283 if (symdata.name == NULL)
285 fprintf (stderr, "test3: [%d]: NULL syminfo name\n", j);
286 symdata.failed = 1;
288 /* Use strncmp, not strcmp, because GCC might create a
289 clone. */
290 else if (strncmp (symdata.name, expected, strlen (expected))
291 != 0)
293 fprintf (stderr,
294 ("test3: [%d]: unexpected syminfo name "
295 "got %s expected %s\n"),
296 j, symdata.name, expected);
297 symdata.failed = 1;
301 if (symdata.failed)
302 data.failed = 1;
306 printf ("%s: backtrace_simple noinline\n", data.failed ? "FAIL" : "PASS");
308 if (data.failed)
309 ++failures;
311 return failures;
314 /* Test the backtrace_simple function with inlined functions. */
316 static inline int test4 (void) __attribute__ ((always_inline, unused));
317 static inline int f32 (int) __attribute__ ((always_inline));
318 static inline int f33 (int, int) __attribute__ ((always_inline));
320 static inline int
321 test4 (void)
323 return f32 (__LINE__) + 1;
326 static inline int
327 f32 (int f1line)
329 return f33 (f1line, __LINE__) + 2;
332 static inline int
333 f33 (int f1line, int f2line)
335 uintptr_t addrs[20];
336 struct sdata data;
337 int f3line;
338 int i;
340 data.addrs = &addrs[0];
341 data.index = 0;
342 data.max = 20;
343 data.failed = 0;
345 f3line = __LINE__ + 1;
346 i = backtrace_simple (state, 0, callback_two, error_callback_two, &data);
348 if (i != 0)
350 fprintf (stderr, "test3: unexpected return value %d\n", i);
351 data.failed = 1;
354 if (!data.failed)
356 struct info all[20];
357 struct bdata bdata;
359 bdata.all = &all[0];
360 bdata.index = 0;
361 bdata.max = 20;
362 bdata.failed = 0;
364 i = backtrace_pcinfo (state, addrs[0], callback_one, error_callback_one,
365 &bdata);
366 if (i != 0)
368 fprintf (stderr,
369 ("test4: unexpected return value "
370 "from backtrace_pcinfo %d\n"),
372 bdata.failed = 1;
375 check ("test4", 0, all, f3line, "f33", "btest.c", &bdata.failed);
376 check ("test4", 1, all, f2line, "f32", "btest.c", &bdata.failed);
377 check ("test4", 2, all, f1line, "test4", "btest.c", &bdata.failed);
379 if (bdata.failed)
380 data.failed = 1;
383 printf ("%s: backtrace_simple inline\n", data.failed ? "FAIL" : "PASS");
385 if (data.failed)
386 ++failures;
388 return failures;
391 static int test5 (void) __attribute__ ((unused));
393 int global = 1;
395 static int
396 test5 (void)
398 struct symdata symdata;
399 int i;
400 uintptr_t addr = (uintptr_t) &global;
402 if (sizeof (global) > 1)
403 addr += 1;
405 symdata.name = NULL;
406 symdata.val = 0;
407 symdata.size = 0;
408 symdata.failed = 0;
410 i = backtrace_syminfo (state, addr, callback_three,
411 error_callback_three, &symdata);
412 if (i == 0)
414 fprintf (stderr,
415 "test5: unexpected return value from backtrace_syminfo %d\n",
417 symdata.failed = 1;
420 if (!symdata.failed)
422 if (symdata.name == NULL)
424 fprintf (stderr, "test5: NULL syminfo name\n");
425 symdata.failed = 1;
427 else if (!(strncmp (symdata.name, "global", 6) == 0
428 && (symdata.name[6] == '\0'|| symdata.name[6] == '.')))
430 fprintf (stderr,
431 "test5: unexpected syminfo name got %s expected %s\n",
432 symdata.name, "global");
433 symdata.failed = 1;
435 else if (symdata.val != (uintptr_t) &global)
437 fprintf (stderr,
438 "test5: unexpected syminfo value got %lx expected %lx\n",
439 (unsigned long) symdata.val,
440 (unsigned long) (uintptr_t) &global);
441 symdata.failed = 1;
443 else if (symdata.size != sizeof (global))
445 fprintf (stderr,
446 "test5: unexpected syminfo size got %lx expected %lx\n",
447 (unsigned long) symdata.size,
448 (unsigned long) sizeof (global));
449 symdata.failed = 1;
453 printf ("%s: backtrace_syminfo variable\n",
454 symdata.failed ? "FAIL" : "PASS");
456 if (symdata.failed)
457 ++failures;
459 return failures;
462 #define MIN_DESCRIPTOR 3
463 #define MAX_DESCRIPTOR 10
465 static int fstat_status[MAX_DESCRIPTOR];
467 /* Check files that are available. */
469 static void
470 check_available_files (void)
472 struct stat s;
473 for (unsigned i = MIN_DESCRIPTOR; i < MAX_DESCRIPTOR; i++)
474 fstat_status[i] = fstat (i, &s);
477 /* Check that are no files left open. */
479 static void
480 check_open_files (void)
482 for (unsigned i = MIN_DESCRIPTOR; i < MAX_DESCRIPTOR; i++)
484 if (fstat_status[i] != 0 && close (i) == 0)
486 fprintf (stderr,
487 "ERROR: descriptor %d still open after tests complete\n",
489 ++failures;
494 /* Run all the tests. */
497 main (int argc ATTRIBUTE_UNUSED, char **argv)
499 check_available_files ();
501 state = backtrace_create_state (argv[0], BACKTRACE_SUPPORTS_THREADS,
502 error_callback_create, NULL);
504 #if BACKTRACE_SUPPORTED
505 test1 ();
506 test2 ();
507 test3 ();
508 test4 ();
509 #if BACKTRACE_SUPPORTS_DATA
510 test5 ();
511 #endif
512 #endif
514 check_open_files ();
516 exit (failures ? EXIT_FAILURE : EXIT_SUCCESS);