Small ChangeLog tweak.
[official-gcc.git] / libbacktrace / edtest.c
blobdaf4dd9f0be425429ee992a9bdd55be2d59d23b2
1 /* edtest.c -- Test for libbacktrace storage allocation stress handling
2 Copyright (C) 2017 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 "config.h"
34 #include <assert.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/types.h>
40 #include "backtrace.h"
41 #include "backtrace-supported.h"
42 #include "internal.h"
44 #if defined(__MSDOS__) || defined(_WIN32) || defined(__OS2__) || defined (__CYGWIN__)
45 # define IS_DIR_SEPARATOR(c) ((c) == '/' || (c) == '\\')
46 #else
47 # define IS_DIR_SEPARATOR(c) ((c) == '/')
48 #endif
50 /* The backtrace state. */
52 static void *state;
54 /* The number of failures. */
56 int failures = 0;
58 static int test1 (void) __attribute__ ((noinline, unused));
59 static int test1 (void) __attribute__ ((noinline, unused));
60 extern int f2 (int);
61 extern int f3 (int, int);
63 static int
64 test1 (void)
66 /* Returning a value here and elsewhere avoids a tailcall which
67 would mess up the backtrace. */
68 return f2 (__LINE__) + 1;
71 /* Used to collect backtrace info. */
73 struct info
75 char *filename;
76 int lineno;
77 char *function;
80 /* Return the base name in a path. */
82 static const char *
83 base (const char *p)
85 const char *last;
86 const char *s;
88 last = NULL;
89 for (s = p; *s != '\0'; ++s)
91 if (IS_DIR_SEPARATOR (*s))
92 last = s + 1;
94 return last != NULL ? last : p;
97 /* Check an entry in a struct info array. */
99 static void
100 check (const char *name, int index, const struct info *all, int want_lineno,
101 const char *want_function, const char *want_file, int *failed)
103 if (*failed)
104 return;
105 if (all[index].filename == NULL || all[index].function == NULL)
107 fprintf (stderr, "%s: [%d]: missing file name or function name\n",
108 name, index);
109 *failed = 1;
110 return;
112 if (strcmp (base (all[index].filename), want_file) != 0)
114 fprintf (stderr, "%s: [%d]: got %s expected %s\n", name, index,
115 all[index].filename, want_file);
116 *failed = 1;
118 if (all[index].lineno != want_lineno)
120 fprintf (stderr, "%s: [%d]: got %d expected %d\n", name, index,
121 all[index].lineno, want_lineno);
122 *failed = 1;
124 if (strcmp (all[index].function, want_function) != 0)
126 fprintf (stderr, "%s: [%d]: got %s expected %s\n", name, index,
127 all[index].function, want_function);
128 *failed = 1;
132 /* Passed to backtrace callback function. */
134 struct bdata
136 struct info *all;
137 size_t index;
138 size_t max;
139 int failed;
142 /* An error callback passed to backtrace. */
144 static void
145 error_callback_one (void *vdata, const char *msg, int errnum)
147 struct bdata *data = (struct bdata *) vdata;
149 fprintf (stderr, "%s", msg);
150 if (errnum > 0)
151 fprintf (stderr, ": %s", strerror (errnum));
152 fprintf (stderr, "\n");
153 data->failed = 1;
156 /* The backtrace callback function. */
158 static int
159 callback_one (void *vdata, uintptr_t pc ATTRIBUTE_UNUSED,
160 const char *filename, int lineno, const char *function)
162 struct bdata *data = (struct bdata *) vdata;
163 struct info *p;
165 if (data->index >= data->max)
167 fprintf (stderr, "callback_one: callback called too many times\n");
168 data->failed = 1;
169 return 1;
172 p = &data->all[data->index];
173 if (filename == NULL)
174 p->filename = NULL;
175 else
177 p->filename = strdup (filename);
178 assert (p->filename != NULL);
180 p->lineno = lineno;
181 if (function == NULL)
182 p->function = NULL;
183 else
185 p->function = strdup (function);
186 assert (p->function != NULL);
188 ++data->index;
190 return 0;
194 f3 (int f1line, int f2line)
196 struct info all[20];
197 struct bdata data;
198 int f3line;
199 int i;
201 data.all = &all[0];
202 data.index = 0;
203 data.max = 20;
204 data.failed = 0;
206 f3line = __LINE__ + 1;
207 i = backtrace_full (state, 0, callback_one, error_callback_one, &data);
209 if (i != 0)
211 fprintf (stderr, "test1: unexpected return value %d\n", i);
212 data.failed = 1;
215 if (data.index < 3)
217 fprintf (stderr,
218 "test1: not enough frames; got %zu, expected at least 3\n",
219 data.index);
220 data.failed = 1;
223 check ("test1", 0, all, f3line, "f3", "edtest.c", &data.failed);
224 check ("test1", 1, all, f2line, "f2", "edtest2_build.c", &data.failed);
225 check ("test1", 2, all, f1line, "test1", "edtest.c", &data.failed);
227 printf ("%s: backtrace_full alloc stress\n", data.failed ? "FAIL" : "PASS");
229 if (data.failed)
230 ++failures;
232 return failures;
235 static void
236 error_callback_create (void *data ATTRIBUTE_UNUSED, const char *msg,
237 int errnum)
239 fprintf (stderr, "%s", msg);
240 if (errnum > 0)
241 fprintf (stderr, ": %s", strerror (errnum));
242 fprintf (stderr, "\n");
243 exit (EXIT_FAILURE);
247 main (int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
249 state = backtrace_create_state (argv[0], BACKTRACE_SUPPORTS_THREADS,
250 error_callback_create, NULL);
252 // Grab the storage allocation lock prior to doing anything interesting.
253 // The intent here is to insure that the backtrace_alloc code is forced
254 // to always call mmap() for new memory as opposed to reusing previously
255 // allocated memory from the free list. Doing things this way helps
256 // simulate what you might see in a multithreaded program in which there
257 // are racing calls to the allocator.
258 struct backtrace_state *state_internal =
259 (struct backtrace_state *) state;
260 state_internal->lock_alloc = 1;
262 // Kick off the test
263 test1();
265 exit (failures > 0 ? EXIT_FAILURE : EXIT_SUCCESS);