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
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
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. */
38 #include <sys/types.h>
40 #include "backtrace.h"
41 #include "backtrace-supported.h"
44 #if defined(__MSDOS__) || defined(_WIN32) || defined(__OS2__) || defined (__CYGWIN__)
45 # define IS_DIR_SEPARATOR(c) ((c) == '/' || (c) == '\\')
47 # define IS_DIR_SEPARATOR(c) ((c) == '/')
50 /* The backtrace state. */
54 /* The number of failures. */
58 static int test1 (void) __attribute__ ((noinline
, unused
));
59 static int test1 (void) __attribute__ ((noinline
, unused
));
61 extern int f3 (int, int);
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. */
80 /* Return the base name in a path. */
89 for (s
= p
; *s
!= '\0'; ++s
)
91 if (IS_DIR_SEPARATOR (*s
))
94 return last
!= NULL
? last
: p
;
97 /* Check an entry in a struct info array. */
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
)
105 if (all
[index
].filename
== NULL
|| all
[index
].function
== NULL
)
107 fprintf (stderr
, "%s: [%d]: missing file name or function name\n",
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
);
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
);
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
);
132 /* Passed to backtrace callback function. */
142 /* An error callback passed to backtrace. */
145 error_callback_one (void *vdata
, const char *msg
, int errnum
)
147 struct bdata
*data
= (struct bdata
*) vdata
;
149 fprintf (stderr
, "%s", msg
);
151 fprintf (stderr
, ": %s", strerror (errnum
));
152 fprintf (stderr
, "\n");
156 /* The backtrace callback function. */
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
;
165 if (data
->index
>= data
->max
)
167 fprintf (stderr
, "callback_one: callback called too many times\n");
172 p
= &data
->all
[data
->index
];
173 if (filename
== NULL
)
177 p
->filename
= strdup (filename
);
178 assert (p
->filename
!= NULL
);
181 if (function
== NULL
)
185 p
->function
= strdup (function
);
186 assert (p
->function
!= NULL
);
194 f3 (int f1line
, int f2line
)
206 f3line
= __LINE__
+ 1;
207 i
= backtrace_full (state
, 0, callback_one
, error_callback_one
, &data
);
211 fprintf (stderr
, "test1: unexpected return value %d\n", i
);
218 "test1: not enough frames; got %zu, expected at least 3\n",
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");
236 error_callback_create (void *data ATTRIBUTE_UNUSED
, const char *msg
,
239 fprintf (stderr
, "%s", msg
);
241 fprintf (stderr
, ": %s", strerror (errnum
));
242 fprintf (stderr
, "\n");
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;
265 exit (failures
> 0 ? EXIT_FAILURE
: EXIT_SUCCESS
);