1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "perfetto/base/build_config.h"
18 #include "test/gtest_and_gmock.h"
19 
20 // This translation unit is built only on Linux and MacOS. See //gn/BUILD.gn.
21 #if PERFETTO_BUILDFLAG(PERFETTO_LOCAL_SYMBOLIZER)
22 
23 #include "src/base/test/utils.h"
24 #include "src/profiling/symbolizer/local_symbolizer.h"
25 #include "src/profiling/symbolizer/subprocess.h"
26 
27 namespace perfetto {
28 namespace profiling {
29 namespace {
30 
RunAndValidateParseLines(std::string raw_contents)31 void RunAndValidateParseLines(std::string raw_contents) {
32   std::istringstream stream(raw_contents);
33   auto read_callback = [&stream](char* buffer, size_t size) {
34     stream.get(buffer, static_cast<int>(size), '\0');
35     return strlen(buffer);
36   };
37   std::vector<std::string> lines = GetLines(read_callback);
38   std::istringstream validation(raw_contents);
39   for (const std::string& actual : lines) {
40     std::string expected;
41     getline(validation, expected);
42     EXPECT_EQ(actual, expected);
43   }
44 }
45 
TEST(LocalSymbolizerTest,ParseLineWindows)46 TEST(LocalSymbolizerTest, ParseLineWindows) {
47   std::string file_name;
48   uint32_t lineno;
49   ASSERT_TRUE(
50       ParseLlvmSymbolizerLine("C:\\Foo\\Bar.cc:123:1", &file_name, &lineno));
51   EXPECT_EQ(file_name, "C:\\Foo\\Bar.cc");
52   EXPECT_EQ(lineno, 123u);
53 }
54 
TEST(LocalSymbolizerTest,ParseLinesExpectedOutput)55 TEST(LocalSymbolizerTest, ParseLinesExpectedOutput) {
56   std::string raw_contents =
57       "FSlateRHIRenderingPolicy::DrawElements(FRHICommandListImmediate&, "
58       "FSlateBackBuffer&, TRefCountPtr<FRHITexture2D>&, "
59       "TRefCountPtr<FRHITexture2D>&, TRefCountPtr<FRHITexture2D>&, int, "
60       "TArray<FSlateRenderBatch, TSizedDefaultAllocator<32> > const&, "
61       "FSlateRenderingParams const&)\n"
62       "F:/P4/EngineReleaseA/Engine/Source/Runtime/SlateRHIRenderer/"
63       "Private\\SlateRHIRenderingPolicy.cpp:1187:19\n";
64   RunAndValidateParseLines(raw_contents);
65 }
66 
TEST(LocalSymbolizerTest,ParseLinesErrorOutput)67 TEST(LocalSymbolizerTest, ParseLinesErrorOutput) {
68   std::string raw_contents =
69       "LLVMSymbolizer: error reading file: No such file or directory\n"
70       "??\n"
71       "??:0:0\n";
72   RunAndValidateParseLines(raw_contents);
73 }
74 
TEST(LocalSymbolizerTest,ParseLinesSingleCharRead)75 TEST(LocalSymbolizerTest, ParseLinesSingleCharRead) {
76   std::string raw_contents =
77       "FSlateRHIRenderingPolicy::DrawElements(FRHICommandListImmediate&, "
78       "FSlateBackBuffer&, TRefCountPtr<FRHITexture2D>&, "
79       "TRefCountPtr<FRHITexture2D>&, TRefCountPtr<FRHITexture2D>&, int, "
80       "TArray<FSlateRenderBatch, TSizedDefaultAllocator<32> > const&, "
81       "FSlateRenderingParams const&)\n"
82       "F:/P4/EngineReleaseA/Engine/Source/Runtime/SlateRHIRenderer/"
83       "Private\\SlateRHIRenderingPolicy.cpp:1187:19\n";
84   std::istringstream stream(raw_contents);
85   auto read_callback = [&stream](char* buffer, size_t) {
86     stream.get(buffer, 1, '\0');
87     return strlen(buffer);
88   };
89   std::vector<std::string> lines = GetLines(read_callback);
90   std::istringstream validation(raw_contents);
91   for (const std::string& actual : lines) {
92     std::string expected;
93     getline(validation, expected);
94     EXPECT_EQ(actual, expected);
95   }
96 }
97 
98 }  // namespace
99 }  // namespace profiling
100 }  // namespace perfetto
101 
102 #endif
103