• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,unwind_sample_option)38 TEST(cmd_debug_unwind, unwind_sample_option) {
39   std::string input_data = GetTestData(PERF_DATA_NO_UNWIND);
40   CaptureStdout capture;
41 
42   ASSERT_TRUE(capture.Start());
43   ASSERT_TRUE(DebugUnwindCmd()->Run({"-i", input_data, "--unwind-sample"}));
44   ASSERT_NE(capture.Finish().find("sample_time: 1516379654300997"), std::string::npos);
45 }
46 
TEST(cmd_debug_unwind,sample_time_option)47 TEST(cmd_debug_unwind, sample_time_option) {
48   std::string input_data = GetTestData(PERF_DATA_NO_UNWIND);
49   CaptureStdout capture;
50 
51   ASSERT_TRUE(capture.Start());
52   ASSERT_TRUE(DebugUnwindCmd()->Run({"-i", input_data, "--unwind-sample", "--sample-time",
53                                      "1516379654300997", "--sample-time",
54                                      "1516379654363914,1516379655959122"}));
55   std::string output = capture.Finish();
56   ASSERT_NE(output.find("sample_time: 1516379654300997"), std::string::npos);
57   ASSERT_NE(output.find("sample_time: 1516379654363914"), std::string::npos);
58   ASSERT_NE(output.find("sample_time: 1516379655959122"), std::string::npos);
59 }
60 
TEST(cmd_debug_unwind,output_option)61 TEST(cmd_debug_unwind, output_option) {
62   std::string input_data = GetTestData(PERF_DATA_NO_UNWIND);
63   TemporaryFile tmpfile;
64   close(tmpfile.release());
65   ASSERT_TRUE(DebugUnwindCmd()->Run({"-i", input_data, "--unwind-sample", "--sample-time",
66                                      "1516379654300997", "-o", tmpfile.path}));
67   std::string output;
68   ASSERT_TRUE(android::base::ReadFileToString(tmpfile.path, &output));
69   ASSERT_NE(output.find("sample_time: 1516379654300997"), std::string::npos);
70 }
71 
TEST(cmd_debug_unwind,symfs_option)72 TEST(cmd_debug_unwind, symfs_option) {
73   std::string input_data = GetTestData(NATIVELIB_IN_APK_PERF_DATA);
74   CaptureStdout capture;
75   ASSERT_TRUE(capture.Start());
76   ASSERT_TRUE(DebugUnwindCmd()->Run({"-i", input_data, "--symfs", GetTestDataDir(),
77                                      "--unwind-sample", "--sample-time", "500329355223"}));
78   ASSERT_NE(capture.Finish().find(
79                 "dso_4: /data/app/com.example.hellojni-1/base.apk!/lib/arm64-v8a/libhello-jni.so"),
80             std::string::npos);
81 }
82 
TEST(cmd_debug_unwind,unwind_with_ip_zero_in_callchain)83 TEST(cmd_debug_unwind, unwind_with_ip_zero_in_callchain) {
84   CaptureStdout capture;
85   ASSERT_TRUE(capture.Start());
86   ASSERT_TRUE(DebugUnwindCmd()->Run({"-i", GetTestData(PERF_DATA_WITH_IP_ZERO_IN_CALLCHAIN),
87                                      "--unwind-sample", "--sample-time", "152526249937103"}));
88   ASSERT_NE(capture.Finish().find("sample_time: 152526249937103"), std::string::npos);
89 }
90 
TEST(cmd_debug_unwind,unwind_embedded_lib_in_apk)91 TEST(cmd_debug_unwind, unwind_embedded_lib_in_apk) {
92   // Check if we can unwind through a native library embedded in an apk. In the profiling data
93   // file, there is a sample with ip address pointing to
94   // /data/app/simpleperf.demo.cpp_api/base.apk!/lib/arm64-v8a/libnative-lib.so.
95   // If unwound successfully, it can reach a function in libc.so.
96   CaptureStdout capture;
97   ASSERT_TRUE(capture.Start());
98   ASSERT_TRUE(DebugUnwindCmd()->Run({"-i", GetTestData("perf_unwind_embedded_lib_in_apk.data"),
99                                      "--symfs", GetTestDataDir(), "--unwind-sample",
100                                      "--sample-time", "20345907755421"}));
101   std::string output = capture.Finish();
102   ASSERT_NE(
103       output.find(
104           "dso_1: /data/app/simpleperf.demo.cpp_api/base.apk!/lib/arm64-v8a/libnative-lib.so"),
105       std::string::npos)
106       << output;
107   ASSERT_NE(output.find("dso_2: /bionic/lib64/libc.so"), std::string::npos) << output;
108 }
109 
TEST(cmd_debug_unwind,unwind_sample_in_unwinding_debug_info_file)110 TEST(cmd_debug_unwind, unwind_sample_in_unwinding_debug_info_file) {
111   CaptureStdout capture;
112   ASSERT_TRUE(capture.Start());
113   ASSERT_TRUE(DebugUnwindCmd()->Run(
114       {"-i", GetTestData("perf_with_failed_unwinding_debug_info.data"), "--unwind-sample"}));
115   std::string output = capture.Finish();
116   ASSERT_NE(output.find("symbol_5: android.os.Handler.post"), std::string::npos) << output;
117 }
118 
TEST(cmd_debug_unwind,skip_sample_print_option)119 TEST(cmd_debug_unwind, skip_sample_print_option) {
120   std::string input_data = GetTestData(PERF_DATA_NO_UNWIND);
121   CaptureStdout capture;
122 
123   ASSERT_TRUE(capture.Start());
124   ASSERT_TRUE(DebugUnwindCmd()->Run({"-i", input_data, "--unwind-sample", "--skip-sample-print"}));
125 
126   std::string output = capture.Finish();
127   ASSERT_EQ(output.find("sample_time:"), std::string::npos);
128   ASSERT_NE(output.find("unwinding_sample_count: 8"), std::string::npos);
129 }
130 
TEST(cmd_debug_unwind,generate_test_file)131 TEST(cmd_debug_unwind, generate_test_file) {
132   TemporaryFile tmpfile;
133   close(tmpfile.release());
134   ASSERT_TRUE(DebugUnwindCmd()->Run(
135       {"-i", GetTestData("perf_with_failed_unwinding_debug_info.data"), "--generate-test-file",
136        "--sample-time", "626968783364202", "-o", tmpfile.path, "--keep-binaries-in-test-file",
137        "perf.data_jit_app_cache:255984-259968,perf.data_jit_app_cache:280144-283632"}));
138 
139   CaptureStdout capture;
140   ASSERT_TRUE(capture.Start());
141   ASSERT_TRUE(DebugUnwindCmd()->Run({"-i", tmpfile.path, "--unwind-sample"}));
142   std::string output = capture.Finish();
143   ASSERT_NE(output.find("symbol_2: android.os.Handler.enqueueMessage"), std::string::npos);
144 }
145 
TEST(cmd_debug_unwind,generate_test_file_with_build_id)146 TEST(cmd_debug_unwind, generate_test_file_with_build_id) {
147   TemporaryFile tmpfile;
148   close(tmpfile.release());
149   ASSERT_TRUE(DebugUnwindCmd()->Run({"-i", GetTestData("perf_display_bitmaps.data"),
150                                      "--generate-test-file", "--sample-time", "684943450156904",
151                                      "-o", tmpfile.path, "--keep-binaries-in-test-file",
152                                      "/apex/com.android.runtime/lib64/bionic/libc.so"}));
153   auto reader = RecordFileReader::CreateInstance(tmpfile.path);
154   ASSERT_TRUE(reader);
155   auto build_ids = reader->ReadBuildIdFeature();
156   ASSERT_EQ(build_ids.size(), 1);
157   ASSERT_STREQ(build_ids[0].filename, "/apex/com.android.runtime/lib64/bionic/libc.so");
158 }
159 
TEST(cmd_debug_unwind,generate_report)160 TEST(cmd_debug_unwind, generate_report) {
161   TemporaryFile tmpfile;
162   close(tmpfile.release());
163   ASSERT_TRUE(
164       DebugUnwindCmd()->Run({"-i", GetTestData("perf_with_failed_unwinding_debug_info.data"),
165                              "--generate-report", "-o", tmpfile.path}));
166   std::string output;
167   ASSERT_TRUE(android::base::ReadFileToString(tmpfile.path, &output));
168   ASSERT_NE(output.find("unwinding_error_code: 4"), std::string::npos);
169   ASSERT_NE(output.find("symbol_2: android.os.Handler.enqueueMessage"), std::string::npos);
170 }
171 
TEST(cmd_debug_unwind,unwind_sample_for_small_map_range)172 TEST(cmd_debug_unwind, unwind_sample_for_small_map_range) {
173   CaptureStdout capture;
174   ASSERT_TRUE(capture.Start());
175   ASSERT_TRUE(DebugUnwindCmd()->Run(
176       {"-i", GetTestData("debug_unwind_small_map_range.data"), "--unwind-sample"}));
177   std::string output = capture.Finish();
178   ASSERT_NE(output.find("dso_3: /apex/com.android.art/lib64/libart.so"), std::string::npos)
179       << output;
180 }
181