• 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 "src/traced/probes/ftrace/test/cpu_reader_support.h"
18 
19 #include "perfetto/base/utils.h"
20 #include "src/traced/probes/ftrace/ftrace_procfs.h"
21 
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 
27 namespace perfetto {
28 namespace {
29 
30 std::map<std::string, std::unique_ptr<ProtoTranslationTable>>* g_tables;
31 
GetBinaryDirectory()32 std::string GetBinaryDirectory() {
33   std::string buf(512, '\0');
34   ssize_t rd = readlink("/proc/self/exe", &buf[0], buf.size());
35   if (rd < 0) {
36     PERFETTO_ELOG("Failed to readlink(\"/proc/self/exe\"");
37     return "";
38   }
39   buf.resize(static_cast<size_t>(rd));
40   size_t end = buf.rfind('/');
41   if (end == std::string::npos) {
42     PERFETTO_ELOG("Failed to find directory.");
43     return "";
44   }
45   return buf.substr(0, end + 1);
46 }
47 
48 }  // namespace
49 
GetTable(const std::string & name)50 ProtoTranslationTable* GetTable(const std::string& name) {
51   if (!g_tables)
52     g_tables =
53         new std::map<std::string, std::unique_ptr<ProtoTranslationTable>>();
54   if (!g_tables->count(name)) {
55     std::string path = "src/traced/probes/ftrace/test/data/" + name + "/";
56     struct stat st;
57     if (lstat(path.c_str(), &st) == -1 && errno == ENOENT) {
58       // For OSS fuzz, which does not run in the correct cwd.
59       path = GetBinaryDirectory() + path;
60     }
61     FtraceProcfs ftrace(path);
62     auto table = ProtoTranslationTable::Create(&ftrace, GetStaticEventInfo(),
63                                                GetStaticCommonFieldsInfo());
64     if (!table)
65       return nullptr;
66     g_tables->emplace(name, std::move(table));
67   }
68   return g_tables->at(name).get();
69 }
70 
PageFromXxd(const std::string & text)71 std::unique_ptr<uint8_t[]> PageFromXxd(const std::string& text) {
72   auto buffer = std::unique_ptr<uint8_t[]>(new uint8_t[base::kPageSize]);
73   const char* ptr = text.data();
74   memset(buffer.get(), 0xfa, base::kPageSize);
75   uint8_t* out = buffer.get();
76   while (*ptr != '\0') {
77     if (*(ptr++) != ':')
78       continue;
79     for (int i = 0; i < 8; i++) {
80       PERFETTO_CHECK(text.size() >=
81                      static_cast<size_t>((ptr - text.data()) + 5));
82       PERFETTO_CHECK(*(ptr++) == ' ');
83       int n = sscanf(ptr, "%02hhx%02hhx", out, out + 1);
84       PERFETTO_CHECK(n == 2);
85       out += n;
86       ptr += 4;
87     }
88     while (*ptr != '\n')
89       ptr++;
90   }
91   return buffer;
92 }
93 
94 }  // namespace perfetto
95