Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d53feceb authored by Chris Manton's avatar Chris Manton
Browse files

Initial dumpsys parsing module

-Still requires parser and printer

Bug: 157647700
Test: atest --host bluetooth_test_gd
Tag: #gd-refactor
Change-Id: I16fdd3f0e0508d5068667eaa487fd8856edb1667
parent ad463825
Loading
Loading
Loading
Loading
+2 −0
Original line number Original line Diff line number Diff line
// Top level module dumpsys data schema
// Top level module dumpsys data schema
include "module_unittest.fbs";
include "module_unittest.fbs";
include "shim/dumpsys.fbs";


namespace bluetooth;
namespace bluetooth;


@@ -7,6 +8,7 @@ attribute "privacy";


table DumpsysData {
table DumpsysData {
    title:string;
    title:string;
    shim_dumpsys_data:bluetooth.shim.DumpsysModuleData (privacy:"Any");
    module_unittest_data:bluetooth.ModuleUnitTestData; // private
    module_unittest_data:bluetooth.ModuleUnitTestData; // private
}
}


+1 −0
Original line number Original line Diff line number Diff line
@@ -10,6 +10,7 @@ filegroup {
filegroup {
filegroup {
    name: "BluetoothShimTestSources",
    name: "BluetoothShimTestSources",
    srcs: [
    srcs: [
            "dumpsys_test.cc",
            "l2cap_test.cc",
            "l2cap_test.cc",
    ],
    ],
}
}
+101 −26
Original line number Original line Diff line number Diff line
@@ -26,6 +26,8 @@
#include <utility>
#include <utility>


#include "bundler_generated.h"
#include "bundler_generated.h"
#include "dumpsys_generated.h"
#include "dumpsys_module_schema_data.h"
#include "flatbuffers/idl.h"
#include "flatbuffers/idl.h"
#include "flatbuffers/reflection_generated.h"
#include "flatbuffers/reflection_generated.h"
#include "module.h"
#include "module.h"
@@ -71,42 +73,89 @@ struct Dumpsys::impl {
 public:
 public:
  void DumpWithArgs(int fd, const char** args, std::promise<void> promise);
  void DumpWithArgs(int fd, const char** args, std::promise<void> promise);


  impl(const Dumpsys& dumpsys_module);
  impl(const Dumpsys& dumpsys_module, const std::string& bundled_schema_data);
  ~impl() = default;
  ~impl() = default;


  int GetNumberOfBundledSchemas() const;

 protected:
 protected:
  void FilterAsUser(std::string* output);
  void FilterAsUser(std::string* dumpsys_data);
  void FilterAsDeveloper(std::string* output);
  void FilterAsDeveloper(std::string* dumpsys_data);
  std::string PrintAsJson(std::string* output) const;
  std::string PrintAsJson(std::string* dumpsys_data) const;


 private:
 private:
  const reflection::Schema* FindBundledSchema(
  const reflection::Schema* FindInBundledSchema(const std::string& name) const;
      const dumpsys::BundledSchema& bundled_schema, const std::string& name) const;
  const dumpsys::BundledSchema* GetBundledSchema() const;
  const Dumpsys& dumpsys_module_;
  const Dumpsys& dumpsys_module_;
  const std::string pre_bundled_schema_;
};
};


const ModuleFactory Dumpsys::Factory = ModuleFactory([]() { return new Dumpsys(); });
const ModuleFactory Dumpsys::Factory =
    ModuleFactory([]() { return new Dumpsys(bluetooth::dumpsys::GetBundledSchemaData()); });

Dumpsys::impl::impl(const Dumpsys& dumpsys_module, const std::string& pre_bundled_schema)
    : dumpsys_module_(dumpsys_module), pre_bundled_schema_(pre_bundled_schema) {}


Dumpsys::impl::impl(const Dumpsys& dumpsys_module) : dumpsys_module_(dumpsys_module) {}
int Dumpsys::impl::GetNumberOfBundledSchemas() const {
  return GetBundledSchema()->map()->size();
}


