diff --git a/.clang-format b/.clang-format index 8492874..af103ec 100644 --- a/.clang-format +++ b/.clang-format @@ -6,3 +6,5 @@ AllowShortCaseLabelsOnASingleLine: false AllowShortFunctionsOnASingleLine: false AllowShortIfStatementsOnASingleLine: false AllowShortLoopsOnASingleLine: false +DerivePointerAlignment: false +PointerAlignment: Left diff --git a/examples/oil-metrics/Metrics.cpp b/examples/oil-metrics/Metrics.cpp index 0503f36..5d7160e 100644 --- a/examples/oil-metrics/Metrics.cpp +++ b/examples/oil-metrics/Metrics.cpp @@ -16,9 +16,9 @@ #include "Metrics.h" namespace Metrics { -std::atomic Metrics::singleton = nullptr; +std::atomic Metrics::singleton = nullptr; -const char *to_string(ArgTiming t) { +const char* to_string(ArgTiming t) { switch (t) { case ENTRY: return "entry"; @@ -29,7 +29,7 @@ const char *to_string(ArgTiming t) { } } -Metrics::Metrics(ObjectIntrospection::options opts, const std::string &savePath) +Metrics::Metrics(ObjectIntrospection::options opts, const std::string& savePath) : opts(opts) { writer = std::fstream(savePath, std::ios_base::out); writer << "{ \"metrics\": [" << std::endl; @@ -43,7 +43,7 @@ Metrics::~Metrics() { } void Metrics::save(std::string object) { - Metrics *m = singleton.load(); + Metrics* m = singleton.load(); std::lock_guard guard(m->writerLock); if (m->hasWritten) { @@ -54,7 +54,7 @@ void Metrics::save(std::string object) { } } -void Metrics::saveArg(const char *name, const char *argName, ArgTiming timing, +void Metrics::saveArg(const char* name, const char* argName, ArgTiming timing, size_t size) { std::string out = "{\"type\": \"size\", \"traceName\": \""; out += name; @@ -69,7 +69,7 @@ void Metrics::saveArg(const char *name, const char *argName, ArgTiming timing, save(out); } -void Metrics::saveDuration(const char *name, +void Metrics::saveDuration(const char* name, std::chrono::milliseconds duration) { std::string out = "{\"type\": \"duration\", \"traceName\": \""; out += name; @@ -80,7 +80,7 @@ void Metrics::saveDuration(const char *name, save(out); } -Tracing::Tracing(const char *name, bool enabled) +Tracing::Tracing(const char* name, bool enabled) : name(name), enabled(enabled) { } @@ -91,7 +91,7 @@ Tracing::~Tracing() { saveDuration(duration); } - for (auto const &exitFunc : exitFuncs) + for (auto const& exitFunc : exitFuncs) exitFunc(); } @@ -99,7 +99,7 @@ bool Tracing::isTimingEnabled() { return enabled || Metrics::isEnabled(); } -bool Tracing::isArgEnabled(const char *argName, ArgTiming timing) { +bool Tracing::isArgEnabled(const char* argName, ArgTiming timing) { return enabled || Metrics::isEnabled(); } @@ -109,7 +109,7 @@ void Tracing::start() { } } -void Tracing::saveArg(const char *argName, ArgTiming timing, size_t size) { +void Tracing::saveArg(const char* argName, ArgTiming timing, size_t size) { Metrics::saveArg(name, argName, timing, size); } diff --git a/examples/oil-metrics/Metrics.h b/examples/oil-metrics/Metrics.h index 0815baa..93be64d 100644 --- a/examples/oil-metrics/Metrics.h +++ b/examples/oil-metrics/Metrics.h @@ -33,7 +33,7 @@ class Metrics { friend class Tracing; public: - Metrics(ObjectIntrospection::options opts, const std::string &savePath); + Metrics(ObjectIntrospection::options opts, const std::string& savePath); ~Metrics(); void enable() { @@ -41,7 +41,7 @@ class Metrics { } private: - static std::atomic singleton; + static std::atomic singleton; ObjectIntrospection::options opts; std::fstream writer; @@ -49,7 +49,7 @@ class Metrics { bool hasWritten = false; bool enableAll = false; - static ObjectIntrospection::options &getOptions() { + static ObjectIntrospection::options& getOptions() { return singleton.load()->opts; } @@ -58,39 +58,39 @@ class Metrics { } static void save(std::string object); - static void saveArg(const char *name, const char *argName, ArgTiming timing, + static void saveArg(const char* name, const char* argName, ArgTiming timing, size_t size); - static void saveDuration(const char *name, + static void saveDuration(const char* name, std::chrono::milliseconds duration); }; class Tracing { public: - Tracing(const char *name, bool enabled = false); + Tracing(const char* name, bool enabled = false); ~Tracing(); void start(); template - void registerArg(const char *argName, T *value); + void registerArg(const char* argName, T* value); private: bool isTimingEnabled(); - bool isArgEnabled(const char *argName, ArgTiming timing); - void saveArg(const char *argName, ArgTiming timing, size_t size); + bool isArgEnabled(const char* argName, ArgTiming timing); + void saveArg(const char* argName, ArgTiming timing, size_t size); void saveDuration(std::chrono::milliseconds duration); template - void inspectArg(const char *argName, ArgTiming timing, T *value); + void inspectArg(const char* argName, ArgTiming timing, T* value); - const char *name; + const char* name; bool enabled; std::chrono::high_resolution_clock::time_point startTime; std::vector> exitFuncs; }; template -void Tracing::registerArg(const char *argName, T *value) { +void Tracing::registerArg(const char* argName, T* value) { if (isArgEnabled(argName, ArgTiming::ENTRY)) { inspectArg(argName, ArgTiming::ENTRY, value); } @@ -108,7 +108,7 @@ void Tracing::registerArg(const char *argName, T *value) { } template -void Tracing::inspectArg(const char *argName, ArgTiming timing, T *value) { +void Tracing::inspectArg(const char* argName, ArgTiming timing, T* value) { size_t size; if (int responseCode = ObjectIntrospection::getObjectSize( value, &size, Metrics::getOptions(), false); diff --git a/include/ObjectIntrospection.h b/include/ObjectIntrospection.h index db429d6..895e257 100644 --- a/include/ObjectIntrospection.h +++ b/include/ObjectIntrospection.h @@ -92,8 +92,8 @@ struct options { bool chaseRawPointers = false; bool generateJitDebugInfo = false; - friend bool operator==(const options &lhs, const options &rhs); - friend bool operator!=(const options &lhs, const options &rhs); + friend bool operator==(const options& lhs, const options& rhs); + friend bool operator!=(const options& lhs, const options& rhs); }; constexpr std::string_view OI_SECTION_PREFIX = ".oi."; @@ -102,29 +102,29 @@ class OILibrary { friend class OILibraryImpl; public: - OILibrary(void *TemplateFunc, options opt); + OILibrary(void* TemplateFunc, options opt); ~OILibrary(); int init(); - int getObjectSize(void *objectAddr, size_t &size); + int getObjectSize(void* objectAddr, size_t& size); options opts; private: - class OILibraryImpl *pimpl_; + class OILibraryImpl* pimpl_; - size_t (*fp)(const void *) = nullptr; + size_t (*fp)(const void*) = nullptr; }; template class CodegenHandler { public: - static int init(const options &opts = {}, bool checkOptions = true) { - OILibrary *lib; + static int init(const options& opts = {}, bool checkOptions = true) { + OILibrary* lib; return getLibrary(lib, opts, checkOptions); } static void teardown() { - OILibrary *lib; + OILibrary* lib; if (int responseCode = getLibrary(lib); responseCode != Response::OIL_SUCCESS) { return; @@ -135,44 +135,44 @@ class CodegenHandler { delete lib; } - static int getObjectSize(const T &objectAddr, size_t &objectSize) { - OILibrary *lib; + static int getObjectSize(const T& objectAddr, size_t& objectSize) { + OILibrary* lib; if (int responseCode = getLibrary(lib); responseCode != Response::OIL_SUCCESS) { return responseCode; } - return lib->getObjectSize((void *)&objectAddr, objectSize); + return lib->getObjectSize((void*)&objectAddr, objectSize); } - static int getObjectSize(const T &objectAddr, size_t &objectSize, - const options &opts, bool checkOptions = true) { - OILibrary *lib; + static int getObjectSize(const T& objectAddr, size_t& objectSize, + const options& opts, bool checkOptions = true) { + OILibrary* lib; if (int responseCode = getLibrary(lib, opts, checkOptions); responseCode != Response::OIL_SUCCESS) { return responseCode; } - return lib->getObjectSize((void *)&objectAddr, objectSize); + return lib->getObjectSize((void*)&objectAddr, objectSize); } private: - static std::atomic *getLib() { - static std::atomic lib = nullptr; + static std::atomic* getLib() { + static std::atomic lib = nullptr; return &lib; } - static std::atomic *> *getBoxedLib() { - static std::atomic *> boxedLib = nullptr; + static std::atomic*>* getBoxedLib() { + static std::atomic*> boxedLib = nullptr; return &boxedLib; } - static int getLibrary(OILibrary *&result) { - std::atomic *curBoxedLib = getBoxedLib()->load(); + static int getLibrary(OILibrary*& result) { + std::atomic* curBoxedLib = getBoxedLib()->load(); if (curBoxedLib == nullptr) return Response::OIL_UNINITIALISED; - OILibrary *curLib = curBoxedLib->load(); + OILibrary* curLib = curBoxedLib->load(); if (curLib == nullptr) return Response::OIL_UNINITIALISED; @@ -180,9 +180,9 @@ class CodegenHandler { return Response::OIL_SUCCESS; } - static int getLibrary(OILibrary *&result, const options &opts, + static int getLibrary(OILibrary*& result, const options& opts, bool checkOptions) { - std::atomic *curBoxedLib = getBoxedLib()->load(); + std::atomic* curBoxedLib = getBoxedLib()->load(); if (curBoxedLib == nullptr) { if (!getBoxedLib()->compare_exchange_strong(curBoxedLib, getLib())) { @@ -190,9 +190,9 @@ class CodegenHandler { } curBoxedLib = getLib(); - int (*sizeFp)(const T &, size_t &) = &getObjectSize; - void *typedFp = reinterpret_cast(sizeFp); - OILibrary *newLib = new OILibrary(typedFp, opts); + int (*sizeFp)(const T&, size_t&) = &getObjectSize; + void* typedFp = reinterpret_cast(sizeFp); + OILibrary* newLib = new OILibrary(typedFp, opts); if (int initCode = newLib->init(); initCode != Response::OIL_SUCCESS) { delete newLib; @@ -203,7 +203,7 @@ class CodegenHandler { getLib()->store(newLib); } - OILibrary *curLib = curBoxedLib->load(); + OILibrary* curLib = curBoxedLib->load(); if (curLib == nullptr) { return Response::OIL_INITIALISING; } @@ -224,7 +224,7 @@ class CodegenHandler { * Ahead-Of-Time (AOT) compilation. */ template -int getObjectSize(const T &objectAddr, size_t &objectSize, const options &opts, +int getObjectSize(const T& objectAddr, size_t& objectSize, const options& opts, bool checkOptions = true) { return CodegenHandler::getObjectSize(objectAddr, objectSize, opts, checkOptions); @@ -234,7 +234,7 @@ int getObjectSize(const T &objectAddr, size_t &objectSize, const options &opts, template int __attribute__((weak)) -getObjectSizeImpl(const T &objectAddr, size_t &objectSize); +getObjectSizeImpl(const T& objectAddr, size_t& objectSize); #endif @@ -249,7 +249,7 @@ getObjectSizeImpl(const T &objectAddr, size_t &objectSize); */ template int __attribute__((noinline)) -getObjectSize(const T &objectAddr, size_t &objectSize) { +getObjectSize(const T& objectAddr, size_t& objectSize) { #ifdef OIL_AOT_COMPILATION if (!getObjectSizeImpl) { return Response::OIL_UNINITIALISED; diff --git a/src/Common.h b/src/Common.h index e16392f..1c8ad68 100644 --- a/src/Common.h +++ b/src/Common.h @@ -38,7 +38,7 @@ struct ClassMember { }; struct DrgnClassMemberInfo { - struct drgn_type *type; + struct drgn_type* type; std::string member_name; uint64_t bit_offset; uint64_t bit_field_size; @@ -46,16 +46,14 @@ struct DrgnClassMemberInfo { }; struct TypeHierarchy { - std::map> - classMembersMap; - std::map> classMembersMap; + std::map>> containerTypeMap; - std::map typedefMap; + std::map typedefMap; std::map sizeMap; - std::set knownDummyTypeList; - std::map pointerToTypeMap; - std::set thriftIssetStructTypes; - std::map> - descendantClasses; + std::set knownDummyTypeList; + std::map pointerToTypeMap; + std::set thriftIssetStructTypes; + std::map> descendantClasses; }; diff --git a/src/ContainerInfo.h b/src/ContainerInfo.h index 7e470b1..33b0d74 100644 --- a/src/ContainerInfo.h +++ b/src/ContainerInfo.h @@ -69,8 +69,8 @@ enum ContainerTypeEnum { LIST_OF_CONTAINER_TYPES #undef X }; -ContainerTypeEnum containerTypeEnumFromStr(std::string &str); -const char *containerTypeEnumToStr(ContainerTypeEnum ty); +ContainerTypeEnum containerTypeEnumFromStr(std::string& str); +const char* containerTypeEnumToStr(ContainerTypeEnum ty); struct ContainerInfo { std::string typeName; @@ -85,9 +85,9 @@ struct ContainerInfo { // adapter std::optional underlyingContainerIndex{}; - static std::unique_ptr loadFromFile(const fs::path &path); + static std::unique_ptr loadFromFile(const fs::path& path); - bool operator<(const ContainerInfo &rhs) const { + bool operator<(const ContainerInfo& rhs) const { return (typeName < rhs.typeName); } }; diff --git a/src/Descs.cpp b/src/Descs.cpp index d824b22..661991e 100644 --- a/src/Descs.cpp +++ b/src/Descs.cpp @@ -28,8 +28,8 @@ extern "C" { #include } -std::ostream &operator<<(std::ostream &os, const FuncDesc::Range &r) { - return os << (void *)r.start << ':' << (void *)r.end; +std::ostream& operator<<(std::ostream& os, const FuncDesc::Range& r) { + return os << (void*)r.start << ':' << (void*)r.end; } /* @@ -38,7 +38,7 @@ std::ostream &operator<<(std::ostream &os, const FuncDesc::Range &r) { * location?). */ std::optional FuncDesc::Arg::findAddress( - struct user_regs_struct *regs, uintptr_t pc) const { + struct user_regs_struct* regs, uintptr_t pc) const { auto prevRip = std::exchange(regs->rip, pc); BOOST_SCOPE_EXIT_ALL(&) { regs->rip = prevRip; @@ -49,7 +49,7 @@ std::optional FuncDesc::Arg::findAddress( drgn_object_deinit(&object); }; - if (auto *err = drgn_object_locate(&locator, regs, &object)) { + if (auto* err = drgn_object_locate(&locator, regs, &object)) { LOG(ERROR) << "Error while finding address of argument: " << err->message; drgn_error_destroy(err); return std::nullopt; @@ -58,7 +58,7 @@ std::optional FuncDesc::Arg::findAddress( return object.address; } -std::optional FuncDesc::getArgumentIndex(const std::string &arg, +std::optional FuncDesc::getArgumentIndex(const std::string& arg, bool validateIndex) const { if (arg == "retval") { return std::nullopt; @@ -79,8 +79,8 @@ std::optional FuncDesc::getArgumentIndex(const std::string &arg, return std::nullopt; } - const auto *argIdxBegin = arg.data() + it; - const auto *argIdxEnd = arg.data() + arg.size(); + const auto* argIdxBegin = arg.data() + it; + const auto* argIdxEnd = arg.data() + arg.size(); uint8_t argIdx = 0; if (auto res = std::from_chars(argIdxBegin, argIdxEnd, argIdx); @@ -105,7 +105,7 @@ std::optional FuncDesc::getArgumentIndex(const std::string &arg, } std::shared_ptr FuncDesc::getArgument( - const std::string &arg) { + const std::string& arg) { std::shared_ptr outArg; if (arg == "retval") { diff --git a/src/Descs.h b/src/Descs.h index 1ea2e77..b41c94c 100644 --- a/src/Descs.h +++ b/src/Descs.h @@ -72,8 +72,8 @@ struct FuncDesc { return arguments[argPos]; } - std::shared_ptr getArgument(const std::string &); - std::optional getArgumentIndex(const std::string &, + std::shared_ptr getArgument(const std::string&); + std::optional getArgumentIndex(const std::string&, bool = true) const; size_t numArgs() const { @@ -84,7 +84,7 @@ struct FuncDesc { } std::optional getRange(uintptr_t addr) { - for (const auto &range : ranges) { + for (const auto& range : ranges) { if (addr >= range.start && addr < range.end) { return range; } @@ -103,7 +103,7 @@ struct FuncDesc { * can be found at the given pc (what about if we don't have this * location?). */ - virtual std::optional findAddress(struct user_regs_struct *regs, + virtual std::optional findAddress(struct user_regs_struct* regs, uintptr_t pc) const = 0; }; @@ -114,21 +114,21 @@ struct FuncDesc { drgn_object_locator_deinit(&locator); } - std::optional findAddress(struct user_regs_struct *regs, + std::optional findAddress(struct user_regs_struct* regs, uintptr_t pc) const final; }; struct Retval final : virtual TargetObject { ~Retval() final = default; - std::optional findAddress(struct user_regs_struct *regs, + std::optional findAddress(struct user_regs_struct* regs, uintptr_t /* pc */) const final { return regs->rax; } }; }; -std::ostream &operator<<(std::ostream &os, const FuncDesc::Range &r); +std::ostream& operator<<(std::ostream& os, const FuncDesc::Range& r); class GlobalDesc { public: diff --git a/src/OICache.cpp b/src/OICache.cpp index 888fe15..6d3434a 100644 --- a/src/OICache.cpp +++ b/src/OICache.cpp @@ -31,25 +31,25 @@ #endif static std::optional> getEntName( - SymbolService &symbols, const irequest &req, OICache::Entity ent) { + SymbolService& symbols, const irequest& req, OICache::Entity ent) { if (ent == OICache::Entity::FuncDescs || ent == OICache::Entity::GlobalDescs) { return req.func; } else { if (req.type == "global") { - const auto &globalDesc = symbols.findGlobalDesc(req.func); + const auto& globalDesc = symbols.findGlobalDesc(req.func); if (!globalDesc) { return std::nullopt; } return globalDesc->typeName; } else { - const auto &funcDesc = symbols.findFuncDesc(req); + const auto& funcDesc = symbols.findFuncDesc(req); if (!funcDesc) { return std::nullopt; } - const auto &arg = funcDesc->getArgument(req.arg); + const auto& arg = funcDesc->getArgument(req.arg); if (!arg) { return std::nullopt; } @@ -59,15 +59,15 @@ static std::optional> getEntName( } } -std::optional OICache::getPath(const irequest &req, +std::optional OICache::getPath(const irequest& req, Entity ent) const { - auto hash = [](const std::string &str) { + auto hash = [](const std::string& str) { return std::to_string(std::hash{}(str)); }; auto ext = extensions[static_cast(ent)]; - const auto &entName = getEntName(*symbols, req, ent); + const auto& entName = getEntName(*symbols, req, ent); if (!entName.has_value()) { return std::nullopt; } @@ -76,7 +76,7 @@ std::optional OICache::getPath(const irequest &req, } template -bool OICache::load(const irequest &req, Entity ent, T &data) { +bool OICache::load(const irequest& req, Entity ent, T& data) { if (!isEnabled()) return false; try { @@ -109,14 +109,14 @@ bool OICache::load(const irequest &req, Entity ent, T &data) { ia >> data; return true; - } catch (const std::exception &e) { + } catch (const std::exception& e) { LOG(WARNING) << "Failed to load from cache: " << e.what(); return false; } } template -bool OICache::store(const irequest &req, Entity ent, const T &data) { +bool OICache::store(const irequest& req, Entity ent, const T& data) { if (!isEnabled()) return false; try { @@ -141,15 +141,15 @@ bool OICache::store(const irequest &req, Entity ent, const T &data) { oa << *buildID; oa << data; return true; - } catch (const std::exception &e) { + } catch (const std::exception& e) { LOG(WARNING) << "Failed to write to cache: " << e.what(); return false; } } -#define INSTANTIATE_ARCHIVE(...) \ - template bool OICache::load(const irequest &, Entity, __VA_ARGS__ &); \ - template bool OICache::store(const irequest &, Entity, const __VA_ARGS__ &); +#define INSTANTIATE_ARCHIVE(...) \ + template bool OICache::load(const irequest&, Entity, __VA_ARGS__&); \ + template bool OICache::store(const irequest&, Entity, const __VA_ARGS__&); INSTANTIATE_ARCHIVE(std::pair) INSTANTIATE_ARCHIVE(std::unordered_map>) @@ -160,7 +160,7 @@ INSTANTIATE_ARCHIVE(std::map) #undef INSTANTIATE_ARCHIVE // Upload all contents of cache for this request -bool OICache::upload([[maybe_unused]] const irequest &req) { +bool OICache::upload([[maybe_unused]] const irequest& req) { #ifndef OSS_ENABLE if (!isEnabled() || downloadedRemote || !enableUpload) return true; @@ -194,7 +194,7 @@ bool OICache::upload([[maybe_unused]] const irequest &req) { } // Try to fetch contents of cache -bool OICache::download([[maybe_unused]] const irequest &req) { +bool OICache::download([[maybe_unused]] const irequest& req) { #ifndef OSS_ENABLE if (!isEnabled() || !enableDownload) return true; @@ -233,7 +233,7 @@ bool OICache::download([[maybe_unused]] const irequest &req) { #endif } -std::string OICache::generateRemoteHash(const irequest &req) { +std::string OICache::generateRemoteHash(const irequest& req) { auto buildID = symbols->locateBuildID(); if (!buildID) { LOG(ERROR) << "Failed to locate buildID"; diff --git a/src/OICache.h b/src/OICache.h index 2fc50a3..deacce4 100644 --- a/src/OICache.h +++ b/src/OICache.h @@ -50,21 +50,21 @@ class OICache { PaddingInfo, MAX }; - static constexpr std::array(Entity::MAX)> + static constexpr std::array(Entity::MAX)> extensions{".cc", ".o", ".fd", ".gd", ".th", ".pd"}; bool isEnabled() const { return !basePath.empty(); } - std::optional getPath(const irequest &, Entity) const; + std::optional getPath(const irequest&, Entity) const; template - bool store(const irequest &, Entity, const T &); + bool store(const irequest&, Entity, const T&); template - bool load(const irequest &, Entity, T &); + bool load(const irequest&, Entity, T&); - bool upload(const irequest &req); - bool download(const irequest &req); + bool upload(const irequest& req); + bool download(const irequest& req); private: - std::string generateRemoteHash(const irequest &); + std::string generateRemoteHash(const irequest&); }; diff --git a/src/OICodeGen.cpp b/src/OICodeGen.cpp index 80bf66c..ea330d4 100644 --- a/src/OICodeGen.cpp +++ b/src/OICodeGen.cpp @@ -45,11 +45,11 @@ static size_t g_level = 0; #define VLOG(verboselevel) \ LOG_IF(INFO, VLOG_IS_ON(verboselevel)) << std::string(2 * g_level, ' ') -std::unique_ptr OICodeGen::buildFromConfig(const Config &c, - SymbolService &s) { +std::unique_ptr OICodeGen::buildFromConfig(const Config& c, + SymbolService& s) { auto cg = std::unique_ptr(new OICodeGen(c, s)); - for (const auto &path : c.containerConfigPaths) { + for (const auto& path : c.containerConfigPaths) { if (!cg->registerContainer(path)) { LOG(ERROR) << "failed to register container: " << path; return nullptr; @@ -59,7 +59,7 @@ std::unique_ptr OICodeGen::buildFromConfig(const Config &c, return cg; } -OICodeGen::OICodeGen(const Config &c, SymbolService &s) +OICodeGen::OICodeGen(const Config& c, SymbolService& s) : config{c}, symbols{s} { // TODO: Should folly::Range just be added as a container? auto typesToStub = std::array{ @@ -89,7 +89,7 @@ OICodeGen::OICodeGen(const Config &c, SymbolService &s) }; config.membersToStub.reserve(typesToStub.size()); - for (const auto &type : typesToStub) { + for (const auto& type : typesToStub) { config.membersToStub.emplace_back(type, "*"); } @@ -103,7 +103,7 @@ OICodeGen::OICodeGen(const Config &c, SymbolService &s) sizeMap["SharedMutex"] = sizeof(folly::SharedMutex); } -bool OICodeGen::registerContainer(const fs::path &path) { +bool OICodeGen::registerContainer(const fs::path& path) { VLOG(1) << "registering container, path: " << path; auto info = ContainerInfo::loadFromFile(path); if (!info || !funcGen.RegisterContainer(info->ctype, path)) { @@ -115,12 +115,12 @@ bool OICodeGen::registerContainer(const fs::path &path) { return true; } -bool OICodeGen::isKnownType(const std::string &type) { +bool OICodeGen::isKnownType(const std::string& type) { std::string matched; return isKnownType(type, matched); } -bool OICodeGen::isKnownType(const std::string &type, std::string &matched) { +bool OICodeGen::isKnownType(const std::string& type, std::string& matched) { if (auto it = std::ranges::find(knownTypes, type); it != knownTypes.end()) { matched = *it; return true; @@ -141,15 +141,15 @@ bool OICodeGen::isKnownType(const std::string &type, std::string &matched) { } std::optional OICodeGen::fullyQualifiedName( - drgn_type *type) { + drgn_type* type) { if (auto entry = fullyQualifiedNames.find(type); entry != fullyQualifiedNames.end()) { return entry->second.contents; } - char *name = nullptr; + char* name = nullptr; size_t length = 0; - auto *err = drgn_type_fully_qualified_name(type, &name, &length); + auto* err = drgn_type_fully_qualified_name(type, &name, &length); if (err != nullptr || name == nullptr) { return std::nullopt; } @@ -159,7 +159,7 @@ std::optional OICodeGen::fullyQualifiedName( return typeNamePair->second.contents; } -std::optional OICodeGen::getContainerInfo(drgn_type *type) { +std::optional OICodeGen::getContainerInfo(drgn_type* type) { auto name = fullyQualifiedName(type); if (!name.has_value()) { return std::nullopt; @@ -168,7 +168,7 @@ std::optional OICodeGen::getContainerInfo(drgn_type *type) { std::string nameStr = std::string(*name); for (auto it = containerInfoList.rbegin(); it != containerInfoList.rend(); ++it) { - const auto &info = *it; + const auto& info = *it; if (std::regex_search(nameStr, info->matcher)) { return *info; } @@ -176,11 +176,11 @@ std::optional OICodeGen::getContainerInfo(drgn_type *type) { return std::nullopt; } -bool OICodeGen::isContainer(drgn_type *type) { +bool OICodeGen::isContainer(drgn_type* type) { return getContainerInfo(type).has_value(); } -std::string OICodeGen::preProcessUniquePtr(drgn_type *type, std::string name) { +std::string OICodeGen::preProcessUniquePtr(drgn_type* type, std::string name) { std::string typeName; std::string deleterName; @@ -218,9 +218,9 @@ std::string OICodeGen::preProcessUniquePtr(drgn_type *type, std::string name) { constexpr size_t defaultDeleterSize = sizeof(std::unique_ptr); constexpr size_t cFunctionDeleterSize = - sizeof(std::unique_ptr); + sizeof(std::unique_ptr); constexpr size_t stdFunctionDeleterSize = - sizeof(std::unique_ptr>); + sizeof(std::unique_ptr>); if (typeSize == defaultDeleterSize) { name.replace(begin, end - begin + 1, "default_delete<" + typeName + ">"); @@ -238,14 +238,14 @@ std::string OICodeGen::preProcessUniquePtr(drgn_type *type, std::string name) { } void OICodeGen::prependQualifiers(enum drgn_qualifiers qualifiers, - std::string &sb) { + std::string& sb) { // const qualifier is the only one we're interested in if ((qualifiers & DRGN_QUALIFIER_CONST) != 0) { sb += "const "; } } -void OICodeGen::removeTemplateParamAtIndex(std::vector ¶ms, +void OICodeGen::removeTemplateParamAtIndex(std::vector& params, const size_t index) { if (index < params.size()) { params.erase(params.begin() + index); @@ -253,14 +253,14 @@ void OICodeGen::removeTemplateParamAtIndex(std::vector ¶ms, } std::string OICodeGen::stripFullyQualifiedName( - const std::string &fullyQualifiedName) { + const std::string& fullyQualifiedName) { std::vector lines; boost::iter_split(lines, fullyQualifiedName, boost::first_finder("::")); return lines[lines.size() - 1]; } std::string OICodeGen::stripFullyQualifiedNameWithSeparators( - const std::string &fullyQualifiedName) { + const std::string& fullyQualifiedName) { std::vector stack; std::string sep = " ,<>()"; std::string tmp; @@ -273,7 +273,7 @@ std::string OICodeGen::stripFullyQualifiedNameWithSeparators( return "conditional_t_" + std::to_string(cond_t_val++); } - for (auto &c : fullyQualifiedName) { + for (auto& c : fullyQualifiedName) { if (sep.find(c) == std::string::npos) { stack.push_back(std::string(1, c)); } else { @@ -311,7 +311,7 @@ std::string OICodeGen::stripFullyQualifiedNameWithSeparators( } tmp = ""; - for (auto &e : stack) { + for (auto& e : stack) { tmp += e; } @@ -320,15 +320,15 @@ std::string OICodeGen::stripFullyQualifiedNameWithSeparators( // Replace a specific template parameter with a generic DummySizedOperator void OICodeGen::replaceTemplateOperator( - TemplateParamList &template_params, - std::vector &template_params_strings, size_t index) { + TemplateParamList& template_params, + std::vector& template_params_strings, size_t index) { if (index >= template_params.size()) { // Should this happen? return; } size_t comparatorSz = 1; - drgn_type *cmpType = template_params[index].first.type; + drgn_type* cmpType = template_params[index].first.type; if (isDrgnSizeComplete(cmpType)) { comparatorSz = drgn_type_size(cmpType); } else { @@ -356,9 +356,9 @@ void OICodeGen::replaceTemplateOperator( } void OICodeGen::replaceTemplateParameters( - drgn_type *type, TemplateParamList &template_params, - std::vector &template_params_strings, - const std::string &nameWithoutTemplate) { + drgn_type* type, TemplateParamList& template_params, + std::vector& template_params_strings, + const std::string& nameWithoutTemplate) { auto containerInfo = getContainerInfo(type); if (!containerInfo.has_value()) { LOG(ERROR) << "Unknown container type: " << nameWithoutTemplate; @@ -373,7 +373,7 @@ void OICodeGen::replaceTemplateParameters( removeTemplateParamAtIndex(template_params_strings, 3); removeTemplateParamAtIndex(template_params_strings, 2); } else { - for (auto const &index : containerInfo->replaceTemplateParamIndex) { + for (auto const& index : containerInfo->replaceTemplateParamIndex) { replaceTemplateOperator(template_params, template_params_strings, index); } if (containerInfo->allocatorIndex) { @@ -383,10 +383,10 @@ void OICodeGen::replaceTemplateParameters( } } -bool OICodeGen::buildName(drgn_type *type, std::string &text, - std::string &outName) { +bool OICodeGen::buildName(drgn_type* type, std::string& text, + std::string& outName) { int ptrDepth = 0; - drgn_type *ut = type; + drgn_type* ut = type; while (drgn_type_kind(ut) == DRGN_TYPE_POINTER) { ut = drgn_type_type(ut).type; ptrDepth++; @@ -408,8 +408,8 @@ bool OICodeGen::buildName(drgn_type *type, std::string &text, return true; } -bool OICodeGen::buildNameInt(drgn_type *type, std::string &nameWithoutTemplate, - std::string &outName) { +bool OICodeGen::buildNameInt(drgn_type* type, std::string& nameWithoutTemplate, + std::string& outName) { // Calling buildName only makes sense if a type is a container and has // template parameters. For a generic template class, we just flatten the // name into anything reasonable (e.g. Foo becomes Foo_int_). @@ -434,7 +434,7 @@ bool OICodeGen::buildNameInt(drgn_type *type, std::string &nameWithoutTemplate, std::vector templateParamsStrings; for (size_t i = 0; i < templateParams.size(); ++i) { - auto &[p, value] = templateParams[i]; + auto& [p, value] = templateParams[i]; enum drgn_qualifiers qualifiers = p.qualifiers; prependQualifiers(qualifiers, outName); @@ -470,7 +470,7 @@ bool OICodeGen::buildNameInt(drgn_type *type, std::string &nameWithoutTemplate, // matter for now. auto enumVal = std::stoull(value); - drgn_type_enumerator *enumerators = drgn_type_enumerators(p.type); + drgn_type_enumerator* enumerators = drgn_type_enumerators(p.type); templateParamName = *fullyQualifiedName(p.type); templateParamName += "::"; templateParamName += enumerators[enumVal].name; @@ -483,21 +483,21 @@ bool OICodeGen::buildNameInt(drgn_type *type, std::string &nameWithoutTemplate, // Use the type's allocated name if it exists templateParamName = it->second; } else if (drgn_type_has_tag(p.type)) { - const char *templateParamNameChr = drgn_type_tag(p.type); + const char* templateParamNameChr = drgn_type_tag(p.type); if (templateParamNameChr != nullptr) { templateParamName = std::string(templateParamNameChr); } else { return false; } } else if (drgn_type_has_name(p.type)) { - const char *templateParamNameChr = drgn_type_name(p.type); + const char* templateParamNameChr = drgn_type_name(p.type); if (templateParamNameChr != nullptr) { templateParamName = std::string(templateParamNameChr); } else { return false; } } else if (drgn_type_kind(p.type) == DRGN_TYPE_FUNCTION) { - char *defStr = nullptr; + char* defStr = nullptr; if (drgn_format_type_name(p, &defStr)) { LOG(ERROR) << "Failed to get formatted string for " << p.type; templateParamName = std::string(""); @@ -506,7 +506,7 @@ bool OICodeGen::buildNameInt(drgn_type *type, std::string &nameWithoutTemplate, free(defStr); } } else if (drgn_type_kind(p.type) == DRGN_TYPE_POINTER) { - char *defStr = nullptr; + char* defStr = nullptr; if (drgn_format_type_name(p, &defStr)) { LOG(ERROR) << "Failed to get formatted string for " << p.type; @@ -532,7 +532,7 @@ bool OICodeGen::buildNameInt(drgn_type *type, std::string &nameWithoutTemplate, templateParamName = std::string("void"); } else if (drgn_type_kind(p.type) == DRGN_TYPE_ARRAY) { size_t elems = 1; - drgn_type *arrayElementType = nullptr; + drgn_type* arrayElementType = nullptr; drgn_utils::getDrgnArrayElementType(p.type, &arrayElementType, elems); if (drgn_type_has_name(arrayElementType)) { @@ -563,7 +563,7 @@ bool OICodeGen::buildNameInt(drgn_type *type, std::string &nameWithoutTemplate, outName = nameWithoutTemplate; for (size_t i = 0; i < templateParamsStrings.size(); ++i) { - auto &[p, value] = templateParams[i]; + auto& [p, value] = templateParams[i]; enum drgn_qualifiers qualifiers = p.qualifiers; prependQualifiers(qualifiers, outName); @@ -578,13 +578,13 @@ bool OICodeGen::buildNameInt(drgn_type *type, std::string &nameWithoutTemplate, } bool OICodeGen::getTemplateParams( - drgn_type *type, size_t numTemplateParams, - std::vector> &v) { - drgn_type_template_parameter *tParams = drgn_type_template_parameters(type); + drgn_type* type, size_t numTemplateParams, + std::vector>& v) { + drgn_type_template_parameter* tParams = drgn_type_template_parameters(type); for (size_t i = 0; i < numTemplateParams; ++i) { - const drgn_object *obj = nullptr; - if (auto *err = drgn_template_parameter_object(&tParams[i], &obj); + const drgn_object* obj = nullptr; + if (auto* err = drgn_template_parameter_object(&tParams[i], &obj); err != nullptr) { LOG(ERROR) << "Error when looking up template parameter " << err->code << " " << err->message; @@ -596,7 +596,7 @@ bool OICodeGen::getTemplateParams( drgn_qualified_type t{}; if (obj == nullptr) { - if (auto *err = drgn_template_parameter_type(&tParams[i], &t); + if (auto* err = drgn_template_parameter_type(&tParams[i], &t); err != nullptr) { LOG(ERROR) << "Error when looking up template parameter " << err->code << " " << err->message; @@ -608,9 +608,9 @@ bool OICodeGen::getTemplateParams( t.qualifiers = obj->qualifiers; if (obj->encoding == DRGN_OBJECT_ENCODING_BUFFER) { uint64_t size = drgn_object_size(obj); - char *buf = nullptr; + char* buf = nullptr; if (size <= sizeof(obj->value.ibuf)) { - buf = (char *)&(obj->value.ibuf); + buf = (char*)&(obj->value.ibuf); } else { buf = obj->value.bufp; } @@ -635,7 +635,7 @@ bool OICodeGen::getTemplateParams( return true; } -std::string OICodeGen::transformTypeName(drgn_type *type, std::string &text) { +std::string OICodeGen::transformTypeName(drgn_type* type, std::string& text) { std::string tmp = stripFullyQualifiedNameWithSeparators(text); boost::regex re{""}; @@ -655,7 +655,7 @@ std::string OICodeGen::transformTypeName(drgn_type *type, std::string &text) { return buildNameOutput; } -bool OICodeGen::getContainerTemplateParams(drgn_type *type, bool &ifStub) { +bool OICodeGen::getContainerTemplateParams(drgn_type* type, bool& ifStub) { if (containerTypeMapDrgn.find(type) != containerTypeMapDrgn.end()) { return true; } @@ -706,10 +706,10 @@ bool OICodeGen::getContainerTemplateParams(drgn_type *type, bool &ifStub) { return enumerateTemplateParamIdxs(type, *containerInfo, paramIdxs, ifStub); } -bool OICodeGen::enumerateTemplateParamIdxs(drgn_type *type, - const ContainerInfo &containerInfo, - const std::vector ¶mIdxs, - bool &ifStub) { +bool OICodeGen::enumerateTemplateParamIdxs(drgn_type* type, + const ContainerInfo& containerInfo, + const std::vector& paramIdxs, + bool& ifStub) { if (paramIdxs.empty()) { return true; } @@ -721,10 +721,10 @@ bool OICodeGen::enumerateTemplateParamIdxs(drgn_type *type, return false; } - drgn_type_template_parameter *tParams = drgn_type_template_parameters(type); + drgn_type_template_parameter* tParams = drgn_type_template_parameters(type); for (auto i : paramIdxs) { drgn_qualified_type t{}; - if (auto *err = drgn_template_parameter_type(&tParams[i], &t); + if (auto* err = drgn_template_parameter_type(&tParams[i], &t); err != nullptr) { LOG(ERROR) << "Error when looking up template parameter " << err->code << " " << err->message; @@ -740,7 +740,7 @@ bool OICodeGen::enumerateTemplateParamIdxs(drgn_type *type, for (auto i : paramIdxs) { drgn_qualified_type t{}; - if (auto *err = drgn_template_parameter_type(&tParams[i], &t); + if (auto* err = drgn_template_parameter_type(&tParams[i], &t); err != nullptr) { LOG(ERROR) << "Error when looking up template parameter " << err->code << " " << err->message; @@ -773,7 +773,7 @@ bool OICodeGen::enumerateTemplateParamIdxs(drgn_type *type, } } - auto &templateTypes = + auto& templateTypes = containerTypeMapDrgn .emplace(type, std::pair(containerInfo, std::vector())) @@ -781,7 +781,7 @@ bool OICodeGen::enumerateTemplateParamIdxs(drgn_type *type, for (auto i : paramIdxs) { drgn_qualified_type t{}; - drgn_error *err = drgn_template_parameter_type(&tParams[i], &t); + drgn_error* err = drgn_template_parameter_type(&tParams[i], &t); if (err) { LOG(ERROR) << "Error when looking up template parameter " << err->code << " " << err->message; @@ -796,13 +796,13 @@ bool OICodeGen::enumerateTemplateParamIdxs(drgn_type *type, return true; } -void OICodeGen::addPaddingForBaseClass(drgn_type *type, - std::vector &def) { +void OICodeGen::addPaddingForBaseClass(drgn_type* type, + std::vector& def) { if (drgn_type_num_members(type) < 1) { return; } - drgn_type_member *members = drgn_type_members(type); + drgn_type_member* members = drgn_type_members(type); VLOG(2) << "Base member offset is " << members[0].bit_offset / CHAR_BIT; @@ -819,7 +819,7 @@ void OICodeGen::addPaddingForBaseClass(drgn_type *type, } } -std::string_view OICodeGen::drgnKindStr(drgn_type *type) { +std::string_view OICodeGen::drgnKindStr(drgn_type* type) { switch (drgn_type_kind(type)) { case DRGN_TYPE_VOID: return "DRGN_TYPE_VOID"; @@ -849,7 +849,7 @@ std::string_view OICodeGen::drgnKindStr(drgn_type *type) { return ""; } -std::string OICodeGen::getAnonName(drgn_type *type, const char *template_) { +std::string OICodeGen::getAnonName(drgn_type* type, const char* template_) { std::string typeName; if (drgn_type_tag(type) != nullptr) { typeName = drgn_type_tag(type); @@ -862,7 +862,7 @@ std::string OICodeGen::getAnonName(drgn_type *type, const char *template_) { unnamedUnion.emplace(type, typeName); // Leak a copy of the typeName to ensure its lifetime is greater than the // drgn_type - char *typeNameCstr = new char[typeName.size() + 1]; + char* typeNameCstr = new char[typeName.size() + 1]; std::copy(std::begin(typeName), std::end(typeName), typeNameCstr); typeNameCstr[typeName.size()] = '\0'; type->_private.tag = typeNameCstr; @@ -873,7 +873,7 @@ std::string OICodeGen::getAnonName(drgn_type *type, const char *template_) { return transformTypeName(type, typeName); } -bool OICodeGen::getMemberDefinition(drgn_type *type) { +bool OICodeGen::getMemberDefinition(drgn_type* type) { // Do a [] lookup to ensure `type` has a entry in classMembersMap // If it has no entry, the lookup will default construct on for us classMembersMap[type]; @@ -889,15 +889,15 @@ bool OICodeGen::getMemberDefinition(drgn_type *type) { return true; } - drgn_type_member *members = drgn_type_members(type); + drgn_type_member* members = drgn_type_members(type); for (size_t i = 0; i < drgn_type_num_members(type); ++i) { - auto &member = members[i]; + auto& member = members[i]; auto memberName = member.name ? std::string(member.name) : "__anon_member_" + std::to_string(i); drgn_qualified_type t{}; uint64_t bitFieldSize = 0; - if (auto *err = drgn_member_type(&member, &t, &bitFieldSize); + if (auto* err = drgn_member_type(&member, &t, &bitFieldSize); err != nullptr) { LOG(ERROR) << "Error when looking up member type '" << memberName << "': (" << err->code << ") " << err->message; @@ -927,19 +927,19 @@ bool OICodeGen::getMemberDefinition(drgn_type *type) { return true; } -std::string OICodeGen::typeToTransformedName(drgn_type *type) { +std::string OICodeGen::typeToTransformedName(drgn_type* type) { auto typeName = drgn_utils::typeToName(type); typeName = transformTypeName(type, typeName); return typeName; } -bool OICodeGen::recordChildren(drgn_type *type) { - drgn_type_template_parameter *parents = drgn_type_parents(type); +bool OICodeGen::recordChildren(drgn_type* type) { + drgn_type_template_parameter* parents = drgn_type_parents(type); for (size_t i = 0; i < drgn_type_num_parents(type); i++) { drgn_qualified_type t{}; - if (auto *err = drgn_template_parameter_type(&parents[i], &t); + if (auto* err = drgn_template_parameter_type(&parents[i], &t); err != nullptr) { LOG(ERROR) << "Error when looking up parent class for type " << type << " err " << err->code << " " << err->message; @@ -947,14 +947,14 @@ bool OICodeGen::recordChildren(drgn_type *type) { continue; } - drgn_type *parent = drgnUnderlyingType(t.type); + drgn_type* parent = drgnUnderlyingType(t.type); if (!isDrgnSizeComplete(parent)) { VLOG(1) << "Incomplete size for parent class (" << drgn_type_tag(parent) << ") of " << drgn_type_tag(type); continue; } - const char *parentName = drgn_type_tag(parent); + const char* parentName = drgn_type_tag(parent); if (parentName == nullptr) { VLOG(1) << "No name for parent class (" << parent << ") of " << drgn_type_tag(type); @@ -991,9 +991,9 @@ bool OICodeGen::enumerateChildClasses() { return false; } - drgn_type_iterator *typesIterator; - auto *prog = symbols.getDrgnProgram(); - drgn_error *err = drgn_type_iterator_create(prog, &typesIterator); + drgn_type_iterator* typesIterator; + auto* prog = symbols.getDrgnProgram(); + drgn_error* err = drgn_type_iterator_create(prog, &typesIterator); if (err) { LOG(ERROR) << "Error initialising drgn_type_iterator: " << err->code << ", " << err->message; @@ -1005,7 +1005,7 @@ bool OICodeGen::enumerateChildClasses() { int j = 0; while (true) { i++; - drgn_qualified_type *t; + drgn_qualified_type* t; err = drgn_type_iterator_next(typesIterator, &t); if (err) { LOG(ERROR) << "Error from drgn_type_iterator_next: " << err->code << ", " @@ -1050,7 +1050,7 @@ bool OICodeGen::populateDefsAndDecls() { rootType = drgn_type_type(rootType.type); } - auto *type = rootType.type; + auto* type = rootType.type; rootTypeToIntrospect = rootType; drgn_qualified_type qtype{}; @@ -1075,9 +1075,9 @@ bool OICodeGen::populateDefsAndDecls() { return enumerateTypesRecurse(rootType.type); } -std::optional OICodeGen::getDrgnTypeSize(drgn_type *type) { +std::optional OICodeGen::getDrgnTypeSize(drgn_type* type) { uint64_t sz = 0; - if (auto *err = drgn_type_sizeof(type, &sz); err != nullptr) { + if (auto* err = drgn_type_sizeof(type, &sz); err != nullptr) { LOG(ERROR) << "dgn_type_sizeof(" << type << "): " << err->code << " " << err->message; drgn_error_destroy(err); @@ -1087,7 +1087,7 @@ std::optional OICodeGen::getDrgnTypeSize(drgn_type *type) { return std::nullopt; } - for (auto &e : sizeMap) { + for (auto& e : sizeMap) { if (typeName->starts_with(e.first)) { VLOG(1) << "Looked up " << *typeName << " in sizeMap"; return e.second; @@ -1100,9 +1100,9 @@ std::optional OICodeGen::getDrgnTypeSize(drgn_type *type) { return sz; } -bool OICodeGen::isDrgnSizeComplete(drgn_type *type) { +bool OICodeGen::isDrgnSizeComplete(drgn_type* type) { uint64_t sz = 0; - if (auto *err = drgn_type_sizeof(type, &sz); err != nullptr) { + if (auto* err = drgn_type_sizeof(type, &sz); err != nullptr) { drgn_error_destroy(err); } else { return true; @@ -1112,7 +1112,7 @@ bool OICodeGen::isDrgnSizeComplete(drgn_type *type) { std::string name; getDrgnTypeNameInt(type, name); - for (auto &kv : sizeMap) { + for (auto& kv : sizeMap) { if (name.starts_with(kv.first)) { return true; } @@ -1122,8 +1122,8 @@ bool OICodeGen::isDrgnSizeComplete(drgn_type *type) { return false; } -drgn_type *OICodeGen::drgnUnderlyingType(drgn_type *type) { - auto *underlyingType = type; +drgn_type* OICodeGen::drgnUnderlyingType(drgn_type* type) { + auto* underlyingType = type; while (drgn_type_kind(underlyingType) == DRGN_TYPE_TYPEDEF) { underlyingType = drgn_type_type(underlyingType).type; @@ -1132,20 +1132,20 @@ drgn_type *OICodeGen::drgnUnderlyingType(drgn_type *type) { return underlyingType; } -bool OICodeGen::enumerateClassParents(drgn_type *type, - const std::string &typeName) { +bool OICodeGen::enumerateClassParents(drgn_type* type, + const std::string& typeName) { if (drgn_type_num_parents(type) == 0) { // Be careful to early exit here to avoid accidentally initialising // parentClasses when there are no parents. return true; } - drgn_type_template_parameter *parents = drgn_type_parents(type); + drgn_type_template_parameter* parents = drgn_type_parents(type); for (size_t i = 0; i < drgn_type_num_parents(type); ++i) { drgn_qualified_type t{}; - if (auto *err = drgn_template_parameter_type(&parents[i], &t); + if (auto* err = drgn_template_parameter_type(&parents[i], &t); err != nullptr) { LOG(ERROR) << "Error when looking up parent class for type " << type << " err " << err->code << " " << err->message; @@ -1173,14 +1173,14 @@ bool OICodeGen::enumerateClassParents(drgn_type *type, return true; } -bool OICodeGen::enumerateClassMembers(drgn_type *type, - const std::string &typeName, - bool &isStubbed) { - drgn_type_member *members = drgn_type_members(type); +bool OICodeGen::enumerateClassMembers(drgn_type* type, + const std::string& typeName, + bool& isStubbed) { + drgn_type_member* members = drgn_type_members(type); for (size_t i = 0; i < drgn_type_num_members(type); ++i) { drgn_qualified_type t{}; - auto *err = drgn_member_type(&members[i], &t, nullptr); + auto* err = drgn_member_type(&members[i], &t, nullptr); if (err != nullptr || !isDrgnSizeComplete(t.type)) { if (err != nullptr) { @@ -1214,9 +1214,9 @@ bool OICodeGen::enumerateClassMembers(drgn_type *type, return true; } -bool OICodeGen::enumerateClassTemplateParams(drgn_type *type, - const std::string &typeName, - bool &isStubbed) { +bool OICodeGen::enumerateClassTemplateParams(drgn_type* type, + const std::string& typeName, + bool& isStubbed) { bool ifStub = false; if (!getContainerTemplateParams(type, ifStub)) { LOG(ERROR) << "Failed to get container template params"; @@ -1237,12 +1237,12 @@ bool OICodeGen::enumerateClassTemplateParams(drgn_type *type, return true; } -bool OICodeGen::ifGenerateMemberDefinition(const std::string &typeName) { +bool OICodeGen::ifGenerateMemberDefinition(const std::string& typeName) { return !isKnownType(typeName); } -bool OICodeGen::generateMemberDefinition(drgn_type *type, - std::string &typeName) { +bool OICodeGen::generateMemberDefinition(drgn_type* type, + std::string& typeName) { if (!getMemberDefinition(type)) { return false; } @@ -1257,7 +1257,7 @@ bool OICodeGen::generateMemberDefinition(drgn_type *type, } uint64_t sz = 0; - if (auto *err = drgn_type_sizeof(type, &sz); err != nullptr) { + if (auto* err = drgn_type_sizeof(type, &sz); err != nullptr) { LOG(ERROR) << "Failed to get size: " << err->code << " " << err->message; drgn_error_destroy(err); return false; @@ -1267,9 +1267,9 @@ bool OICodeGen::generateMemberDefinition(drgn_type *type, } std::optional> -OICodeGen::isMemberToStub(const std::string &typeName, - const std::string &member) { - auto it = std::ranges::find_if(config.membersToStub, [&](auto &s) { +OICodeGen::isMemberToStub(const std::string& typeName, + const std::string& member) { + auto it = std::ranges::find_if(config.membersToStub, [&](auto& s) { return typeName.starts_with(s.first) && s.second == member; }); if (it == std::end(config.membersToStub)) { @@ -1279,14 +1279,14 @@ OICodeGen::isMemberToStub(const std::string &typeName, } std::optional OICodeGen::isTypeToStub( - const std::string &typeName) { + const std::string& typeName) { if (auto opt = isMemberToStub(typeName, "*")) { return std::ref(opt.value().first); } return std::nullopt; } -bool OICodeGen::isTypeToStub(drgn_type *type, const std::string &typeName) { +bool OICodeGen::isTypeToStub(drgn_type* type, const std::string& typeName) { if (isTypeToStub(typeName)) { VLOG(1) << "Found type to stub "; knownDummyTypeList.insert(type); @@ -1296,8 +1296,8 @@ bool OICodeGen::isTypeToStub(drgn_type *type, const std::string &typeName) { return false; } -bool OICodeGen::isEmptyClassOrFunctionType(drgn_type *type, - const std::string &typeName) { +bool OICodeGen::isEmptyClassOrFunctionType(drgn_type* type, + const std::string& typeName) { return (!isKnownType(typeName) && drgn_type_has_members(type) && drgn_type_num_members(type) == 0); } @@ -1309,7 +1309,7 @@ bool OICodeGen::isEmptyClassOrFunctionType(drgn_type *type, * A class requiring a virtual table pointer (because it or its bases have * one or more virtual member functions or virtual base classes). */ -bool OICodeGen::isDynamic(drgn_type *type) const { +bool OICodeGen::isDynamic(drgn_type* type) const { if (!config.polymorphicInheritance || !drgn_type_has_virtuality(type)) { return false; } @@ -1319,10 +1319,10 @@ bool OICodeGen::isDynamic(drgn_type *type) const { return true; } - drgn_type_member_function *functions = drgn_type_functions(type); + drgn_type_member_function* functions = drgn_type_functions(type); for (size_t i = 0; i < drgn_type_num_functions(type); i++) { drgn_qualified_type t{}; - if (auto *err = drgn_member_function_type(&functions[i], &t)) { + if (auto* err = drgn_member_function_type(&functions[i], &t)) { LOG(ERROR) << "Error when looking up member function for type " << type << " err " << err->code << " " << err->message; drgn_error_destroy(err); @@ -1337,7 +1337,7 @@ bool OICodeGen::isDynamic(drgn_type *type) const { return false; } -bool OICodeGen::enumerateClassType(drgn_type *type) { +bool OICodeGen::enumerateClassType(drgn_type* type) { std::string typeName = getStructName(type); VLOG(2) << "Class name : " << typeName << " " << type; @@ -1380,8 +1380,8 @@ bool OICodeGen::enumerateClassType(drgn_type *type) { } } else if (ifGenerateMemberDefinition(typeName)) { if (isDynamic(type)) { - const auto &children = childClasses[drgn_type_tag(type)]; - for (const auto &child : children) { + const auto& children = childClasses[drgn_type_tag(type)]; + for (const auto& child : children) { enumerateTypesRecurse(child); } } @@ -1398,7 +1398,7 @@ bool OICodeGen::enumerateClassType(drgn_type *type) { return true; } -bool OICodeGen::enumerateTypeDefType(drgn_type *type) { +bool OICodeGen::enumerateTypeDefType(drgn_type* type) { std::string typeName; if (drgn_type_has_name(type)) { typeName = drgn_type_name(type); @@ -1436,7 +1436,7 @@ bool OICodeGen::enumerateTypeDefType(drgn_type *type) { tname = drgn_type_name(qtype.type); } else { uint64_t sz = 0; - if (auto *err = drgn_type_sizeof(type, &sz); err != nullptr) { + if (auto* err = drgn_type_sizeof(type, &sz); err != nullptr) { LOG(ERROR) << "Failed to get size: " << err->code << " " << err->message << " " << typeName; drgn_error_destroy(err); @@ -1458,7 +1458,7 @@ bool OICodeGen::enumerateTypeDefType(drgn_type *type) { return ret; } -bool OICodeGen::enumerateEnumType(drgn_type *type) { +bool OICodeGen::enumerateEnumType(drgn_type* type) { std::string typeName; if (drgn_type_tag(type) != nullptr) { @@ -1484,8 +1484,8 @@ bool OICodeGen::enumerateEnumType(drgn_type *type) { return true; } -static drgn_type *getPtrUnderlyingType(drgn_type *type) { - drgn_type *underlyingType = type; +static drgn_type* getPtrUnderlyingType(drgn_type* type) { + drgn_type* underlyingType = type; while (drgn_type_kind(underlyingType) == DRGN_TYPE_POINTER || drgn_type_kind(underlyingType) == DRGN_TYPE_TYPEDEF) { @@ -1495,7 +1495,7 @@ static drgn_type *getPtrUnderlyingType(drgn_type *type) { return underlyingType; } -bool OICodeGen::enumeratePointerType(drgn_type *type) { +bool OICodeGen::enumeratePointerType(drgn_type* type) { // Not handling pointers right now. Pointer members in classes are going to be // tricky. If we enumerate objects from pointers there are many questions :- // 1. How to handle uninitialized pointers @@ -1512,7 +1512,7 @@ bool OICodeGen::enumeratePointerType(drgn_type *type) { // If type is a function pointer, directly store the underlying type in // pointerToTypeMap, so that TreeBuilder can easily replace function // pointers with uintptr_t - drgn_type *utype = getPtrUnderlyingType(type); + drgn_type* utype = getPtrUnderlyingType(type); if (drgn_type_kind(utype) == DRGN_TYPE_FUNCTION) { VLOG(2) << "Type " << type << " is a function pointer to " << utype; pointerToTypeMap.emplace(type, utype); @@ -1520,7 +1520,7 @@ bool OICodeGen::enumeratePointerType(drgn_type *type) { } pointerToTypeMap.emplace(type, qtype.type); - drgn_type *underlyingType = drgnUnderlyingType(qtype.type); + drgn_type* underlyingType = drgnUnderlyingType(qtype.type); bool isComplete = isDrgnSizeComplete(underlyingType); if (drgn_type_kind(underlyingType) == DRGN_TYPE_FUNCTION || isComplete) { @@ -1533,7 +1533,7 @@ bool OICodeGen::enumeratePointerType(drgn_type *type) { return ret; } -bool OICodeGen::enumeratePrimitiveType(drgn_type *type) { +bool OICodeGen::enumeratePrimitiveType(drgn_type* type) { std::string typeName; if (!drgn_type_has_name(type)) { @@ -1548,10 +1548,10 @@ bool OICodeGen::enumeratePrimitiveType(drgn_type *type) { return true; } -bool OICodeGen::enumerateArrayType(drgn_type *type) { +bool OICodeGen::enumerateArrayType(drgn_type* type) { uint64_t ret = 0; - if (auto *err = drgn_type_sizeof(type, &ret); err != nullptr) { + if (auto* err = drgn_type_sizeof(type, &ret); err != nullptr) { LOG(ERROR) << "Error when looking up size from drgn " << err->code << " " << err->message << " " << std::endl; drgn_error_destroy(err); @@ -1566,7 +1566,7 @@ bool OICodeGen::enumerateArrayType(drgn_type *type) { return true; } -bool OICodeGen::enumerateTypesRecurse(drgn_type *type) { +bool OICodeGen::enumerateTypesRecurse(drgn_type* type) { auto kind = drgn_type_kind(type); if (kind == DRGN_TYPE_VOID || kind == DRGN_TYPE_FUNCTION) { @@ -1584,7 +1584,7 @@ bool OICodeGen::enumerateTypesRecurse(drgn_type *type) { getDrgnTypeNameInt(type, outName); drgn_qualified_type qtype = {type, {}}; - char *defStr = nullptr; + char* defStr = nullptr; std::string typeDefStr; if (drgn_format_type(qtype, &defStr) != nullptr) { @@ -1640,7 +1640,7 @@ bool OICodeGen::enumerateTypesRecurse(drgn_type *type) { return ret; } -std::optional OICodeGen::getNameForType(drgn_type *type) { +std::optional OICodeGen::getNameForType(drgn_type* type) { if (auto search = typeToNameMap.find(type); search != typeToNameMap.end()) { return search->second; } @@ -1650,8 +1650,8 @@ std::optional OICodeGen::getNameForType(drgn_type *type) { } void OICodeGen::getFuncDefClassMembers( - std::string &code, drgn_type *type, - std::unordered_map &memberNames, bool skipPadding) { + std::string& code, drgn_type* type, + std::unordered_map& memberNames, bool skipPadding) { if (drgn_type_kind(type) == DRGN_TYPE_TYPEDEF) { // Handle case where parent is a typedef getFuncDefClassMembers(code, drgnUnderlyingType(type), memberNames); @@ -1659,7 +1659,7 @@ void OICodeGen::getFuncDefClassMembers( } if (parentClasses.find(type) != parentClasses.end()) { - for (auto &p : parentClasses[type]) { + for (auto& p : parentClasses[type]) { // paddingIndexMap[type] already cover the parents' paddings, // so skip the parents' padding generation to avoid double counting getFuncDefClassMembers(code, p.type, memberNames, true); @@ -1673,14 +1673,14 @@ void OICodeGen::getFuncDefClassMembers( if (!skipPadding) { auto paddingIt = paddingIndexMap.find(type); if (paddingIt != paddingIndexMap.end()) { - const auto &paddingRange = paddingIt->second; + const auto& paddingRange = paddingIt->second; for (auto i = paddingRange.first; i < paddingRange.second; ++i) { code += "SAVE_SIZE(sizeof(t.__padding_" + std::to_string(i) + "));\n"; } } } - const auto &members = classMembersMap[type]; + const auto& members = classMembersMap[type]; bool captureThriftIsset = thriftIssetStructTypes.contains(type); if (captureThriftIsset) { @@ -1708,7 +1708,7 @@ void OICodeGen::getFuncDefClassMembers( code += "}\n"; } - const auto &member = members[i]; + const auto& member = members[i]; std::string memberName = member.member_name; std::replace(memberName.begin(), memberName.end(), '.', '_'); @@ -1733,24 +1733,24 @@ void OICodeGen::getFuncDefClassMembers( } } -void OICodeGen::enumerateDescendants(drgn_type *type, drgn_type *baseType) { +void OICodeGen::enumerateDescendants(drgn_type* type, drgn_type* baseType) { auto it = childClasses.find(drgn_type_tag(type)); if (it == childClasses.end()) { return; } // TODO this list may end up containing duplicates - const auto &children = it->second; + const auto& children = it->second; descendantClasses[baseType].insert(descendantClasses[baseType].end(), children.begin(), children.end()); - for (const auto &child : children) { + for (const auto& child : children) { enumerateDescendants(child, baseType); } } -void OICodeGen::getFuncDefinitionStr(std::string &code, drgn_type *type, - const std::string &typeName) { +void OICodeGen::getFuncDefinitionStr(std::string& code, drgn_type* type, + const std::string& typeName) { if (classMembersMap.find(type) == classMembersMap.end()) { return; } @@ -1774,12 +1774,12 @@ void OICodeGen::getFuncDefinitionStr(std::string &code, drgn_type *type, if (it == descendantClasses.end()) { return; } - const auto &descendants = it->second; + const auto& descendants = it->second; std::vector> concreteClasses; concreteClasses.reserve(descendants.size()); - for (const auto &child : descendants) { + for (const auto& child : descendants) { auto fqChildName = *fullyQualifiedName(child); // We must split this assignment and append because the C++ standard lacks @@ -1800,8 +1800,8 @@ void OICodeGen::getFuncDefinitionStr(std::string &code, drgn_type *type, concreteClasses.push_back({oiChildName, vtableSym}); } - for (const auto &child : concreteClasses) { - const auto &className = child.first; + for (const auto& child : concreteClasses) { + const auto& className = child.first; code += "void getSizeTypeConcrete(const " + className + "& t, size_t& returnArg);\n"; } @@ -1822,7 +1822,7 @@ void OICodeGen::getFuncDefinitionStr(std::string &code, drgn_type *type, // // This works for C++ compilers which follow the GNU v3 ABI, i.e. GCC and // Clang. Other compilers may differ. - const auto &[className, vtableSym] = concreteClasses[i]; + const auto& [className, vtableSym] = concreteClasses[i]; uintptr_t vtableMinAddr = vtableSym.addr; uintptr_t vtableMaxAddr = vtableSym.addr + vtableSym.size; code += " if (vptrVal >= 0x" + @@ -1844,10 +1844,10 @@ void OICodeGen::getFuncDefinitionStr(std::string &code, drgn_type *type, } } -std::string OICodeGen::templateTransformType(const std::string &typeName) { +std::string OICodeGen::templateTransformType(const std::string& typeName) { std::string s; s.reserve(typeName.size()); - for (const auto &c : typeName) { + for (const auto& c : typeName) { if (c == '<' || c == '>' || c == ',' || c == ' ' || c == ':' || c == '(' || c == ')' || c == '&' || c == '*' || c == '-' || c == '\'' || c == '[' || c == ']') { @@ -1859,12 +1859,12 @@ std::string OICodeGen::templateTransformType(const std::string &typeName) { return s; } -std::string OICodeGen::structNameTransformType(const std::string &typeName) { +std::string OICodeGen::structNameTransformType(const std::string& typeName) { std::string s; bool prevColon = false; s.reserve(typeName.size()); - for (const auto &c : typeName) { + for (const auto& c : typeName) { if (c == ':') { if (prevColon) { prevColon = false; @@ -1889,20 +1889,20 @@ std::string OICodeGen::structNameTransformType(const std::string &typeName) { } void OICodeGen::memberTransformName( - std::map &templateTransformMap, - std::string &typeName) { + std::map& templateTransformMap, + std::string& typeName) { std::vector sortedTypes; - for (auto &e : templateTransformMap) { + for (auto& e : templateTransformMap) { sortedTypes.push_back(e.first); } std::sort(sortedTypes.begin(), sortedTypes.end(), - [](const std::string &first, const std::string &second) { + [](const std::string& first, const std::string& second) { return first.size() > second.size(); }); - for (auto &e : sortedTypes) { + for (auto& e : sortedTypes) { std::string search = e; std::string replace = templateTransformMap[e]; boost::replace_all(typeName, search, replace); @@ -1910,7 +1910,7 @@ void OICodeGen::memberTransformName( } OICodeGen::SortedTypeDefMap OICodeGen::getSortedTypeDefMap( - const std::map &typedefTypeMap) { + const std::map& typedefTypeMap) { auto typeMap = typedefTypeMap; SortedTypeDefMap typedefVec; @@ -1927,8 +1927,8 @@ OICodeGen::SortedTypeDefMap OICodeGen::getSortedTypeDefMap( return typedefVec; } -bool OICodeGen::getEnumUnderlyingTypeStr(drgn_type *e, - std::string &enumUnderlyingTypeStr) { +bool OICodeGen::getEnumUnderlyingTypeStr(drgn_type* e, + std::string& enumUnderlyingTypeStr) { std::string name; if (drgn_type_tag(e) != nullptr) { name.assign(drgn_type_tag(e)); @@ -1937,7 +1937,7 @@ bool OICodeGen::getEnumUnderlyingTypeStr(drgn_type *e, } uint64_t sz = 0; - if (auto *err = drgn_type_sizeof(e, &sz); err != nullptr) { + if (auto* err = drgn_type_sizeof(e, &sz); err != nullptr) { LOG(ERROR) << "Error when looking up size from drgn " << err->code << " " << err->message << " "; drgn_error_destroy(err); @@ -1967,7 +1967,7 @@ bool OICodeGen::getEnumUnderlyingTypeStr(drgn_type *e, return true; } -bool OICodeGen::getDrgnTypeNameInt(drgn_type *type, std::string &outName) { +bool OICodeGen::getDrgnTypeNameInt(drgn_type* type, std::string& outName) { std::string name; if (drgn_type_kind(type) == DRGN_TYPE_ENUM) { @@ -1987,14 +1987,14 @@ bool OICodeGen::getDrgnTypeNameInt(drgn_type *type, std::string &outName) { } else if (drgn_type_has_name(type)) { name.assign(drgn_type_name(type)); } else if (drgn_type_kind(type) == DRGN_TYPE_POINTER) { - drgn_type *underlyingType = getPtrUnderlyingType(type); + drgn_type* underlyingType = getPtrUnderlyingType(type); if (config.chaseRawPointers && drgn_type_kind(underlyingType) != DRGN_TYPE_FUNCTION) { // For pointers, figure out name for the underlying type then add // appropriate number of '*' { int ptrDepth = 0; - drgn_type *ut = type; + drgn_type* ut = type; while (drgn_type_kind(ut) == DRGN_TYPE_POINTER) { ut = drgn_type_type(ut).type; ptrDepth++; @@ -2025,11 +2025,11 @@ bool OICodeGen::getDrgnTypeNameInt(drgn_type *type, std::string &outName) { return true; } -bool OICodeGen::getDrgnTypeName(drgn_type *type, std::string &outName) { +bool OICodeGen::getDrgnTypeName(drgn_type* type, std::string& outName) { return getDrgnTypeNameInt(type, outName); } -void OICodeGen::addTypeToName(drgn_type *type, std::string name) { +void OICodeGen::addTypeToName(drgn_type* type, std::string name) { VLOG(2) << "Trying to assign name to type: " << name << " " << type; if (typeToNameMap.find(type) != typeToNameMap.end()) { @@ -2072,7 +2072,7 @@ void OICodeGen::addTypeToName(drgn_type *type, std::string name) { } void OICodeGen::getClassMembersIncludingParent( - drgn_type *type, std::vector &out) { + drgn_type* type, std::vector& out) { if (drgn_type_kind(type) == DRGN_TYPE_TYPEDEF) { // Handle case where parent is a typedef getClassMembersIncludingParent(drgnUnderlyingType(type), out); @@ -2080,27 +2080,27 @@ void OICodeGen::getClassMembersIncludingParent( } if (parentClasses.find(type) != parentClasses.end()) { - for (auto &parent : parentClasses[type]) { + for (auto& parent : parentClasses[type]) { getClassMembersIncludingParent(parent.type, out); } } - for (auto &mem : classMembersMap[type]) { + for (auto& mem : classMembersMap[type]) { out.push_back(mem); } } -std::map> - &OICodeGen::getClassMembersMap() { - for (auto &e : classMembersMap) { +std::map>& +OICodeGen::getClassMembersMap() { + for (auto& e : classMembersMap) { std::vector v; getClassMembersIncludingParent(e.first, v); classMembersMapCopy[e.first] = v; } - for (auto &e : classMembersMapCopy) { + for (auto& e : classMembersMapCopy) { VLOG(1) << "ClassCopy " << e.first << std::endl; - for (auto &m : e.second) { + for (auto& m : e.second) { VLOG(1) << " " << m.type << " " << m.member_name << std::endl; } } @@ -2118,27 +2118,27 @@ void OICodeGen::printAllTypes() { VLOG(2) << "Printing all types"; VLOG(2) << "Classes"; - for (auto &e : classMembersMap) { + for (auto& e : classMembersMap) { VLOG(2) << "Class " << e.first; - auto &members = e.second; - for (auto &m : members) { + auto& members = e.second; + for (auto& m : members) { VLOG(2) << " " << m.member_name << " " << m.type; } } VLOG(2) << "Struct defs "; - for (auto &e : structDefType) { + for (auto& e : structDefType) { VLOG(2) << "Defined struct " << e; } VLOG(2) << "FuncDef structs "; - for (auto &e : funcDefTypeList) { + for (auto& e : funcDefTypeList) { VLOG(2) << "FuncDef struct " << e; } VLOG(2) << "Dummy structs "; - for (const auto &e : knownDummyTypeList) { + for (const auto& e : knownDummyTypeList) { VLOG(2) << "Dummy struct " << e; } } @@ -2147,25 +2147,25 @@ void OICodeGen::printAllTypeNames() { VLOG(2) << "Printing all type names "; VLOG(2) << "Classes"; - for (auto &e : classMembersMap) { + for (auto& e : classMembersMap) { auto typeName = getNameForType(e.first); if (!typeName.has_value()) { continue; } VLOG(2) << "Class " << *typeName << " " << e.first; - auto &members = e.second; - for (auto &m : members) { + auto& members = e.second; + for (auto& m : members) { VLOG(2) << " " << m.member_name << " " << m.type; } } - for (auto &kv : unnamedUnion) { + for (auto& kv : unnamedUnion) { VLOG(2) << "Unnamed union/struct " << kv.first << " " << kv.second; } VLOG(2) << "Structs defs "; - for (auto &e : structDefType) { + for (auto& e : structDefType) { auto typeName = getNameForType(e); if (!typeName.has_value()) { continue; @@ -2175,7 +2175,7 @@ void OICodeGen::printAllTypeNames() { } VLOG(2) << "\nFuncDef structs "; - for (auto &e : funcDefTypeList) { + for (auto& e : funcDefTypeList) { auto typeName = getNameForType(e); if (!typeName.has_value()) { continue; @@ -2186,7 +2186,7 @@ void OICodeGen::printAllTypeNames() { VLOG(2) << "\nDummy structs "; - for (auto &e : knownDummyTypeList) { + for (auto& e : knownDummyTypeList) { auto typeName = getNameForType(e); if (!typeName.has_value()) { continue; @@ -2196,7 +2196,7 @@ void OICodeGen::printAllTypeNames() { } } -bool OICodeGen::generateStructDef(drgn_type *e, std::string &code) { +bool OICodeGen::generateStructDef(drgn_type* e, std::string& code) { if (classMembersMap.find(e) == classMembersMap.end()) { LOG(ERROR) << "Failed to find in classMembersMap " << e; return false; @@ -2322,14 +2322,14 @@ bool OICodeGen::generateStructDef(drgn_type *e, std::string &code) { return true; } -bool OICodeGen::isNumMemberGreaterThanZero(drgn_type *type) { +bool OICodeGen::isNumMemberGreaterThanZero(drgn_type* type) { if (drgn_type_num_members(type) > 0) { return true; } if (parentClasses.find(type) != parentClasses.end()) { - for (auto &p : parentClasses[type]) { - auto *underlyingType = drgnUnderlyingType(p.type); + for (auto& p : parentClasses[type]) { + auto* underlyingType = drgnUnderlyingType(p.type); if (isNumMemberGreaterThanZero(underlyingType)) { return true; } @@ -2339,7 +2339,7 @@ bool OICodeGen::isNumMemberGreaterThanZero(drgn_type *type) { return false; } -bool OICodeGen::addPadding(uint64_t padding_bits, std::string &code) { +bool OICodeGen::addPadding(uint64_t padding_bits, std::string& code) { if (padding_bits == 0) { return false; } @@ -2369,7 +2369,7 @@ bool OICodeGen::addPadding(uint64_t padding_bits, std::string &code) { return true; } -static inline void addSizeComment(bool genPaddingStats, std::string &code, +static inline void addSizeComment(bool genPaddingStats, std::string& code, size_t offset, size_t sizeInBits) { if (!genPaddingStats) { return; @@ -2390,8 +2390,8 @@ static inline void addSizeComment(bool genPaddingStats, std::string &code, } void OICodeGen::deduplicateMemberName( - std::unordered_map &memberNames, - std::string &memberName) { + std::unordered_map& memberNames, + std::string& memberName) { if (!memberName.empty()) { auto srchRes = memberNames.find(memberName); if (srchRes == memberNames.end()) { @@ -2405,15 +2405,15 @@ void OICodeGen::deduplicateMemberName( } std::optional OICodeGen::generateMember( - const DrgnClassMemberInfo &m, - std::unordered_map &memberNames, uint64_t currOffsetBits, - std::string &code, bool isInUnion) { + const DrgnClassMemberInfo& m, + std::unordered_map& memberNames, uint64_t currOffsetBits, + std::string& code, bool isInUnion) { // Generate unique name for member std::string memberName = m.member_name; deduplicateMemberName(memberNames, memberName); std::replace(memberName.begin(), memberName.end(), '.', '_'); - auto *memberType = m.type; + auto* memberType = m.type; auto szBytes = getDrgnTypeSize(memberType); if (!szBytes.has_value()) { @@ -2433,7 +2433,7 @@ std::optional OICodeGen::generateMember( // TODO: No idea how to handle flexible array member or zero length array size_t elems = 1; - drgn_type *arrayElementType = nullptr; + drgn_type* arrayElementType = nullptr; drgn_utils::getDrgnArrayElementType(memberType, &arrayElementType, elems); auto tmpStr = getNameForType(arrayElementType); @@ -2502,12 +2502,12 @@ std::optional OICodeGen::generateMember( } bool OICodeGen::generateParent( - drgn_type *p, std::unordered_map &memberNames, - uint64_t &currOffsetBits, std::string &code, size_t offsetToNextMember) { + drgn_type* p, std::unordered_map& memberNames, + uint64_t& currOffsetBits, std::string& code, size_t offsetToNextMember) { // Parent class could be a typedef PaddingInfo paddingInfo{}; bool violatesAlignmentRequirement = false; - auto *underlyingType = drgnUnderlyingType(p); + auto* underlyingType = drgnUnderlyingType(p); uint64_t offsetBits = 0; if (!generateStructMembers(underlyingType, memberNames, code, offsetBits, @@ -2533,7 +2533,7 @@ bool OICodeGen::generateParent( } /*Helper function that returns the alignment constraints in bits*/ -std::optional OICodeGen::getAlignmentRequirements(drgn_type *e) { +std::optional OICodeGen::getAlignmentRequirements(drgn_type* e) { const uint64_t minimumAlignmentBits = CHAR_BIT; uint64_t alignmentRequirement = CHAR_BIT; std::string outName; @@ -2561,7 +2561,7 @@ std::optional OICodeGen::getAlignmentRequirements(drgn_type *e) { alignmentRequirement = 64; } else { auto numMembers = drgn_type_num_members(e); - auto *members = drgn_type_members(e); + auto* members = drgn_type_members(e); for (size_t i = 0; i < numMembers; ++i) { drgn_qualified_type memberType{}; if (drgn_member_type(&members[i], &memberType, nullptr) != nullptr) { @@ -2588,16 +2588,16 @@ std::optional OICodeGen::getAlignmentRequirements(drgn_type *e) { return 64; case DRGN_TYPE_TYPEDEF: case DRGN_TYPE_ARRAY: - auto *underlyingType = drgn_type_type(e).type; + auto* underlyingType = drgn_type_type(e).type; return getAlignmentRequirements(underlyingType); } return minimumAlignmentBits; } bool OICodeGen::generateStructMembers( - drgn_type *e, std::unordered_map &memberNames, - std::string &code, uint64_t &out_offset_bits, PaddingInfo &paddingInfo, - bool &violatesAlignmentRequirement, size_t offsetToNextMemberInSubclass) { + drgn_type* e, std::unordered_map& memberNames, + std::string& code, uint64_t& out_offset_bits, PaddingInfo& paddingInfo, + bool& violatesAlignmentRequirement, size_t offsetToNextMemberInSubclass) { if (classMembersMap.find(e) == classMembersMap.end()) { VLOG(1) << "Failed to find type in classMembersMap: " << e; } @@ -2605,7 +2605,7 @@ bool OICodeGen::generateStructMembers( size_t parentIndex = 0; size_t memberIndex = 0; - auto &members = classMembersMap[e]; + auto& members = classMembersMap[e]; uint64_t currOffsetBits = 0; uint64_t alignmentRequirement = 8; @@ -2675,7 +2675,7 @@ bool OICodeGen::generateStructMembers( // Determine whether this type is a Thrift struct. Only try and read the // isset values if the struct layout is as expected. // i.e. __isset must be the last member in the struct - const auto &memberName = members[memberIndex].member_name; + const auto& memberName = members[memberIndex].member_name; bool isThriftIssetStruct = typeName.starts_with("isset_bitset<") && memberName == "__isset"; @@ -2856,15 +2856,15 @@ bool OICodeGen::generateStructMembers( return true; } -bool OICodeGen::generateStructDefs(std::string &code) { - std::vector structDefTypeCopy = structDefType; - std::map> parentClassesCopy = +bool OICodeGen::generateStructDefs(std::string& code) { + std::vector structDefTypeCopy = structDefType; + std::map> parentClassesCopy = parentClasses; while (!structDefTypeCopy.empty()) { for (auto it = structDefTypeCopy.cbegin(); it != structDefTypeCopy.cend();) { - drgn_type *e = *it; + drgn_type* e = *it; if (classMembersMap.find(e) == classMembersMap.end()) { LOG(ERROR) << "Failed to find in classMembersMap " << e; return false; @@ -2874,8 +2874,8 @@ bool OICodeGen::generateStructDefs(std::string &code) { // Make sure that all parent class types are defined if (parentClassesCopy.find(e) != parentClassesCopy.end()) { - auto &parents = parentClassesCopy[e]; - for (auto &p : parents) { + auto& parents = parentClassesCopy[e]; + for (auto& p : parents) { auto it2 = std::find(structDefTypeCopy.begin(), structDefTypeCopy.end(), p.type); if (it2 != structDefTypeCopy.cend()) { @@ -2887,9 +2887,9 @@ bool OICodeGen::generateStructDefs(std::string &code) { // Make sure that all member types are defined if (!skip) { - auto &members = classMembersMap[e]; - for (auto &m : members) { - auto *underlyingType = drgnUnderlyingType(m.type); + auto& members = classMembersMap[e]; + for (auto& m : members) { + auto* underlyingType = drgnUnderlyingType(m.type); if (underlyingType != e) { auto it2 = std::find(structDefTypeCopy.begin(), @@ -2928,9 +2928,9 @@ bool OICodeGen::generateStructDefs(std::string &code) { return true; } -bool OICodeGen::addStaticAssertsForType(drgn_type *type, +bool OICodeGen::addStaticAssertsForType(drgn_type* type, bool generateAssertsForOffsets, - std::string &code) { + std::string& code) { auto struct_name = getNameForType(type); if (!struct_name.has_value()) { return false; @@ -2967,7 +2967,7 @@ bool OICodeGen::addStaticAssertsForType(drgn_type *type, * } // nsB * } // nsA */ -void OICodeGen::declareThriftStruct(std::string &code, std::string_view name) { +void OICodeGen::declareThriftStruct(std::string& code, std::string_view name) { if (auto pos = name.find("::"); pos != name.npos) { auto ns = name.substr(0, pos); code += "namespace "; @@ -2985,7 +2985,7 @@ void OICodeGen::declareThriftStruct(std::string &code, std::string_view name) { } } -bool OICodeGen::generateJitCode(std::string &code) { +bool OICodeGen::generateJitCode(std::string& code) { // Include relevant headers code.append("// relevant header includes -----\n"); @@ -2995,7 +2995,7 @@ bool OICodeGen::generateJitCode(std::string &code) { // when people may not expect it. { bool found = false; - for (const auto &el : containerInfoList) { + for (const auto& el : containerInfoList) { if (el->typeName == "std::array<") { containerTypesFuncDef.insert(*el); found = true; @@ -3010,7 +3010,7 @@ bool OICodeGen::generateJitCode(std::string &code) { } std::set includedHeaders = config.defaultHeaders; - for (auto &e : containerTypesFuncDef) { + for (auto& e : containerTypesFuncDef) { includedHeaders.insert(e.header); } @@ -3021,7 +3021,7 @@ bool OICodeGen::generateJitCode(std::string &code) { // Required for the offsetof() macro includedHeaders.insert("cstddef"); - for (const auto &e : includedHeaders) { + for (const auto& e : includedHeaders) { code.append("#include <"); code.append(e); code.append(">\n"); @@ -3069,13 +3069,13 @@ bool OICodeGen::generateJitCode(std::string &code) { definitionsCode.append("// namespace uses\n"); std::set usedNamespaces = config.defaultNamespaces; - for (const auto &v : containerTypesFuncDef) { - for (auto &e : v.ns) { + for (const auto& v : containerTypesFuncDef) { + for (auto& e : v.ns) { usedNamespaces.insert(std::string(e)); } } - for (auto &e : usedNamespaces) { + for (auto& e : usedNamespaces) { definitionsCode.append("using "); definitionsCode.append(e); definitionsCode.append(";\n"); @@ -3084,7 +3084,7 @@ bool OICodeGen::generateJitCode(std::string &code) { printAllTypeNames(); definitionsCode.append("// forward declarations -----\n"); - for (auto &e : structDefType) { + for (auto& e : structDefType) { if (drgn_type_kind(e) == DRGN_TYPE_STRUCT || drgn_type_kind(e) == DRGN_TYPE_CLASS) { definitionsCode.append("struct"); @@ -3110,7 +3110,7 @@ bool OICodeGen::generateJitCode(std::string &code) { } definitionsCode.append("// stubbed classes -----\n"); - for (const auto &e : knownDummyTypeList) { + for (const auto& e : knownDummyTypeList) { std::string name; if (!getDrgnTypeName(e, name)) { return false; @@ -3122,7 +3122,7 @@ bool OICodeGen::generateJitCode(std::string &code) { } uint64_t sz = 0; - if (auto *err = drgn_type_sizeof(e, &sz); err != nullptr) { + if (auto* err = drgn_type_sizeof(e, &sz); err != nullptr) { bool shouldReturn = false; std::string knownTypeName; @@ -3163,7 +3163,7 @@ bool OICodeGen::generateJitCode(std::string &code) { } definitionsCode.append("// enums -----\n"); - for (auto &e : enumTypes) { + for (auto& e : enumTypes) { auto name = getNameForType(e); if (!name.has_value()) { @@ -3171,7 +3171,7 @@ bool OICodeGen::generateJitCode(std::string &code) { } uint64_t sz = 0; - if (auto *err = drgn_type_sizeof(e, &sz); err != nullptr) { + if (auto* err = drgn_type_sizeof(e, &sz); err != nullptr) { LOG(ERROR) << "Error when looking up size from drgn " << err->code << " " << err->message << " "; drgn_error_destroy(err); @@ -3204,7 +3204,7 @@ bool OICodeGen::generateJitCode(std::string &code) { definitionsCode.append("// typedefs -----\n"); // Typedefs - for (auto const &e : sortedTypeDefMap) { + for (auto const& e : sortedTypeDefMap) { auto tmpStr1 = getNameForType(e.first); auto tmpStr2 = getNameForType(e.second); @@ -3242,7 +3242,7 @@ bool OICodeGen::generateJitCode(std::string &code) { )"); } - for (auto &e : structDefType) { + for (auto& e : structDefType) { if (drgn_type_kind(e) != DRGN_TYPE_UNION) { auto typeName = getNameForType(e); @@ -3293,7 +3293,7 @@ bool OICodeGen::generateJitCode(std::string &code) { )"); } - for (auto &e : structDefType) { + for (auto& e : structDefType) { auto name = getNameForType(e); if (!name.has_value()) { @@ -3315,7 +3315,7 @@ bool OICodeGen::generateJitCode(std::string &code) { funcGen.DefineAddData(functionsCode); } - for (auto &structType : structDefType) { + for (auto& structType : structDefType) { // Don't generate member offset asserts for unions since we pad them out bool generateOffsetAsserts = (drgn_type_kind(structType) != DRGN_TYPE_UNION); @@ -3326,14 +3326,14 @@ bool OICodeGen::generateJitCode(std::string &code) { } } - for (auto &container : containerTypeMapDrgn) { + for (auto& container : containerTypeMapDrgn) { // Don't generate member offset asserts since we don't unwind containers if (!addStaticAssertsForType(container.first, false, functionsCode)) { return false; } } { - auto *type = rootTypeToIntrospect.type; + auto* type = rootTypeToIntrospect.type; auto tmpStr = getNameForType(type); if (!tmpStr.has_value()) { @@ -3380,7 +3380,7 @@ bool OICodeGen::generateJitCode(std::string &code) { // this struct from the debug info like we do for the types we're probing. // That would require significant refactoring of CodeGen, however. std::string thriftDefinitions; - for (const auto &t : thriftIssetStructTypes) { + for (const auto& t : thriftIssetStructTypes) { auto fullyQualified = *fullyQualifiedName(t); declareThriftStruct(thriftDefinitions, fullyQualified); thriftDefinitions.append(R"( @@ -3411,7 +3411,7 @@ bool OICodeGen::generateJitCode(std::string &code) { return true; } -bool OICodeGen::isUnnamedStruct(drgn_type *type) { +bool OICodeGen::isUnnamedStruct(drgn_type* type) { return unnamedUnion.find(type) != unnamedUnion.end(); } @@ -3420,7 +3420,7 @@ bool OICodeGen::generateNamesForTypes() { printAllTypes(); - for (auto &e : structDefType) { + for (auto& e : structDefType) { std::string name; if (!getDrgnTypeName(e, name)) { return false; @@ -3439,7 +3439,7 @@ bool OICodeGen::generateNamesForTypes() { // // This would result in 2 types a typedef and an enum. Make sure the // underlying enum is named first. - for (auto &e : enumTypes) { + for (auto& e : enumTypes) { if (drgn_type_tag(e)) { std::string name = drgn_type_tag(e); addTypeToName(e, name); @@ -3450,7 +3450,7 @@ bool OICodeGen::generateNamesForTypes() { } } - for (auto &e : typedefTypes) { + for (auto& e : typedefTypes) { std::string name; if (!getDrgnTypeName(e.first, name)) { return false; @@ -3461,11 +3461,11 @@ bool OICodeGen::generateNamesForTypes() { } VLOG(1) << "Printing size map"; - for (auto &e : sizeMap) { + for (auto& e : sizeMap) { VLOG(1) << "sizeMap[" << e.first << "] : " << e.second; } - for (auto &e : knownDummyTypeList) { + for (auto& e : knownDummyTypeList) { std::string name; if (!getDrgnTypeName(e, name)) { return false; @@ -3496,10 +3496,10 @@ bool OICodeGen::generateNamesForTypes() { // having to iterate 2 times. // First pass, ignore pointer types - for (const auto &e : funcDefTypeList) { + for (const auto& e : funcDefTypeList) { if (isUnnamedStruct(e)) { bool found = false; - for (auto &t : typedefTypes) { + for (auto& t : typedefTypes) { if (t.second == e && drgn_type_kind(t.second) != DRGN_TYPE_ENUM) { found = true; break; @@ -3521,7 +3521,7 @@ bool OICodeGen::generateNamesForTypes() { } // First pass, ignore pointer types - for (auto &e : typedefTypes) { + for (auto& e : typedefTypes) { if (isUnnamedStruct(e.second) && drgn_type_kind(e.second) != DRGN_TYPE_ENUM) { } else { @@ -3540,10 +3540,10 @@ bool OICodeGen::generateNamesForTypes() { } // Second pass, name the pointer types - for (auto &e : funcDefTypeList) { + for (auto& e : funcDefTypeList) { if (drgn_type_kind(e) == DRGN_TYPE_POINTER) { int ptrDepth = 0; - drgn_type *ut = e; + drgn_type* ut = e; while (drgn_type_kind(ut) == DRGN_TYPE_POINTER) { ut = drgn_type_type(ut).type; ptrDepth++; @@ -3564,10 +3564,10 @@ bool OICodeGen::generateNamesForTypes() { } // Second pass, name the pointer types - for (auto &e : typedefTypes) { + for (auto& e : typedefTypes) { if (drgn_type_kind(e.second) == DRGN_TYPE_POINTER) { int ptrDepth = 0; - drgn_type *ut = e.second; + drgn_type* ut = e.second; while (drgn_type_kind(ut) == DRGN_TYPE_POINTER) { ut = drgn_type_type(ut).type; ptrDepth++; @@ -3598,7 +3598,7 @@ bool OICodeGen::generateNamesForTypes() { return true; } -bool OICodeGen::generate(std::string &code) { +bool OICodeGen::generate(std::string& code) { if (!populateDefsAndDecls()) { return false; } @@ -3618,8 +3618,8 @@ bool OICodeGen::generate(std::string &code) { * Generate static_asserts for the offsets of each member of the given type */ bool OICodeGen::staticAssertMemberOffsets( - const std::string &struct_name, drgn_type *struct_type, - std::string &assert_str, std::unordered_map &memberNames, + const std::string& struct_name, drgn_type* struct_type, + std::string& assert_str, std::unordered_map& memberNames, uint64_t base_offset) { if (knownDummyTypeList.find(struct_type) != knownDummyTypeList.end()) { return true; @@ -3632,7 +3632,7 @@ bool OICodeGen::staticAssertMemberOffsets( assert_str, memberNames, base_offset); } - const auto *tag = drgn_type_tag(struct_type); + const auto* tag = drgn_type_tag(struct_type); if (tag && isContainer(struct_type)) { // We don't generate members for container types return true; @@ -3640,7 +3640,7 @@ bool OICodeGen::staticAssertMemberOffsets( if (parentClasses.find(struct_type) != parentClasses.end()) { // Recurse into parents to find inherited members - for (const auto &parent : parentClasses[struct_type]) { + for (const auto& parent : parentClasses[struct_type]) { auto parentOffset = base_offset + parent.bit_offset / CHAR_BIT; if (!staticAssertMemberOffsets(struct_name, parent.type, assert_str, memberNames, parentOffset)) { @@ -3653,7 +3653,7 @@ bool OICodeGen::staticAssertMemberOffsets( return true; } - auto *members = drgn_type_members(struct_type); + auto* members = drgn_type_members(struct_type); for (size_t i = 0; i < drgn_type_num_members(struct_type); ++i) { if (members[i].name == nullptr) { // Types can be defined in a class without assigning a member name e.g. @@ -3667,7 +3667,7 @@ bool OICodeGen::staticAssertMemberOffsets( drgn_qualified_type memberQualType{}; uint64_t bitFieldSize = 0; - if (auto *err = + if (auto* err = drgn_member_type(&members[i], &memberQualType, &bitFieldSize); err != nullptr) { LOG(ERROR) << "Error when looking up member type " << err->code << " " @@ -3721,7 +3721,7 @@ std::string OICodeGen::Config::toString() const { // The list of ignored members must also be part of the remote hash std::string ignoreMembers = "IgnoreMembers="; - for (const auto &ignore : membersToStub) { + for (const auto& ignore : membersToStub) { ignoreMembers += ignore.first; ignoreMembers += "::"; ignoreMembers += ignore.second; diff --git a/src/OICodeGen.h b/src/OICodeGen.h index 1cae0e2..d0c147d 100644 --- a/src/OICodeGen.h +++ b/src/OICodeGen.h @@ -38,10 +38,10 @@ extern "C" { namespace fs = std::filesystem; struct ParentMember { - drgn_type *type; + drgn_type* type; uint64_t bit_offset; - bool operator<(const ParentMember &parent) const { + bool operator<(const ParentMember& parent) const { return (bit_offset < parent.bit_offset); } }; @@ -72,19 +72,19 @@ class OICodeGen { private: // Private constructor. Please use the fallible `OICodeGen::buildFromConfig` // for the expected behaviour. - OICodeGen(const Config &, SymbolService &); + OICodeGen(const Config&, SymbolService&); public: - static std::unique_ptr buildFromConfig(const Config &, - SymbolService &); - bool generate(std::string &code); + static std::unique_ptr buildFromConfig(const Config&, + SymbolService&); + bool generate(std::string& code); [[deprecated("Use generate(std::string&) instead.")]] bool - generateFunctionsForTypesDrgn(std::string &code) { + generateFunctionsForTypesDrgn(std::string& code) { return generate(code); } - bool registerContainer(const fs::path &); + bool registerContainer(const fs::path&); // TODO: remove me once all the callsites are gone static void initializeCodeGen(); @@ -97,18 +97,18 @@ class OICodeGen { TypeHierarchy getTypeHierarchy(); std::map getPaddingInfo(); - bool isContainer(drgn_type *type); + bool isContainer(drgn_type* type); - static drgn_type *drgnUnderlyingType(drgn_type *type); + static drgn_type* drgnUnderlyingType(drgn_type* type); - bool buildName(drgn_type *type, std::string &text, std::string &outName); + bool buildName(drgn_type* type, std::string& text, std::string& outName); - std::string typeToTransformedName(drgn_type *type); + std::string typeToTransformedName(drgn_type* type); - bool enumerateTypesRecurse(drgn_type *type); - static std::string_view drgnKindStr(drgn_type *type); - std::set processedTypes; - bool isDynamic(drgn_type *type) const; + bool enumerateTypesRecurse(drgn_type* type); + static std::string_view drgnKindStr(drgn_type* type); + std::set processedTypes; + bool isDynamic(drgn_type* type) const; private: Config config{}; @@ -120,57 +120,57 @@ class OICodeGen { using TemplateParamList = std::vector>; - using SortedTypeDefMap = std::vector>; + using SortedTypeDefMap = std::vector>; std::string rootTypeStr; std::string linkageName; - std::map unnamedUnion; + std::map unnamedUnion; std::map sizeMap; - std::map containerTypeMapDrgn; + std::map containerTypeMapDrgn; std::vector> containerInfoList; - std::vector enumTypes; + std::vector enumTypes; std::vector knownTypes; drgn_qualified_type rootType; drgn_qualified_type rootTypeToIntrospect; std::map typedefMap; - std::map> parentClasses; - std::map> childClasses; - std::map> descendantClasses; + std::map> parentClasses; + std::map> childClasses; + std::map> descendantClasses; - SymbolService &symbols; + SymbolService& symbols; size_t pad_index = 0; - std::unordered_map> paddingIndexMap; + std::unordered_map> paddingIndexMap; - std::map typedefTypes; - std::map> classMembersMap; - std::map> classMembersMapCopy; - std::map typeToNameMap; - std::map nameToTypeMap; - std::set funcDefTypeList; - std::vector structDefType; - std::set knownDummyTypeList; - std::map pointerToTypeMap; - std::set thriftIssetStructTypes; - std::vector topoSortedStructTypes; + std::map typedefTypes; + std::map> classMembersMap; + std::map> classMembersMapCopy; + std::map typeToNameMap; + std::map nameToTypeMap; + std::set funcDefTypeList; + std::vector structDefType; + std::set knownDummyTypeList; + std::map pointerToTypeMap; + std::set thriftIssetStructTypes; + std::vector topoSortedStructTypes; std::set containerTypesFuncDef; std::map paddedStructs; - std::map> &getClassMembersMap(); + std::map>& getClassMembersMap(); class DrgnString { struct FreeDeleter { - void operator()(void *allocated) { + void operator()(void* allocated) { free(allocated); } }; public: std::string_view contents; - DrgnString(char *data, size_t length) + DrgnString(char* data, size_t length) : contents{data, length}, _data{data} { } DrgnString() = delete; @@ -179,142 +179,142 @@ class OICodeGen { std::unique_ptr _data; }; - static void prependQualifiers(enum drgn_qualifiers, std::string &sb); + static void prependQualifiers(enum drgn_qualifiers, std::string& sb); static std::string stripFullyQualifiedName( - const std::string &fullyQualifiedName); + const std::string& fullyQualifiedName); std::string stripFullyQualifiedNameWithSeparators( - const std::string &fullyQualifiedname); - static void removeTemplateParamAtIndex(std::vector ¶ms, + const std::string& fullyQualifiedname); + static void removeTemplateParamAtIndex(std::vector& params, const size_t index); - std::unordered_map fullyQualifiedNames; - std::optional fullyQualifiedName(drgn_type *type); + std::unordered_map fullyQualifiedNames; + std::optional fullyQualifiedName(drgn_type* type); static SortedTypeDefMap getSortedTypeDefMap( - const std::map &typedefTypeMap); + const std::map& typedefTypeMap); - std::optional getContainerInfo(drgn_type *type); + std::optional getContainerInfo(drgn_type* type); void printAllTypes(); void printAllTypeNames(); - static void addPaddingForBaseClass(drgn_type *type, - std::vector &def); - void addTypeToName(drgn_type *type, std::string name); + static void addPaddingForBaseClass(drgn_type* type, + std::vector& def); + void addTypeToName(drgn_type* type, std::string name); bool generateNamesForTypes(); - bool generateJitCode(std::string &code); - bool generateStructDefs(std::string &code); - bool generateStructDef(drgn_type *e, std::string &code); - bool getDrgnTypeName(drgn_type *type, std::string &outName); + bool generateJitCode(std::string& code); + bool generateStructDefs(std::string& code); + bool generateStructDef(drgn_type* e, std::string& code); + bool getDrgnTypeName(drgn_type* type, std::string& outName); - bool getDrgnTypeNameInt(drgn_type *type, std::string &outName); - bool recordChildren(drgn_type *type); + bool getDrgnTypeNameInt(drgn_type* type, std::string& outName); + bool recordChildren(drgn_type* type); bool enumerateChildClasses(); bool populateDefsAndDecls(); static void memberTransformName( - std::map &templateTransformMap, - std::string &typeName); + std::map& templateTransformMap, + std::string& typeName); - bool getMemberDefinition(drgn_type *type); - bool isKnownType(const std::string &type); - bool isKnownType(const std::string &type, std::string &matched); + bool getMemberDefinition(drgn_type* type); + bool isKnownType(const std::string& type); + bool isKnownType(const std::string& type, std::string& matched); static bool getTemplateParams( - drgn_type *type, size_t numTemplateParams, - std::vector> &v); + drgn_type* type, size_t numTemplateParams, + std::vector>& v); - bool enumerateTemplateParamIdxs(drgn_type *type, - const ContainerInfo &containerInfo, - const std::vector ¶mIdxs, - bool &ifStub); - bool getContainerTemplateParams(drgn_type *type, bool &ifStub); - void enumerateDescendants(drgn_type *type, drgn_type *baseType); - void getFuncDefinitionStr(std::string &code, drgn_type *type, - const std::string &typeName); - std::optional getDrgnTypeSize(drgn_type *type); + bool enumerateTemplateParamIdxs(drgn_type* type, + const ContainerInfo& containerInfo, + const std::vector& paramIdxs, + bool& ifStub); + bool getContainerTemplateParams(drgn_type* type, bool& ifStub); + void enumerateDescendants(drgn_type* type, drgn_type* baseType); + void getFuncDefinitionStr(std::string& code, drgn_type* type, + const std::string& typeName); + std::optional getDrgnTypeSize(drgn_type* type); - std::optional getNameForType(drgn_type *type); + std::optional getNameForType(drgn_type* type); - static std::string preProcessUniquePtr(drgn_type *type, std::string name); - std::string transformTypeName(drgn_type *type, std::string &text); - static std::string templateTransformType(const std::string &typeName); - static std::string structNameTransformType(const std::string &typeName); + static std::string preProcessUniquePtr(drgn_type* type, std::string name); + std::string transformTypeName(drgn_type* type, std::string& text); + static std::string templateTransformType(const std::string& typeName); + static std::string structNameTransformType(const std::string& typeName); - bool addPadding(uint64_t padding_bits, std::string &code); + bool addPadding(uint64_t padding_bits, std::string& code); static void deduplicateMemberName( - std::unordered_map &memberNames, - std::string &memberName); + std::unordered_map& memberNames, + std::string& memberName); std::optional generateMember( - const DrgnClassMemberInfo &m, - std::unordered_map &memberNames, - uint64_t currOffsetBits, std::string &code, bool isInUnion); - bool generateParent(drgn_type *p, - std::unordered_map &memberNames, - uint64_t &currOffsetBits, std::string &code, + const DrgnClassMemberInfo& m, + std::unordered_map& memberNames, + uint64_t currOffsetBits, std::string& code, bool isInUnion); + bool generateParent(drgn_type* p, + std::unordered_map& memberNames, + uint64_t& currOffsetBits, std::string& code, size_t offsetToNextMember); - std::optional getAlignmentRequirements(drgn_type *e); - bool generateStructMembers(drgn_type *e, - std::unordered_map &memberNames, - std::string &code, uint64_t &out_offset_bits, - PaddingInfo &paddingInfo, - bool &violatesAlignmentRequirement, + std::optional getAlignmentRequirements(drgn_type* e); + bool generateStructMembers(drgn_type* e, + std::unordered_map& memberNames, + std::string& code, uint64_t& out_offset_bits, + PaddingInfo& paddingInfo, + bool& violatesAlignmentRequirement, size_t offsetToNextMember); - void getFuncDefClassMembers(std::string &code, drgn_type *type, - std::unordered_map &memberNames, + void getFuncDefClassMembers(std::string& code, drgn_type* type, + std::unordered_map& memberNames, bool skipPadding = false); - bool isDrgnSizeComplete(drgn_type *type); + bool isDrgnSizeComplete(drgn_type* type); - static bool getEnumUnderlyingTypeStr(drgn_type *e, - std::string &enumUnderlyingTypeStr); + static bool getEnumUnderlyingTypeStr(drgn_type* e, + std::string& enumUnderlyingTypeStr); - bool ifEnumerateClass(const std::string &typeName); + bool ifEnumerateClass(const std::string& typeName); - bool enumerateClassParents(drgn_type *type, const std::string &typeName); - bool enumerateClassMembers(drgn_type *type, const std::string &typeName, - bool &isStubbed); - bool enumerateClassTemplateParams(drgn_type *type, - const std::string &typeName, - bool &isStubbed); - bool ifGenerateMemberDefinition(const std::string &typeName); - bool generateMemberDefinition(drgn_type *type, std::string &typeName); + bool enumerateClassParents(drgn_type* type, const std::string& typeName); + bool enumerateClassMembers(drgn_type* type, const std::string& typeName, + bool& isStubbed); + bool enumerateClassTemplateParams(drgn_type* type, + const std::string& typeName, + bool& isStubbed); + bool ifGenerateMemberDefinition(const std::string& typeName); + bool generateMemberDefinition(drgn_type* type, std::string& typeName); std::optional> isMemberToStub( - const std::string &type, const std::string &member); - std::optional isTypeToStub(const std::string &typeName); - bool isTypeToStub(drgn_type *type, const std::string &typeName); - bool isEmptyClassOrFunctionType(drgn_type *type, const std::string &typeName); - bool enumerateClassType(drgn_type *type); - bool enumerateTypeDefType(drgn_type *type); - bool enumerateEnumType(drgn_type *type); - bool enumeratePointerType(drgn_type *type); - bool enumeratePrimitiveType(drgn_type *type); - bool enumerateArrayType(drgn_type *type); + const std::string& type, const std::string& member); + std::optional isTypeToStub(const std::string& typeName); + bool isTypeToStub(drgn_type* type, const std::string& typeName); + bool isEmptyClassOrFunctionType(drgn_type* type, const std::string& typeName); + bool enumerateClassType(drgn_type* type); + bool enumerateTypeDefType(drgn_type* type); + bool enumerateEnumType(drgn_type* type); + bool enumeratePointerType(drgn_type* type); + bool enumeratePrimitiveType(drgn_type* type); + bool enumerateArrayType(drgn_type* type); - bool isUnnamedStruct(drgn_type *type); + bool isUnnamedStruct(drgn_type* type); - std::string getAnonName(drgn_type *, const char *); - std::string getStructName(drgn_type *type) { + std::string getAnonName(drgn_type*, const char*); + std::string getStructName(drgn_type* type) { return getAnonName(type, "__anon_struct_"); } - std::string getUnionName(drgn_type *type) { + std::string getUnionName(drgn_type* type) { return getAnonName(type, "__anon_union_"); } - static void declareThriftStruct(std::string &code, std::string_view name); + static void declareThriftStruct(std::string& code, std::string_view name); - bool isNumMemberGreaterThanZero(drgn_type *type); - void getClassMembersIncludingParent(drgn_type *type, - std::vector &out); + bool isNumMemberGreaterThanZero(drgn_type* type); + void getClassMembersIncludingParent(drgn_type* type, + std::vector& out); bool staticAssertMemberOffsets( - const std::string &struct_name, drgn_type *struct_type, - std::string &assert_str, - std::unordered_map &member_names, + const std::string& struct_name, drgn_type* struct_type, + std::string& assert_str, + std::unordered_map& member_names, uint64_t base_offset = 0); - bool addStaticAssertsForType(drgn_type *type, bool generateAssertsForOffsets, - std::string &code); - bool buildNameInt(drgn_type *type, std::string &nameWithoutTemplate, - std::string &outName); + bool addStaticAssertsForType(drgn_type* type, bool generateAssertsForOffsets, + std::string& code); + bool buildNameInt(drgn_type* type, std::string& nameWithoutTemplate, + std::string& outName); void replaceTemplateOperator( - std::vector> &template_params, - std::vector &template_params_strings, size_t index); + std::vector>& template_params, + std::vector& template_params_strings, size_t index); void replaceTemplateParameters( - drgn_type *type, - std::vector> &template_params, - std::vector &template_params_strings, - const std::string &nameWithoutTemplate); + drgn_type* type, + std::vector>& template_params, + std::vector& template_params_strings, + const std::string& nameWithoutTemplate); }; diff --git a/src/OICompiler.cpp b/src/OICompiler.cpp index a775ec3..644ee91 100644 --- a/src/OICompiler.cpp +++ b/src/OICompiler.cpp @@ -52,10 +52,10 @@ using namespace llvm; using namespace llvm::object; using namespace ObjectIntrospection; -static const char *symbolLookupCallback( - [[maybe_unused]] void *disInfo, [[maybe_unused]] uint64_t referenceValue, - uint64_t *referenceType, [[maybe_unused]] uint64_t referencePC, - [[maybe_unused]] const char **referenceName) { +static const char* symbolLookupCallback( + [[maybe_unused]] void* disInfo, [[maybe_unused]] uint64_t referenceValue, + uint64_t* referenceType, [[maybe_unused]] uint64_t referencePC, + [[maybe_unused]] const char** referenceName) { *referenceType = LLVMDisassembler_ReferenceType_InOut_None; return nullptr; } @@ -101,7 +101,7 @@ OICompiler::Disassembler::operator()() { } size_t instSize = LLVMDisasmInstruction( - disassemblerContext, const_cast(std::data(funcText)), + disassemblerContext, const_cast(std::data(funcText)), std::size(funcText), 0, std::data(disassemblyBuffer), std::size(disassemblyBuffer)); if (instSize == 0) { @@ -192,8 +192,8 @@ class OIMemoryManager : public RTDyldMemoryManager { uintptr_t dataSegBase = 0; const uintptr_t dataSegLimit = 0; - uint8_t *allocate(uintptr_t Size, unsigned Alignment, bool isCode) { - auto *allocOffset = isCode ? &textSegBase : &dataSegBase; + uint8_t* allocate(uintptr_t Size, unsigned Alignment, bool isCode) { + auto* allocOffset = isCode ? &textSegBase : &dataSegBase; auto allocLimit = isCode ? textSegLimit : dataSegLimit; VLOG(1) << "allocateFromSlab " << (isCode ? "Code " : "Data ") << " Size " @@ -211,19 +211,19 @@ class OIMemoryManager : public RTDyldMemoryManager { report_fatal_error("Can't allocate enough memory from slab"); } - auto §ions = isCode ? functionSections : dataSections; - sections.emplace_back((void *)allocAddr, Size); + auto& sections = isCode ? functionSections : dataSections; + sections.emplace_back((void*)allocAddr, Size); *allocOffset = newAllocOffset; VLOG(1) << "allocateFromSlab return: " << std::hex << allocAddr; - return (uint8_t *)allocAddr; + return (uint8_t*)allocAddr; } }; SmallVector Slabs{}; OIMemoryManager(std::shared_ptr ss, - const std::unordered_map &synths) + const std::unordered_map& synths) : RTDyldMemoryManager{}, symbols{std::move(ss)}, syntheticSymbols{synths} { @@ -236,33 +236,33 @@ class OIMemoryManager : public RTDyldMemoryManager { void reserveAllocationSpace(uintptr_t, uint32_t, uintptr_t, uint32_t, uintptr_t, uint32_t) override; - uint8_t *allocateCodeSection(uintptr_t, unsigned, unsigned, + uint8_t* allocateCodeSection(uintptr_t, unsigned, unsigned, StringRef) override; - uint8_t *allocateDataSection(uintptr_t, unsigned, unsigned, StringRef, + uint8_t* allocateDataSection(uintptr_t, unsigned, unsigned, StringRef, bool) override; /* Hook to set up proper memory permission. We don't handle that */ - bool finalizeMemory(std::string *) override { + bool finalizeMemory(std::string*) override { return false; } /* Hook to locate symbols in the remote process */ - JITSymbol findSymbol(const std::string &) override; + JITSymbol findSymbol(const std::string&) override; /* * We don't use EH frames in this context, as we generate then copy to another * process, and enabling them causes issues with folly crashing on oid exit. */ - void registerEHFrames(uint8_t *, uint64_t, size_t) override { + void registerEHFrames(uint8_t*, uint64_t, size_t) override { } void deregisterEHFrames() override { } private: std::shared_ptr symbols; - const std::unordered_map &syntheticSymbols; + const std::unordered_map& syntheticSymbols; - Slab ¤tSlab() { + Slab& currentSlab() { assert(!Slabs.empty()); return Slabs.back(); } @@ -285,7 +285,7 @@ void OIMemoryManager::reserveAllocationSpace( Slabs.emplace_back(totalSz, codeSize, roDataSize + rwDataSize + 128); - const auto &currSlab = currentSlab(); + const auto& currSlab = currentSlab(); VLOG(1) << "reserveAllocationSpace: " << std::hex << "SlabBase " << currSlab.memBlock.base() << " textSegBaseAlloc " << currSlab.textSegBase << " textSegLimit " << currSlab.textSegLimit @@ -293,7 +293,7 @@ void OIMemoryManager::reserveAllocationSpace( << currSlab.dataSegLimit; } -uint8_t *OIMemoryManager::allocateCodeSection( +uint8_t* OIMemoryManager::allocateCodeSection( uintptr_t size, unsigned alignment, [[maybe_unused]] unsigned sectionID, StringRef sectionName) { VLOG(1) << "allocateCodeSection(Size = " << size @@ -303,7 +303,7 @@ uint8_t *OIMemoryManager::allocateCodeSection( return currentSlab().allocate(size, alignment, true /* isCode */); } -uint8_t *OIMemoryManager::allocateDataSection( +uint8_t* OIMemoryManager::allocateDataSection( uintptr_t size, unsigned alignment, [[maybe_unused]] unsigned sectionID, StringRef sectionName, [[maybe_unused]] bool isReadOnly) { VLOG(1) << "allocateDataSection(Size = " << size @@ -321,7 +321,7 @@ uint8_t *OIMemoryManager::allocateDataSection( * We can't rely on LLVM to do this job because we are resolving symbols of a * remote process. LLVM only handles resolving symbols for the current process. */ -JITSymbol OIMemoryManager::findSymbol(const std::string &name) { +JITSymbol OIMemoryManager::findSymbol(const std::string& name) { if (auto synth = syntheticSymbols.find(name); synth != end(syntheticSymbols)) { VLOG(1) << "findSymbol(" << name << ") = Synth " << std::hex @@ -365,8 +365,8 @@ JITSymbol OIMemoryManager::findSymbol(const std::string &name) { } std::optional OICompiler::decodeInst( - const std::vector &funcText, uintptr_t offset) { - auto disassembler = Disassembler((const uint8_t *)funcText.data() + offset, + const std::vector& funcText, uintptr_t offset) { + auto disassembler = Disassembler((const uint8_t*)funcText.data() + offset, funcText.size() - offset); auto inst = disassembler(); @@ -398,17 +398,17 @@ OICompiler::~OICompiler() = default; static constexpr size_t kMaxInterFuncInstrPadding = 16; static void debugDisAsm( - const SmallVector &Slabs, - const OICompiler::RelocResult::RelocInfos &ObjectRelocInfos) { + const SmallVector& Slabs, + const OICompiler::RelocResult::RelocInfos& ObjectRelocInfos) { VLOG(1) << "\nDisassembled Code"; /* Outer loop on each Object files that has been loaded */ assert(Slabs.size() == ObjectRelocInfos.size()); - for (const auto &S : boost::combine(Slabs, ObjectRelocInfos)) { - const auto &[ObjFile, ObjRelInfo] = std::tie(S.get<0>(), S.get<1>()); + for (const auto& S : boost::combine(Slabs, ObjectRelocInfos)) { + const auto& [ObjFile, ObjRelInfo] = std::tie(S.get<0>(), S.get<1>()); /* Inner loop on each Function Section of a given Object file */ - for (const auto &textSec : ObjFile.functionSections) { + for (const auto& textSec : ObjFile.functionSections) { const auto offset = (uintptr_t)textSec.base() - (uintptr_t)ObjFile.memBlock.base(); const auto baseRelocAddress = ObjRelInfo.RelocAddr + offset; @@ -416,7 +416,7 @@ static void debugDisAsm( size_t instrCnt = 0; size_t byteCnt = 0; size_t consNop = 0; - auto dg = OICompiler::Disassembler((uint8_t *)textSec.base(), + auto dg = OICompiler::Disassembler((uint8_t*)textSec.base(), textSec.allocatedSize()); while (auto inst = dg()) { instrCnt++; @@ -451,8 +451,8 @@ static void debugDisAsm( } } -bool OICompiler::compile(const std::string &code, const fs::path &sourcePath, - const fs::path &objectPath) { +bool OICompiler::compile(const std::string& code, const fs::path& sourcePath, + const fs::path& objectPath) { Metrics::Tracing _("compile"); /* @@ -488,15 +488,15 @@ bool OICompiler::compile(const std::string &code, const fs::path &sourcePath, compInv->getFrontendOpts().OutputFile = objectPath.string(); compInv->getFrontendOpts().ProgramAction = clang::frontend::EmitObj; - auto &headerSearchOptions = compInv->getHeaderSearchOpts(); + auto& headerSearchOptions = compInv->getHeaderSearchOpts(); - for (const auto &path : config.userHeaderPaths) { + for (const auto& path : config.userHeaderPaths) { headerSearchOptions.AddPath( path.c_str(), clang::frontend::IncludeDirGroup::IndexHeaderMap, false, false); } - for (const auto &path : config.sysHeaderPaths) { + for (const auto& path : config.sysHeaderPaths) { headerSearchOptions.AddPath( path.c_str(), clang::frontend::IncludeDirGroup::System, false, false); } @@ -554,15 +554,15 @@ bool OICompiler::compile(const std::string &code, const fs::path &sourcePath, } std::optional OICompiler::applyRelocs( - uintptr_t baseRelocAddress, const std::set &objectFiles, - const std::unordered_map &syntheticSymbols) { + uintptr_t baseRelocAddress, const std::set& objectFiles, + const std::unordered_map& syntheticSymbols) { Metrics::Tracing relocationTracing("relocation"); memMgr = std::make_unique(symbols, syntheticSymbols); RuntimeDyld dyld(*memMgr, *memMgr); /* Load all the object files into the MemoryManager */ - for (const auto &objPath : objectFiles) { + for (const auto& objPath : objectFiles) { VLOG(1) << "Loading object file " << objPath; auto objFile = ObjectFile::createObjectFile(objPath.c_str()); if (!objFile) { @@ -583,8 +583,8 @@ std::optional OICompiler::applyRelocs( /* Provides mapping addresses to the MemoryManager */ uintptr_t currentRelocAddress = baseRelocAddress; - for (const auto &slab : memMgr->Slabs) { - for (const auto &funcSection : slab.functionSections) { + for (const auto& slab : memMgr->Slabs) { + for (const auto& funcSection : slab.functionSections) { auto offset = (uintptr_t)funcSection.base() - (uintptr_t)slab.memBlock.base(); dyld.mapSectionAddress(funcSection.base(), currentRelocAddress + offset); @@ -593,7 +593,7 @@ std::optional OICompiler::applyRelocs( << currentRelocAddress + offset; } - for (const auto &dataSection : slab.dataSections) { + for (const auto& dataSection : slab.dataSections) { auto offset = (uintptr_t)dataSection.base() - (uintptr_t)slab.memBlock.base(); dyld.mapSectionAddress(dataSection.base(), currentRelocAddress + offset); @@ -622,7 +622,7 @@ std::optional OICompiler::applyRelocs( /* Copy symbol table into `res` */ auto symbolTable = dyld.getSymbolTable(); res.symbols.reserve(symbolTable.size()); - for (const auto &[symName, sym] : symbolTable) { + for (const auto& [symName, sym] : symbolTable) { res.symbols.emplace(symName.str(), sym.getAddress()); } diff --git a/src/OICompiler.h b/src/OICompiler.h index 3fde68d..bc23456 100644 --- a/src/OICompiler.h +++ b/src/OICompiler.h @@ -110,7 +110,7 @@ class OICompiler { * Create a disassembler from anything that resemble a std::span. */ template - Disassembler(Args &&...args) : funcText(std::forward(args)...) { + Disassembler(Args&&... args) : funcText(std::forward(args)...) { } /* @@ -137,7 +137,7 @@ class OICompiler { * * @return true if the compilation succeeded, false otherwise. */ - bool compile(const std::string &, const fs::path &, const fs::path &); + bool compile(const std::string&, const fs::path&, const fs::path&); /** * Load the @param objectFiles in memory and apply relocation at @@ -157,8 +157,8 @@ class OICompiler { * another call. */ std::optional applyRelocs( - uintptr_t, const std::set &, - const std::unordered_map &); + uintptr_t, const std::set&, + const std::unordered_map&); /** * Locates all the offsets of the given @param insts opcodes @@ -173,13 +173,13 @@ class OICompiler { */ template static std::optional> locateOpcodes( - const FuncTextRange &funcText, const NeedlesRange &needles); + const FuncTextRange& funcText, const NeedlesRange& needles); /** * @return a string representation of the opcode(s) of the instruction found * at @param offset within function's binary instructions @param funcText. */ - static std::optional decodeInst(const std::vector &, + static std::optional decodeInst(const std::vector&, uintptr_t); private: @@ -200,13 +200,13 @@ class OICompiler { template std::optional> OICompiler::locateOpcodes( - const FuncTextRange &funcText, const NeedlesRange &needles) { - auto DG = Disassembler((uint8_t *)std::data(funcText), std::size(funcText)); + const FuncTextRange& funcText, const NeedlesRange& needles) { + auto DG = Disassembler((uint8_t*)std::data(funcText), std::size(funcText)); std::vector locs; while (auto inst = DG()) { auto it = std::find_if( - std::begin(needles), std::end(needles), [&](const auto &needle) { + std::begin(needles), std::end(needles), [&](const auto& needle) { // Inst->opcodes.starts_with(needle); return 0 == inst->opcodes.find(OICompiler::Disassembler::Span( diff --git a/src/OID.cpp b/src/OID.cpp index bf1e140..0dd4b9a 100644 --- a/src/OID.cpp +++ b/src/OID.cpp @@ -206,9 +206,9 @@ void installSigHandlers(void) { sigaction(SIGALRM, &nact, nullptr); } -std::optional strunittol(const char *str) { +std::optional strunittol(const char* str) { errno = 0; - char *strend = nullptr; + char* strend = nullptr; long retval = strtol(str, &strend, 10); if (errno != 0) { return std::nullopt; @@ -266,11 +266,11 @@ struct Config { } // namespace Oid -static ExitStatus::ExitStatus runScript(const std::string &fileName, - std::istream &script, - const Oid::Config &oidConfig, - const OICodeGen::Config &codeGenConfig, - const TreeBuilder::Config &tbConfig) { +static ExitStatus::ExitStatus runScript(const std::string& fileName, + std::istream& script, + const Oid::Config& oidConfig, + const OICodeGen::Config& codeGenConfig, + const TreeBuilder::Config& tbConfig) { if (!fileName.empty()) { VLOG(1) << "SCR FILE: " << fileName; } @@ -452,7 +452,7 @@ static ExitStatus::ExitStatus runScript(const std::string &fileName, return ExitStatus::Success; } -int main(int argc, char *argv[]) { +int main(int argc, char* argv[]) { int debugLevel = 1; Oid::Config oidConfig = {}; std::string scriptFile; diff --git a/src/OIDebugger.cpp b/src/OIDebugger.cpp index 61de9f3..e9593e8 100644 --- a/src/OIDebugger.cpp +++ b/src/OIDebugger.cpp @@ -60,10 +60,10 @@ using namespace ObjectIntrospection; bool OIDebugger::isGlobalDataProbeEnabled(void) const { return std::any_of(cbegin(pdata), cend(pdata), - [](const auto &r) { return r.type == "global"; }); + [](const auto& r) { return r.type == "global"; }); } -bool OIDebugger::parseScript(std::istream &script) { +bool OIDebugger::parseScript(std::istream& script) { Metrics::Tracing _("parse_script"); OIScanner scanner(&script); @@ -92,7 +92,7 @@ bool OIDebugger::patchFunctions(void) { assert(pdata.numReqs() != 0); Metrics::Tracing _("patch_functions"); - for (const auto &preq : pdata) { + for (const auto& preq : pdata) { VLOG(1) << "Type " << preq.type << " Func " << preq.func << " Args: " << boost::join(preq.args, ","); @@ -117,7 +117,7 @@ bool OIDebugger::patchFunctions(void) { * Single step an instruction in the target process 'pid' and leave the target * thread stopped. Returns the current rip. */ -uint64_t OIDebugger::singlestepInst(pid_t pid, struct user_regs_struct ®s) { +uint64_t OIDebugger::singlestepInst(pid_t pid, struct user_regs_struct& regs) { int status = 0; Metrics::Tracing _("single_step_inst"); @@ -143,8 +143,8 @@ uint64_t OIDebugger::singlestepInst(pid_t pid, struct user_regs_struct ®s) { return regs.rip; } -void OIDebugger::dumpRegs(const char *text, pid_t pid, - struct user_regs_struct *regs) { +void OIDebugger::dumpRegs(const char* text, pid_t pid, + struct user_regs_struct* regs) { VLOG(1) << "(" << text << ")" << " dumpRegs: pid: " << std::dec << pid << std::hex << " rip " << regs->rip << " rbp: " << regs->rbp << " rsp " << regs->rsp @@ -204,8 +204,8 @@ bool OIDebugger::setupLogFile(void) { * Using the text segment to store the path in the remote process' memory. * The memory will be re-used anyway and the path will get overwritten. */ - if (!writeTargetMemory((void *)logFilePath.c_str(), - (void *)segConfig.textSegBase, logFilePathLen)) { + if (!writeTargetMemory((void*)logFilePath.c_str(), + (void*)segConfig.textSegBase, logFilePathLen)) { LOG(ERROR) << "Failed to write Log File's path into target process"; return false; } @@ -267,7 +267,7 @@ bool OIDebugger::segmentInit(void) { segConfig.existingConfig = true; segmentConfigFile.seekg(0); - segmentConfigFile.write((char *)&segConfig, sizeof(segConfig)); + segmentConfigFile.write((char*)&segConfig, sizeof(segConfig)); VLOG(1) << "segConfig size " << sizeof(segConfig); @@ -318,7 +318,7 @@ void OIDebugger::createSegmentConfigFile(void) { /* Read config */ segmentConfigFile = std::fstream(segConfigFilePath, ios::in | ios::out | ios::binary); - segmentConfigFile.read((char *)&segConfig, sizeof(c)); + segmentConfigFile.read((char*)&segConfig, sizeof(c)); if (segmentConfigFile.fail()) { LOG(ERROR) << "createSegmentConfigFile: failed to read from " @@ -423,7 +423,7 @@ OIDebugger::StatusType OIDebugger::getTaskState(pid_t pid) { /* For debug - do not remove */ void OIDebugger::dumpAlltaskStates(void) { VLOG(1) << "Task State Dump"; - for (auto const &p : threadList) { + for (auto const& p : threadList) { auto state = getTaskState(p); VLOG(1) << "Task " << p << " state: " << taskStateToString(state) << " (" << static_cast(state) << ")"; @@ -457,9 +457,9 @@ bool OIDebugger::contTargetThread(bool detach) const { return true; } -bool OIDebugger::replayTrappedInstr(const trapInfo &t, pid_t pid, - struct user_regs_struct ®s, - struct user_fpregs_struct &fpregs) const { +bool OIDebugger::replayTrappedInstr(const trapInfo& t, pid_t pid, + struct user_regs_struct& regs, + struct user_fpregs_struct& fpregs) const { /* * Single step the original instruction which has been patched over * with a breakpoint trap. The original instruction now resides in @@ -524,13 +524,13 @@ bool OIDebugger::replayTrappedInstr(const trapInfo &t, pid_t pid, return true; } -bool OIDebugger::locateObjectsAddresses(const trapInfo &tInfo, - struct user_regs_struct ®s) { +bool OIDebugger::locateObjectsAddresses(const trapInfo& tInfo, + struct user_regs_struct& regs) { /* * Write objects into prologue in target. */ bool ret = true; - for (const auto &arg : tInfo.args) { + for (const auto& arg : tInfo.args) { auto remoteObjAddr = remoteObjAddrs.find(arg); if (remoteObjAddr == remoteObjAddrs.end()) { LOG(ERROR) << "Entry: failed to find remoteObjAddr! Skipping..."; @@ -546,8 +546,8 @@ bool OIDebugger::locateObjectsAddresses(const trapInfo &tInfo, } VLOG(4) << "Entry: arg addr: " << std::hex << *addr; - if (!writeTargetMemory((void *)(&addr.value()), - (void *)remoteObjAddr->second, sizeof(*addr))) { + if (!writeTargetMemory((void*)(&addr.value()), (void*)remoteObjAddr->second, + sizeof(*addr))) { LOG(ERROR) << "Entry: writeTargetMemory remoteObjAddr failed!"; ret = false; continue; @@ -558,8 +558,8 @@ bool OIDebugger::locateObjectsAddresses(const trapInfo &tInfo, } OIDebugger::processTrapRet OIDebugger::processFuncTrap( - const trapInfo &tInfo, pid_t pid, struct user_regs_struct ®s, - struct user_fpregs_struct &fpregs) { + const trapInfo& tInfo, pid_t pid, struct user_regs_struct& regs, + struct user_fpregs_struct& fpregs) { assert(tInfo.trapKind != OID_TRAP_JITCODERET); processTrapRet ret = OID_CONT; @@ -570,10 +570,10 @@ OIDebugger::processTrapRet OIDebugger::processFuncTrap( auto t = std::make_shared(tInfo); /* Save interrupted registers into trap information */ - memcpy((void *)&t->savedRegs, (void *)®s, sizeof(t->savedRegs)); + memcpy((void*)&t->savedRegs, (void*)®s, sizeof(t->savedRegs)); /* Save fpregs into trap information */ - memcpy((void *)&t->savedFPregs, (void *)&fpregs, sizeof(t->savedFPregs)); + memcpy((void*)&t->savedFPregs, (void*)&fpregs, sizeof(t->savedFPregs)); /* Start by locating each Target Object's address */ if (!locateObjectsAddresses(*t, regs)) { @@ -664,7 +664,7 @@ OIDebugger::processTrapRet OIDebugger::processFuncTrap( t->fromVect = true; VLOG(4) << "processTrap: redirect pid " << std::dec << pid << " to address " - << std::hex << (void *)tInfo.prologueObjAddr << " tInfo: " << tInfo + << std::hex << (void*)tInfo.prologueObjAddr << " tInfo: " << tInfo << " " << tInfo.prologueObjAddr << " " << tInfo.fromVect; errno = 0; @@ -689,7 +689,7 @@ OIDebugger::processTrapRet OIDebugger::processFuncTrap( } OIDebugger::processTrapRet OIDebugger::processJitCodeRet( - const trapInfo &tInfo __attribute__((unused)), pid_t pid) { + const trapInfo& tInfo __attribute__((unused)), pid_t pid) { OIDebugger::processTrapRet ret = OIDebugger::OID_CONT; assert(tInfo.trapKind == OID_TRAP_JITCODERET); @@ -784,7 +784,7 @@ OIDebugger::processTrapRet OIDebugger::processJitCodeRet( * in this case) and introspect the global data. It would be good if we had * a cheap way of asserting that the global thread is stopped. */ -bool OIDebugger::processGlobal(const std::string &varName) { +bool OIDebugger::processGlobal(const std::string& varName) { assert(mode == OID_MODE_THREAD); VLOG(1) << "Introspecting global variable: " << varName; @@ -827,10 +827,10 @@ bool OIDebugger::processGlobal(const std::string &varName) { regs.rip -= 2; /* Save interrupted registers into trap information */ - memcpy((void *)&t->savedRegs, (void *)®s, sizeof(t->savedRegs)); + memcpy((void*)&t->savedRegs, (void*)®s, sizeof(t->savedRegs)); /* Save fpregs into trap information */ - memcpy((void *)&t->savedFPregs, (void *)&fpregs, sizeof(t->savedFPregs)); + memcpy((void*)&t->savedFPregs, (void*)&fpregs, sizeof(t->savedFPregs)); regs.rip = segConfig.textSegBase; dumpRegs("processGlobal2", traceePid, ®s); @@ -858,7 +858,7 @@ bool OIDebugger::processGlobal(const std::string &varName) { return false; } - if (!writeTargetMemory((void *)&addr, (void *)remoteObjAddr->second, + if (!writeTargetMemory((void*)&addr, (void*)remoteObjAddr->second, sizeof(addr))) { LOG(ERROR) << "processGlobal: writeTargetMemory remoteObjAddr failed!"; } @@ -1241,14 +1241,14 @@ OIDebugger::processTrapRet OIDebugger::processTrap(pid_t pid, bool blocking, return ret; } -std::optional> OIDebugger::findRetLocs(FuncDesc &fd) { +std::optional> OIDebugger::findRetLocs(FuncDesc& fd) { size_t maxSize = std::accumulate( fd.ranges.begin(), fd.ranges.end(), size_t(0), - [](auto currMax, auto &r) { return std::max(currMax, r.size()); }); + [](auto currMax, auto& r) { return std::max(currMax, r.size()); }); std::vector retLocs; std::vector text(maxSize); - for (const auto &range : fd.ranges) { + for (const auto& range : fd.ranges) { /* * We already have enough capacity to accomodate any range from the function * But we must ensure the actual `size` of the vector matches what is being @@ -1260,7 +1260,7 @@ std::optional> OIDebugger::findRetLocs(FuncDesc &fd) { text.resize(range.size()); /* Copy the range of instruction into the text vector to be disassembled */ - if (!readTargetMemory((void *)range.start, text.data(), text.size())) { + if (!readTargetMemory((void*)range.start, text.data(), text.size())) { LOG(ERROR) << "Could not read function range " << fd.symName << "@" << range; return std::nullopt; @@ -1310,10 +1310,10 @@ std::optional> OIDebugger::findRetLocs(FuncDesc &fd) { * If it's not in the replayInstMap, return the address to the next free entry * in the cache and put the entry in the map. */ -std::optional OIDebugger::nextReplayInstrAddr(const trapInfo &t) { +std::optional OIDebugger::nextReplayInstrAddr(const trapInfo& t) { if (auto it = replayInstMap.find(t.trapAddr); it != end(replayInstMap)) { - VLOG(1) << "Found instruction for trap " << (void *)t.trapAddr - << " at address " << (void *)it->second; + VLOG(1) << "Found instruction for trap " << (void*)t.trapAddr + << " at address " << (void*)it->second; return it->second; } @@ -1330,8 +1330,8 @@ std::optional OIDebugger::nextReplayInstrAddr(const trapInfo &t) { return std::nullopt; } - VLOG(1) << "Orig instructions for trap " << (void *)t.trapAddr - << " will get saved at " << (void *)newInstrAddr; + VLOG(1) << "Orig instructions for trap " << (void*)t.trapAddr + << " will get saved at " << (void*)newInstrAddr; replayInstMap.emplace(t.trapAddr, newInstrAddr); return newInstrAddr; @@ -1374,7 +1374,7 @@ std::optional OIDebugger::nextReplayInstrAddr(const trapInfo &t) { * instrumentation much be done as a single unit. */ -bool OIDebugger::functionPatch(const prequest &req) { +bool OIDebugger::functionPatch(const prequest& req) { assert(req.type != "global"); auto fd = symbols->findFuncDesc(req.getReqForArg(0)); @@ -1398,15 +1398,15 @@ bool OIDebugger::functionPatch(const prequest &req) { /* 1. Locate all TRAP points and create a corresponding empty trapInfo in * tiVec */ bool hasArg = std::any_of(begin(req.args), end(req.args), - [](auto &arg) { return arg != "retval"; }); + [](auto& arg) { return arg != "retval"; }); if (req.type == "entry" || hasArg) { trapType tType = req.type == "return" ? OID_TRAP_VECT_ENTRYRET : OID_TRAP_VECT_ENTRY; uintptr_t trapAddr = fd->ranges[0].start; if (req.args[0].starts_with("arg") || req.args[0] == "this") { - auto *argument = - dynamic_cast(fd->getArgument(req.args[0]).get()); + auto* argument = + dynamic_cast(fd->getArgument(req.args[0]).get()); if (argument->locator.locations_size > 0) { /* * The `std::max` is necessary because sometimes when a binary is @@ -1440,9 +1440,9 @@ bool OIDebugger::functionPatch(const prequest &req) { localIov.reserve(tiVec.size()); remoteIov.reserve(tiVec.size()); - for (auto &ti : tiVec) { - localIov.push_back({(void *)ti->origTextBytes, sizeof(ti->origTextBytes)}); - remoteIov.push_back({(void *)ti->trapAddr, sizeof(ti->origTextBytes)}); + for (auto& ti : tiVec) { + localIov.push_back({(void*)ti->origTextBytes, sizeof(ti->origTextBytes)}); + remoteIov.push_back({(void*)ti->trapAddr, sizeof(ti->origTextBytes)}); } errno = 0; @@ -1463,7 +1463,7 @@ bool OIDebugger::functionPatch(const prequest &req) { /* Re-use remoteIov to write the original instructions in our textSegment */ remoteIov.clear(); - for (auto &trap : tiVec) { + for (auto& trap : tiVec) { trap->patchedText = trap->origText; trap->patchedTextBytes[0] = int3Inst; @@ -1476,13 +1476,13 @@ bool OIDebugger::functionPatch(const prequest &req) { trap->replayInstAddr = *replayInstrAddr; remoteIov.push_back( - {(void *)trap->replayInstAddr, sizeof(trap->origTextBytes)}); + {(void*)trap->replayInstAddr, sizeof(trap->origTextBytes)}); if (trap->trapKind == OID_TRAP_VECT_ENTRY || trap->trapKind == OID_TRAP_VECT_ENTRYRET) { /* Capture the arguments to probe */ trap->args.reserve(req.args.size()); - for (const auto &arg : req.args) { + for (const auto& arg : req.args) { if (auto targetObj = fd->getArgument(arg)) { trap->args.push_back(std::move(targetObj)); } else { @@ -1517,9 +1517,9 @@ bool OIDebugger::functionPatch(const prequest &req) { } /* 5. Insert the traps in the target process */ - for (const auto &trap : tiVec) { + for (const auto& trap : tiVec) { VLOG(1) << "Patching function " << req.func << " @" - << (void *)trap->trapAddr; + << (void*)trap->trapAddr; activeTraps.emplace(trap->trapAddr, trap); errno = 0; @@ -1551,7 +1551,7 @@ std::optional OIDebugger::remoteSyscall(Args... _args) { } uint64_t patchAddr = sym->addr; - VLOG(1) << "Address of main: " << (void *)patchAddr; + VLOG(1) << "Address of main: " << (void*)patchAddr; /* Saving current registers states */ errno = 0; @@ -1600,7 +1600,7 @@ std::optional OIDebugger::remoteSyscall(Args... _args) { * arch/ABI arg1 arg2 arg3 arg4 arg5 arg6 arg7 * x86-64 rdi rsi rdx r10 r8 r9 - */ - const std::array argToReg = { + const std::array argToReg = { &newregs.rdi, &newregs.rsi, &newregs.rdx, &newregs.r10, &newregs.r8, &newregs.r9, }; @@ -1692,7 +1692,7 @@ std::optional OIDebugger::remoteSyscall(Args... _args) { bool OIDebugger::setupSegment(SegType seg) { Metrics::Tracing _("setup_segment"); - std::optional segAddr; + std::optional segAddr; if (seg == SegType::text) { segAddr = remoteSyscall(nullptr, textSegSize, // addr & size @@ -1796,7 +1796,7 @@ bool OIDebugger::removeTraps(pid_t pid) { } for (auto it = activeTraps.begin(); it != activeTraps.end();) { - const auto &tInfo = it->second; + const auto& tInfo = it->second; /* We don't care about our own traps */ if (tInfo->trapKind == OID_TRAP_JITCODERET) { @@ -1833,7 +1833,7 @@ bool OIDebugger::removeTraps(pid_t pid) { return true; } -bool OIDebugger::removeTrap(pid_t pid, const trapInfo &t) { +bool OIDebugger::removeTrap(pid_t pid, const trapInfo& t) { std::array repatchedBytes{}; memcpy(repatchedBytes.data(), t.origTextBytes, repatchedBytes.size()); @@ -1858,7 +1858,7 @@ bool OIDebugger::removeTrap(pid_t pid, const trapInfo &t) { VLOG(4) << "removeTrap removing int3 at " << std::hex << t.trapAddr; if (ptrace(PTRACE_POKETEXT, (!pid ? traceePid : pid), t.trapAddr, - *reinterpret_cast(repatchedBytes.data())) < 0) { + *reinterpret_cast(repatchedBytes.data())) < 0) { LOG(ERROR) << "Execute: Couldn't poke text: " << strerror(errno); return false; } @@ -1917,7 +1917,7 @@ OIDebugger::OIDebugger(fs::path debugInfo, std::string configFile, * @param[in] target_addr - target address where new data are to be written * @param[in] bufsz - length of 'target_addr' buffer in bytes */ -bool OIDebugger::writeTargetMemory(void *local_buffer, void *target_addr, +bool OIDebugger::writeTargetMemory(void* local_buffer, void* target_addr, size_t bufsz) const { VLOG(1) << "Writing buffer " << std::hex << local_buffer << ", bufsz " << std::dec << bufsz << " into target " << std::hex << target_addr; @@ -1960,7 +1960,7 @@ bool OIDebugger::writeTargetMemory(void *local_buffer, void *target_addr, * @param[in] local_addr - local address where new data are to be written * @param[in] bufsz - length of 'local_addr' buffer in bytes */ -bool OIDebugger::readTargetMemory(void *remote_buffer, void *local_addr, +bool OIDebugger::readTargetMemory(void* remote_buffer, void* local_addr, size_t bufsz) const { VLOG(1) << "Reading buffer " << std::hex << remote_buffer << ", bufsz " << std::dec << bufsz << " into local " << std::hex << local_addr; @@ -1996,11 +1996,11 @@ bool OIDebugger::readTargetMemory(void *remote_buffer, void *local_addr, std::optional> OIDebugger::locateJitCodeStart( - const irequest &req, const OICompiler::RelocResult::SymTable &jitSymbols) { + const irequest& req, const OICompiler::RelocResult::SymTable& jitSymbols) { // Get type of probed object to locate the JIT code start OIDebugger::ObjectAddrMap::key_type targetObj; if (req.type == "global") { - const auto &gd = symbols->findGlobalDesc(req.func); + const auto& gd = symbols->findGlobalDesc(req.func); if (!gd) { LOG(ERROR) << "Failed to find GlobalDesc for " << req.func; return std::nullopt; @@ -2008,13 +2008,13 @@ OIDebugger::locateJitCodeStart( targetObj = gd; } else { - const auto &fd = symbols->findFuncDesc(req); + const auto& fd = symbols->findFuncDesc(req); if (!fd) { LOG(ERROR) << "Failed to find FuncDesc for " << req.func; return std::nullopt; } - const auto &farg = fd->getArgument(req.arg); + const auto& farg = fd->getArgument(req.arg); if (!farg) { LOG(ERROR) << "Failed to get argument for " << req.func << ':' << req.arg; return std::nullopt; @@ -2023,13 +2023,13 @@ OIDebugger::locateJitCodeStart( targetObj = farg; } - auto &typeName = std::visit( - [](auto &&obj) -> std::string & { return obj->typeName; }, targetObj); + auto& typeName = std::visit( + [](auto&& obj) -> std::string& { return obj->typeName; }, targetObj); auto typeHash = std::hash{}(typeName); auto jitCodeName = (boost::format("_Z24getSize_%016x") % typeHash).str(); uintptr_t jitCodeStart = 0; - for (const auto &[symName, symAddr] : jitSymbols) { + for (const auto& [symName, symAddr] : jitSymbols) { if (symName.starts_with(jitCodeName)) { jitCodeStart = symAddr; break; @@ -2078,7 +2078,7 @@ OIDebugger::locateJitCodeStart( */ bool OIDebugger::writePrologue( - const prequest &preq, const OICompiler::RelocResult::SymTable &jitSymbols) { + const prequest& preq, const OICompiler::RelocResult::SymTable& jitSymbols) { size_t off = 0; uint8_t newInsts[prologueLength]; @@ -2091,7 +2091,7 @@ bool OIDebugger::writePrologue( size_t argCount = preq.type == "global" ? 1 : preq.args.size(); for (size_t i = 0; i < argCount; i++) { - const auto &req = preq.getReqForArg(i); + const auto& req = preq.getReqForArg(i); auto jitCodeStart = locateJitCodeStart(req, jitSymbols); if (!jitCodeStart.has_value()) { @@ -2101,13 +2101,13 @@ bool OIDebugger::writePrologue( } VLOG(1) << "Generating prologue for argument '" << req.arg - << "', using probe at " << (void *)jitCodeStart->second; + << "', using probe at " << (void*)jitCodeStart->second; newInsts[off++] = movabsrdi0Inst; newInsts[off++] = movabsrdi1Inst; remoteObjAddrs.emplace(std::move(jitCodeStart->first), segConfig.textSegBase + off); - std::visit([](auto &&obj) { obj = nullptr; }, + std::visit([](auto&& obj) { obj = nullptr; }, jitCodeStart->first); // Invalidate ptr after move memcpy(newInsts + off, &objectAddr, sizeof(objectAddr)); off += sizeof(objectAddr); @@ -2140,7 +2140,7 @@ bool OIDebugger::writePrologue( assert(off <= prologueLength); - return writeTargetMemory(&newInsts, (void *)segConfig.textSegBase, + return writeTargetMemory(&newInsts, (void*)segConfig.textSegBase, prologueLength); } @@ -2150,7 +2150,7 @@ bool OIDebugger::writePrologue( */ bool OIDebugger::compileCode() { assert(pdata.numReqs() == 1); - const auto &preq = pdata.getReq(); + const auto& preq = pdata.getReq(); OICompiler compiler{symbols, compilerConfig}; std::set objectFiles{}; @@ -2163,7 +2163,7 @@ bool OIDebugger::compileCode() { */ size_t argCount = preq.type == "global" ? 1 : preq.args.size(); for (size_t i = 0; i < argCount; i++) { - const auto &req = preq.getReqForArg(i); + const auto& req = preq.getReqForArg(i); if (cache.isEnabled()) { // try to download cache artifacts if present @@ -2255,7 +2255,7 @@ bool OIDebugger::compileCode() { cache.store(req, OICache::Entity::FuncDescs, symbols->funcDescs); } - const auto &[rootType, typeHierarchy, paddingInfo] = typeInfos.at(req); + const auto& [rootType, typeHierarchy, paddingInfo] = typeInfos.at(req); cache.store(req, OICache::Entity::TypeHierarchy, std::make_pair(rootType, typeHierarchy)); cache.store(req, OICache::Entity::PaddingInfo, paddingInfo); @@ -2273,7 +2273,7 @@ bool OIDebugger::compileCode() { }; VLOG(2) << "Relocating..."; - for (const auto &o : objectFiles) { + for (const auto& o : objectFiles) { VLOG(2) << " * " << o; } auto relocRes = compiler.applyRelocs(segConfig.jitCodeStart, objectFiles, @@ -2283,12 +2283,12 @@ bool OIDebugger::compileCode() { return false; } - const auto &[_, segments, jitSymbols] = relocRes.value(); - for (const auto &[symName, symAddr] : jitSymbols) { + const auto& [_, segments, jitSymbols] = relocRes.value(); + for (const auto& [symName, symAddr] : jitSymbols) { VLOG(2) << "sym " << symName << '@' << std::hex << symAddr; } - const auto &lastSeg = segments.back(); + const auto& lastSeg = segments.back(); auto segmentsLimit = lastSeg.RelocAddr + lastSeg.Size; auto remoteSegmentLimit = segConfig.textSegBase + segConfig.textSegSize; if (segmentsLimit > remoteSegmentLimit) { @@ -2299,34 +2299,34 @@ bool OIDebugger::compileCode() { return false; } - for (const auto &[BaseAddr, RelocAddr, Size] : segments) { - if (!writeTargetMemory((void *)BaseAddr, (void *)RelocAddr, Size)) { + for (const auto& [BaseAddr, RelocAddr, Size] : segments) { + if (!writeTargetMemory((void*)BaseAddr, (void*)RelocAddr, Size)) { return false; } } if (!writeTargetMemory(&segConfig.dataSegBase, - (void *)syntheticSymbols["dataBase"], + (void*)syntheticSymbols["dataBase"], sizeof(segConfig.dataSegBase))) { LOG(ERROR) << "Failed to write dataSegBase in probe's dataBase"; return false; } - if (!writeTargetMemory(&dataSegSize, (void *)syntheticSymbols["dataSize"], + if (!writeTargetMemory(&dataSegSize, (void*)syntheticSymbols["dataSize"], sizeof(dataSegSize))) { LOG(ERROR) << "Failed to write dataSegSize in probe's dataSize"; return false; } if (!writeTargetMemory(&segConfig.cookie, - (void *)syntheticSymbols["cookieValue"], + (void*)syntheticSymbols["cookieValue"], sizeof(segConfig.cookie))) { LOG(ERROR) << "Failed to write cookie in probe's cookieValue"; return false; } int logFile = enableJitLogging ? segConfig.logFile : 0; - if (!writeTargetMemory(&logFile, (void *)syntheticSymbols["logFile"], + if (!writeTargetMemory(&logFile, (void*)syntheticSymbols["logFile"], sizeof(logFile))) { LOG(ERROR) << "Failed to write logFile in probe's cookieValue"; return false; @@ -2349,7 +2349,7 @@ void OIDebugger::restoreState(void) { */ const size_t activeTrapsCount = std::count_if( activeTraps.cbegin(), activeTraps.cend(), - [](const auto &t) { return t.second->trapKind != OID_TRAP_JITCODERET; }); + [](const auto& t) { return t.second->trapKind != OID_TRAP_JITCODERET; }); VLOG(1) << "Active traps still within the target process: " << activeTrapsCount; assert(activeTrapsCount == 0); @@ -2367,7 +2367,7 @@ void OIDebugger::restoreState(void) { * thread could still be in oid JIT code here or trapping in from it normal * execution path. */ - for (auto const &p : threadList) { + for (auto const& p : threadList) { auto state = getTaskState(p); VLOG(1) << "Task " << p << " state: " << taskStateToString(state) << " (" << static_cast(state) << ")"; @@ -2465,8 +2465,8 @@ void OIDebugger::restoreState(void) { dumpRegs("Before1", p, ®s); } - memcpy((void *)®s, (void *)&t->savedRegs, sizeof(regs)); - memcpy((void *)&fpregs, (void *)&t->savedFPregs, sizeof(fpregs)); + memcpy((void*)®s, (void*)&t->savedRegs, sizeof(regs)); + memcpy((void*)&fpregs, (void*)&t->savedFPregs, sizeof(fpregs)); /* * Note that we need to rewind the original %rip as it has trapped @@ -2568,7 +2568,7 @@ bool OIDebugger::targetAttach() { /* TODO - Handle exceptions */ auto pidPath = fs::path("/proc") / std::to_string(traceePid) / "task"; try { - for (const auto &entry : fs::directory_iterator(pidPath)) { + for (const auto& entry : fs::directory_iterator(pidPath)) { auto file = entry.path().filename().string(); auto pid = std::stoi(file); @@ -2590,7 +2590,7 @@ bool OIDebugger::targetAttach() { threadList.push_back(pid); } } - } catch (std::filesystem::filesystem_error const &ex) { + } catch (std::filesystem::filesystem_error const& ex) { LOG(ERROR) << "directory_iterator exception: " << ex.path1() << ex.code().message(); @@ -2695,8 +2695,8 @@ void OIDebugger::setDataSegmentSize(size_t size) { VLOG(1) << "setDataSegmentSize: segment size: " << dataSegSize; } -bool OIDebugger::decodeTargetData(const DataHeader &dataHeader, - std::vector &outVec) const { +bool OIDebugger::decodeTargetData(const DataHeader& dataHeader, + std::vector& outVec) const { VLOG(1) << "== magicId: " << std::hex << dataHeader.magicId; VLOG(1) << "== cookie: " << std::hex << dataHeader.cookie; VLOG(1) << "== size: " << dataHeader.size; @@ -2763,8 +2763,8 @@ bool OIDebugger::decodeTargetData(const DataHeader &dataHeader, return true; } -static bool dumpDataSegment(const irequest &req, - const std::vector &dataSeg) { +static bool dumpDataSegment(const irequest& req, + const std::vector& dataSeg) { char dumpPath[PATH_MAX] = {0}; auto dumpPathSize = snprintf(dumpPath, sizeof(dumpPath), "/tmp/dataseg.%d.%s.dump", getpid(), @@ -2782,7 +2782,7 @@ static bool dumpDataSegment(const irequest &req, } const auto outVecBytes = std::as_bytes(std::span{dataSeg}); - dumpFile.write((const char *)outVecBytes.data(), outVecBytes.size()); + dumpFile.write((const char*)outVecBytes.data(), outVecBytes.size()); if (!dumpFile) { LOG(ERROR) << "Failed to write to data-segment file '" << dumpPath << "': " << strerror(errno); @@ -2796,7 +2796,7 @@ bool OIDebugger::processTargetData() { Metrics::Tracing _("process_target_data"); std::vector buf{dataSegSize}; - if (!readTargetMemory(reinterpret_cast(segConfig.dataSegBase), + if (!readTargetMemory(reinterpret_cast(segConfig.dataSegBase), buf.data(), dataSegSize)) { LOG(ERROR) << "Failed to read data segment from target process"; return false; @@ -2805,7 +2805,7 @@ bool OIDebugger::processTargetData() { auto res = reinterpret_cast(buf.data()); assert(pdata.numReqs() == 1); - const auto &preq = pdata.getReq(); + const auto& preq = pdata.getReq(); PaddingHunter paddingHunter{}; TreeBuilder typeTree(treeBuilderConfig); @@ -2820,10 +2820,10 @@ bool OIDebugger::processTargetData() { std::vector outVec{}; for (size_t i = 0; i < argCount; i++) { - const auto &req = preq.getReqForArg(i); + const auto& req = preq.getReqForArg(i); LOG(INFO) << "Processing data for argument: " << req.arg; - const auto &dataHeader = *reinterpret_cast(res); + const auto& dataHeader = *reinterpret_cast(res); res += dataHeader.size; outVec.clear(); @@ -2845,8 +2845,8 @@ bool OIDebugger::processTargetData() { return false; } - const auto &[rootType, typeHierarchy, paddingInfos] = typeInfo->second; - VLOG(1) << "Root type addr: " << (void *)rootType.type.type; + const auto& [rootType, typeHierarchy, paddingInfos] = typeInfo->second; + VLOG(1) << "Root type addr: " << (void*)rootType.type.type; if (treeBuilderConfig.genPaddingStats) { paddingHunter.localPaddedStructs = paddingInfos; @@ -2856,7 +2856,7 @@ bool OIDebugger::processTargetData() { try { typeTree.build(outVec, rootType.varName, rootType.type.type, typeHierarchy); - } catch (std::exception &e) { + } catch (std::exception& e) { LOG(ERROR) << "Failed to run TreeBuilder for " << req.arg; LOG(ERROR) << e.what(); @@ -2893,7 +2893,7 @@ bool OIDebugger::processTargetData() { return true; } -std::optional OIDebugger::generateCode(const irequest &req) { +std::optional OIDebugger::generateCode(const irequest& req) { auto root = symbols->getRootType(req); if (!root.has_value()) { return std::nullopt; diff --git a/src/OIDebugger.h b/src/OIDebugger.h index 774a154..f3787a9 100644 --- a/src/OIDebugger.h +++ b/src/OIDebugger.h @@ -58,13 +58,13 @@ class OIDebugger { OIDebugger::processTrapRet processTrap(pid_t, bool = true, bool = true); bool contTargetThread(bool detach = true) const; bool isGlobalDataProbeEnabled(void) const; - static uint64_t singlestepInst(pid_t, struct user_regs_struct &); + static uint64_t singlestepInst(pid_t, struct user_regs_struct&); static bool singleStepFunc(pid_t, uint64_t); - bool parseScript(std::istream &script); + bool parseScript(std::istream& script); bool patchFunctions(); void stopAll(); bool removeTraps(pid_t); - bool removeTrap(pid_t, const trapInfo &); + bool removeTrap(pid_t, const trapInfo&); void enableDrgn(); bool unmapSegments(bool deleteSegConf = false); bool isInterrupted(void) const { @@ -92,20 +92,20 @@ class OIDebugger { bool uploadCache() { return std::all_of( - std::begin(pdata), std::end(pdata), [this](const auto &req) { + std::begin(pdata), std::end(pdata), [this](const auto& req) { return std::all_of( std::begin(req.args), std::end(req.args), - [this, &req](const auto &arg) { + [this, &req](const auto& arg) { return cache.upload(irequest{req.type, req.func, arg}); }); }); } bool downloadCache() { return std::all_of( - std::begin(pdata), std::end(pdata), [this](const auto &req) { + std::begin(pdata), std::end(pdata), [this](const auto& req) { return std::all_of( std::begin(req.args), std::end(req.args), - [this, &req](const auto &arg) { + [this, &req](const auto& arg) { return cache.download(irequest{req.type, req.func, arg}); }); }); @@ -192,40 +192,38 @@ class OIDebugger { bool setupSegment(SegType); bool unmapSegment(SegType); - bool writeTargetMemory(void *, void *, size_t) const; - bool readTargetMemory(void *, void *, size_t) const; + bool writeTargetMemory(void*, void*, size_t) const; + bool readTargetMemory(void*, void*, size_t) const; std::optional> - locateJitCodeStart(const irequest &, - const OICompiler::RelocResult::SymTable &); - bool writePrologue(const prequest &, - const OICompiler::RelocResult::SymTable &); - bool readInstFromTarget(uintptr_t, uint8_t *, size_t); + locateJitCodeStart(const irequest&, const OICompiler::RelocResult::SymTable&); + bool writePrologue(const prequest&, const OICompiler::RelocResult::SymTable&); + bool readInstFromTarget(uintptr_t, uint8_t*, size_t); void createSegmentConfigFile(void); void deleteSegmentConfig(bool); - std::optional> makeTrapInfo(const prequest &, + std::optional> makeTrapInfo(const prequest&, const trapType, const uint64_t); - bool functionPatch(const prequest &); + bool functionPatch(const prequest&); bool canProcessTrapForThread(pid_t) const; - bool replayTrappedInstr(const trapInfo &, pid_t, struct user_regs_struct &, - struct user_fpregs_struct &) const; - bool locateObjectsAddresses(const trapInfo &, struct user_regs_struct &); - processTrapRet processFuncTrap(const trapInfo &, pid_t, - struct user_regs_struct &, - struct user_fpregs_struct &); - processTrapRet processJitCodeRet(const trapInfo &, pid_t); - bool processGlobal(const std::string &); - static void dumpRegs(const char *, pid_t, struct user_regs_struct *); - std::optional nextReplayInstrAddr(const trapInfo &); + bool replayTrappedInstr(const trapInfo&, pid_t, struct user_regs_struct&, + struct user_fpregs_struct&) const; + bool locateObjectsAddresses(const trapInfo&, struct user_regs_struct&); + processTrapRet processFuncTrap(const trapInfo&, pid_t, + struct user_regs_struct&, + struct user_fpregs_struct&); + processTrapRet processJitCodeRet(const trapInfo&, pid_t); + bool processGlobal(const std::string&); + static void dumpRegs(const char*, pid_t, struct user_regs_struct*); + std::optional nextReplayInstrAddr(const trapInfo&); static int getExtendedWaitEventType(int); static bool isExtendedWait(int); void dumpAlltaskStates(void); - std::optional> findRetLocs(FuncDesc &); + std::optional> findRetLocs(FuncDesc&); OICompiler::Config compilerConfig{}; OICodeGen::Config generatorConfig{}; TreeBuilder::Config treeBuilderConfig{}; - std::optional generateCode(const irequest &); + std::optional generateCode(const irequest&); std::fstream segmentConfigFile; fs::path segConfigFilePath; @@ -272,7 +270,7 @@ class OIDebugger { #pragma GCC diagnostic pop }; - bool decodeTargetData(const DataHeader &, std::vector &) const; + bool decodeTargetData(const DataHeader&, std::vector&) const; static constexpr size_t prologueLength = 64; static constexpr size_t constLength = 64; diff --git a/src/OILexer.h b/src/OILexer.h index ec9c3a8..e28dd2f 100644 --- a/src/OILexer.h +++ b/src/OILexer.h @@ -31,21 +31,21 @@ namespace ObjectIntrospection { class OIScanner : public yyFlexLexer { public: - OIScanner(std::istream *in) : yyFlexLexer(in){}; + OIScanner(std::istream* in) : yyFlexLexer(in){}; virtual ~OIScanner(){}; // get rid of override virtual function warning using FlexLexer::yylex; - virtual int yylex(OIParser::semantic_type *const lval, - OIParser::location_type *location); + virtual int yylex(OIParser::semantic_type* const lval, + OIParser::location_type* location); // YY_DECL defined in OILexer.l // Method body created by flex in OILexer.yy.cc private: /* yyval ptr */ - OIParser::semantic_type *yylval = nullptr; + OIParser::semantic_type* yylval = nullptr; }; } // namespace ObjectIntrospection diff --git a/src/OILibraryImpl.cpp b/src/OILibraryImpl.cpp index 220df19..c4f1675 100644 --- a/src/OILibraryImpl.cpp +++ b/src/OILibraryImpl.cpp @@ -34,7 +34,7 @@ extern "C" { namespace ObjectIntrospection { -OILibraryImpl::OILibraryImpl(OILibrary *self, void *TemplateFunc) +OILibraryImpl::OILibraryImpl(OILibrary* self, void* TemplateFunc) : _self(self), _TemplateFunc(TemplateFunc) { if (_self->opts.debugLevel != 0) { google::LogToStderr(); @@ -54,7 +54,7 @@ OILibraryImpl::~OILibraryImpl() { } bool OILibraryImpl::mapSegment() { - void *textSeg = + void* textSeg = mmap(NULL, segConfig.textSegSize, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (textSeg == MAP_FAILED) { @@ -105,7 +105,7 @@ class Cleanup { } }; -void close_file(std::FILE *fp) { +void close_file(std::FILE* fp) { std::fclose(fp); } @@ -131,11 +131,11 @@ int OILibraryImpl::compileCode() { auto objectPath = fs::path((boost::format("/dev/fd/%1%") % objectMemfd).str()); - struct drgn_program *prog = symbols->getDrgnProgram(); + struct drgn_program* prog = symbols->getDrgnProgram(); if (!prog) { return Response::OIL_COMPILATION_FAILURE; } - struct drgn_symbol *sym; + struct drgn_symbol* sym; if (auto err = drgn_program_find_symbol_by_address( prog, (uintptr_t)_TemplateFunc, &sym)) { LOG(ERROR) << "Error when finding symbol by address " << err->code << " " @@ -143,7 +143,7 @@ int OILibraryImpl::compileCode() { drgn_error_destroy(err); return Response::OIL_COMPILATION_FAILURE; } - const char *name = drgn_symbol_name(sym); + const char* name = drgn_symbol_name(sym); drgn_symbol_destroy(sym); // TODO: change this to the new drgn interface from symbol -> type @@ -187,13 +187,13 @@ int OILibraryImpl::compileCode() { return Response::OIL_RELOCATION_FAILURE; } - const auto &[_, segments, jitSymbols] = relocRes.value(); + const auto& [_, segments, jitSymbols] = relocRes.value(); // Locate the probe's entry point _self->fp = nullptr; - for (const auto &[symName, symAddr] : jitSymbols) { + for (const auto& [symName, symAddr] : jitSymbols) { if (symName.starts_with("_Z7getSize")) { - _self->fp = (size_t(*)(const void *))symAddr; + _self->fp = (size_t(*)(const void*))symAddr; break; } } @@ -202,8 +202,8 @@ int OILibraryImpl::compileCode() { } // Copy relocated segments in their final destination - for (const auto &[BaseAddr, RelocAddr, Size] : segments) - memcpy((void *)RelocAddr, (void *)BaseAddr, Size); + for (const auto& [BaseAddr, RelocAddr, Size] : segments) + memcpy((void*)RelocAddr, (void*)BaseAddr, Size); return Response::OIL_SUCCESS; } diff --git a/src/OILibraryImpl.h b/src/OILibraryImpl.h index fe56f4f..3e73dac 100644 --- a/src/OILibraryImpl.h +++ b/src/OILibraryImpl.h @@ -24,7 +24,7 @@ namespace ObjectIntrospection { class OILibraryImpl { public: - OILibraryImpl(OILibrary *, void *); + OILibraryImpl(OILibrary*, void*); ~OILibraryImpl(); bool mapSegment(); @@ -35,16 +35,16 @@ class OILibraryImpl { void enableLayoutAnalysis(); private: - class OILibrary *_self; + class OILibrary* _self; - void *_TemplateFunc; + void* _TemplateFunc; OICompiler::Config compilerConfig{}; OICodeGen::Config generatorConfig{}; std::shared_ptr symbols{}; struct c { - void *textSegBase = nullptr; + void* textSegBase = nullptr; size_t textSegSize = 1u << 22; } segConfig; }; diff --git a/src/OIOpts.h b/src/OIOpts.h index d3207a0..6309be1 100644 --- a/src/OIOpts.h +++ b/src/OIOpts.h @@ -26,21 +26,21 @@ extern "C" { struct OIOpt { char shortName; - const char *longName; + const char* longName; int has_arg; - const char *argName; - const char *usage; + const char* argName; + const char* usage; }; template class OIOpts { public: template - constexpr explicit OIOpts(Opts &&...options) + constexpr explicit OIOpts(Opts&&... options) : _opts{std::forward(options)...} { // Create the short opts string size_t shortOptIndex = 0; - for (const auto &opt : _opts) { + for (const auto& opt : _opts) { _shortOpts[shortOptIndex++] = opt.shortName; for (int i = 0; i < opt.has_arg; ++i) _shortOpts[shortOptIndex++] = ':'; @@ -52,7 +52,7 @@ class OIOpts { // Create the array of long opts for (size_t i = 0; i < _opts.size(); ++i) { - const auto &opt = _opts[i]; + const auto& opt = _opts[i]; _longOpts[i] = {opt.longName, opt.has_arg, nullptr, opt.shortName}; } @@ -60,15 +60,15 @@ class OIOpts { _longOpts[_opts.size()] = {nullptr, no_argument, nullptr, '\0'}; } - constexpr const char *shortOpts() const { + constexpr const char* shortOpts() const { return _shortOpts.data(); } - constexpr const struct option *longOpts() const { + constexpr const struct option* longOpts() const { return _longOpts.data(); } template - friend std::ostream &operator<<(std::ostream &os, const OIOpts &opts); + friend std::ostream& operator<<(std::ostream& os, const OIOpts& opts); private: std::array _opts; @@ -77,16 +77,16 @@ class OIOpts { }; template -std::ostream &operator<<(std::ostream &os, const OIOpts &opts) { +std::ostream& operator<<(std::ostream& os, const OIOpts& opts) { int maxLongName = 0; - for (const auto &opt : opts._opts) { + for (const auto& opt : opts._opts) { size_t longNameWidth = strlen(opt.longName); if (opt.argName) longNameWidth += 1 + strlen(opt.argName); maxLongName = std::max(maxLongName, (int)longNameWidth); } - for (const auto &opt : opts._opts) { + for (const auto& opt : opts._opts) { auto fullName = std::string(opt.longName); if (opt.argName) { fullName += ' '; diff --git a/src/OIParser.h b/src/OIParser.h index 4d05627..2c4e242 100644 --- a/src/OIParser.h +++ b/src/OIParser.h @@ -40,7 +40,7 @@ namespace std { template <> struct hash { - std::size_t operator()(const irequest &req) const noexcept { + std::size_t operator()(const irequest& req) const noexcept { auto h = hash(); return h(req.type) ^ h(req.func) ^ h(req.arg); } @@ -48,7 +48,7 @@ struct hash { template <> struct equal_to { - bool operator()(const irequest &lhs, const irequest &rhs) const noexcept { + bool operator()(const irequest& lhs, const irequest& rhs) const noexcept { return lhs.type == rhs.type && lhs.func == rhs.func && lhs.arg == rhs.arg; } }; @@ -89,7 +89,7 @@ class ParseData { return reqs.size(); } - [[nodiscard]] const prequest &getReq(size_t idx = 0) const noexcept { + [[nodiscard]] const prequest& getReq(size_t idx = 0) const noexcept { assert(idx < reqs.size()); return reqs[idx]; } diff --git a/src/PaddingHunter.cpp b/src/PaddingHunter.cpp index 2dbe440..d5a9ecf 100644 --- a/src/PaddingHunter.cpp +++ b/src/PaddingHunter.cpp @@ -19,7 +19,7 @@ #include void PaddingHunter::processLocalPaddingInfo() { - for (auto &lPS : localPaddedStructs) { + for (auto& lPS : localPaddedStructs) { if (paddedStructs.find(lPS.first) != paddedStructs.end()) { if (localPaddedStructs[lPS.first].instancesCnt > paddedStructs[lPS.first].instancesCnt) { @@ -38,24 +38,24 @@ void PaddingHunter::outputPaddingInfo() { uint64_t sum = 0; std::vector> paddedStructsVec; - for (auto &paddedStruct : paddedStructs) { + for (auto& paddedStruct : paddedStructs) { paddedStructsVec.push_back({paddedStruct.first, paddedStruct.second}); } - for (auto &paddedStruct : paddedStructsVec) { + for (auto& paddedStruct : paddedStructsVec) { sum += paddedStruct.second.paddingSize * paddedStruct.second.instancesCnt; } paddingStatsFile << "Total Saving Opportunity: " << sum << "\n\n\n"; std::sort(paddedStructsVec.begin(), paddedStructsVec.end(), - [](const std::pair &left, - const std::pair &right) { + [](const std::pair& left, + const std::pair& right) { return left.second.instancesCnt * left.second.savingSize > right.second.instancesCnt * right.second.savingSize; }); - for (auto &paddedStruct : paddedStructsVec) { + for (auto& paddedStruct : paddedStructsVec) { paddingStatsFile << "Name: " << paddedStruct.first << ", object size: " << paddedStruct.second.structSize << ", saving size: " << paddedStruct.second.savingSize diff --git a/src/Serialize.cpp b/src/Serialize.cpp index 409b8b6..a9cb531 100644 --- a/src/Serialize.cpp +++ b/src/Serialize.cpp @@ -51,25 +51,25 @@ using oarchive = boost::archive::text_oarchive; "No class version was defined for type `" #Type \ "`, please add an invocation of `DEFINE_TYPE_VERSION` for this " \ "type."); \ - template void serialize(iarchive &, Type &, const unsigned int); \ - template void serialize(oarchive &, Type &, const unsigned int); + template void serialize(iarchive&, Type&, const unsigned int); \ + template void serialize(oarchive&, Type&, const unsigned int); template -void serialize(Archive &ar, PaddingInfo &p, const unsigned int version) { +void serialize(Archive& ar, PaddingInfo& p, const unsigned int version) { verify_version(version); - ar &p.structSize; - ar &p.paddingSize; - ar &p.definition; - ar &p.instancesCnt; - ar &p.savingSize; + ar& p.structSize; + ar& p.paddingSize; + ar& p.definition; + ar& p.instancesCnt; + ar& p.savingSize; } INSTANCIATE_SERIALIZE(PaddingInfo) template -void serialize(Archive &ar, ContainerInfo &info, const unsigned int version) { +void serialize(Archive& ar, ContainerInfo& info, const unsigned int version) { verify_version(version); - ar &info.typeName; + ar& info.typeName; // Unfortunately boost serialization doesn't support `std::optional`, // so we have to do this ourselves size_t numTemplateParams = 0; @@ -77,7 +77,7 @@ void serialize(Archive &ar, ContainerInfo &info, const unsigned int version) { numTemplateParams = info.numTemplateParams.value_or(std::numeric_limits::max()); } - ar &numTemplateParams; + ar& numTemplateParams; if (Archive::is_loading::value) { if (numTemplateParams == std::numeric_limits::max()) { info.numTemplateParams = std::nullopt; @@ -85,124 +85,124 @@ void serialize(Archive &ar, ContainerInfo &info, const unsigned int version) { info.numTemplateParams = numTemplateParams; } } - ar &info.ctype; - ar &info.header; - ar &info.ns; + ar& info.ctype; + ar& info.header; + ar& info.ns; } INSTANCIATE_SERIALIZE(ContainerInfo) template -void serialize(Archive &ar, struct drgn_location_description &location, +void serialize(Archive& ar, struct drgn_location_description& location, const unsigned int version) { verify_version(version); - ar &location.start; - ar &location.end; - ar &location.expr_size; + ar& location.start; + ar& location.end; + ar& location.expr_size; if (Archive::is_loading::value) { // It is important to call `malloc` here instead of allocating with `new` // since these structs are usually allocated and deallocated directly by // `drgn`, which is written in C. location.expr = - (const char *)malloc(sizeof(*location.expr) * location.expr_size); + (const char*)malloc(sizeof(*location.expr) * location.expr_size); } - ar &make_array(const_cast(location.expr), location.expr_size); + ar& make_array(const_cast(location.expr), location.expr_size); } INSTANCIATE_SERIALIZE(struct drgn_location_description) template -void serialize(Archive &ar, struct drgn_object_locator &locator, +void serialize(Archive& ar, struct drgn_object_locator& locator, const unsigned int version) { verify_version(version); - ar &locator.module_start; - ar &locator.module_end; - ar &locator.module_bias; - ar &locator.locations_size; - ar &locator.frame_base_locations_size; + ar& locator.module_start; + ar& locator.module_end; + ar& locator.module_bias; + ar& locator.locations_size; + ar& locator.frame_base_locations_size; if (Archive::is_loading::value) { // It is important to call `malloc` here instead of allocating with `new` // since these structs are usually allocated and deallocated directly by // `drgn`, which is written in C. - locator.locations = (struct drgn_location_description *)malloc( + locator.locations = (struct drgn_location_description*)malloc( sizeof(*locator.locations) * locator.locations_size); - locator.frame_base_locations = (struct drgn_location_description *)malloc( + locator.frame_base_locations = (struct drgn_location_description*)malloc( sizeof(*locator.frame_base_locations) * locator.frame_base_locations_size); } - ar &make_array(locator.locations, + ar& make_array(locator.locations, locator.locations_size); - ar &make_array( + ar& make_array( locator.frame_base_locations, locator.frame_base_locations_size); - ar &locator.qualified_type; + ar& locator.qualified_type; } INSTANCIATE_SERIALIZE(struct drgn_object_locator) template -void serialize(Archive &ar, FuncDesc::Arg &arg, const unsigned int version) { +void serialize(Archive& ar, FuncDesc::Arg& arg, const unsigned int version) { verify_version(version); - ar &arg.typeName; - ar &arg.valid; - ar &arg.locator; + ar& arg.typeName; + ar& arg.valid; + ar& arg.locator; } INSTANCIATE_SERIALIZE(FuncDesc::Arg) template -void serialize(Archive &ar, FuncDesc::Retval &retval, +void serialize(Archive& ar, FuncDesc::Retval& retval, const unsigned int version) { verify_version(version); - ar &retval.typeName; - ar &retval.valid; + ar& retval.typeName; + ar& retval.valid; } INSTANCIATE_SERIALIZE(FuncDesc::Retval) template -void serialize(Archive &ar, FuncDesc::Range &range, +void serialize(Archive& ar, FuncDesc::Range& range, const unsigned int version) { verify_version(version); - ar &range.start; - ar &range.end; + ar& range.start; + ar& range.end; } INSTANCIATE_SERIALIZE(FuncDesc::Range) template -void serialize(Archive &ar, FuncDesc &fd, const unsigned int version) { +void serialize(Archive& ar, FuncDesc& fd, const unsigned int version) { verify_version(version); - ar &fd.symName; - ar &fd.ranges; - ar &fd.isMethod; - ar &fd.arguments; - ar &fd.retval; + ar& fd.symName; + ar& fd.ranges; + ar& fd.isMethod; + ar& fd.arguments; + ar& fd.retval; } INSTANCIATE_SERIALIZE(FuncDesc) template -void serialize(Archive &ar, GlobalDesc &gd, const unsigned int version) { +void serialize(Archive& ar, GlobalDesc& gd, const unsigned int version) { verify_version(version); - ar &gd.symName; - ar &gd.typeName; - ar &gd.baseAddr; + ar& gd.symName; + ar& gd.typeName; + ar& gd.baseAddr; } INSTANCIATE_SERIALIZE(GlobalDesc) template -static void serialize_c_string(Archive &ar, char **string) { +static void serialize_c_string(Archive& ar, char** string) { size_t length; if (Archive::is_saving::value) { length = *string ? strlen(*string) : 0; } - ar &length; + ar& length; if (Archive::is_loading::value) { - *string = length ? (char *)malloc(sizeof(char) * (length + 1)) : NULL; + *string = length ? (char*)malloc(sizeof(char) * (length + 1)) : NULL; } if (length > 0) { - ar &make_array(*string, length + 1); + ar& make_array(*string, length + 1); } } @@ -214,7 +214,7 @@ static void serialize_c_string(Archive &ar, char **string) { // what you're doing (or ask someone who does) before touching anything. // ########################################################################### template -void serialize(Archive &ar, struct drgn_type &type, +void serialize(Archive& ar, struct drgn_type& type, const unsigned int version) { #define assert_in_same_union(member_1, member_2) \ do { \ @@ -239,10 +239,10 @@ void serialize(Archive &ar, struct drgn_type &type, // `.kind` MUST be serialized first, not just because it's declared first, // but because all of the `drgn_type_has_*` functions rely on the value of // `.kind` - ar &type._private.kind; - ar &type._private.is_complete; - ar &type._private.primitive; - ar &type._private.qualifiers; + ar& type._private.kind; + ar& type._private.is_complete; + ar& type._private.primitive; + ar& type._private.qualifiers; // `.program` is NULL, per the initial `memset` if (Archive::is_loading::value) { @@ -258,9 +258,9 @@ void serialize(Archive &ar, struct drgn_type &type, // First union: `name`, `tag` assert_in_same_union(name, tag); if (drgn_type_has_name(&type)) { - serialize_c_string(ar, const_cast(&type._private.name)); + serialize_c_string(ar, const_cast(&type._private.name)); } else if (drgn_type_has_tag(&type)) { - serialize_c_string(ar, const_cast(&type._private.tag)); + serialize_c_string(ar, const_cast(&type._private.tag)); } // Second union: `size`, `length`, `num_enumerators`, `is_variadic` @@ -268,13 +268,13 @@ void serialize(Archive &ar, struct drgn_type &type, assert_in_same_union(size, num_enumerators); assert_in_same_union(size, is_variadic); if (drgn_type_has_size(&type)) { - ar &type._private.size; + ar& type._private.size; } else if (drgn_type_has_length(&type)) { - ar &type._private.length; + ar& type._private.length; } else if (drgn_type_has_enumerators(&type)) { - ar &type._private.num_enumerators; + ar& type._private.num_enumerators; } else if (drgn_type_has_is_variadic(&type)) { - ar &type._private.is_variadic; + ar& type._private.is_variadic; } // Third union: `is_signed`, `num_members`, `num_paramters` @@ -286,7 +286,7 @@ void serialize(Archive &ar, struct drgn_type &type, assert_in_same_union(little_endian, enumerators); assert_in_same_union(little_endian, parameters); if (drgn_type_has_little_endian(&type)) { - ar &type._private.little_endian; + ar& type._private.little_endian; } else if (drgn_type_has_members(&type)) { // Leave `members` set to NULL per the initial `memset`, // see "AVOIDING OVERSERIALIZATION" comment above @@ -302,17 +302,17 @@ void serialize(Archive &ar, struct drgn_type &type, // and `num_parents` set to NULL/0 per the initial `memset`, see // "AVOIDING OVERSERIALIZATION" comment above - ar &type._private.die_addr; + ar& type._private.die_addr; // `.module` is NULL, per the initial `memset` if (Archive::is_saving::value) { - struct drgn_error *err = drgn_type_sizeof(&type, &type._private.oi_size); + struct drgn_error* err = drgn_type_sizeof(&type, &type._private.oi_size); if (err) { drgn_error_destroy(err); type._private.oi_size = std::numeric_limits::max(); } } - ar &type._private.oi_size; + ar& type._private.oi_size; // It's important that `oi_name` is declared here and not inside the // if statement so that its data isn't freed when we call @@ -322,10 +322,10 @@ void serialize(Archive &ar, struct drgn_type &type, oi_name = drgn_utils::typeToName(&type); type._private.oi_name = oi_name.c_str(); } - serialize_c_string(ar, const_cast(&type._private.oi_name)); + serialize_c_string(ar, const_cast(&type._private.oi_name)); if (drgn_type_kind(&type) == DRGN_TYPE_ARRAY) { - ar &type._private.type; + ar& type._private.type; } #undef assert_in_same_union } @@ -333,48 +333,48 @@ void serialize(Archive &ar, struct drgn_type &type, INSTANCIATE_SERIALIZE(struct drgn_type) template -void serialize(Archive &ar, struct DrgnClassMemberInfo &m, +void serialize(Archive& ar, struct DrgnClassMemberInfo& m, const unsigned int version) { verify_version(version); - ar &m.type; - ar &m.member_name; - ar &m.bit_offset; - ar &m.bit_field_size; + ar& m.type; + ar& m.member_name; + ar& m.bit_offset; + ar& m.bit_field_size; } INSTANCIATE_SERIALIZE(DrgnClassMemberInfo) template -void serialize(Archive &ar, struct drgn_qualified_type &type, +void serialize(Archive& ar, struct drgn_qualified_type& type, const unsigned int version) { verify_version(version); - ar &type.type; - ar &type.qualifiers; + ar& type.type; + ar& type.qualifiers; } INSTANCIATE_SERIALIZE(struct drgn_qualified_type) template -void serialize(Archive &ar, RootInfo &rootInfo, const unsigned int version) { +void serialize(Archive& ar, RootInfo& rootInfo, const unsigned int version) { verify_version(version); - ar &rootInfo.varName; - ar &rootInfo.type; + ar& rootInfo.varName; + ar& rootInfo.type; } INSTANCIATE_SERIALIZE(RootInfo) template -void serialize(Archive &ar, struct TypeHierarchy &th, +void serialize(Archive& ar, struct TypeHierarchy& th, const unsigned int version) { verify_version(version); - ar &th.classMembersMap; - ar &th.containerTypeMap; - ar &th.typedefMap; - ar &th.sizeMap; - ar &th.knownDummyTypeList; - ar &th.pointerToTypeMap; - ar &th.thriftIssetStructTypes; - ar &th.descendantClasses; + ar& th.classMembersMap; + ar& th.containerTypeMap; + ar& th.typedefMap; + ar& th.sizeMap; + ar& th.knownDummyTypeList; + ar& th.pointerToTypeMap; + ar& th.thriftIssetStructTypes; + ar& th.descendantClasses; } INSTANCIATE_SERIALIZE(struct TypeHierarchy) diff --git a/src/Serialize.h b/src/Serialize.h index d6d8ee6..ed6ba54 100644 --- a/src/Serialize.h +++ b/src/Serialize.h @@ -59,7 +59,7 @@ namespace boost::serialization { #define DECL_SERIALIZE(Type) \ template \ - void serialize(Archive &, Type &, const unsigned int) + void serialize(Archive&, Type&, const unsigned int) DECL_SERIALIZE(PaddingInfo); DECL_SERIALIZE(ContainerInfo); diff --git a/src/SymbolService.cpp b/src/SymbolService.cpp index 7d4efb5..f89ff67 100644 --- a/src/SymbolService.cpp +++ b/src/SymbolService.cpp @@ -35,7 +35,7 @@ extern "C" { } static bool LoadExecutableAddressRange( - pid_t pid, std::vector> &exeAddrs) { + pid_t pid, std::vector>& exeAddrs) { std::ifstream f("/proc/" + std::to_string(pid) + "/maps"); if (f.is_open()) { @@ -74,13 +74,13 @@ static bool LoadExecutableAddressRange( #undef PREMISSIONS_LEN static bool isExecutableAddr( - uint64_t addr, const std::vector> &exeAddrs) { + uint64_t addr, const std::vector>& exeAddrs) { assert(std::is_sorted(begin(exeAddrs), end(exeAddrs))); // Find the smallest exeAddrs range where addr < range.end auto it = std::upper_bound( begin(exeAddrs), end(exeAddrs), std::make_pair(addr, addr), - [](const auto &r1, const auto &r2) { return r1.second < r2.second; }); + [](const auto& r1, const auto& r2) { return r1.second < r2.second; }); return it != end(exeAddrs) && addr >= it->first; } @@ -104,7 +104,7 @@ struct ModParams { std::string_view symName; GElf_Sym sym; GElf_Addr value; - std::vector> &exeAddrs; + std::vector>& exeAddrs; bool demangle; }; @@ -119,9 +119,9 @@ struct ModParams { * */ -static int moduleCallback(Dwfl_Module *mod, void ** /* userData */, - const char *name, Dwarf_Addr /* start */, void *arg) { - ModParams *m = (ModParams *)arg; +static int moduleCallback(Dwfl_Module* mod, void** /* userData */, + const char* name, Dwarf_Addr /* start */, void* arg) { + ModParams* m = (ModParams*)arg; int nsym = dwfl_module_getsymtab(mod); VLOG(1) << "mod name: " << name << " " @@ -144,10 +144,10 @@ static int moduleCallback(Dwfl_Module *mod, void ** /* userData */, /* I think the first entry is always UNDEF */ for (int i = 1; i < nsym; ++i) { - Elf *elf = nullptr; + Elf* elf = nullptr; GElf_Word shndxp = 0; - const char *lookupResult = dwfl_module_getsym_info( + const char* lookupResult = dwfl_module_getsym_info( mod, i, &m->sym, &m->value, &shndxp, &elf, nullptr); if (lookupResult == nullptr || lookupResult[0] == '\0') { continue; @@ -204,8 +204,8 @@ static int moduleCallback(Dwfl_Module *mod, void ** /* userData */, * @return - A std::optional with the symbol's information */ std::optional SymbolService::locateSymbol( - const std::string &symName, bool demangle) { - static char *debuginfo_path; + const std::string& symName, bool demangle) { + static char* debuginfo_path; static const Dwfl_Callbacks proc_callbacks{ .find_elf = dwfl_linux_proc_find_elf, .find_debuginfo = dwfl_standard_find_debuginfo, @@ -213,7 +213,7 @@ std::optional SymbolService::locateSymbol( .debuginfo_path = &debuginfo_path, }; - Dwfl *dwfl = dwfl_begin(&proc_callbacks); + Dwfl* dwfl = dwfl_begin(&proc_callbacks); if (dwfl == nullptr) { LOG(ERROR) << "dwfl_begin: " << dwfl_errmsg(dwfl_errno()); return std::nullopt; @@ -232,8 +232,8 @@ std::optional SymbolService::locateSymbol( break; } case 1: { - const auto &exe = std::get(target); - Dwfl_Module *mod = + const auto& exe = std::get(target); + Dwfl_Module* mod = dwfl_report_offline(dwfl, exe.c_str(), exe.c_str(), -1); if (mod == nullptr) { LOG(ERROR) << "dwfl_report_offline: " << dwfl_errmsg(dwfl_errno()); @@ -268,7 +268,7 @@ std::optional SymbolService::locateSymbol( .exeAddrs = executableAddrs, .demangle = demangle}; - dwfl_getmodules(dwfl, moduleCallback, (void *)&m, 0); + dwfl_getmodules(dwfl, moduleCallback, (void*)&m, 0); if (m.value == 0) { return std::nullopt; @@ -276,7 +276,7 @@ std::optional SymbolService::locateSymbol( return SymbolInfo{m.value, m.sym.st_size}; } -static std::string bytesToHexString(const unsigned char *bytes, int nbbytes) { +static std::string bytesToHexString(const unsigned char* bytes, int nbbytes) { static const char characters[] = "0123456789abcdef"; std::string ret(nbbytes * 2, 0); @@ -296,21 +296,21 @@ static std::string bytesToHexString(const unsigned char *bytes, int nbbytes) { * to this callback. So we always return DWARF_CB_ABORT, as this is * the only build ID we are interested in. */ -static int buildIDCallback(Dwfl_Module *mod, void ** /* userData */, - const char *name, Dwarf_Addr /* start */, - void *arg) { - auto *buildID = static_cast *>(arg); +static int buildIDCallback(Dwfl_Module* mod, void** /* userData */, + const char* name, Dwarf_Addr /* start */, + void* arg) { + auto* buildID = static_cast*>(arg); // We must call dwfl_module_getelf before using dwfl_module_build_id GElf_Addr bias = 0; - Elf *elf = dwfl_module_getelf(mod, &bias); + Elf* elf = dwfl_module_getelf(mod, &bias); if (elf == nullptr) { LOG(ERROR) << "Failed to getelf for " << name << ": " << dwfl_errmsg(-1); return DWARF_CB_ABORT; } GElf_Addr vaddr = 0; - const unsigned char *bytes = nullptr; + const unsigned char* bytes = nullptr; int nbbytes = dwfl_module_build_id(mod, &bytes, &vaddr); if (nbbytes <= 0) { @@ -326,7 +326,7 @@ static int buildIDCallback(Dwfl_Module *mod, void ** /* userData */, } std::optional SymbolService::locateBuildID() { - static char *debuginfoPath; + static char* debuginfoPath; static const Dwfl_Callbacks procCallbacks = { .find_elf = dwfl_linux_proc_find_elf, .find_debuginfo = dwfl_standard_find_debuginfo, @@ -334,7 +334,7 @@ std::optional SymbolService::locateBuildID() { .debuginfo_path = &debuginfoPath, }; - Dwfl *dwfl = dwfl_begin(&procCallbacks); + Dwfl* dwfl = dwfl_begin(&procCallbacks); if (dwfl == nullptr) { LOG(ERROR) << "dwfl_begin: " << dwfl_errmsg(dwfl_errno()); return std::nullopt; @@ -353,7 +353,7 @@ std::optional SymbolService::locateBuildID() { break; } case 1: { - const auto &exe = std::get(target); + const auto& exe = std::get(target); if (dwfl_report_offline(dwfl, exe.c_str(), exe.c_str(), -1) == nullptr) { LOG(ERROR) << "dwfl_report_offline: " << dwfl_errmsg(dwfl_errno()); return std::nullopt; @@ -367,12 +367,12 @@ std::optional SymbolService::locateBuildID() { } std::optional buildID; - dwfl_getmodules(dwfl, buildIDCallback, (void *)&buildID, 0); + dwfl_getmodules(dwfl, buildIDCallback, (void*)&buildID, 0); return buildID; } -struct drgn_program *SymbolService::getDrgnProgram() { +struct drgn_program* SymbolService::getDrgnProgram() { if (hardDisableDrgn) { LOG(ERROR) << "drgn is disabled, refusing to initialize"; return nullptr; @@ -385,15 +385,15 @@ struct drgn_program *SymbolService::getDrgnProgram() { LOG(INFO) << "Initialising drgn. This might take a while"; switch (target.index()) { case 0: { - if (auto *err = drgn_program_from_pid(std::get(target), &prog)) { + if (auto* err = drgn_program_from_pid(std::get(target), &prog)) { LOG(ERROR) << "Failed to initialize drgn: " << err->code << " " << err->message; return nullptr; } auto executable = fs::read_symlink( "/proc/" + std::to_string(std::get(target)) + "/exe"); - const auto *executableCStr = executable.c_str(); - if (auto *err = drgn_program_load_debug_info(prog, &executableCStr, 1, + const auto* executableCStr = executable.c_str(); + if (auto* err = drgn_program_load_debug_info(prog, &executableCStr, 1, false, false)) { LOG(ERROR) << "Error loading debug info: " << err->message; return nullptr; @@ -401,14 +401,14 @@ struct drgn_program *SymbolService::getDrgnProgram() { break; } case 1: { - if (auto *err = drgn_program_create(nullptr, &prog)) { + if (auto* err = drgn_program_create(nullptr, &prog)) { LOG(ERROR) << "Failed to create empty drgn program: " << err->code << " " << err->message; return nullptr; } - const char *path = std::get(target).c_str(); - if (auto *err = + const char* path = std::get(target).c_str(); + if (auto* err = drgn_program_load_debug_info(prog, &path, 1, false, false)) { LOG(ERROR) << "Failed to read debug info: " << err->code << " " << err->message; @@ -430,9 +430,9 @@ struct drgn_program *SymbolService::getDrgnProgram() { * Although 'parseFormalParam' has an all-encompassing sounding name, its sole * task is to extract the location information for this parameter if any exist. */ -static void parseFormalParam(Dwarf_Die ¶m, struct drgn_module *module, - struct drgn_program *prog, Dwarf_Die &funcDie, - std::shared_ptr &fd) { +static void parseFormalParam(Dwarf_Die& param, struct drgn_module* module, + struct drgn_program* prog, Dwarf_Die& funcDie, + std::shared_ptr& fd) { /* * NOTE: It is vital that the function descriptors list of arguments * are in order and that an entry exists for each argument position @@ -441,7 +441,7 @@ static void parseFormalParam(Dwarf_Die ¶m, struct drgn_module *module, * any new error handling. */ auto farg = fd->addArgument(); - auto *err = + auto* err = drgn_object_locator_init(prog, module, &funcDie, ¶m, &farg->locator); if (err) { LOG(ERROR) << "Could not initialize drgn_object_locator for parameter: " @@ -450,7 +450,7 @@ static void parseFormalParam(Dwarf_Die ¶m, struct drgn_module *module, return; } - const char *name = nullptr; + const char* name = nullptr; Dwarf_Attribute attr; if (dwarf_attr_integrate(¶m, DW_AT_name, &attr)) { if (!(name = dwarf_formstring(&attr))) { @@ -473,14 +473,14 @@ static void parseFormalParam(Dwarf_Die ¶m, struct drgn_module *module, VLOG(1) << "Adding function arg address: " << farg; } -static bool handleInlinedFunction(const irequest &request, +static bool handleInlinedFunction(const irequest& request, std::shared_ptr funcDesc, - struct drgn_qualified_type &funcType, - Dwarf_Die &funcDie, - struct drgn_module *&module) { + struct drgn_qualified_type& funcType, + Dwarf_Die& funcDie, + struct drgn_module*& module) { VLOG(1) << "Function '" << funcDesc->symName << "' has been inlined"; - struct drgn_type_inlined_instances_iterator *iter = nullptr; - auto *err = drgn_type_inlined_instances_iterator_init(funcType.type, &iter); + struct drgn_type_inlined_instances_iterator* iter = nullptr; + auto* err = drgn_type_inlined_instances_iterator_init(funcType.type, &iter); if (err) { LOG(ERROR) << "Error creating inlined instances iterator: " << err->message; return false; @@ -492,8 +492,8 @@ static bool handleInlinedFunction(const irequest &request, if (!index.has_value()) { return false; } - auto *argumentName = drgn_type_parameters(funcType.type)[index.value()].name; - struct drgn_type *inlinedInstance = nullptr; + auto* argumentName = drgn_type_parameters(funcType.type)[index.value()].name; + struct drgn_type* inlinedInstance = nullptr; bool foundInstance = false; // The index at which the parameter was actually found in the inlined // instance. This may differ from the index of the parameter in the function @@ -515,7 +515,7 @@ static bool handleInlinedFunction(const irequest &request, } auto numParameters = drgn_type_num_parameters(inlinedInstance); - auto *parameters = drgn_type_parameters(inlinedInstance); + auto* parameters = drgn_type_parameters(inlinedInstance); for (size_t i = 0; i < numParameters; i++) { if (strcmp(argumentName, parameters[i].name) == 0) { foundInstance = true; @@ -535,7 +535,7 @@ static bool handleInlinedFunction(const irequest &request, drgn_type_num_parameters(funcType.type); // Allocating with `calloc` since `drgn` manages the lifetimes of its // own structures, and it is written in C. - inlinedInstance->_private.parameters = (struct drgn_type_parameter *)calloc( + inlinedInstance->_private.parameters = (struct drgn_type_parameter*)calloc( inlinedInstance->_private.num_parameters, sizeof(*inlinedInstance->_private.parameters)); inlinedInstance->_private.parameters[index.value()] = targetParameter; @@ -552,14 +552,14 @@ static bool handleInlinedFunction(const irequest &request, } static std::optional> createFuncDesc( - struct drgn_program *prog, const irequest &request) { + struct drgn_program* prog, const irequest& request) { VLOG(1) << "Creating function description for: " << request.func; Dwarf_Die funcDie; struct drgn_qualified_type ft {}; - struct drgn_module *module = nullptr; + struct drgn_module* module = nullptr; - if (auto *err = drgn_program_find_type_by_symbol_name( + if (auto* err = drgn_program_find_type_by_symbol_name( prog, request.func.c_str(), &ft, &funcDie, &module)) { LOG(ERROR) << "Error when finding type by symbol: " << err->code << " " << err->message; @@ -662,13 +662,13 @@ static std::optional> createFuncDesc( * one if it doesn't exist. Just take the * up front hit of looking everything up now. */ -std::shared_ptr SymbolService::findFuncDesc(const irequest &request) { +std::shared_ptr SymbolService::findFuncDesc(const irequest& request) { if (auto it = funcDescs.find(request.func); it != end(funcDescs)) { VLOG(1) << "Found funcDesc for " << request.func; return it->second; } - struct drgn_program *drgnProg = getDrgnProgram(); + struct drgn_program* drgnProg = getDrgnProgram(); if (drgnProg == nullptr) { return nullptr; } @@ -685,7 +685,7 @@ std::shared_ptr SymbolService::findFuncDesc(const irequest &request) { } std::shared_ptr SymbolService::findGlobalDesc( - const std::string &global) { + const std::string& global) { if (auto it = globalDescs.find(global); it != end(globalDescs)) { VLOG(1) << "Found globalDesc for " << global; return it->second; @@ -700,7 +700,7 @@ std::shared_ptr SymbolService::findGlobalDesc( VLOG(1) << "locateGlobal: address of " << global << " " << std::hex << sym->addr; - struct drgn_program *drgnProg = getDrgnProgram(); + struct drgn_program* drgnProg = getDrgnProgram(); if (drgnProg == nullptr) { return nullptr; } @@ -713,7 +713,7 @@ std::shared_ptr SymbolService::findGlobalDesc( drgn_object_deinit(&globalObj); }; - if (auto *err = drgn_program_find_object(drgnProg, global.c_str(), nullptr, + if (auto* err = drgn_program_find_object(drgnProg, global.c_str(), nullptr, DRGN_FIND_OBJECT_ANY, &globalObj)) { LOG(ERROR) << "Failed to lookup global variable '" << global << "': " << err->code << " " << err->message; @@ -729,14 +729,14 @@ std::shared_ptr SymbolService::findGlobalDesc( return gd; } -std::string SymbolService::getTypeName(struct drgn_type *type) { +std::string SymbolService::getTypeName(struct drgn_type* type) { if (drgn_type_kind(type) == DRGN_TYPE_POINTER) { type = drgn_type_type(type).type; } return drgn_utils::typeToName(type); } -std::optional SymbolService::getRootType(const irequest &req) { +std::optional SymbolService::getRootType(const irequest& req) { if (req.type == "global") { /* * This is super simple as all we have to do is locate assign the @@ -749,14 +749,14 @@ std::optional SymbolService::getRootType(const irequest &req) { return std::nullopt; } - auto *prog = getDrgnProgram(); + auto* prog = getDrgnProgram(); if (prog == nullptr) { return std::nullopt; } drgn_object global{}; drgn_object_init(&global, prog); - if (auto *err = drgn_program_find_object(prog, req.func.c_str(), nullptr, + if (auto* err = drgn_program_find_object(prog, req.func.c_str(), nullptr, DRGN_FIND_OBJECT_ANY, &global); err != nullptr) { LOG(ERROR) << "Failed to lookup global variable '" << req.func @@ -781,13 +781,13 @@ std::optional SymbolService::getRootType(const irequest &req) { // auto tmp = boost::core::demangle(req->func.c_str()); // auto demangledName = tmp.substr(0, tmp.find("(")); - auto *prog = getDrgnProgram(); + auto* prog = getDrgnProgram(); if (prog == nullptr) { return std::nullopt; } drgn_qualified_type ft{}; - if (auto *err = drgn_program_find_type_by_symbol_name(prog, req.func.c_str(), + if (auto* err = drgn_program_find_type_by_symbol_name(prog, req.func.c_str(), &ft, nullptr, nullptr); err != nullptr) { LOG(ERROR) << "Error when finding type by symbol " << err->code << " " @@ -806,7 +806,7 @@ std::optional SymbolService::getRootType(const irequest &req) { return std::nullopt; } - auto *params = drgn_type_parameters(ft.type); + auto* params = drgn_type_parameters(ft.type); auto paramsCount = drgn_type_num_parameters(ft.type); if (paramsCount == 0) { LOG(ERROR) << "Function " << req.func << " has no parameters"; @@ -833,7 +833,7 @@ std::optional SymbolService::getRootType(const irequest &req) { } drgn_qualified_type paramType{}; - if (auto *err = drgn_parameter_type(¶ms[argIdx], ¶mType); + if (auto* err = drgn_parameter_type(¶ms[argIdx], ¶mType); err != nullptr) { LOG(ERROR) << "Failed to get params: " << err->code << " " << err->message; drgn_error_destroy(err); diff --git a/src/SymbolService.h b/src/SymbolService.h index 41575fd..621eb99 100644 --- a/src/SymbolService.h +++ b/src/SymbolService.h @@ -41,16 +41,16 @@ class SymbolService { SymbolService(std::variant); ~SymbolService(); - struct drgn_program *getDrgnProgram(); + struct drgn_program* getDrgnProgram(); std::optional locateBuildID(); - std::optional locateSymbol(const std::string &, + std::optional locateSymbol(const std::string&, bool demangle = false); - std::shared_ptr findFuncDesc(const irequest &); - std::shared_ptr findGlobalDesc(const std::string &); - static std::string getTypeName(struct drgn_type *); - std::optional getRootType(const irequest &); + std::shared_ptr findFuncDesc(const irequest&); + std::shared_ptr findGlobalDesc(const std::string&); + static std::string getTypeName(struct drgn_type*); + std::optional getRootType(const irequest&); std::unordered_map> funcDescs; std::unordered_map> globalDescs; @@ -61,7 +61,7 @@ class SymbolService { private: std::variant target{0}; - struct drgn_program *prog{nullptr}; + struct drgn_program* prog{nullptr}; std::vector> executableAddrs{}; bool hardDisableDrgn = false; diff --git a/src/Syscall.h b/src/Syscall.h index 5ee37e3..04202a0 100644 --- a/src/Syscall.h +++ b/src/Syscall.h @@ -66,10 +66,10 @@ struct Syscall { * The types passed to `struct Syscall` come directly from `man 2 `. * Don't hesitate to expand this list if you need more syscalls! */ -using SysOpen = Syscall<"open", SYS_open, int, const char *, int, mode_t>; +using SysOpen = Syscall<"open", SYS_open, int, const char*, int, mode_t>; using SysClose = Syscall<"close", SYS_close, int, int>; using SysFsync = Syscall<"fsync", SYS_fsync, int, int>; using SysMmap = - Syscall<"mmap", SYS_mmap, void *, void *, size_t, int, int, int, off_t>; -using SysMunmap = Syscall<"munmap", SYS_munmap, int, void *, size_t>; + Syscall<"mmap", SYS_mmap, void*, void*, size_t, int, int, int, off_t>; +using SysMunmap = Syscall<"munmap", SYS_munmap, int, void*, size_t>; diff --git a/src/TimeUtils.h b/src/TimeUtils.h index bfb333d..18651a4 100644 --- a/src/TimeUtils.h +++ b/src/TimeUtils.h @@ -19,6 +19,6 @@ using time_hr = std::chrono::high_resolution_clock; template -auto time_ns(Duration const &d) { +auto time_ns(Duration const& d) { return std::chrono::duration_cast(d).count(); } diff --git a/src/TrapInfo.h b/src/TrapInfo.h index 9ecb05d..ef3efba 100644 --- a/src/TrapInfo.h +++ b/src/TrapInfo.h @@ -122,13 +122,13 @@ class trapInfo { } }; -inline std::ostream &operator<<(std::ostream &out, const trapInfo &t) { - static const char *trapTypeDescs[] = { +inline std::ostream& operator<<(std::ostream& out, const trapInfo& t) { + static const char* trapTypeDescs[] = { "JIT Code Return", // OID_TRAP_JITCODERET "Vector Entry", // OID_TRAP_VECT_ENTRY "Vector Entry Return", // OID_TRAP_VECT_ENTRYRET "Vector Return", // OID_TRAP_VECT_RET }; return out << "Trap " << trapTypeDescs[t.trapKind] << " @" - << (void *)t.trapAddr; + << (void*)t.trapAddr; } diff --git a/src/TreeBuilder.cpp b/src/TreeBuilder.cpp index c214ddd..0d0ad84 100644 --- a/src/TreeBuilder.cpp +++ b/src/TreeBuilder.cpp @@ -63,7 +63,7 @@ TreeBuilder::TreeBuilder(Config c) : config{std::move(c)} { } struct TreeBuilder::Variable { - struct drgn_type *type; + struct drgn_type* type; std::string_view name; std::string typePath; std::optional isset = std::nullopt; @@ -196,12 +196,12 @@ TreeBuilder::~TreeBuilder() { bool TreeBuilder::emptyOutput() const { return std::ranges::all_of(rootIDs, - [](auto &id) { return id == ERROR_NODE_ID; }); + [](auto& id) { return id == ERROR_NODE_ID; }); } -void TreeBuilder::build(const std::vector &data, - const std::string &argName, struct drgn_type *type, - const TypeHierarchy &typeHierarchy) { +void TreeBuilder::build(const std::vector& data, + const std::string& argName, struct drgn_type* type, + const TypeHierarchy& typeHierarchy) { th = &typeHierarchy; oidData = &data; @@ -212,7 +212,7 @@ void TreeBuilder::build(const std::vector &data, VLOG(1) << "Building tree..."; { - auto &rootID = rootIDs.emplace_back(nextNodeID++); + auto& rootID = rootIDs.emplace_back(nextNodeID++); try { // The first value is the address of the root object @@ -276,11 +276,11 @@ void TreeBuilder::dumpJson() { } void TreeBuilder::setPaddedStructs( - std::map *_paddedStructs) { + std::map* _paddedStructs) { this->paddedStructs = _paddedStructs; } -static std::string drgnTypeToName(struct drgn_type *type) { +static std::string drgnTypeToName(struct drgn_type* type) { if (type->_private.program != nullptr) { return drgn_utils::typeToName(type); } @@ -288,15 +288,15 @@ static std::string drgnTypeToName(struct drgn_type *type) { return type->_private.oi_name ? type->_private.oi_name : ""; } -static struct drgn_error *drgnTypeSizeof(struct drgn_type *type, - uint64_t *ret) { +static struct drgn_error* drgnTypeSizeof(struct drgn_type* type, + uint64_t* ret) { static struct drgn_error incompleteTypeError = { .code = DRGN_ERROR_TYPE, .needs_destroy = false, .errnum = 0, .path = NULL, .address = 0, - .message = (char *)"cannot get size of incomplete type", + .message = (char*)"cannot get size of incomplete type", }; if (drgn_type_kind(type) == DRGN_TYPE_FUNCTION) { @@ -318,9 +318,9 @@ static struct drgn_error *drgnTypeSizeof(struct drgn_type *type, return nullptr; } -uint64_t TreeBuilder::getDrgnTypeSize(struct drgn_type *type) { +uint64_t TreeBuilder::getDrgnTypeSize(struct drgn_type* type) { uint64_t size = 0; - struct drgn_error *err = drgnTypeSizeof(type, &size); + struct drgn_error* err = drgnTypeSizeof(type, &size); BOOST_SCOPE_EXIT(err) { drgn_error_destroy(err); } @@ -330,7 +330,7 @@ uint64_t TreeBuilder::getDrgnTypeSize(struct drgn_type *type) { } std::string typeName = drgnTypeToName(type); - for (auto &[typeName2, size2] : th->sizeMap) + for (auto& [typeName2, size2] : th->sizeMap) if (typeName.starts_with(typeName2)) return size2; @@ -347,17 +347,17 @@ uint64_t TreeBuilder::next() { if (oidDataIndex >= oidData->size()) { throw std::runtime_error("Unexpected end of data"); } - VLOG(3) << "next = " << (void *)(*oidData)[oidDataIndex]; + VLOG(3) << "next = " << (void*)(*oidData)[oidDataIndex]; return (*oidData)[oidDataIndex++]; } -bool TreeBuilder::isContainer(const Variable &variable) { +bool TreeBuilder::isContainer(const Variable& variable) { return th->containerTypeMap.contains(variable.type) || (drgn_type_kind(variable.type) == DRGN_TYPE_ARRAY && drgn_type_length(variable.type) > 0); } -bool TreeBuilder::isPrimitive(struct drgn_type *type) { +bool TreeBuilder::isPrimitive(struct drgn_type* type) { while (drgn_type_kind(type) == DRGN_TYPE_TYPEDEF) { auto entry = th->typedefMap.find(type); if (entry == th->typedefMap.end()) @@ -376,14 +376,14 @@ bool TreeBuilder::shouldProcess(uintptr_t pointer) { return unprocessed; } -static std::string_view drgnKindStr(struct drgn_type *type) { +static std::string_view drgnKindStr(struct drgn_type* type) { auto kind = OICodeGen::drgnKindStr(type); // -1 is for the null terminator kind.remove_prefix(sizeof("DRGN_TYPE_") - 1); return kind; } -void TreeBuilder::setSize(TreeBuilder::Node &node, uint64_t dynamicSize, +void TreeBuilder::setSize(TreeBuilder::Node& node, uint64_t dynamicSize, uint64_t memberSizes) { node.dynamicSize = dynamicSize; if (memberSizes > node.staticSize + node.dynamicSize) { @@ -469,14 +469,14 @@ TreeBuilder::Node TreeBuilder::process(NodeID id, Variable variable) { } else if (isContainer(variable)) { processContainer(variable, node); } else { - drgn_type *objectType = variable.type; + drgn_type* objectType = variable.type; if (auto it = th->descendantClasses.find(objectType); it != th->descendantClasses.end()) { // The first item of data in dynamic classes identifies which // concrete type we should process it as, represented as an index // into the vector of child classes, or -1 to processes this type // as itself. - const auto &descendants = it->second; + const auto& descendants = it->second; auto val = next(); if (val != (uint64_t)-1) { objectType = descendants[val]; @@ -490,7 +490,7 @@ TreeBuilder::Node TreeBuilder::process(NodeID id, Variable variable) { break; } - const auto &members = entry->second; + const auto& members = entry->second; node.children = {nextNodeID, nextNodeID + members.size()}; nextNodeID += members.size(); auto childID = node.children->first; @@ -511,7 +511,7 @@ TreeBuilder::Node TreeBuilder::process(NodeID id, Variable variable) { isset = val; } } - const auto &member = members[i]; + const auto& member = members[i]; auto child = process(childID++, Variable{member.type, member.member_name, @@ -548,7 +548,7 @@ TreeBuilder::Node TreeBuilder::process(NodeID id, Variable variable) { return node; } -void TreeBuilder::processContainer(const Variable &variable, Node &node) { +void TreeBuilder::processContainer(const Variable& variable, Node& node) { VLOG(1) << "Processing container [" << node.id << "] of type '" << node.typeName << "'"; ContainerTypeEnum kind = UNKNOWN_TYPE; @@ -556,7 +556,7 @@ void TreeBuilder::processContainer(const Variable &variable, Node &node) { if (drgn_type_kind(variable.type) == DRGN_TYPE_ARRAY) { kind = ARRAY_TYPE; - struct drgn_type *arrayElementType = nullptr; + struct drgn_type* arrayElementType = nullptr; size_t numElems = 0; drgn_utils::getDrgnArrayElementType(variable.type, &arrayElementType, numElems); @@ -571,9 +571,9 @@ void TreeBuilder::processContainer(const Variable &variable, Node &node) { node.typeName + "'"); } - auto &[containerInfo, templateTypes] = entry->second; + auto& [containerInfo, templateTypes] = entry->second; kind = containerInfo.ctype; - for (const auto &tt : templateTypes) { + for (const auto& tt : templateTypes) { elementTypes.push_back(tt); } } @@ -591,10 +591,10 @@ void TreeBuilder::processContainer(const Variable &variable, Node &node) { // Initialize, then take a reference to the underlying value for convenience // so that we don't have to dereference the optional every time we want to use // it. - auto &containerStats = + auto& containerStats = node.containerStats.emplace(Node::ContainerStats{0, 0, 0}); - for (auto &type : elementTypes) { + for (auto& type : elementTypes) { containerStats.elementStaticSize += getDrgnTypeSize(type.type); } @@ -659,7 +659,7 @@ void TreeBuilder::processContainer(const Variable &variable, Node &node) { case STD_VARIANT_TYPE: { containerStats.length = containerStats.capacity = 1; containerStats.elementStaticSize = 0; - for (auto &type : elementTypes) { + for (auto& type : elementTypes) { auto paramSize = getDrgnTypeSize(type.type); containerStats.elementStaticSize = std::max(containerStats.elementStaticSize, paramSize); @@ -797,7 +797,7 @@ void TreeBuilder::processContainer(const Variable &variable, Node &node) { containerStats.elementStaticSize += next(); size_t bucketCount = next(); // Both libc++ and libstdc++ define buckets as an array of raw pointers - setSize(node, node.dynamicSize + bucketCount * sizeof(void *), 0); + setSize(node, node.dynamicSize + bucketCount * sizeof(void*), 0); containerStats.length = containerStats.capacity = next(); } break; case F14_MAP: @@ -846,7 +846,7 @@ void TreeBuilder::processContainer(const Variable &variable, Node &node) { } if (std::ranges::all_of( elementTypes.cbegin(), elementTypes.cend(), - [this](auto &type) { return isPrimitive(type.type); })) { + [this](auto& type) { return isPrimitive(type.type); })) { VLOG(1) << "Container [" << node.id << "] contains only primitive types, skipping processing its members"; @@ -865,7 +865,7 @@ void TreeBuilder::processContainer(const Variable &variable, Node &node) { auto childID = node.children->first; uint64_t memberSizes = 0; for (size_t i = 0; i < containerStats.length; i++) { - for (auto &type : elementTypes) { + for (auto& type : elementTypes) { auto child = process(childID++, {.type = type.type, .name = "", @@ -878,7 +878,7 @@ void TreeBuilder::processContainer(const Variable &variable, Node &node) { } template -std::string_view TreeBuilder::serialize(const T &data) { +std::string_view TreeBuilder::serialize(const T& data) { buffer->clear(); msgpack::pack(*buffer, data); // It is *very* important that we construct the `std::string_view` with an @@ -886,7 +886,7 @@ std::string_view TreeBuilder::serialize(const T &data) { return std::string_view(buffer->data(), buffer->size()); } -void TreeBuilder::JSON(NodeID id, std::ofstream &output) { +void TreeBuilder::JSON(NodeID id, std::ofstream& output) { std::string data; auto status = db->Get(rocksdb::ReadOptions(), std::to_string(id), &data); if (!status.ok()) { diff --git a/src/TreeBuilder.h b/src/TreeBuilder.h index a29be72..f12e0fc 100644 --- a/src/TreeBuilder.h +++ b/src/TreeBuilder.h @@ -49,10 +49,10 @@ class TreeBuilder { TreeBuilder(Config); ~TreeBuilder(); - void build(const std::vector &, const std::string &, - struct drgn_type *, const TypeHierarchy &); + void build(const std::vector&, const std::string&, + struct drgn_type*, const TypeHierarchy&); void dumpJson(); - void setPaddedStructs(std::map *paddedStructs); + void setPaddedStructs(std::map* paddedStructs); bool emptyOutput() const; private: @@ -62,9 +62,9 @@ class TreeBuilder { struct Node; struct Variable; - const TypeHierarchy *th = nullptr; - const std::vector *oidData = nullptr; - std::map *paddedStructs = nullptr; + const TypeHierarchy* th = nullptr; + const std::vector* oidData = nullptr; + std::map* paddedStructs = nullptr; /* * The RocksDB output needs versioning so they are imported correctly in @@ -98,20 +98,20 @@ class TreeBuilder { * to allocate a new buffer every time we serialize a `Node`. */ std::unique_ptr buffer; - rocksdb::DB *db = nullptr; + rocksdb::DB* db = nullptr; std::unordered_set pointers{}; - uint64_t getDrgnTypeSize(struct drgn_type *type); + uint64_t getDrgnTypeSize(struct drgn_type* type); uint64_t next(); - bool isContainer(const Variable &variable); - bool isPrimitive(struct drgn_type *type); + bool isContainer(const Variable& variable); + bool isPrimitive(struct drgn_type* type); bool shouldProcess(uintptr_t pointer); Node process(NodeID id, Variable variable); - void processContainer(const Variable &variable, Node &node); + void processContainer(const Variable& variable, Node& node); template - std::string_view serialize(const T &); - void JSON(NodeID id, std::ofstream &output); + std::string_view serialize(const T&); + void JSON(NodeID id, std::ofstream& output); - static void setSize(TreeBuilder::Node &node, uint64_t dynamicSize, + static void setSize(TreeBuilder::Node& node, uint64_t dynamicSize, uint64_t memberSizes); }; diff --git a/test/integration/runner_common.cpp b/test/integration/runner_common.cpp index 498d4e8..a44b518 100644 --- a/test/integration/runner_common.cpp +++ b/test/integration/runner_common.cpp @@ -49,7 +49,7 @@ void usage(std::string_view progname) { std::cout << opts; } -int main(int argc, char *argv[]) { +int main(int argc, char* argv[]) { ::testing::InitGoogleTest(&argc, argv); int c; @@ -86,7 +86,7 @@ int main(int argc, char *argv[]) { void IntegrationBase::SetUp() { // Move into a temporary directory to run the test in an isolated environment auto tmp_dir = TmpDirStr(); - char *res = mkdtemp(&tmp_dir[0]); + char* res = mkdtemp(&tmp_dir[0]); if (!res) abort(); workingDir = std::move(tmp_dir); @@ -94,7 +94,7 @@ void IntegrationBase::SetUp() { } void IntegrationBase::TearDown() { - const ::testing::TestInfo *const test_info = + const ::testing::TestInfo* const test_info = ::testing::UnitTest::GetInstance()->current_test_info(); if (preserve || (preserve_on_failure && test_info->result()->Failed())) { std::cerr << "Working directory preserved at: " << workingDir << std::endl; @@ -103,7 +103,7 @@ void IntegrationBase::TearDown() { } } -int IntegrationBase::exit_code(Proc &proc) { +int IntegrationBase::exit_code(Proc& proc) { proc.ctx.run(); proc.proc.wait(); @@ -122,7 +122,7 @@ int IntegrationBase::exit_code(Proc &proc) { return proc.proc.exit_code(); } -fs::path IntegrationBase::createCustomConfig(const std::string &extraConfig) { +fs::path IntegrationBase::createCustomConfig(const std::string& extraConfig) { // If no extra config provided, return the config path unaltered. if (extraConfig.empty()) { return configFile; @@ -135,19 +135,19 @@ fs::path IntegrationBase::createCustomConfig(const std::string &extraConfig) { // moving the file to the temporary directory. fs::path configDirectory = fs::path(configFile).remove_filename(); - if (toml::table *types = config["types"].as_table()) { - if (toml::array *arr = (*types)["containers"].as_array()) { - arr->for_each([&](auto &&el) { + if (toml::table* types = config["types"].as_table()) { + if (toml::array* arr = (*types)["containers"].as_array()) { + arr->for_each([&](auto&& el) { if constexpr (toml::is_string) { el = configDirectory / el.get(); } }); } } - if (toml::table *headers = config["headers"].as_table()) { - for (auto &path : {"user_paths", "system_paths"}) { - if (toml::array *arr = (*headers)[path].as_array()) { - arr->for_each([&](auto &&el) { + if (toml::table* headers = config["headers"].as_table()) { + for (auto& path : {"user_paths", "system_paths"}) { + if (toml::array* arr = (*headers)[path].as_array()) { + arr->for_each([&](auto&& el) { if constexpr (toml::is_string) { el = configDirectory / el.get(); } @@ -215,7 +215,7 @@ OidProc OidIntegration::runOidOnProcess(OidOpts opts, if (verbose) { std::cerr << "Running: " << targetExe << "\n"; std::cerr << "Running: " << oidExe << " "; - for (const auto &arg : oid_args) { + for (const auto& arg : oid_args) { std::cerr << arg << " "; } std::cerr << std::endl; @@ -272,9 +272,9 @@ OidProc OidIntegration::runOidOnProcess(OidOpts opts, }; } -void OidIntegration::compare_json(const bpt::ptree &expected_json, - const bpt::ptree &actual_json, - const std::string &full_key, bool expect_eq) { +void OidIntegration::compare_json(const bpt::ptree& expected_json, + const bpt::ptree& actual_json, + const std::string& full_key, bool expect_eq) { if (expected_json.empty()) { if (expect_eq) { ASSERT_EQ(expected_json.data(), actual_json.data()) @@ -303,14 +303,14 @@ void OidIntegration::compare_json(const bpt::ptree &expected_json, } // Compare as Key-Value pairs - for (const auto &[key, val] : expected_json) { + for (const auto& [key, val] : expected_json) { if (key == "NOT") { auto curr_key = full_key + ".NOT"; ASSERT_EQ(true, expect_eq) << "Invalid expected data: " << curr_key << " - Can not use nested \"NOT\" expressions"; if (val.empty()) { // Check that a given single key does not exist - const auto &key_to_check = val.data(); + const auto& key_to_check = val.data(); auto actual_it = actual_json.find(key_to_check); auto curr_key = full_key + "." + key_to_check; if (actual_it != actual_json.not_found()) { diff --git a/test/integration/runner_common.h b/test/integration/runner_common.h index 45002d5..4c88f8f 100644 --- a/test/integration/runner_common.h +++ b/test/integration/runner_common.h @@ -11,14 +11,14 @@ #include struct OidOpts { - boost::asio::io_context &ctx; + boost::asio::io_context& ctx; std::string targetArgs; std::string script; std::string scriptSource; }; struct Proc { - boost::asio::io_context &ctx; + boost::asio::io_context& ctx; boost::process::child proc; boost::process::child tee_stdout; boost::process::child tee_stderr; @@ -37,8 +37,8 @@ class IntegrationBase : public ::testing::Test { void TearDown() override; void SetUp() override; - int exit_code(Proc &proc); - std::filesystem::path createCustomConfig(const std::string &extra); + int exit_code(Proc& proc); + std::filesystem::path createCustomConfig(const std::string& extra); std::filesystem::path workingDir; @@ -59,9 +59,9 @@ class OidIntegration : public IntegrationBase { * Compares two JSON objects for equality if "expect_eq" is true, or for * inequality if "expect_eq" is false. */ - void compare_json(const boost::property_tree::ptree &expected_json, - const boost::property_tree::ptree &actual_json, - const std::string &full_key = "root", + void compare_json(const boost::property_tree::ptree& expected_json, + const boost::property_tree::ptree& actual_json, + const std::string& full_key = "root", bool expect_eq = true); }; diff --git a/test/integration_mttest.cpp b/test/integration_mttest.cpp index 38466d8..2760538 100644 --- a/test/integration_mttest.cpp +++ b/test/integration_mttest.cpp @@ -26,7 +26,7 @@ typedef union { float f; char c; struct { - int *myArray[3]; + int* myArray[3]; float myArray2[2][2][3]; } myStruct; double array[10][20]; @@ -50,20 +50,20 @@ typedef ST ST2; std::string bar; };*/ -using Deleter1 = void (*)(int *); -using Deleter2 = std::function; +using Deleter1 = void (*)(int*); +using Deleter2 = std::function; -int *create() { - int *i = new int; +int* create() { + int* i = new int; *i = 10; return i; } -void destroy(int *i) { +void destroy(int* i) { delete i; } struct Destroyer { - void operator()(int *i) const { + void operator()(int* i) const { delete i; } }; @@ -146,25 +146,25 @@ Foo myGlobalFoo; // std::unique_ptr myGlobalFoo; // pass in the loop counter in the args -std::vector doStuff(Foo &foo, - std::vector> &m, - std::vector &f, - std::vector> &p) { +std::vector doStuff(Foo& foo, + std::vector>& m, + std::vector& f, + std::vector>& p) { std::vector altvect = {1, 3, 5, 7}; foo.inc(); foo.incN(altvect.size()); foo.incVectSize(altvect); std::cout << " doStuff entries: " << f.size() << std::endl; - std::cout << " addr of f = " << reinterpret_cast(&f) << std::endl; - std::cout << " addr of m = " << reinterpret_cast(&m) << std::endl; - std::cout << " addr of p = " << reinterpret_cast(&p) << std::endl; + std::cout << " addr of f = " << reinterpret_cast(&f) << std::endl; + std::cout << " addr of m = " << reinterpret_cast(&m) << std::endl; + std::cout << " addr of p = " << reinterpret_cast(&p) << std::endl; std::cout << " addr of myGlobalFoo = " - << reinterpret_cast(&myGlobalFoo) << std::endl; + << reinterpret_cast(&myGlobalFoo) << std::endl; std::vector newvect(altvect); - std::cout << " addr of newvect = " << reinterpret_cast(&newvect) + std::cout << " addr of newvect = " << reinterpret_cast(&newvect) << std::endl; /* @@ -179,7 +179,7 @@ std::vector doStuff(Foo &foo, return newvect; } -void doStuff(std::vector &f, int i) { +void doStuff(std::vector& f, int i) { f.push_back(i); std::cout << "Entries in f: " << f.size() << std::endl; } @@ -188,9 +188,9 @@ void doNothing() { std::cout << "I do nothing, the function does nothing" << std::endl; } -void *doit(void *arg) { +void* doit(void* arg) { doNothing(); - int *loopcnt = reinterpret_cast(arg); + int* loopcnt = reinterpret_cast(arg); std::vector f; f.reserve(200); std::vector> mv; @@ -277,11 +277,11 @@ void *doit(void *arg) { pthread_exit(arg); } -int main(int argc, char *argv[]) { +int main(int argc, char* argv[]) { int i = 0; int err; pthread_t tid[2]; - char *b; + char* b; // facebook::initFacebook(&argc, &argv); @@ -323,7 +323,7 @@ int main(int argc, char *argv[]) { std::cout << "nameList vector addr = " << &nameList << std::endl; for (int i = 0; i < 1; ++i) { - err = pthread_create(&(tid[i]), NULL, &doit, (void **)&loopcnt); + err = pthread_create(&(tid[i]), NULL, &doit, (void**)&loopcnt); if (err != 0) { std::cout << "Failed to create thread:[ " << strerror(err) << " ]" @@ -337,7 +337,7 @@ int main(int argc, char *argv[]) { } for (int i = 0; i < 1; ++i) { - pthread_join(tid[i], (void **)&b); + pthread_join(tid[i], (void**)&b); } exit(EXIT_SUCCESS); diff --git a/test/integration_sleepy.cpp b/test/integration_sleepy.cpp index 82315b4..3615ba2 100644 --- a/test/integration_sleepy.cpp +++ b/test/integration_sleepy.cpp @@ -77,31 +77,31 @@ Foo myGlobalFoo; // std::unique_ptr myGlobalFoo; // pass in the loop counter in the args -std::vector doStuff(Foo &foo, - std::vector> &m, - std::vector &f, - std::vector> &p) { +std::vector doStuff(Foo& foo, + std::vector>& m, + std::vector& f, + std::vector>& p) { std::vector altvect = {1, 3, 5, 7}; foo.inc(); foo.incVectSize(altvect); myGlobalFoo.vectorOfStr.push_back(std::string("Test String")); std::cout << " doStuff entries: " << f.size() << std::endl; - std::cout << " addr of f = " << reinterpret_cast(&f) << std::endl; - std::cout << " addr of m = " << reinterpret_cast(&m) << std::endl; - std::cout << " addr of p = " << reinterpret_cast(&p) << std::endl; + std::cout << " addr of f = " << reinterpret_cast(&f) << std::endl; + std::cout << " addr of m = " << reinterpret_cast(&m) << std::endl; + std::cout << " addr of p = " << reinterpret_cast(&p) << std::endl; std::cout << " addr of myGlobalFoo = " - << reinterpret_cast(&myGlobalFoo) << std::endl; + << reinterpret_cast(&myGlobalFoo) << std::endl; std::vector newvect(altvect); - std::cout << " addr of newvect = " << reinterpret_cast(&newvect) + std::cout << " addr of newvect = " << reinterpret_cast(&newvect) << std::endl; return newvect; } -void doStuff(std::vector &f, int i) { +void doStuff(std::vector& f, int i) { f.push_back(i); std::cout << "Entries in f: " << f.size() << std::endl; } @@ -110,9 +110,9 @@ void doNothing() { std::cout << "I do nothing, the function does nothing" << std::endl; } -void *doit(void *arg) { +void* doit(void* arg) { doNothing(); - int *loopcnt = reinterpret_cast(arg); + int* loopcnt = reinterpret_cast(arg); std::vector f; f.reserve(200); std::vector> mv; @@ -199,11 +199,11 @@ void *doit(void *arg) { pthread_exit(arg); } -int main(int argc, char *argv[]) { +int main(int argc, char* argv[]) { int i = 0; int err; pthread_t tid[2]; - char *b; + char* b; // facebook::initFacebook(&argc, &argv); @@ -245,7 +245,7 @@ int main(int argc, char *argv[]) { std::cout << "nameList vector addr = " << &nameList << std::endl; for (int i = 0; i < 1; ++i) { - err = pthread_create(&(tid[i]), NULL, &doit, (void **)&loopcnt); + err = pthread_create(&(tid[i]), NULL, &doit, (void**)&loopcnt); if (err != 0) { std::cout << "Failed to create thread:[ " << strerror(err) << " ]" @@ -259,7 +259,7 @@ int main(int argc, char *argv[]) { } for (int i = 0; i < 1; ++i) { - pthread_join(tid[i], (void **)&b); + pthread_join(tid[i], (void**)&b); } exit(EXIT_SUCCESS); diff --git a/test/mttest.h b/test/mttest.h index 4b94759..563284a 100644 --- a/test/mttest.h +++ b/test/mttest.h @@ -12,13 +12,13 @@ struct OIDTestingTwoString { }; struct customTwoStringEq { - bool operator()(const OIDTestingTwoString &a, const OIDTestingTwoString &b) { + bool operator()(const OIDTestingTwoString& a, const OIDTestingTwoString& b) { return (a.first == a.first && a.second == b.second); } }; struct customHash { - std::size_t operator()(const OIDTestingTwoString &two) const { + std::size_t operator()(const OIDTestingTwoString& two) const { return ((std::hash()(two.first) ^ (std::hash()(two.second)))); } diff --git a/test/test_compiler.cpp b/test/test_compiler.cpp index feabfe5..5f942d5 100644 --- a/test/test_compiler.cpp +++ b/test/test_compiler.cpp @@ -38,7 +38,7 @@ TEST(CompilerTest, CompileAndRelocate) { )"; auto tmpdir = fs::temp_directory_path() / "test-XXXXXX"; - EXPECT_NE(mkdtemp(const_cast(tmpdir.c_str())), nullptr); + EXPECT_NE(mkdtemp(const_cast(tmpdir.c_str())), nullptr); auto sourcePath = tmpdir / "src.cpp"; auto objectPath = tmpdir / "obj.o"; @@ -48,7 +48,7 @@ TEST(CompilerTest, CompileAndRelocate) { EXPECT_TRUE(compiler.compile(code, sourcePath, objectPath)); const size_t relocSlabSize = 4096; - void *relocSlab = + void* relocSlab = mmap(nullptr, relocSlabSize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); EXPECT_NE(relocSlab, nullptr); @@ -57,20 +57,20 @@ TEST(CompilerTest, CompileAndRelocate) { compiler.applyRelocs((uintptr_t)relocSlab, {objectPath}, {}); EXPECT_TRUE(relocResult.has_value()); - auto &[_, segs, jitSymbols] = relocResult.value(); + auto& [_, segs, jitSymbols] = relocResult.value(); EXPECT_EQ(segs.size(), 1); EXPECT_EQ(jitSymbols.size(), 3); // 2 functions + 1 static array { - const auto &lastSeg = segs.back(); + const auto& lastSeg = segs.back(); auto lastSegLimit = lastSeg.RelocAddr + lastSeg.Size; auto relocLimit = (uintptr_t)relocSlab + relocSlabSize; EXPECT_LE(lastSegLimit, relocLimit); } - for (const auto &[Base, Reloc, Size] : segs) - std::memcpy((void *)Reloc, (void *)Base, Size); + for (const auto& [Base, Reloc, Size] : segs) + std::memcpy((void*)Reloc, (void*)Base, Size); { auto symAddr = jitSymbols.at("constant"); @@ -113,7 +113,7 @@ TEST(CompilerTest, CompileAndRelocateMultipleObjs) { )"; auto tmpdir = fs::temp_directory_path() / "test-XXXXXX"; - EXPECT_NE(mkdtemp(const_cast(tmpdir.c_str())), nullptr); + EXPECT_NE(mkdtemp(const_cast(tmpdir.c_str())), nullptr); auto sourceXPath = tmpdir / "srcX.cpp"; auto objectXPath = tmpdir / "objX.o"; @@ -126,7 +126,7 @@ TEST(CompilerTest, CompileAndRelocateMultipleObjs) { EXPECT_TRUE(compiler.compile(codeY, sourceYPath, objectYPath)); const size_t relocSlabSize = 8192; - void *relocSlab = + void* relocSlab = mmap(nullptr, relocSlabSize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); EXPECT_NE(relocSlab, nullptr); @@ -135,20 +135,20 @@ TEST(CompilerTest, CompileAndRelocateMultipleObjs) { {objectXPath, objectYPath}, {}); EXPECT_TRUE(relocResult.has_value()); - auto &[_, segs, jitSymbols] = relocResult.value(); + auto& [_, segs, jitSymbols] = relocResult.value(); EXPECT_EQ(segs.size(), 2); EXPECT_EQ(jitSymbols.size(), 4); // 2 functions + 2 static array { - const auto &lastSeg = segs.back(); + const auto& lastSeg = segs.back(); auto lastSegLimit = lastSeg.RelocAddr + lastSeg.Size; auto relocLimit = (uintptr_t)relocSlab + relocSlabSize; EXPECT_LE(lastSegLimit, relocLimit); } - for (const auto &[Base, Reloc, Size] : segs) - std::memcpy((void *)Reloc, (void *)Base, Size); + for (const auto& [Base, Reloc, Size] : segs) + std::memcpy((void*)Reloc, (void*)Base, Size); { auto symAddr = jitSymbols.at("sumXs"); diff --git a/test/test_parser.cpp b/test/test_parser.cpp index 3954bcf..d9bf935 100644 --- a/test/test_parser.cpp +++ b/test/test_parser.cpp @@ -12,7 +12,7 @@ using ::testing::HasSubstr; using namespace ObjectIntrospection; // Utilities -static ParseData parseString(const std::string &script) { +static ParseData parseString(const std::string& script) { ParseData pdata; std::istringstream input{script}; OIScanner scanner{&input}; @@ -23,7 +23,7 @@ static ParseData parseString(const std::string &script) { return pdata; } -static std::string parseBadString(const std::string &script) { +static std::string parseBadString(const std::string& script) { ParseData pdata; std::istringstream input{script}; OIScanner scanner{&input}; @@ -34,9 +34,9 @@ static std::string parseBadString(const std::string &script) { return testing::internal::GetCapturedStderr(); } -static void EXPECT_REQ_EQ(const prequest &req, const std::string &type, - const std::string &func, - const std::vector &args) { +static void EXPECT_REQ_EQ(const prequest& req, const std::string& type, + const std::string& func, + const std::vector& args) { EXPECT_EQ(req.type, type); EXPECT_EQ(req.func, func); EXPECT_EQ(req.args, args); diff --git a/tools/OITB.cpp b/tools/OITB.cpp index 623f75d..3212cf3 100644 --- a/tools/OITB.cpp +++ b/tools/OITB.cpp @@ -44,7 +44,7 @@ constexpr static OIOpts opts{ "(in addition to the default RocksDB output)"}, }; -static void usage(std::ostream &out) { +static void usage(std::ostream& out) { out << "Run TreeBuilder on the given data-segment dump.\n"; out << "oitb aims to facilitate debugging issues from TreeBuilder, by " "allowing local iterations and debugging.\n"; @@ -59,7 +59,7 @@ static void usage(std::ostream &out) { } template -[[noreturn]] static void fatal_error(Args &&...args) { +[[noreturn]] static void fatal_error(Args&&... args) { std::cerr << "error: "; (std::cerr << ... << args); std::cerr << "\n\n"; @@ -107,15 +107,15 @@ static auto loadDatasegDump(fs::path datasegDumpPath) { { /* Read the dump into the dataseg vector */ auto datasegBytes = std::as_writable_bytes(std::span{dataseg}); - dump.read((char *)datasegBytes.data(), datasegBytes.size_bytes()); + dump.read((char*)datasegBytes.data(), datasegBytes.size_bytes()); } return dataseg; } [[maybe_unused]] /* For debugging... */ -static std::ostream & -operator<<(std::ostream &out, TreeBuilder::Config tbc) { +static std::ostream& +operator<<(std::ostream& out, TreeBuilder::Config tbc) { out << "TreeBuilde::Config = ["; out << "\n logAllStructs = " << tbc.logAllStructs; out << "\n chaseRawPointers = " << tbc.chaseRawPointers; @@ -126,7 +126,7 @@ operator<<(std::ostream &out, TreeBuilder::Config tbc) { return out; } -int main(int argc, char *argv[]) { +int main(int argc, char* argv[]) { google::InitGoogleLogging(*argv); google::LogToStderr(); google::SetStderrLogging(google::INFO);