2022-12-19 14:37:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2023-03-23 20:31:56 +00:00
|
|
|
#include <glog/logging.h>
|
|
|
|
|
2022-12-19 14:37:51 +00:00
|
|
|
#include <filesystem>
|
|
|
|
#include <fstream>
|
|
|
|
|
2023-04-26 16:20:53 +01:00
|
|
|
#include "oi/OICache.h"
|
|
|
|
#include "oi/OICodeGen.h"
|
|
|
|
#include "oi/OICompiler.h"
|
|
|
|
#include "oi/OIParser.h"
|
|
|
|
#include "oi/SymbolService.h"
|
|
|
|
#include "oi/TrapInfo.h"
|
|
|
|
#include "oi/TreeBuilder.h"
|
|
|
|
#include "oi/X86InstDefs.h"
|
2022-12-19 14:37:51 +00:00
|
|
|
|
2023-07-26 15:31:53 +01:00
|
|
|
namespace oi::detail {
|
2022-12-19 14:37:51 +00:00
|
|
|
|
|
|
|
class OIDebugger {
|
2023-06-28 16:17:45 +01:00
|
|
|
OIDebugger(const OICodeGen::Config&, OICompiler::Config, TreeBuilder::Config);
|
2022-12-19 14:37:51 +00:00
|
|
|
|
|
|
|
public:
|
2023-06-28 16:17:45 +01:00
|
|
|
OIDebugger(pid_t,
|
|
|
|
const OICodeGen::Config&,
|
|
|
|
OICompiler::Config,
|
|
|
|
TreeBuilder::Config);
|
2023-07-26 15:31:53 +01:00
|
|
|
OIDebugger(std::filesystem::path,
|
2023-06-28 16:17:45 +01:00
|
|
|
const OICodeGen::Config&,
|
2023-04-18 18:02:37 +01:00
|
|
|
OICompiler::Config,
|
2023-04-17 19:13:59 +01:00
|
|
|
TreeBuilder::Config);
|
|
|
|
|
2022-12-19 14:37:51 +00:00
|
|
|
bool segmentInit(void);
|
|
|
|
bool stopTarget(void);
|
|
|
|
bool interruptTarget(void);
|
|
|
|
bool compileCode();
|
|
|
|
bool processTargetData();
|
|
|
|
bool executeCode(pid_t);
|
|
|
|
void setDataSegmentSize(size_t);
|
|
|
|
void restoreState(void);
|
|
|
|
bool segConfigExists(void) const {
|
|
|
|
return segConfig.existingConfig;
|
|
|
|
};
|
|
|
|
enum oidMode { OID_MODE_THREAD, OID_MODE_FUNC };
|
|
|
|
void setMode(oidMode newMode) {
|
|
|
|
mode = newMode;
|
|
|
|
};
|
|
|
|
bool targetAttach(void);
|
|
|
|
enum processTrapRet { OID_ERR, OID_CONT, OID_DONE };
|
|
|
|
OIDebugger::processTrapRet processTrap(pid_t, bool = true, bool = true);
|
|
|
|
bool contTargetThread(bool detach = true) const;
|
|
|
|
bool isGlobalDataProbeEnabled(void) const;
|
2023-03-22 16:16:17 +00:00
|
|
|
static uint64_t singlestepInst(pid_t, struct user_regs_struct&);
|
2022-12-19 14:37:51 +00:00
|
|
|
static bool singleStepFunc(pid_t, uint64_t);
|
2023-03-22 16:16:17 +00:00
|
|
|
bool parseScript(std::istream& script);
|
2022-12-19 14:37:51 +00:00
|
|
|
bool patchFunctions();
|
|
|
|
void stopAll();
|
|
|
|
bool removeTraps(pid_t);
|
2023-03-22 16:16:17 +00:00
|
|
|
bool removeTrap(pid_t, const trapInfo&);
|
2022-12-19 14:37:51 +00:00
|
|
|
void enableDrgn();
|
|
|
|
bool unmapSegments(bool deleteSegConf = false);
|
|
|
|
bool isInterrupted(void) const {
|
|
|
|
return oidShouldExit;
|
|
|
|
};
|
|
|
|
|
2023-07-26 15:31:53 +01:00
|
|
|
void setCacheBasePath(std::filesystem::path basePath) {
|
|
|
|
if (std::filesystem::exists(basePath.parent_path()) &&
|
|
|
|
!std::filesystem::exists(basePath)) {
|
2022-12-19 14:37:51 +00:00
|
|
|
// Create cachedir if parent directory exists
|
|
|
|
// TODO if returning false here, throw an error
|
2023-07-26 15:31:53 +01:00
|
|
|
std::filesystem::create_directory(basePath);
|
2022-12-19 14:37:51 +00:00
|
|
|
}
|
|
|
|
cache.basePath = std::move(basePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setCacheRemoteEnabled(bool upload, bool download) {
|
|
|
|
cache.enableUpload = upload;
|
|
|
|
cache.enableDownload = download;
|
|
|
|
cache.abortOnLoadFail = download && !upload;
|
|
|
|
}
|
|
|
|
|
2023-03-23 20:31:56 +00:00
|
|
|
bool validateCache() {
|
|
|
|
if ((cache.enableUpload || cache.enableDownload) && !cache.isEnabled()) {
|
|
|
|
LOG(ERROR) << "Cache download/upload option specified when cache is "
|
|
|
|
"disabled - aborting!";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-12-19 14:37:51 +00:00
|
|
|
void setHardDisableDrgn(bool val) {
|
|
|
|
symbols->setHardDisableDrgn(val);
|
|
|
|
}
|
2023-07-07 11:55:40 +01:00
|
|
|
void setStrict(bool val) {
|
|
|
|
treeBuilderConfig.strict = val;
|
|
|
|
}
|
2022-12-19 14:37:51 +00:00
|
|
|
|
|
|
|
bool uploadCache() {
|
|
|
|
return std::all_of(
|
2023-03-22 16:16:17 +00:00
|
|
|
std::begin(pdata), std::end(pdata), [this](const auto& req) {
|
2022-12-19 14:37:51 +00:00
|
|
|
return std::all_of(
|
2023-11-13 16:50:09 +00:00
|
|
|
std::begin(req.args),
|
|
|
|
std::end(req.args),
|
2023-03-22 16:16:17 +00:00
|
|
|
[this, &req](const auto& arg) {
|
2022-12-19 14:37:51 +00:00
|
|
|
return cache.upload(irequest{req.type, req.func, arg});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
bool downloadCache() {
|
|
|
|
return std::all_of(
|
2023-03-22 16:16:17 +00:00
|
|
|
std::begin(pdata), std::end(pdata), [this](const auto& req) {
|
2022-12-19 14:37:51 +00:00
|
|
|
return std::all_of(
|
2023-11-13 16:50:09 +00:00
|
|
|
std::begin(req.args),
|
|
|
|
std::end(req.args),
|
2023-03-22 16:16:17 +00:00
|
|
|
[this, &req](const auto& arg) {
|
2022-12-19 14:37:51 +00:00
|
|
|
return cache.download(irequest{req.type, req.func, arg});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
std::pair<RootInfo, TypeHierarchy> getTreeBuilderTyping() {
|
|
|
|
assert(pdata.numReqs() == 1);
|
|
|
|
auto [type, th, _] = typeInfos.at(pdata.getReq().getReqForArg());
|
|
|
|
return {type, th};
|
|
|
|
};
|
|
|
|
|
|
|
|
std::map<std::string, PaddingInfo> getPaddingInfo() {
|
|
|
|
assert(pdata.numReqs() == 1);
|
|
|
|
return std::get<2>(typeInfos.at(pdata.getReq().getReqForArg()));
|
|
|
|
}
|
|
|
|
|
2023-07-26 15:31:53 +01:00
|
|
|
void setCustomCodeFile(std::filesystem::path newCCT) {
|
2022-12-19 14:37:51 +00:00
|
|
|
customCodeFile = std::move(newCCT);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool debug = false;
|
|
|
|
pid_t traceePid{};
|
|
|
|
uint64_t objectAddr{};
|
|
|
|
oidMode mode{OID_MODE_THREAD};
|
|
|
|
enum class SegType { text, data };
|
|
|
|
enum class StatusType {
|
|
|
|
sleep,
|
|
|
|
traced,
|
|
|
|
running,
|
|
|
|
zombie,
|
|
|
|
dead,
|
|
|
|
diskSleep,
|
|
|
|
stopped,
|
|
|
|
other,
|
|
|
|
bad
|
|
|
|
};
|
|
|
|
static OIDebugger::StatusType getTaskState(pid_t pid);
|
|
|
|
static std::string taskStateToString(OIDebugger::StatusType);
|
|
|
|
size_t dataSegSize{1 << 20};
|
|
|
|
size_t textSegSize{(1 << 22) + (1 << 20)};
|
|
|
|
std::vector<pid_t> threadList;
|
|
|
|
ParseData pdata{};
|
|
|
|
uint64_t replayInstsCurIdx{};
|
|
|
|
bool oidShouldExit{false};
|
|
|
|
uint64_t count{};
|
|
|
|
bool sigIntHandlerActive{false};
|
|
|
|
const int sizeofInt3 = 1;
|
2023-02-28 00:49:42 +00:00
|
|
|
const int sizeofUd2 = 2;
|
2022-12-19 14:37:51 +00:00
|
|
|
const int replayInstSize = 512;
|
|
|
|
bool trapsRemoved{false};
|
|
|
|
std::shared_ptr<SymbolService> symbols;
|
2023-06-28 16:17:45 +01:00
|
|
|
OICache cache;
|
2022-12-19 14:37:51 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Map address of valid INT3 instruction to metadata for that interrupt.
|
|
|
|
* It MUST be an ordered map (std::map) to handle overlapping traps.
|
|
|
|
*/
|
|
|
|
std::map<uint64_t, std::shared_ptr<trapInfo>> activeTraps;
|
|
|
|
std::unordered_map<pid_t, std::shared_ptr<trapInfo>> threadTrapState;
|
|
|
|
std::unordered_map<uintptr_t, uintptr_t> replayInstMap;
|
|
|
|
|
2023-04-18 18:02:37 +01:00
|
|
|
std::unordered_map<
|
|
|
|
irequest,
|
|
|
|
std::tuple<RootInfo, TypeHierarchy, std::map<std::string, PaddingInfo>>>
|
2022-12-19 14:37:51 +00:00
|
|
|
typeInfos;
|
|
|
|
|
|
|
|
template <typename Sys, typename... Args>
|
|
|
|
std::optional<typename Sys::RetType> remoteSyscall(Args...);
|
|
|
|
bool setupLogFile(void);
|
|
|
|
bool cleanupLogFile(void);
|
|
|
|
|
|
|
|
using ObjectAddrMap =
|
|
|
|
std::unordered_map<std::variant<std::shared_ptr<GlobalDesc>,
|
|
|
|
std::shared_ptr<FuncDesc::TargetObject>>,
|
|
|
|
uintptr_t>;
|
|
|
|
|
|
|
|
ObjectAddrMap remoteObjAddrs{};
|
|
|
|
|
|
|
|
bool setupSegment(SegType);
|
|
|
|
bool unmapSegment(SegType);
|
2023-03-22 16:16:17 +00:00
|
|
|
bool writeTargetMemory(void*, void*, size_t) const;
|
|
|
|
bool readTargetMemory(void*, void*, size_t) const;
|
2022-12-19 14:37:51 +00:00
|
|
|
std::optional<std::pair<OIDebugger::ObjectAddrMap::key_type, uintptr_t>>
|
2023-03-22 16:16:17 +00:00
|
|
|
locateJitCodeStart(const irequest&, const OICompiler::RelocResult::SymTable&);
|
|
|
|
bool writePrologue(const prequest&, const OICompiler::RelocResult::SymTable&);
|
|
|
|
bool readInstFromTarget(uintptr_t, uint8_t*, size_t);
|
2022-12-19 14:37:51 +00:00
|
|
|
void createSegmentConfigFile(void);
|
|
|
|
void deleteSegmentConfig(bool);
|
2023-03-22 16:16:17 +00:00
|
|
|
std::optional<std::shared_ptr<trapInfo>> makeTrapInfo(const prequest&,
|
2022-12-19 14:37:51 +00:00
|
|
|
const trapType,
|
|
|
|
const uint64_t);
|
2023-03-22 16:16:17 +00:00
|
|
|
bool functionPatch(const prequest&);
|
2022-12-19 14:37:51 +00:00
|
|
|
bool canProcessTrapForThread(pid_t) const;
|
2023-04-18 18:02:37 +01:00
|
|
|
bool replayTrappedInstr(const trapInfo&,
|
|
|
|
pid_t,
|
|
|
|
struct user_regs_struct&,
|
2023-03-22 16:16:17 +00:00
|
|
|
struct user_fpregs_struct&) const;
|
|
|
|
bool locateObjectsAddresses(const trapInfo&, struct user_regs_struct&);
|
2023-04-18 18:02:37 +01:00
|
|
|
processTrapRet processFuncTrap(const trapInfo&,
|
|
|
|
pid_t,
|
2023-03-22 16:16:17 +00:00
|
|
|
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<uintptr_t> nextReplayInstrAddr(const trapInfo&);
|
2022-12-19 14:37:51 +00:00
|
|
|
static int getExtendedWaitEventType(int);
|
|
|
|
static bool isExtendedWait(int);
|
|
|
|
void dumpAlltaskStates(void);
|
2023-03-22 16:16:17 +00:00
|
|
|
std::optional<std::vector<uintptr_t>> findRetLocs(FuncDesc&);
|
2023-07-21 15:57:04 +01:00
|
|
|
bool contTargetThread(pid_t, unsigned long = 0) const;
|
2022-12-19 14:37:51 +00:00
|
|
|
|
|
|
|
OICompiler::Config compilerConfig{};
|
2023-06-28 16:17:45 +01:00
|
|
|
const OICodeGen::Config& generatorConfig;
|
2022-12-19 14:37:51 +00:00
|
|
|
TreeBuilder::Config treeBuilderConfig{};
|
2023-03-22 16:16:17 +00:00
|
|
|
std::optional<std::string> generateCode(const irequest&);
|
2022-12-19 14:37:51 +00:00
|
|
|
|
|
|
|
std::fstream segmentConfigFile;
|
2023-07-26 15:31:53 +01:00
|
|
|
std::filesystem::path segConfigFilePath;
|
|
|
|
std::filesystem::path customCodeFile;
|
2022-12-19 14:37:51 +00:00
|
|
|
|
2023-11-21 10:50:31 +00:00
|
|
|
struct {
|
|
|
|
int traceeFd = -1;
|
|
|
|
int debuggerFd = -1;
|
|
|
|
} logFds;
|
|
|
|
|
2022-12-19 14:37:51 +00:00
|
|
|
struct c {
|
|
|
|
uintptr_t textSegBase{};
|
|
|
|
size_t textSegSize{};
|
|
|
|
uintptr_t constStart{};
|
|
|
|
uintptr_t jitCodeStart{};
|
|
|
|
uintptr_t replayInstBase{};
|
|
|
|
bool existingConfig{false};
|
|
|
|
uintptr_t dataSegBase{};
|
|
|
|
size_t dataSegSize{};
|
|
|
|
uintptr_t cookie{};
|
|
|
|
} segConfig{};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The first 3 words of the data segment contain:
|
|
|
|
* 1. The OID identifier a.k.a. "magic id", 01DE8 in hex
|
|
|
|
* 2. A random value (cookie) to make sure that the data
|
|
|
|
* segment we are reading from was not populated in
|
|
|
|
* an older run.
|
|
|
|
* 3. The size of the data segment as written by the JIT-ed
|
|
|
|
* code.
|
|
|
|
*/
|
|
|
|
struct DataHeader {
|
|
|
|
uintptr_t magicId;
|
|
|
|
uintptr_t cookie;
|
|
|
|
uintptr_t size;
|
2023-06-22 17:27:21 +01:00
|
|
|
uintptr_t timeTakenNs;
|
2023-11-15 16:13:54 +00:00
|
|
|
size_t pointersSize;
|
|
|
|
size_t pointersCapacity;
|
2022-12-19 14:37:51 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Flexible Array Member are not standard in C++, but this is
|
|
|
|
* exactly what we need for the `data` field. These pragmas
|
|
|
|
* disable the pedantic warnings, so the compiler stops yelling at us.
|
|
|
|
* We want the header to be the size of the fields above. This is
|
|
|
|
* important for the `decodeTargetData` method, to give the right size
|
|
|
|
* to `folly::ByteRange range(…)`.
|
|
|
|
*/
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wpedantic"
|
|
|
|
uint8_t data[];
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
};
|
|
|
|
|
2023-03-22 16:16:17 +00:00
|
|
|
bool decodeTargetData(const DataHeader&, std::vector<uint64_t>&) const;
|
2022-12-19 14:37:51 +00:00
|
|
|
|
|
|
|
static constexpr size_t prologueLength = 64;
|
|
|
|
static constexpr size_t constLength = 64;
|
|
|
|
};
|
2023-07-26 15:31:53 +01:00
|
|
|
|
|
|
|
} // namespace oi::detail
|