1 /* ttest.c -- Test for libbacktrace library
2    Copyright (C) 2017-2018 Free Software Foundation, Inc.
3    Written by Ian Lance Taylor, Google.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8 
9     (1) Redistributions of source code must retain the above copyright
10     notice, this list of conditions and the following disclaimer.
11 
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.
16 
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.
20 
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.  */
32 
33 /* Test using the libbacktrace library from multiple threads.  */
34 
35 #include <assert.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 
40 #include <pthread.h>
41 
42 #include "filenames.h"
43 
44 #include "backtrace.h"
45 #include "backtrace-supported.h"
46 
47 #include "testlib.h"
48 
49 static int f2 (int) __attribute__ ((noinline));
50 static int f3 (int, int) __attribute__ ((noinline));
51 
52 /* Test that a simple backtrace works.  This is called via
53    pthread_create.  It returns the number of failures, as void *.  */
54 
55 static void *
test1_thread(void * arg ATTRIBUTE_UNUSED)56 test1_thread (void *arg ATTRIBUTE_UNUSED)
57 {
58   /* Returning a value here and elsewhere avoids a tailcall which
59      would mess up the backtrace.  */
60   return (void *) (uintptr_t) (f2 (__LINE__) - 2);
61 }
62 
63 static int
f2(int f1line)64 f2 (int f1line)
65 {
66   return f3 (f1line, __LINE__) + 2;
67 }
68 
69 static int
f3(int f1line,int f2line)70 f3 (int f1line, int f2line)
71 {
72   struct info all[20];
73   struct bdata data;
74   int f3line;
75   int i;
76 
77   data.all = &all[0];
78   data.index = 0;
79   data.max = 20;
80   data.failed = 0;
81 
82   f3line = __LINE__ + 1;
83   i = backtrace_full (state, 0, callback_one, error_callback_one, &data);
84 
85   if (i != 0)
86     {
87       fprintf (stderr, "test1: unexpected return value %d\n", i);
88       data.failed = 1;
89     }
90 
91   if (data.index < 3)
92     {
93       fprintf (stderr,
94 	       "test1: not enough frames; got %zu, expected at least 3\n",
95 	       data.index);
96       data.failed = 1;
97     }
98 
99   check ("test1", 0, all, f3line, "f3", "ttest.c", &data.failed);
100   check ("test1", 1, all, f2line, "f2", "ttest.c", &data.failed);
101   check ("test1", 2, all, f1line, "test1_thread", "ttest.c", &data.failed);
102 
103   return data.failed;
104 }
105 
106 /* Run the test with 10 threads simultaneously.  */
107 
108 #define THREAD_COUNT 10
109 
110 static void test1 (void) __attribute__ ((unused));
111 
112 static void
test1(void)113 test1 (void)
114 {
115   pthread_t atid[THREAD_COUNT];
116   int i;
117   int errnum;
118   int this_fail;
119   void *ret;
120 
121   for (i = 0; i < THREAD_COUNT; i++)
122     {
123       errnum = pthread_create (&atid[i], NULL, test1_thread, NULL);
124       if (errnum != 0)
125 	{
126 	  fprintf (stderr, "pthread_create %d: %s\n", i, strerror (errnum));
127 	  exit (EXIT_FAILURE);
128 	}
129     }
130 
131   this_fail = 0;
132   for (i = 0; i < THREAD_COUNT; i++)
133     {
134       errnum = pthread_join (atid[i], &ret);
135       if (errnum != 0)
136 	{
137 	  fprintf (stderr, "pthread_join %d: %s\n", i, strerror (errnum));
138 	  exit (EXIT_FAILURE);
139 	}
140       this_fail += (int) (uintptr_t) ret;
141     }
142 
143   printf ("%s: threaded backtrace_full noinline\n", this_fail > 0 ? "FAIL" : "PASS");
144 
145   failures += this_fail;
146 }
147 
148 int
main(int argc ATTRIBUTE_UNUSED,char ** argv)149 main (int argc ATTRIBUTE_UNUSED, char **argv)
150 {
151   state = backtrace_create_state (argv[0], BACKTRACE_SUPPORTS_THREADS,
152 				  error_callback_create, NULL);
153 
154 #if BACKTRACE_SUPPORTED
155 #if BACKTRACE_SUPPORTS_THREADS
156   test1 ();
157 #endif
158 #endif
159 
160   exit (failures ? EXIT_FAILURE : EXIT_SUCCESS);
161 }
162