blob: c8942b6947b380f64e71af05a8d9da2eccd6ffb1 [file] [log] [blame]
Yabin Cui323e9452015-04-20 18:07:17 -07001/*
2 * Copyright (C) 2015 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
Colin Cross1e8ad072016-12-17 12:38:46 -080019#include <libgen.h>
20
Yabin Cuibe7ec662016-03-02 13:56:28 -080021#include <memory>
22
23#include <android-base/file.h>
Elliott Hughes66dd09e2015-12-04 14:00:57 -080024#include <android-base/logging.h>
Yabin Cui1b9b1c12018-10-29 14:23:48 -070025#include <android-base/strings.h>
Yabin Cui323e9452015-04-20 18:07:17 -070026
Yabin Cui58961322016-06-10 12:15:11 -070027#if defined(__ANDROID__)
Yabin Cui80a1e122017-08-11 14:52:51 -070028#include <android-base/properties.h>
Yabin Cui58961322016-06-10 12:15:11 -070029#endif
30
Yabin Cui616b3a02017-07-14 15:59:56 -070031#include "command.h"
32#include "environment.h"
Yabin Cui569f64a2016-02-05 17:32:08 -080033#include "get_test_data.h"
Yabin Cuibe7ec662016-03-02 13:56:28 -080034#include "read_elf.h"
Yabin Cui616b3a02017-07-14 15:59:56 -070035#include "test_util.h"
Yabin Cui569f64a2016-02-05 17:32:08 -080036#include "utils.h"
Yabin Cui616b3a02017-07-14 15:59:56 -070037#include "workload.h"
Yabin Cui569f64a2016-02-05 17:32:08 -080038
Yabin Cuiacbdb242020-07-07 15:56:34 -070039using namespace simpleperf;
40
Yabin Cui569f64a2016-02-05 17:32:08 -080041static std::string testdata_dir;
42
Yabin Cui6acf8c62016-03-18 13:48:42 -070043#if defined(__ANDROID__)
Yabin Cui58961322016-06-10 12:15:11 -070044
Yabin Cuif560a6f2016-12-14 17:43:26 -080045class ScopedEnablingPerf {
Yabin Cui58961322016-06-10 12:15:11 -070046 public:
Yabin Cuif560a6f2016-12-14 17:43:26 -080047 ScopedEnablingPerf() {
Yabin Cui80a1e122017-08-11 14:52:51 -070048 prop_value_ = android::base::GetProperty("security.perf_harden", "");
Yabin Cuif560a6f2016-12-14 17:43:26 -080049 SetProp("0");
Yabin Cui58961322016-06-10 12:15:11 -070050 }
51
Yabin Cuif560a6f2016-12-14 17:43:26 -080052 ~ScopedEnablingPerf() {
Yabin Cui80a1e122017-08-11 14:52:51 -070053 if (!prop_value_.empty()) {
Yabin Cuif560a6f2016-12-14 17:43:26 -080054 SetProp(prop_value_);
Yabin Cui58961322016-06-10 12:15:11 -070055 }
56 }
57
58 private:
Yabin Cui80a1e122017-08-11 14:52:51 -070059 void SetProp(const std::string& value) {
60 android::base::SetProperty("security.perf_harden", value);
61
Yabin Cuif560a6f2016-12-14 17:43:26 -080062 // Sleep one second to wait for security.perf_harden changing
Yabin Cui17cffbb2020-07-28 17:11:39 -070063 // perf_event_allow_path.
Yabin Cuif560a6f2016-12-14 17:43:26 -080064 sleep(1);
65 }
66
Yabin Cui80a1e122017-08-11 14:52:51 -070067 std::string prop_value_;
Yabin Cui58961322016-06-10 12:15:11 -070068};
69
Yabin Cui6acf8c62016-03-18 13:48:42 -070070#endif // defined(__ANDROID__)
Yabin Cuibe7ec662016-03-02 13:56:28 -080071
Yabin Cui323e9452015-04-20 18:07:17 -070072int main(int argc, char** argv) {
Yabin Cui56233cc2019-09-30 16:46:26 -070073 // To test profiling apps, simpleperf_unit_test needs to copy itself to the app's directory,
74 // and run the binary as simpleperf executable.
75 if (android::base::Basename(argv[0]) == "simpleperf") {
76 return RunSimpleperfCmd(argc, argv) ? 0 : 1;
77 }
78
Krzysztof KosiƄski5f895342023-03-03 04:26:06 +000079 testing::InitGoogleTest(&argc, argv);
Yabin Cuif560a6f2016-12-14 17:43:26 -080080 android::base::InitLogging(argv, android::base::StderrLogger);
Yabin Cui8f680f62016-03-18 18:47:43 -070081 android::base::LogSeverity log_severity = android::base::WARNING;
Yabin Cui803c27e2019-01-25 10:47:25 -080082 testdata_dir = std::string(dirname(argv[0])) + "/testdata";
Yabin Cui569f64a2016-02-05 17:32:08 -080083 for (int i = 1; i < argc; ++i) {
84 if (strcmp(argv[i], "-t") == 0 && i + 1 < argc) {
85 testdata_dir = argv[i + 1];
Yabin Cuibe7ec662016-03-02 13:56:28 -080086 i++;
Yabin Cui8f680f62016-03-18 18:47:43 -070087 } else if (strcmp(argv[i], "--log") == 0) {
88 if (i + 1 < argc) {
89 ++i;
90 if (!GetLogSeverity(argv[i], &log_severity)) {
91 LOG(ERROR) << "Unknown log severity: " << argv[i];
92 return 1;
93 }
94 } else {
95 LOG(ERROR) << "Missing argument for --log option.\n";
96 return 1;
97 }
Yabin Cui569f64a2016-02-05 17:32:08 -080098 }
99 }
Yabin Cui8f680f62016-03-18 18:47:43 -0700100 android::base::ScopedLogSeverity severity(log_severity);
Yabin Cuibe7ec662016-03-02 13:56:28 -0800101
Yabin Cui6acf8c62016-03-18 13:48:42 -0700102#if defined(__ANDROID__)
Yabin Cui17cffbb2020-07-28 17:11:39 -0700103 // A cts test is testing if perf_event_allow_path is 3, so restore perf_harden
Yabin Cui616b3a02017-07-14 15:59:56 -0700104 // value after current test to not break that test.
105 ScopedEnablingPerf scoped_enabling_perf;
Yabin Cuibe7ec662016-03-02 13:56:28 -0800106#endif
Yabin Cui6acf8c62016-03-18 13:48:42 -0700107
Yabin Cui803c27e2019-01-25 10:47:25 -0800108 if (!::testing::GTEST_FLAG(list_tests)) {
109 if (!IsDir(testdata_dir)) {
110 LOG(ERROR) << "testdata wasn't found. Use \"" << argv[0] << " -t <testdata_dir>\"";
111 return 1;
112 }
Yabin Cui569f64a2016-02-05 17:32:08 -0800113 }
Yabin Cui1b9b1c12018-10-29 14:23:48 -0700114 if (!android::base::EndsWith(testdata_dir, OS_PATH_SEPARATOR)) {
115 testdata_dir += OS_PATH_SEPARATOR;
Yabin Cui569f64a2016-02-05 17:32:08 -0800116 }
Yabin Cuibe7ec662016-03-02 13:56:28 -0800117 LOG(INFO) << "testdata is in " << testdata_dir;
Yabin Cui323e9452015-04-20 18:07:17 -0700118 return RUN_ALL_TESTS();
119}
Yabin Cui569f64a2016-02-05 17:32:08 -0800120
121std::string GetTestData(const std::string& filename) {
122 return testdata_dir + filename;
123}
Yabin Cuib1a885b2016-02-14 19:18:02 -0800124
125const std::string& GetTestDataDir() {
126 return testdata_dir;
127}