1 /*
2  * Copyright (C) 2018 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 <gtest/gtest.h>
18 
19 #include <unistd.h>
20 
21 #include <memory>
22 #include <string>
23 #include <vector>
24 
25 #include <android-base/file.h>
26 
27 #include "command.h"
28 #include "get_test_data.h"
29 #include "record_file.h"
30 #include "test_util.h"
31 
32 using namespace simpleperf;
33 
DebugUnwindCmd()34 static std::unique_ptr<Command> DebugUnwindCmd() {
35   return CreateCommandInstance("debug-unwind");
36 }
37 
TEST(cmd_debug_unwind,smoke)38 TEST(cmd_debug_unwind, smoke) {
39   std::string input_data = GetTestData(PERF_DATA_NO_UNWIND);
40   CaptureStdout capture;
41   TemporaryFile tmp_file;
42   ASSERT_TRUE(capture.Start());
43   ASSERT_TRUE(DebugUnwindCmd()->Run({"-i", input_data, "-o", tmp_file.path}));
44   ASSERT_NE(capture.Finish().find("Unwinding sample count: 8"), std::string::npos);
45 
46   ASSERT_TRUE(capture.Start());
47   ASSERT_TRUE(DebugUnwindCmd()->Run({"-i", input_data, "-o", tmp_file.path, "--time",
48                                      "1516379654300997"}));
49   ASSERT_NE(capture.Finish().find("Unwinding sample count: 1"), std::string::npos);
50 }
51 
TEST(cmd_debug_unwind,symfs_option)52 TEST(cmd_debug_unwind, symfs_option) {
53   std::string input_data = GetTestData(NATIVELIB_IN_APK_PERF_DATA);
54   CaptureStdout capture;
55   TemporaryFile tmp_file;
56   ASSERT_TRUE(capture.Start());
57   ASSERT_TRUE(DebugUnwindCmd()->Run({"-i", input_data, "-o", tmp_file.path, "--symfs",
58                                      GetTestDataDir()}));
59   ASSERT_NE(capture.Finish().find("Unwinding sample count: 55"), std::string::npos);
60   std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(tmp_file.path);
61   ASSERT_TRUE(reader);
62   const std::map<int, PerfFileFormat::SectionDesc>& features = reader->FeatureSectionDescriptors();
63   ASSERT_NE(features.find(PerfFileFormat::FEAT_FILE), features.end());
64   ASSERT_NE(features.find(PerfFileFormat::FEAT_META_INFO), features.end());
65   auto meta_info = reader->GetMetaInfoFeature();
66   ASSERT_EQ(meta_info["debug_unwind"], "true");
67 }
68 
TEST(cmd_debug_unwind,unwind_with_ip_zero_in_callchain)69 TEST(cmd_debug_unwind, unwind_with_ip_zero_in_callchain) {
70   TemporaryFile tmp_file;
71   CaptureStdout capture;
72   ASSERT_TRUE(capture.Start());
73   ASSERT_TRUE(DebugUnwindCmd()->Run({"-i", GetTestData(PERF_DATA_WITH_IP_ZERO_IN_CALLCHAIN),
74                                      "-o", tmp_file.path}));
75   ASSERT_NE(capture.Finish().find("Unwinding sample count: 1"), std::string::npos);
76 }
77 
TEST(cmd_debug_unwind,unwind_embedded_lib_in_apk)78 TEST(cmd_debug_unwind, unwind_embedded_lib_in_apk) {
79   // Check if we can unwind through a native library embedded in an apk. In the profiling data
80   // file, there is a sample with ip address pointing to
81   // /data/app/simpleperf.demo.cpp_api/base.apk!/lib/arm64-v8a/libnative-lib.so.
82   // If unwound successfully, it can reach a function in libc.so.
83   TemporaryFile tmp_file;
84   ASSERT_TRUE(DebugUnwindCmd()->Run({"-i", GetTestData("perf_unwind_embedded_lib_in_apk.data"),
85                                      "--symfs", GetTestDataDir(), "-o", tmp_file.path}));
86   CaptureStdout capture;
87   ASSERT_TRUE(capture.Start());
88   ASSERT_TRUE(CreateCommandInstance("report-sample")->Run(
89       {"--show-callchain", "-i", tmp_file.path}));
90   std::string output = capture.Finish();
91   ASSERT_NE(output.find("libnative-lib.so"), std::string::npos);
92   ASSERT_NE(output.find("libc.so"), std::string::npos);
93 }
94