void Dumpsys::impl::FilterAsDeveloper(std::string* output) {
const dumpsys::BundledSchema* Dumpsys::impl::GetBundledSchema() const {
  ASSERT(output != nullptr);
  const dumpsys::BundledSchema* bundled_schema =
      flatbuffers::GetRoot<dumpsys::BundledSchema>(pre_bundled_schema_.data());
  ASSERT(bundled_schema != nullptr);
  return bundled_schema;
}

const reflection::Schema* Dumpsys::impl::FindInBundledSchema(const std::string& name) const {
  const flatbuffers::Vector<flatbuffers::Offset<dumpsys::BundledSchemaMap>>* map = GetBundledSchema()->map();

  for (auto it = map->cbegin(); it != map->cend(); ++it) {
    if (it->name()->str() == name) {
      flatbuffers::Verifier verifier(reinterpret_cast<const uint8_t*>(it->data()->Data()), it->data()->size());
      if (!reflection::VerifySchemaBuffer(verifier)) {
        LOG_WARN("Unable to verify schema buffer name:%s", name.c_str());
        return nullptr;
      }
      return reflection::GetSchema(it->data()->Data());
    }
  }

  LOG_WARN("Unable to find bundled schema name:%s", name.c_str());
  LOG_WARN("  title:%s root_name:%s", GetBundledSchema()->title()->c_str(), GetBundledSchema()->root_name()->c_str());
  for (auto it = map->cbegin(); it != map->cend(); ++it) {
    LOG_WARN("    schema:%s", it->name()->c_str());
  }
  return nullptr;
}

void Dumpsys::impl::FilterAsDeveloper(std::string* dumpsys_data) {
  ASSERT(dumpsys_data != nullptr);
  LOG_INFO("%s UNIMPLEMENTED", __func__);
  LOG_INFO("%s UNIMPLEMENTED", __func__);
}
}


void Dumpsys::impl::FilterAsUser(std::string* output) {
void Dumpsys::impl::FilterAsUser(std::string* dumpsys_data) {
  ASSERT(output != nullptr);
  ASSERT(dumpsys_data != nullptr);
  LOG_INFO("%s UNIMPLEMENTED", __func__);
  LOG_INFO("%s UNIMPLEMENTED", __func__);
}
}


const reflection::Schema* Dumpsys::impl::FindBundledSchema(
std::string Dumpsys::impl::PrintAsJson(std::string* dumpsys_data) const {
    const dumpsys::BundledSchema& bundled_schema, const std::string& name) const {
  ASSERT(dumpsys_data != nullptr);
  // TODO(cmanton) Return proper schema given schema container and name to index

  return nullptr;
  const flatbuffers::String* root_name = GetBundledSchema()->root_name();
  if (root_name == nullptr) {
    char buf[255];
    snprintf(buf, sizeof(buf), "ERROR: Unable to find root name in prebundled schema\n");
    return std::string(buf);
  }
  }


std::string Dumpsys::impl::PrintAsJson(std::string* output) const {
  const reflection::Schema* schema = FindInBundledSchema(root_name->str());
  return std::string("UNIMPLEMENTED");
  if (schema == nullptr) {
    char buf[255];
    snprintf(buf, sizeof(buf), "ERROR: Unable to find schema root name:%s\n", root_name->c_str());
    return std::string(buf);
  }
  return std::string("UNIMPLEMENTED\n");
}
}


