Updated French translation for the ld/ and gold/ sub-directories
[binutils-gdb.git] / libbacktrace / allocfail.c
blobbd0fbb658a9878f74876efce01d77e87438c9ded
1 /* allocfail.c -- Test for libbacktrace library
2 Copyright (C) 2018-2021 Free Software Foundation, Inc.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are
6 met:
8 (1) Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
11 (2) Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in
13 the documentation and/or other materials provided with the
14 distribution.
16 (3) The name of the author may not be used to
17 endorse or promote products derived from this software without
18 specific prior written permission.
20 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 POSSIBILITY OF SUCH DAMAGE. */
32 #include <assert.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
38 #include "filenames.h"
40 #include "backtrace.h"
41 #include "backtrace-supported.h"
43 #include "testlib.h"
45 extern uint64_t get_nr_allocs (void);
46 extern void set_fail_at_alloc (uint64_t);
47 extern int at_fail_alloc_p (void);
49 static int test1 (void) __attribute__ ((noinline, unused));
50 static int f2 (int) __attribute__ ((noinline));
51 static int f3 (int, int) __attribute__ ((noinline));
53 static unsigned callback_errors = 0;
55 static void
56 error_callback_full (void *vdata ATTRIBUTE_UNUSED,
57 const char *msg ATTRIBUTE_UNUSED,
58 int errnum ATTRIBUTE_UNUSED)
60 if (at_fail_alloc_p ())
62 set_fail_at_alloc (0);
63 return;
66 callback_errors++;
69 static int
70 callback_full (void *vdata ATTRIBUTE_UNUSED, uintptr_t pc ATTRIBUTE_UNUSED,
71 const char *filename ATTRIBUTE_UNUSED,
72 int lineno ATTRIBUTE_UNUSED,
73 const char *function ATTRIBUTE_UNUSED)
76 return 0;
79 static int
80 test1 (void)
82 return f2 (__LINE__) + 1;
85 static int
86 f2 (int f1line)
88 return f3 (f1line, __LINE__) + 2;
91 static int
92 f3 (int f1line ATTRIBUTE_UNUSED, int f2line ATTRIBUTE_UNUSED)
94 int i;
96 i = backtrace_full (state, 0, callback_full, error_callback_full, NULL);
98 if (i != 0)
100 fprintf (stderr, "test1: unexpected return value %d\n", i);
101 ++failures;
104 if (callback_errors)
105 ++failures;
107 return failures;
110 /* Run all the tests. */
113 main (int argc, char **argv)
115 uint64_t fail_at = 0;
117 if (argc == 2)
119 fail_at = atoi (argv[1]);
120 set_fail_at_alloc (fail_at);
123 state = backtrace_create_state (argv[0], BACKTRACE_SUPPORTS_THREADS,
124 error_callback_full, NULL);
125 if (state == NULL)
126 exit (failures ? EXIT_FAILURE : EXIT_SUCCESS);
128 #if BACKTRACE_SUPPORTED
129 test1 ();
130 #endif
132 if (argc == 1)
133 fprintf (stderr, "%llu\n", (long long unsigned) get_nr_allocs ());
135 exit (failures ? EXIT_FAILURE : EXIT_SUCCESS);