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   RegisterAllCommands();
74   // To test profiling apps, simpleperf_unit_test needs to copy itself to the app's directory,
75   // and run the binary as simpleperf executable.
76   if (android::base::Basename(argv[0]) == "simpleperf") {
77     return RunSimpleperfCmd(argc, argv) ? 0 : 1;
78   }
79 
80   testing::InitGoogleTest(&argc, argv);
81   android::base::InitLogging(argv, android::base::StderrLogger);
82   android::base::LogSeverity log_severity = android::base::WARNING;
83   testdata_dir = std::string(dirname(argv[0])) + "/testdata";
84   for (int i = 1; i < argc; ++i) {
85     if (strcmp(argv[i], "-t") == 0 && i + 1 < argc) {
86       testdata_dir = argv[i + 1];
87       i++;
88     } else if (strcmp(argv[i], "--log") == 0) {
89       if (i + 1 < argc) {
90         ++i;
91         if (!GetLogSeverity(argv[i], &log_severity)) {
92           LOG(ERROR) << "Unknown log severity: " << argv[i];
93           return 1;
94         }
95       } else {
96         LOG(ERROR) << "Missing argument for --log option.\n";
97         return 1;
98       }
99     }
100   }
101   android::base::ScopedLogSeverity severity(log_severity);
102 
103 #if defined(__ANDROID__)
104   // A cts test is testing if perf_event_allow_path is 3, so restore perf_harden
105   // value after current test to not break that test.
106   ScopedEnablingPerf scoped_enabling_perf;
107 #endif
108 
109   if (!::testing::GTEST_FLAG(list_tests)) {
110     if (!IsDir(testdata_dir)) {
111       LOG(ERROR) << "testdata wasn't found. Use \"" << argv[0] << " -t <testdata_dir>\"";
112       return 1;
113     }
114   }
115   if (!android::base::EndsWith(testdata_dir, OS_PATH_SEPARATOR)) {
116     testdata_dir += OS_PATH_SEPARATOR;
117   }
118   LOG(INFO) << "testdata is in " << testdata_dir;
119   return RUN_ALL_TESTS();
120 }
121 
GetTestData(const std::string & filename)122 std::string GetTestData(const std::string& filename) {
123   return testdata_dir + filename;
124 }
125 
GetTestDataDir()126 const std::string& GetTestDataDir() {
127   return testdata_dir;
128 }
129