Home | History | Annotate | Download | only in test
      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/ftrace_reader/test/cpu_reader_support.h"
     18 
     19 #include "perfetto/base/utils.h"
     20 #include "src/ftrace_reader/ftrace_procfs.h"
     21 
     22 #include <string.h>
     23 
     24 namespace perfetto {
     25 namespace {
     26 
     27 std::map<std::string, std::unique_ptr<ProtoTranslationTable>>* g_tables;
     28 
     29 }  // namespace
     30 
     31 ProtoTranslationTable* GetTable(const std::string& name) {
     32   if (!g_tables)
     33     g_tables =
     34         new std::map<std::string, std::unique_ptr<ProtoTranslationTable>>();
     35   if (!g_tables->count(name)) {
     36     std::string path = "src/ftrace_reader/test/data/" + name + "/";
     37     FtraceProcfs ftrace(path);
     38     auto table = ProtoTranslationTable::Create(&ftrace, GetStaticEventInfo(),
     39                                                GetStaticCommonFieldsInfo());
     40     g_tables->emplace(name, std::move(table));
     41   }
     42   return g_tables->at(name).get();
     43 }
     44 
     45 std::unique_ptr<uint8_t[]> PageFromXxd(const std::string& text) {
     46   auto buffer = std::unique_ptr<uint8_t[]>(new uint8_t[base::kPageSize]);
     47   const char* ptr = text.data();
     48   memset(buffer.get(), 0xfa, base::kPageSize);
     49   uint8_t* out = buffer.get();
     50   while (*ptr != '\0') {
     51     if (*(ptr++) != ':')
     52       continue;
     53     for (int i = 0; i < 8; i++) {
     54       PERFETTO_CHECK(text.size() >=
     55                      static_cast<size_t>((ptr - text.data()) + 5));
     56       PERFETTO_CHECK(*(ptr++) == ' ');
     57       int n = sscanf(ptr, "%02hhx%02hhx", out, out + 1);
     58       PERFETTO_CHECK(n == 2);
     59       out += n;
     60       ptr += 4;
     61     }
     62     while (*ptr != '\n')
     63       ptr++;
     64   }
     65   return buffer;
     66 }
     67 
     68 }  // namespace perfetto
     69