2 * Copyright © 2020 Google, Inc.
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:
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
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
25 #include <gtest/gtest.h>
27 #include "util/u_debug_stack.h"
29 static void __attribute__((noinline
))
32 struct debug_stack_frame backtrace
[16];
34 fprintf(stderr
, "--- backtrace from func_a:\n");
35 debug_backtrace_capture(backtrace
, 0, 16);
36 debug_backtrace_dump(backtrace
, 16);
39 static void __attribute__((noinline
))
42 struct debug_stack_frame backtrace
[16];
46 fprintf(stderr
, "--- backtrace from func_b:\n");
47 debug_backtrace_capture(backtrace
, 0, 16);
48 debug_backtrace_dump(backtrace
, 16);
51 static void __attribute__((noinline
))
52 func_c(struct debug_stack_frame
*frames
)
54 debug_backtrace_capture(frames
, 0, 16);
57 TEST(u_debug_stack_test
, basics
)
59 struct debug_stack_frame backtrace
[16];
60 struct debug_stack_frame stored_backtrace
[16];
62 func_c(stored_backtrace
);
64 fprintf(stderr
, "--- backtrace from main to stderr:\n");
65 debug_backtrace_capture(backtrace
, 0, 16);
66 debug_backtrace_print(stderr
, backtrace
, 16);
68 fprintf(stderr
, "--- backtrace from main again to debug_printf:\n");
69 debug_backtrace_capture(backtrace
, 0, 16);
70 debug_backtrace_dump(backtrace
, 16);
76 fprintf(stderr
, "--- stored backtrace from start of main:\n");
77 debug_backtrace_dump(stored_backtrace
, 16);
80 #if _POSIX_C_SOURCE >= 200809L
82 TEST(u_debug_stack_test
, capture_not_overwritten
)
84 struct debug_stack_frame backtrace1
[16], backtrace2
[16];
90 /* Old android implementation uses one global capture per thread. Test that
91 * we can store multiple captures and that they decode to different
96 debug_backtrace_capture(backtrace2
, 0, 16);
98 fp
= open_memstream(&bt1
, &size
);
99 debug_backtrace_print(fp
, backtrace1
, 16);
102 fp
= open_memstream(&bt2
, &size
);
103 debug_backtrace_print(fp
, backtrace2
, 16);
107 EXPECT_STRNE(bt1
, bt2
);