1 //===-- sanitizer_common_printer_test.cpp ---------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file is a part of sanitizer_common test suite. 10 // 11 //===----------------------------------------------------------------------===// 12 #include "sanitizer_common/sanitizer_stacktrace_printer.h" 13 14 #include "gtest/gtest.h" 15 16 namespace __sanitizer { 17 18 TEST(SanitizerStacktracePrinter, RenderSourceLocation) { 19 InternalScopedString str(128); 20 RenderSourceLocation(&str, "/dir/file.cc", 10, 5, false, ""); 21 EXPECT_STREQ("/dir/file.cc:10:5", str.data()); 22 23 str.clear(); 24 RenderSourceLocation(&str, "/dir/file.cc", 11, 0, false, ""); 25 EXPECT_STREQ("/dir/file.cc:11", str.data()); 26 27 str.clear(); 28 RenderSourceLocation(&str, "/dir/file.cc", 0, 0, false, ""); 29 EXPECT_STREQ("/dir/file.cc", str.data()); 30 31 str.clear(); 32 RenderSourceLocation(&str, "/dir/file.cc", 10, 5, false, "/dir/"); 33 EXPECT_STREQ("file.cc:10:5", str.data()); 34 35 str.clear(); 36 RenderSourceLocation(&str, "/dir/file.cc", 10, 5, true, ""); 37 EXPECT_STREQ("/dir/file.cc(10,5)", str.data()); 38 39 str.clear(); 40 RenderSourceLocation(&str, "/dir/file.cc", 11, 0, true, ""); 41 EXPECT_STREQ("/dir/file.cc(11)", str.data()); 42 43 str.clear(); 44 RenderSourceLocation(&str, "/dir/file.cc", 0, 0, true, ""); 45 EXPECT_STREQ("/dir/file.cc", str.data()); 46 47 str.clear(); 48 RenderSourceLocation(&str, "/dir/file.cc", 10, 5, true, "/dir/"); 49 EXPECT_STREQ("file.cc(10,5)", str.data()); 50 } 51 52 TEST(SanitizerStacktracePrinter, RenderModuleLocation) { 53 InternalScopedString str(128); 54 RenderModuleLocation(&str, "/dir/exe", 0x123, kModuleArchUnknown, ""); 55 EXPECT_STREQ("(/dir/exe+0x123)", str.data()); 56 57 // Check that we strip file prefix if necessary. 58 str.clear(); 59 RenderModuleLocation(&str, "/dir/exe", 0x123, kModuleArchUnknown, "/dir/"); 60 EXPECT_STREQ("(exe+0x123)", str.data()); 61 62 // Check that we render the arch. 63 str.clear(); 64 RenderModuleLocation(&str, "/dir/exe", 0x123, kModuleArchX86_64H, "/dir/"); 65 EXPECT_STREQ("(exe:x86_64h+0x123)", str.data()); 66 } 67 68 TEST(SanitizerStacktracePrinter, RenderFrame) { 69 int frame_no = 42; 70 AddressInfo info; 71 info.address = 0x400000; 72 info.module = internal_strdup("/path/to/my/module"); 73 info.module_offset = 0x200; 74 info.function = internal_strdup("function_foo"); 75 info.function_offset = 0x100; 76 info.file = internal_strdup("/path/to/my/source"); 77 info.line = 10; 78 info.column = 5; 79 InternalScopedString str(256); 80 81 // Dump all the AddressInfo fields. 82 RenderFrame(&str, "%% Frame:%n PC:%p Module:%m ModuleOffset:%o " 83 "Function:%f FunctionOffset:%q Source:%s Line:%l " 84 "Column:%c", 85 frame_no, info, false, "/path/to/", "function_"); 86 EXPECT_STREQ("% Frame:42 PC:0x400000 Module:my/module ModuleOffset:0x200 " 87 "Function:foo FunctionOffset:0x100 Source:my/source Line:10 " 88 "Column:5", 89 str.data()); 90 info.Clear(); 91 str.clear(); 92 93 // Test special format specifiers. 94 info.address = 0x400000; 95 RenderFrame(&str, "%M", frame_no, info, false); 96 EXPECT_NE(nullptr, internal_strstr(str.data(), "400000")); 97 str.clear(); 98 99 RenderFrame(&str, "%L", frame_no, info, false); 100 EXPECT_STREQ("(<unknown module>)", str.data()); 101 str.clear(); 102 103 info.module = internal_strdup("/path/to/module"); 104 info.module_offset = 0x200; 105 RenderFrame(&str, "%M", frame_no, info, false); 106 EXPECT_NE(nullptr, internal_strstr(str.data(), "(module+0x")); 107 EXPECT_NE(nullptr, internal_strstr(str.data(), "200")); 108 str.clear(); 109 110 RenderFrame(&str, "%L", frame_no, info, false); 111 EXPECT_STREQ("(/path/to/module+0x200)", str.data()); 112 str.clear(); 113 114 info.function = internal_strdup("my_function"); 115 RenderFrame(&str, "%F", frame_no, info, false); 116 EXPECT_STREQ("in my_function", str.data()); 117 str.clear(); 118 119 info.function_offset = 0x100; 120 RenderFrame(&str, "%F %S", frame_no, info, false); 121 EXPECT_STREQ("in my_function+0x100 <null>", str.data()); 122 str.clear(); 123 124 info.file = internal_strdup("my_file"); 125 RenderFrame(&str, "%F %S", frame_no, info, false); 126 EXPECT_STREQ("in my_function my_file", str.data()); 127 str.clear(); 128 129 info.line = 10; 130 RenderFrame(&str, "%F %S", frame_no, info, false); 131 EXPECT_STREQ("in my_function my_file:10", str.data()); 132 str.clear(); 133 134 info.column = 5; 135 RenderFrame(&str, "%S %L", frame_no, info, false); 136 EXPECT_STREQ("my_file:10:5 my_file:10:5", str.data()); 137 str.clear(); 138 139 RenderFrame(&str, "%S %L", frame_no, info, true); 140 EXPECT_STREQ("my_file(10,5) my_file(10,5)", str.data()); 141 str.clear(); 142 143 info.column = 0; 144 RenderFrame(&str, "%F %S", frame_no, info, true); 145 EXPECT_STREQ("in my_function my_file(10)", str.data()); 146 str.clear(); 147 148 info.line = 0; 149 RenderFrame(&str, "%F %S", frame_no, info, true); 150 EXPECT_STREQ("in my_function my_file", str.data()); 151 str.clear(); 152 153 info.Clear(); 154 } 155 156 } // namespace __sanitizer 157