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/macros.h"
28 #include "util/u_debug_stack.h"
30 static void ATTRIBUTE_NOINLINE
33 struct debug_stack_frame backtrace
[16];
35 fprintf(stderr
, "--- backtrace from func_a:\n");
36 debug_backtrace_capture(backtrace
, 0, 16);
37 debug_backtrace_dump(backtrace
, 16);
40 static void ATTRIBUTE_NOINLINE
43 struct debug_stack_frame backtrace
[16];
47 fprintf(stderr
, "--- backtrace from func_b:\n");
48 debug_backtrace_capture(backtrace
, 0, 16);
49 debug_backtrace_dump(backtrace
, 16);
52 static void ATTRIBUTE_NOINLINE
53 func_c(struct debug_stack_frame
*frames
)
55 debug_backtrace_capture(frames
, 0, 16);
58 TEST(u_debug_stack_test
, basics
)
60 struct debug_stack_frame backtrace
[16];
61 struct debug_stack_frame stored_backtrace
[16];
63 func_c(stored_backtrace
);
65 fprintf(stderr
, "--- backtrace from main to stderr:\n");
66 debug_backtrace_capture(backtrace
, 0, 16);
67 debug_backtrace_print(stderr
, backtrace
, 16);
69 fprintf(stderr
, "--- backtrace from main again to debug_printf:\n");
70 debug_backtrace_capture(backtrace
, 0, 16);
71 debug_backtrace_dump(backtrace
, 16);
77 fprintf(stderr
, "--- stored backtrace from start of main:\n");
78 debug_backtrace_dump(stored_backtrace
, 16);
81 #if _POSIX_C_SOURCE >= 200809L
83 TEST(u_debug_stack_test
, capture_not_overwritten
)
85 struct debug_stack_frame backtrace1
[16], backtrace2
[16];
91 /* Old android implementation uses one global capture per thread. Test that
92 * we can store multiple captures and that they decode to different
97 debug_backtrace_capture(backtrace2
, 0, 16);
99 fp
= open_memstream(&bt1
, &size
);
100 debug_backtrace_print(fp
, backtrace1
, 16);
103 fp
= open_memstream(&bt2
, &size
);
104 debug_backtrace_print(fp
, backtrace2
, 16);
108 EXPECT_STRNE(bt1
, bt2
);