void Dumpsys::impl::DumpWithArgs(int fd, const char** args, std::promise<void> promise) {
void Dumpsys::impl::DumpWithArgs(int fd, const char** args, std::promise<void> promise) {
@@ -114,20 +163,23 @@ void Dumpsys::impl::DumpWithArgs(int fd, const char** args, std::promise<void> p
  const auto registry = dumpsys_module_.GetModuleRegistry();
  const auto registry = dumpsys_module_.GetModuleRegistry();


  ModuleDumper dumper(*registry, kDumpsysTitle);
  ModuleDumper dumper(*registry, kDumpsysTitle);
  std::string output;
  std::string dumpsys_data;
  // Get the dumpstate into out string
  dumper.DumpState(&dumpsys_data);
  dumper.DumpState(&output);


  if (parsed_dumpsys_args.IsDeveloper()) {
  if (parsed_dumpsys_args.IsDeveloper()) {
    FilterAsDeveloper(&output);
    dprintf(fd, " ----- Filtering as Developer -----\n");
    FilterAsDeveloper(&dumpsys_data);
  } else {
  } else {
    FilterAsUser(&output);
    dprintf(fd, " ----- Filtering as User -----\n");
    FilterAsUser(&dumpsys_data);
  }
  }


  dprintf(fd, "%s", PrintAsJson(&output).c_str());
  dprintf(fd, "%s", PrintAsJson(&dumpsys_data).c_str());
  promise.set_value();
  promise.set_value();
}
}


Dumpsys::Dumpsys(const std::string& pre_bundled_schema) : pre_bundled_schema_(pre_bundled_schema) {}

void Dumpsys::Dump(int fd, const char** args) {
void Dumpsys::Dump(int fd, const char** args) {
  std::promise<void> promise;
  std::promise<void> promise;
  auto future = promise.get_future();
  auto future = promise.get_future();
@@ -145,13 +197,36 @@ os::Handler* Dumpsys::GetGdShimHandler() {
void Dumpsys::ListDependencies(ModuleList* list) {}
void Dumpsys::ListDependencies(ModuleList* list) {}


void Dumpsys::Start() {
void Dumpsys::Start() {
  pimpl_ = std::make_unique<impl>(*this);
  pimpl_ = std::make_unique<impl>(*this, pre_bundled_schema_);
}
}


void Dumpsys::Stop() {
void Dumpsys::Stop() {
  pimpl_.reset();
  pimpl_.reset();
}
}


DumpsysDataFinisher Dumpsys::GetDumpsysData(flatbuffers::FlatBufferBuilder* fb_builder) const {
  auto name = fb_builder->CreateString("----- Shim Dumpsys -----");
  auto example_piecemeal_string = fb_builder->CreateString("Example Piecemeal String");
  auto example_instant_string = fb_builder->CreateString("Example Instant String");

  ExamplePiecemealTableBuilder example_piecemeal_table_builder(*fb_builder);
  example_piecemeal_table_builder.add_example_string(example_piecemeal_string);
  example_piecemeal_table_builder.add_example_int(123);
  example_piecemeal_table_builder.add_example_float(1.23);
  auto example_piecemeal_table = example_piecemeal_table_builder.Finish();

  auto example_instant_table = CreateExampleInstantTable(*fb_builder, example_instant_string, 246, 2.46);

  DumpsysModuleDataBuilder builder(*fb_builder);
  builder.add_title(name);
  builder.add_number_of_bundled_schemas(pimpl_->GetNumberOfBundledSchemas());
  builder.add_example_piecemeal_table(example_piecemeal_table);
  builder.add_example_instant_table(example_instant_table);
  auto dumpsys_data = builder.Finish();

  return [dumpsys_data](DumpsysDataBuilder* builder) { builder->add_shim_dumpsys_data(dumpsys_data); };
}

std::string Dumpsys::ToString() const {
std::string Dumpsys::ToString() const {
  return kModuleName;
  return kModuleName;
}
}
+15 −0
Original line number Original line Diff line number Diff line
@@ -3,8 +3,23 @@ namespace bluetooth.shim;


attribute "privacy";
attribute "privacy";


table ExamplePiecemealTable {
    example_string:string (privacy:"Any");
    example_int:int (privacy:"Any");
    example_float:float (privacy:"Any");
}

table ExampleInstantTable {
    example_string:string (privacy:"Any");
    example_int:int (privacy:"Any");
    example_float:float (privacy:"Any");
}

table DumpsysModuleData {
table DumpsysModuleData {
    title:string (privacy:"Any");
    title:string (privacy:"Any");
    number_of_bundled_schemas:int (privacy:"Any");
    example_piecemeal_table:ExamplePiecemealTable (privacy:"Any");
    example_instant_table:ExampleInstantTable (privacy:"Any");
}
}


root_type DumpsysModuleData;
root_type DumpsysModuleData;
+3 −1
Original line number Original line Diff line number Diff line
@@ -30,7 +30,7 @@ class Dumpsys : public bluetooth::Module {
  // Convenience thread used by shim layer for task execution
  // Convenience thread used by shim layer for task execution
  os::Handler* GetGdShimHandler();
  os::Handler* GetGdShimHandler();


  Dumpsys() = default;
  Dumpsys(const std::string& pre_bundled_schema);
  ~Dumpsys() = default;
  ~Dumpsys() = default;


  static const ModuleFactory Factory;
  static const ModuleFactory Factory;
@@ -40,10 +40,12 @@ class Dumpsys : public bluetooth::Module {
  void Start() override;                             // Module
  void Start() override;                             // Module
  void Stop() override;                              // Module
  void Stop() override;                              // Module
  std::string ToString() const override;             // Module
  std::string ToString() const override;             // Module
  DumpsysDataFinisher GetDumpsysData(flatbuffers::FlatBufferBuilder* builder) const override;  // Module


 private:
 private:
  struct impl;
  struct impl;
  std::unique_ptr<impl> pimpl_;
  std::unique_ptr<impl> pimpl_;
  const std::string& pre_bundled_schema_;
  DISALLOW_COPY_AND_ASSIGN(Dumpsys);
  DISALLOW_COPY_AND_ASSIGN(Dumpsys);
};
};


Loading