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