1 /*
2  * Copyright © 2020 Google, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 
24 #include <stdio.h>
25 #include <gtest/gtest.h>
26 
27 #include "util/macros.h"
28 #include "util/u_debug_stack.h"
29 
30 static void ATTRIBUTE_NOINLINE
func_a(void)31 func_a(void)
32 {
33    struct debug_stack_frame backtrace[16];
34 
35    fprintf(stderr, "--- backtrace from func_a:\n");
36    debug_backtrace_capture(backtrace, 0, 16);
37    debug_backtrace_dump(backtrace, 16);
38 }
39 
40 static void ATTRIBUTE_NOINLINE
func_b(void)41 func_b(void)
42 {
43    struct debug_stack_frame backtrace[16];
44 
45    func_a();
46 
47    fprintf(stderr, "--- backtrace from func_b:\n");
48    debug_backtrace_capture(backtrace, 0, 16);
49    debug_backtrace_dump(backtrace, 16);
50 }
51 
52 static void ATTRIBUTE_NOINLINE
func_c(struct debug_stack_frame * frames)53 func_c(struct debug_stack_frame *frames)
54 {
55    debug_backtrace_capture(frames, 0, 16);
56 }
57 
TEST(u_debug_stack_test,basics)58 TEST(u_debug_stack_test, basics)
59 {
60    struct debug_stack_frame backtrace[16];
61    struct debug_stack_frame stored_backtrace[16];
62 
63    func_c(stored_backtrace);
64 
65    fprintf(stderr, "--- backtrace from main to stderr:\n");
66    debug_backtrace_capture(backtrace, 0, 16);
67    debug_backtrace_print(stderr, backtrace, 16);
68 
69    fprintf(stderr, "--- backtrace from main again to debug_printf:\n");
70    debug_backtrace_capture(backtrace, 0, 16);
71    debug_backtrace_dump(backtrace, 16);
72 
73    func_a();
74 
75    func_b();
76 
77    fprintf(stderr, "--- stored backtrace from start of main:\n");
78    debug_backtrace_dump(stored_backtrace, 16);
79 }
80 
81 #if _POSIX_C_SOURCE >= 200809L
82 
TEST(u_debug_stack_test,capture_not_overwritten)83 TEST(u_debug_stack_test, capture_not_overwritten)
84 {
85    struct debug_stack_frame backtrace1[16], backtrace2[16];
86 
87    FILE *fp;
88    char *bt1, *bt2;
89    size_t size;
90 
91    /* Old android implementation uses one global capture per thread. Test that
92     * we can store multiple captures and that they decode to different
93     * backtraces.
94     */
95 
96    func_c(backtrace1);
97    debug_backtrace_capture(backtrace2, 0, 16);
98 
99    fp = open_memstream(&bt1, &size);
100    debug_backtrace_print(fp, backtrace1, 16);
101    fclose(fp);
102 
103    fp = open_memstream(&bt2, &size);
104    debug_backtrace_print(fp, backtrace2, 16);
105    fclose(fp);
106 
107    if (size > 0) {
108       EXPECT_STRNE(bt1, bt2);
109    }
110 
111    free(bt1);
112    free(bt2);
113 }
114 
115 #endif
116