From 4c331fb5a220a9232637b021e98044c53eb718df Mon Sep 17 00:00:00 2001 From: Jake Hillion Date: Tue, 6 Jun 2023 08:13:51 -0700 Subject: [PATCH] clean up common.h --- oi/CMakeLists.txt | 2 - oi/ContainerInfo.h | 2 +- oi/{Common.h => ContainerTypeEnum.h} | 53 ------------------------- oi/OICodeGen.h | 2 +- oi/OIDebugger.cpp | 2 + oi/Serialize.h | 2 +- oi/SymbolService.cpp | 9 +++++ oi/SymbolService.h | 2 +- oi/TreeBuilder.h | 2 +- oi/TypeHierarchy.h | 59 ++++++++++++++++++++++++++++ oi/type_graph/AddChildren.cpp | 4 ++ oi/type_graph/AddChildren.h | 1 + oi/type_graph/DrgnParser.cpp | 6 +++ oi/type_graph/DrgnParser.h | 8 ++-- tools/OITB.cpp | 2 +- 15 files changed, 90 insertions(+), 66 deletions(-) rename oi/{Common.h => ContainerTypeEnum.h} (62%) create mode 100644 oi/TypeHierarchy.h diff --git a/oi/CMakeLists.txt b/oi/CMakeLists.txt index bcf3776..eae31d9 100644 --- a/oi/CMakeLists.txt +++ b/oi/CMakeLists.txt @@ -30,8 +30,6 @@ add_library(container_info ContainerInfo.cpp ) target_link_libraries(container_info - drgn_utils # This shouldn't be needed! Clean up Commoh.h! - glog::glog toml ) diff --git a/oi/ContainerInfo.h b/oi/ContainerInfo.h index ab31238..b869f28 100644 --- a/oi/ContainerInfo.h +++ b/oi/ContainerInfo.h @@ -21,7 +21,7 @@ #include #include -#include "oi/Common.h" +#include "oi/ContainerTypeEnum.h" ContainerTypeEnum containerTypeEnumFromStr(std::string& str); const char* containerTypeEnumToStr(ContainerTypeEnum ty); diff --git a/oi/Common.h b/oi/ContainerTypeEnum.h similarity index 62% rename from oi/Common.h rename to oi/ContainerTypeEnum.h index df113f3..56a6817 100644 --- a/oi/Common.h +++ b/oi/ContainerTypeEnum.h @@ -14,16 +14,6 @@ * limitations under the License. */ #pragma once -#include -#include -#include -#include - -extern "C" { -#include -} - -constexpr int oidMagicId = 0x01DE8; #define LIST_OF_CONTAINER_TYPES \ X(UNKNOWN_TYPE) \ @@ -73,46 +63,3 @@ enum ContainerTypeEnum { LIST_OF_CONTAINER_TYPES #undef X }; - -struct RootInfo { - std::string varName; - struct drgn_qualified_type type; -}; - -struct ClassMember { - std::string typeName; - std::string varName; -}; - -struct DrgnClassMemberInfo { - struct drgn_type* type; - std::string member_name; - uint64_t bit_offset; - uint64_t bit_field_size; - bool isStubbed; -}; - -struct TypeHierarchy { - std::map> classMembersMap; - std::map< - struct drgn_type*, - std::pair>> - containerTypeMap; - std::map typedefMap; - std::map sizeMap; - std::set knownDummyTypeList; - std::map pointerToTypeMap; - std::set thriftIssetStructTypes; - std::map> descendantClasses; -}; - -// Helper for std::variant and std::visit -// https://en.cppreference.com/w/cpp/utility/variant/visit -template -struct visitor : Ts... { - using Ts::operator()...; -}; - -// Type deduction for the helper above -template -visitor(Ts...) -> visitor; diff --git a/oi/OICodeGen.h b/oi/OICodeGen.h index 50879d9..b9a7558 100644 --- a/oi/OICodeGen.h +++ b/oi/OICodeGen.h @@ -26,11 +26,11 @@ class SymbolService; struct irequest; -#include "oi/Common.h" #include "oi/ContainerInfo.h" #include "oi/Features.h" #include "oi/FuncGen.h" #include "oi/PaddingHunter.h" +#include "oi/TypeHierarchy.h" extern "C" { #include diff --git a/oi/OIDebugger.cpp b/oi/OIDebugger.cpp index b225653..2b04538 100644 --- a/oi/OIDebugger.cpp +++ b/oi/OIDebugger.cpp @@ -61,6 +61,8 @@ extern "C" { using namespace std; using namespace ObjectIntrospection; +constexpr int oidMagicId = 0x01DE8; + bool OIDebugger::isGlobalDataProbeEnabled(void) const { return std::any_of(cbegin(pdata), cend(pdata), [](const auto& r) { return r.type == "global"; }); diff --git a/oi/Serialize.h b/oi/Serialize.h index d1f3f0e..313a021 100644 --- a/oi/Serialize.h +++ b/oi/Serialize.h @@ -24,9 +24,9 @@ #include #include -#include "oi/Common.h" #include "oi/PaddingHunter.h" #include "oi/SymbolService.h" +#include "oi/TypeHierarchy.h" #define DEFINE_TYPE_VERSION(Type, size, version) \ static_assert( \ diff --git a/oi/SymbolService.cpp b/oi/SymbolService.cpp index def4b00..f7ea362 100644 --- a/oi/SymbolService.cpp +++ b/oi/SymbolService.cpp @@ -34,6 +34,15 @@ extern "C" { #include "dwarf.h" } +template +struct visitor : Ts... { + using Ts::operator()...; +}; + +// Type deduction for the helper above +template +visitor(Ts...) -> visitor; + static bool LoadExecutableAddressRange( pid_t pid, std::vector>& exeAddrs) { std::ifstream f("/proc/" + std::to_string(pid) + "/maps"); diff --git a/oi/SymbolService.h b/oi/SymbolService.h index 74a575d..65b7672 100644 --- a/oi/SymbolService.h +++ b/oi/SymbolService.h @@ -23,8 +23,8 @@ #include #include -#include "oi/Common.h" #include "oi/Descs.h" +#include "oi/TypeHierarchy.h" namespace fs = std::filesystem; diff --git a/oi/TreeBuilder.h b/oi/TreeBuilder.h index c0c989c..ec5063c 100644 --- a/oi/TreeBuilder.h +++ b/oi/TreeBuilder.h @@ -23,8 +23,8 @@ #include #include -#include "oi/Common.h" #include "oi/Features.h" +#include "oi/TypeHierarchy.h" // The rocksdb includes are extremely heavy and bloat compile times, // so we just forward-declare `DB` to avoid making other compile units diff --git a/oi/TypeHierarchy.h b/oi/TypeHierarchy.h new file mode 100644 index 0000000..48ac5e0 --- /dev/null +++ b/oi/TypeHierarchy.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include +#include +#include +#include + +#include "oi/ContainerTypeEnum.h" + +extern "C" { +#include +} + +struct RootInfo { + std::string varName; + struct drgn_qualified_type type; +}; + +struct ClassMember { + std::string typeName; + std::string varName; +}; + +struct DrgnClassMemberInfo { + struct drgn_type* type; + std::string member_name; + uint64_t bit_offset; + uint64_t bit_field_size; + bool isStubbed; +}; + +struct TypeHierarchy { + std::map> classMembersMap; + std::map< + struct drgn_type*, + std::pair>> + containerTypeMap; + std::map typedefMap; + std::map sizeMap; + std::set knownDummyTypeList; + std::map pointerToTypeMap; + std::set thriftIssetStructTypes; + std::map> descendantClasses; +}; diff --git a/oi/type_graph/AddChildren.cpp b/oi/type_graph/AddChildren.cpp index 74867f7..670a519 100644 --- a/oi/type_graph/AddChildren.cpp +++ b/oi/type_graph/AddChildren.cpp @@ -22,6 +22,10 @@ #include "oi/DrgnUtils.h" #include "oi/SymbolService.h" +extern "C" { +#include +} + template using ref = std::reference_wrapper; diff --git a/oi/type_graph/AddChildren.h b/oi/type_graph/AddChildren.h index 783cb22..5d02452 100644 --- a/oi/type_graph/AddChildren.h +++ b/oi/type_graph/AddChildren.h @@ -24,6 +24,7 @@ #include "Visitor.h" class SymbolService; +struct drgn_type; namespace type_graph { diff --git a/oi/type_graph/DrgnParser.cpp b/oi/type_graph/DrgnParser.cpp index c676637..944bf57 100644 --- a/oi/type_graph/DrgnParser.cpp +++ b/oi/type_graph/DrgnParser.cpp @@ -439,6 +439,12 @@ bool DrgnParser::chasePointer() const { return chaseRawPointers_; } +DrgnParserError::DrgnParserError(const std::string& msg, struct drgn_error* err) + : std::runtime_error{msg + ": " + std::to_string(err->code) + " " + + err->message}, + err_(err) { +} + DrgnParserError::~DrgnParserError() { drgn_error_destroy(err_); } diff --git a/oi/type_graph/DrgnParser.h b/oi/type_graph/DrgnParser.h index efde64a..3f9304f 100644 --- a/oi/type_graph/DrgnParser.h +++ b/oi/type_graph/DrgnParser.h @@ -23,6 +23,8 @@ struct drgn_type; struct drgn_type_template_parameter; +struct drgn_error; + struct ContainerInfo; namespace type_graph { @@ -84,11 +86,7 @@ class DrgnParserError : public std::runtime_error { public: DrgnParserError(const std::string& msg) : std::runtime_error{msg} { } - DrgnParserError(const std::string& msg, struct drgn_error* err) - : std::runtime_error{msg + ": " + std::to_string(err->code) + " " + - err->message}, - err_(err) { - } + DrgnParserError(const std::string& msg, struct drgn_error* err); ~DrgnParserError(); diff --git a/tools/OITB.cpp b/tools/OITB.cpp index 36a0438..f7113d9 100644 --- a/tools/OITB.cpp +++ b/tools/OITB.cpp @@ -24,11 +24,11 @@ #include #include "glog/vlog_is_on.h" -#include "oi/Common.h" #include "oi/OIOpts.h" #include "oi/PaddingHunter.h" #include "oi/Serialize.h" #include "oi/TreeBuilder.h" +#include "oi/TypeHierarchy.h" namespace fs = std::filesystem;