1 /*
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
19 #include <libgen.h>
20
21 #include <memory>
22
23 #include <android-base/file.h>
24 #include <android-base/logging.h>
25 #include <android-base/strings.h>
26
27 #if defined(__ANDROID__)
28 #include <android-base/properties.h>
29 #endif
30
31 #include "command.h"
32 #include "environment.h"
33 #include "get_test_data.h"
34 #include "read_elf.h"
35 #include "test_util.h"
36 #include "utils.h"
37 #include "workload.h"
38
39 using namespace simpleperf;
40
41 static std::string testdata_dir;
42
43 #if defined(__ANDROID__)
44
45 class ScopedEnablingPerf {
46 public:
ScopedEnablingPerf()47 ScopedEnablingPerf() {
48 prop_value_ = android::base::GetProperty("security.perf_harden", "");
49 SetProp("0");
50 }
51
~ScopedEnablingPerf()52 ~ScopedEnablingPerf() {
53 if (!prop_value_.empty()) {
54 SetProp(prop_value_);
55 }
56 }
57
58 private:
SetProp(const std::string & value)59 void SetProp(const std::string& value) {
60 android::base::SetProperty("security.perf_harden", value);
61
62 // Sleep one second to wait for security.perf_harden changing
63 // perf_event_allow_path.
64 sleep(1);
65 }
66
67 std::string prop_value_;
68 };
69
70 #endif // defined(__ANDROID__)
71
main(int argc,char ** argv)72 int main(int argc, char** argv) {
73 // 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
79 android::base::InitLogging(argv, android::base::StderrLogger);
80 android::base::LogSeverity log_severity = android::base::WARNING;
81 testdata_dir = std::string(dirname(argv[0])) + "/testdata";
82 for (int i = 1; i < argc; ++i) {
83 if (strcmp(argv[i], "-t") == 0 && i + 1 < argc) {
84 testdata_dir = argv[i + 1];
85 i++;
86 } else if (strcmp(argv[i], "--log") == 0) {
87 if (i + 1 < argc) {
88 ++i;
89 if (!GetLogSeverity(argv[i], &log_severity)) {
90 LOG(ERROR) << "Unknown log severity: " << argv[i];
91 return 1;
92 }
93 } else {
94 LOG(ERROR) << "Missing argument for --log option.\n";
95 return 1;
96 }
97 }
98 }
99 android::base::ScopedLogSeverity severity(log_severity);
100
101 #if defined(__ANDROID__)
102 // A cts test is testing if perf_event_allow_path is 3, so restore perf_harden
103 // value after current test to not break that test.
104 ScopedEnablingPerf scoped_enabling_perf;
105 #endif
106
107 testing::InitGoogleTest(&argc, argv);
108 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 }
113 }
114 if (!android::base::EndsWith(testdata_dir, OS_PATH_SEPARATOR)) {
115 testdata_dir += OS_PATH_SEPARATOR;
116 }
117 LOG(INFO) << "testdata is in " << testdata_dir;
118 return RUN_ALL_TESTS();
119 }
120
GetTestData(const std::string & filename)121 std::string GetTestData(const std::string& filename) {
122 return testdata_dir + filename;
123 }
124
GetTestDataDir()125 const std::string& GetTestDataDir() {
126 return testdata_dir;
127 }
128