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; 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; 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; 80 81 // Dump all the AddressInfo fields. 82 RenderFrame(&str, 83 "%% Frame:%n PC:%p Module:%m ModuleOffset:%o " 84 "Function:%f FunctionOffset:%q Source:%s Line:%l " 85 "Column:%c", 86 frame_no, info.address, &info, false, "/path/to/", "function_"); 87 EXPECT_STREQ("% Frame:42 PC:0x400000 Module:my/module ModuleOffset:0x200 " 88 "Function:foo FunctionOffset:0x100 Source:my/source Line:10 " 89 "Column:5", 90 str.data()); 91 info.Clear(); 92 str.clear(); 93 94 // Test special format specifiers. 95 info.address = 0x400000; 96 RenderFrame(&str, "%M", frame_no, info.address, &info, false); 97 EXPECT_NE(nullptr, internal_strstr(str.data(), "400000")); 98 str.clear(); 99 100 RenderFrame(&str, "%L", frame_no, info.address, &info, false); 101 EXPECT_STREQ("(<unknown module>)", str.data()); 102 str.clear(); 103 104 info.module = internal_strdup("/path/to/module"); 105 info.module_offset = 0x200; 106 RenderFrame(&str, "%M", frame_no, info.address, &info, false); 107 EXPECT_NE(nullptr, internal_strstr(str.data(), "(module+0x")); 108 EXPECT_NE(nullptr, internal_strstr(str.data(), "200")); 109 str.clear(); 110 111 RenderFrame(&str, "%L", frame_no, info.address, &info, false); 112 EXPECT_STREQ("(/path/to/module+0x200)", str.data()); 113 str.clear(); 114 115 info.function = internal_strdup("my_function"); 116 RenderFrame(&str, "%F", frame_no, info.address, &info, false); 117 EXPECT_STREQ("in my_function", str.data()); 118 str.clear(); 119 120 info.function_offset = 0x100; 121 RenderFrame(&str, "%F %S", frame_no, info.address, &info, false); 122 EXPECT_STREQ("in my_function+0x100 <null>", str.data()); 123 str.clear(); 124 125 info.file = internal_strdup("my_file"); 126 RenderFrame(&str, "%F %S", frame_no, info.address, &info, false); 127 EXPECT_STREQ("in my_function my_file", str.data()); 128 str.clear(); 129 130 info.line = 10; 131 RenderFrame(&str, "%F %S", frame_no, info.address, &info, false); 132 EXPECT_STREQ("in my_function my_file:10", str.data()); 133 str.clear(); 134 135 info.column = 5; 136 RenderFrame(&str, "%S %L", frame_no, info.address, &info, false); 137 EXPECT_STREQ("my_file:10:5 my_file:10:5", str.data()); 138 str.clear(); 139 140 RenderFrame(&str, "%S %L", frame_no, info.address, &info, true); 141 EXPECT_STREQ("my_file(10,5) my_file(10,5)", str.data()); 142 str.clear(); 143 144 info.column = 0; 145 RenderFrame(&str, "%F %S", frame_no, info.address, &info, true); 146 EXPECT_STREQ("in my_function my_file(10)", str.data()); 147 str.clear(); 148 149 info.line = 0; 150 RenderFrame(&str, "%F %S", frame_no, info.address, &info, true); 151 EXPECT_STREQ("in my_function my_file", str.data()); 152 str.clear(); 153 154 info.Clear(); 155 } 156 157 } // namespace __sanitizer 158