annotate libbacktrace/edtest.c @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* edtest.c -- Test for libbacktrace storage allocation stress handling
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2 Copyright (C) 2017-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 Redistribution and use in source and binary forms, with or without
kono
parents:
diff changeset
5 modification, are permitted provided that the following conditions are
kono
parents:
diff changeset
6 met:
kono
parents:
diff changeset
7
kono
parents:
diff changeset
8 (1) Redistributions of source code must retain the above copyright
kono
parents:
diff changeset
9 notice, this list of conditions and the following disclaimer.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 (2) Redistributions in binary form must reproduce the above copyright
kono
parents:
diff changeset
12 notice, this list of conditions and the following disclaimer in
kono
parents:
diff changeset
13 the documentation and/or other materials provided with the
kono
parents:
diff changeset
14 distribution.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 (3) The name of the author may not be used to
kono
parents:
diff changeset
17 endorse or promote products derived from this software without
kono
parents:
diff changeset
18 specific prior written permission.
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
kono
parents:
diff changeset
21 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
kono
parents:
diff changeset
22 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
kono
parents:
diff changeset
23 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
kono
parents:
diff changeset
24 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
kono
parents:
diff changeset
25 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
kono
parents:
diff changeset
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
kono
parents:
diff changeset
27 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
kono
parents:
diff changeset
28 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
kono
parents:
diff changeset
29 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
kono
parents:
diff changeset
30 POSSIBILITY OF SUCH DAMAGE. */
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 #include "config.h"
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 #include <assert.h>
kono
parents:
diff changeset
35 #include <stdio.h>
kono
parents:
diff changeset
36 #include <stdlib.h>
kono
parents:
diff changeset
37 #include <string.h>
kono
parents:
diff changeset
38 #include <sys/types.h>
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 #include "backtrace.h"
kono
parents:
diff changeset
41 #include "backtrace-supported.h"
kono
parents:
diff changeset
42 #include "internal.h"
kono
parents:
diff changeset
43
kono
parents:
diff changeset
44 #include "testlib.h"
kono
parents:
diff changeset
45
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
46 static int test1 (void) __attribute__ ((noinline, noclone, unused));
111
kono
parents:
diff changeset
47 extern int f2 (int);
kono
parents:
diff changeset
48 extern int f3 (int, int);
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 static int
kono
parents:
diff changeset
51 test1 (void)
kono
parents:
diff changeset
52 {
kono
parents:
diff changeset
53 /* Returning a value here and elsewhere avoids a tailcall which
kono
parents:
diff changeset
54 would mess up the backtrace. */
kono
parents:
diff changeset
55 return f2 (__LINE__) + 1;
kono
parents:
diff changeset
56 }
kono
parents:
diff changeset
57
kono
parents:
diff changeset
58 int
kono
parents:
diff changeset
59 f3 (int f1line, int f2line)
kono
parents:
diff changeset
60 {
kono
parents:
diff changeset
61 struct info all[20];
kono
parents:
diff changeset
62 struct bdata data;
kono
parents:
diff changeset
63 int f3line;
kono
parents:
diff changeset
64 int i;
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 data.all = &all[0];
kono
parents:
diff changeset
67 data.index = 0;
kono
parents:
diff changeset
68 data.max = 20;
kono
parents:
diff changeset
69 data.failed = 0;
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 f3line = __LINE__ + 1;
kono
parents:
diff changeset
72 i = backtrace_full (state, 0, callback_one, error_callback_one, &data);
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 if (i != 0)
kono
parents:
diff changeset
75 {
kono
parents:
diff changeset
76 fprintf (stderr, "test1: unexpected return value %d\n", i);
kono
parents:
diff changeset
77 data.failed = 1;
kono
parents:
diff changeset
78 }
kono
parents:
diff changeset
79
kono
parents:
diff changeset
80 if (data.index < 3)
kono
parents:
diff changeset
81 {
kono
parents:
diff changeset
82 fprintf (stderr,
kono
parents:
diff changeset
83 "test1: not enough frames; got %zu, expected at least 3\n",
kono
parents:
diff changeset
84 data.index);
kono
parents:
diff changeset
85 data.failed = 1;
kono
parents:
diff changeset
86 }
kono
parents:
diff changeset
87
kono
parents:
diff changeset
88 check ("test1", 0, all, f3line, "f3", "edtest.c", &data.failed);
kono
parents:
diff changeset
89 check ("test1", 1, all, f2line, "f2", "edtest2_build.c", &data.failed);
kono
parents:
diff changeset
90 check ("test1", 2, all, f1line, "test1", "edtest.c", &data.failed);
kono
parents:
diff changeset
91
kono
parents:
diff changeset
92 printf ("%s: backtrace_full alloc stress\n", data.failed ? "FAIL" : "PASS");
kono
parents:
diff changeset
93
kono
parents:
diff changeset
94 if (data.failed)
kono
parents:
diff changeset
95 ++failures;
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 return failures;
kono
parents:
diff changeset
98 }
kono
parents:
diff changeset
99
kono
parents:
diff changeset
100 int
kono
parents:
diff changeset
101 main (int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
102 {
kono
parents:
diff changeset
103 state = backtrace_create_state (argv[0], BACKTRACE_SUPPORTS_THREADS,
kono
parents:
diff changeset
104 error_callback_create, NULL);
kono
parents:
diff changeset
105
kono
parents:
diff changeset
106 // Grab the storage allocation lock prior to doing anything interesting.
kono
parents:
diff changeset
107 // The intent here is to insure that the backtrace_alloc code is forced
kono
parents:
diff changeset
108 // to always call mmap() for new memory as opposed to reusing previously
kono
parents:
diff changeset
109 // allocated memory from the free list. Doing things this way helps
kono
parents:
diff changeset
110 // simulate what you might see in a multithreaded program in which there
kono
parents:
diff changeset
111 // are racing calls to the allocator.
kono
parents:
diff changeset
112 struct backtrace_state *state_internal =
kono
parents:
diff changeset
113 (struct backtrace_state *) state;
kono
parents:
diff changeset
114 state_internal->lock_alloc = 1;
kono
parents:
diff changeset
115
kono
parents:
diff changeset
116 // Kick off the test
kono
parents:
diff changeset
117 test1();
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 exit (failures > 0 ? EXIT_FAILURE : EXIT_SUCCESS);
kono
parents:
diff changeset
120 }