Running clang-format on alloy.

All except x64_sequences, which needs work.
This commit is contained in:
Ben Vanik
2014-07-10 20:20:00 -07:00
parent 0158380cfc
commit 7daa85179c
139 changed files with 6925 additions and 6998 deletions

View File

@@ -9,18 +9,16 @@
#include <alloy/runtime/debug_info.h>
using namespace alloy;
using namespace alloy::runtime;
namespace alloy {
namespace runtime {
DebugInfo::DebugInfo() :
source_disasm_(0),
raw_hir_disasm_(0),
hir_disasm_(0),
machine_code_disasm_(0),
source_map_count_(0),
source_map_(NULL) {
}
DebugInfo::DebugInfo()
: source_disasm_(0),
raw_hir_disasm_(0),
hir_disasm_(0),
machine_code_disasm_(0),
source_map_count_(0),
source_map_(NULL) {}
DebugInfo::~DebugInfo() {
xe_free(source_map_);
@@ -70,3 +68,6 @@ SourceMapEntry* DebugInfo::LookupCodeOffset(uint64_t offset) {
}
return NULL;
}
} // namespace runtime
} // namespace alloy

View File

@@ -12,35 +12,28 @@
#include <alloy/core.h>
namespace alloy {
namespace runtime {
enum DebugInfoFlags {
DEBUG_INFO_NONE = 0,
DEBUG_INFO_SOURCE_DISASM = (1 << 1),
DEBUG_INFO_RAW_HIR_DISASM = (1 << 2),
DEBUG_INFO_HIR_DISASM = (1 << 3),
DEBUG_INFO_MACHINE_CODE_DISASM = (1 << 4),
DEBUG_INFO_SOURCE_MAP = (1 << 5),
DEBUG_INFO_DEFAULT = DEBUG_INFO_SOURCE_MAP,
DEBUG_INFO_ALL_DISASM = 0xFFFF,
DEBUG_INFO_NONE = 0,
DEBUG_INFO_SOURCE_DISASM = (1 << 1),
DEBUG_INFO_RAW_HIR_DISASM = (1 << 2),
DEBUG_INFO_HIR_DISASM = (1 << 3),
DEBUG_INFO_MACHINE_CODE_DISASM = (1 << 4),
DEBUG_INFO_SOURCE_MAP = (1 << 5),
DEBUG_INFO_DEFAULT = DEBUG_INFO_SOURCE_MAP,
DEBUG_INFO_ALL_DISASM = 0xFFFF,
};
typedef struct SourceMapEntry_s {
uint64_t source_offset; // Original source address/offset.
uint64_t hir_offset; // Block ordinal (16b) | Instr ordinal (16b)
uint64_t code_offset; // Offset from emitted code start.
uint64_t source_offset; // Original source address/offset.
uint64_t hir_offset; // Block ordinal (16b) | Instr ordinal (16b)
uint64_t code_offset; // Offset from emitted code start.
} SourceMapEntry;
class DebugInfo {
public:
public:
DebugInfo();
~DebugInfo();
@@ -53,13 +46,12 @@ public:
const char* machine_code_disasm() const { return machine_code_disasm_; }
void set_machine_code_disasm(char* value) { machine_code_disasm_ = value; }
void InitializeSourceMap(size_t source_map_count,
SourceMapEntry* source_map);
void InitializeSourceMap(size_t source_map_count, SourceMapEntry* source_map);
SourceMapEntry* LookupSourceOffset(uint64_t offset);
SourceMapEntry* LookupHIROffset(uint64_t offset);
SourceMapEntry* LookupCodeOffset(uint64_t offset);
private:
private:
char* source_disasm_;
char* raw_hir_disasm_;
char* hir_disasm_;
@@ -69,9 +61,7 @@ private:
SourceMapEntry* source_map_;
};
} // namespace runtime
} // namespace alloy
#endif // ALLOY_RUNTIME_DEBUG_INFO_H_

View File

@@ -13,19 +13,15 @@
#include <alloy/runtime/runtime.h>
using namespace alloy;
using namespace alloy::runtime;
namespace alloy {
namespace runtime {
Breakpoint::Breakpoint(Type type, uint64_t address)
: type_(type), address_(address) {}
Breakpoint::Breakpoint(Type type, uint64_t address) :
type_(type), address_(address) {
}
Breakpoint::~Breakpoint() {}
Breakpoint::~Breakpoint() {
}
Debugger::Debugger(Runtime* runtime) :
runtime_(runtime) {}
Debugger::Debugger(Runtime* runtime) : runtime_(runtime) {}
Debugger::~Debugger() {}
@@ -134,8 +130,8 @@ int Debugger::RemoveBreakpoint(Breakpoint* breakpoint) {
return 0;
}
void Debugger::FindBreakpoints(
uint64_t address, std::vector<Breakpoint*>& out_breakpoints) {
void Debugger::FindBreakpoints(uint64_t address,
std::vector<Breakpoint*>& out_breakpoints) {
std::lock_guard<std::mutex> guard(breakpoints_lock_);
out_breakpoints.clear();
@@ -191,8 +187,8 @@ void Debugger::OnFunctionDefined(FunctionInfo* symbol_info,
}
}
void Debugger::OnBreakpointHit(
ThreadState* thread_state, Breakpoint* breakpoint) {
void Debugger::OnBreakpointHit(ThreadState* thread_state,
Breakpoint* breakpoint) {
// Suspend all threads immediately.
SuspendAllThreads();
@@ -202,3 +198,6 @@ void Debugger::OnBreakpointHit(
// Note that we stay suspended.
}
} // namespace runtime
} // namespace alloy

View File

@@ -15,7 +15,6 @@
#include <alloy/core.h>
namespace alloy {
namespace runtime {
@@ -25,14 +24,14 @@ class FunctionInfo;
class Runtime;
class ThreadState;
class Breakpoint {
public:
public:
enum Type {
TEMP_TYPE,
CODE_TYPE,
};
public:
public:
Breakpoint(Type type, uint64_t address);
~Breakpoint();
@@ -42,42 +41,41 @@ public:
const char* id() const { return id_.c_str(); }
void set_id(const char* id) { id_ = id; }
private:
private:
Type type_;
uint64_t address_;
std::string id_;
};
class DebugEvent {
public:
DebugEvent(Debugger* debugger) :
debugger_(debugger) {}
public:
DebugEvent(Debugger* debugger) : debugger_(debugger) {}
virtual ~DebugEvent() {}
Debugger* debugger() const { return debugger_; }
protected:
protected:
Debugger* debugger_;
};
class BreakpointHitEvent : public DebugEvent {
public:
BreakpointHitEvent(
Debugger* debugger, ThreadState* thread_state, Breakpoint* breakpoint) :
thread_state_(thread_state), breakpoint_(breakpoint),
DebugEvent(debugger) {}
public:
BreakpointHitEvent(Debugger* debugger, ThreadState* thread_state,
Breakpoint* breakpoint)
: thread_state_(thread_state),
breakpoint_(breakpoint),
DebugEvent(debugger) {}
virtual ~BreakpointHitEvent() {}
ThreadState* thread_state() const { return thread_state_; }
Breakpoint* breakpoint() const { return breakpoint_; }
protected:
protected:
ThreadState* thread_state_;
Breakpoint* breakpoint_;
};
class Debugger {
public:
public:
Debugger(Runtime* runtime);
~Debugger();
@@ -87,12 +85,12 @@ public:
int ResumeThread(uint32_t thread_id);
int ResumeAllThreads(bool force = false);
void ForEachThread(std::function<void (ThreadState*)> callback);
void ForEachThread(std::function<void(ThreadState*)> callback);
int AddBreakpoint(Breakpoint* breakpoint);
int RemoveBreakpoint(Breakpoint* breakpoint);
void FindBreakpoints(
uint64_t address, std::vector<Breakpoint*>& out_breakpoints);
void FindBreakpoints(uint64_t address,
std::vector<Breakpoint*>& out_breakpoints);
void OnThreadCreated(ThreadState* thread_state);
void OnThreadDestroyed(ThreadState* thread_state);
@@ -100,10 +98,10 @@ public:
void OnBreakpointHit(ThreadState* thread_state, Breakpoint* breakpoint);
public:
public:
Delegate<BreakpointHitEvent> breakpoint_hit;
private:
private:
Runtime* runtime_;
std::mutex threads_lock_;
@@ -115,9 +113,7 @@ private:
BreakpointMultimap breakpoints_;
};
} // namespace runtime
} // namespace alloy
#endif // ALLOY_RUNTIME_DEBUGGER_H_

View File

@@ -9,9 +9,8 @@
#include <alloy/runtime/entry_table.h>
using namespace alloy;
using namespace alloy::runtime;
namespace alloy {
namespace runtime {
EntryTable::EntryTable() = default;
@@ -75,8 +74,7 @@ std::vector<Function*> EntryTable::FindWithAddress(uint64_t address) {
std::vector<Function*> fns;
for (auto it = map_.begin(); it != map_.end(); ++it) {
Entry* entry = it->second;
if (address >= entry->address &&
address <= entry->end_address) {
if (address >= entry->address && address <= entry->end_address) {
if (entry->status == Entry::STATUS_READY) {
fns.push_back(entry->function);
}
@@ -84,3 +82,6 @@ std::vector<Function*> EntryTable::FindWithAddress(uint64_t address) {
}
return fns;
}
} // namespace runtime
} // namespace alloy

View File

@@ -14,30 +14,27 @@
#include <alloy/core.h>
namespace alloy {
namespace runtime {
class Function;
typedef struct Entry_t {
typedef enum {
STATUS_NEW = 0,
STATUS_NEW = 0,
STATUS_COMPILING,
STATUS_READY,
STATUS_FAILED,
} Status;
uint64_t address;
uint64_t end_address;
Status status;
uint64_t address;
uint64_t end_address;
Status status;
Function* function;
} Entry;
class EntryTable {
public:
public:
EntryTable();
~EntryTable();
@@ -46,16 +43,14 @@ public:
std::vector<Function*> FindWithAddress(uint64_t address);
private:
private:
// TODO(benvanik): replace with a better data structure.
std::mutex lock_;
typedef std::unordered_map<uint64_t, Entry*> EntryMap;
EntryMap map_;
};
} // namespace runtime
} // namespace alloy
#endif // ALLOY_RUNTIME_ENTRY_TABLE_H_

View File

@@ -13,14 +13,13 @@
#include <alloy/runtime/symbol_info.h>
#include <alloy/runtime/thread_state.h>
using namespace alloy;
using namespace alloy::runtime;
namespace alloy {
namespace runtime {
Function::Function(FunctionInfo* symbol_info) :
address_(symbol_info->address()),
symbol_info_(symbol_info), debug_info_(0) {
}
Function::Function(FunctionInfo* symbol_info)
: address_(symbol_info->address()),
symbol_info_(symbol_info),
debug_info_(0) {}
Function::~Function() = default;
@@ -75,16 +74,14 @@ int Function::Call(ThreadState* thread_state, uint64_t return_address) {
}
int result = 0;
if (symbol_info_->behavior() == FunctionInfo::BEHAVIOR_EXTERN) {
auto handler = symbol_info_->extern_handler();
if (handler) {
handler(thread_state->raw_context(),
symbol_info_->extern_arg0(),
handler(thread_state->raw_context(), symbol_info_->extern_arg0(),
symbol_info_->extern_arg1());
} else {
XELOGW("undefined extern call to %.8X %s",
symbol_info_->address(),
XELOGW("undefined extern call to %.8X %s", symbol_info_->address(),
symbol_info_->name());
result = 1;
}
@@ -97,3 +94,6 @@ int Function::Call(ThreadState* thread_state, uint64_t return_address) {
}
return result;
}
} // namespace runtime
} // namespace alloy

View File

@@ -16,7 +16,6 @@
#include <alloy/core.h>
#include <alloy/runtime/debug_info.h>
namespace alloy {
namespace runtime {
@@ -24,9 +23,8 @@ class Breakpoint;
class FunctionInfo;
class ThreadState;
class Function {
public:
public:
Function(FunctionInfo* symbol_info);
virtual ~Function();
@@ -41,14 +39,13 @@ public:
int Call(ThreadState* thread_state, uint64_t return_address);
protected:
protected:
Breakpoint* FindBreakpoint(uint64_t address);
virtual int AddBreakpointImpl(Breakpoint* breakpoint) { return 0; }
virtual int RemoveBreakpointImpl(Breakpoint* breakpoint) { return 0; }
virtual int CallImpl(ThreadState* thread_state,
uint64_t return_address) = 0;
virtual int CallImpl(ThreadState* thread_state, uint64_t return_address) = 0;
protected:
protected:
uint64_t address_;
FunctionInfo* symbol_info_;
DebugInfo* debug_info_;
@@ -58,9 +55,7 @@ protected:
std::vector<Breakpoint*> breakpoints_;
};
} // namespace runtime
} // namespace alloy
#endif // ALLOY_RUNTIME_FUNCTION_H_

View File

@@ -13,16 +13,11 @@
#include <alloy/runtime/function.h>
#include <alloy/runtime/runtime.h>
using namespace alloy;
using namespace alloy::runtime;
namespace alloy {
namespace runtime {
Instrument::Instrument(Runtime* runtime) :
runtime_(runtime),
memory_(runtime->memory()),
is_attached_(false) {
}
Instrument::Instrument(Runtime* runtime)
: runtime_(runtime), memory_(runtime->memory()), is_attached_(false) {}
Instrument::~Instrument() {
if (is_attached_) {
@@ -34,7 +29,7 @@ bool Instrument::Attach() {
if (is_attached_) {
return false;
}
//runtime->AttachInstrument(this);
// runtime->AttachInstrument(this);
is_attached_ = true;
return true;
}
@@ -44,16 +39,12 @@ bool Instrument::Detach() {
return false;
}
is_attached_ = false;
//runtime->DetachInstrument(this);
// runtime->DetachInstrument(this);
return true;
}
FunctionInstrument::FunctionInstrument(
Runtime* runtime, Function* function) :
target_(function),
Instrument(runtime) {
}
FunctionInstrument::FunctionInstrument(Runtime* runtime, Function* function)
: target_(function), Instrument(runtime) {}
bool FunctionInstrument::Attach() {
if (!Instrument::Attach()) {
@@ -88,12 +79,9 @@ void FunctionInstrument::Exit(ThreadState* thread_state) {
//
}
MemoryInstrument::MemoryInstrument(
Runtime* runtime, uint64_t address, uint64_t end_address) :
address_(address), end_address_(end_address),
Instrument(runtime) {
}
MemoryInstrument::MemoryInstrument(Runtime* runtime, uint64_t address,
uint64_t end_address)
: address_(address), end_address_(end_address), Instrument(runtime) {}
bool MemoryInstrument::Attach() {
if (!Instrument::Attach()) {
@@ -116,7 +104,10 @@ bool MemoryInstrument::Detach() {
return true;
}
void MemoryInstrument::Access(
ThreadState* thread_state, uint64_t address, AccessType type) {
void MemoryInstrument::Access(ThreadState* thread_state, uint64_t address,
AccessType type) {
// TODO(benvanik): get thread local instance
}
} // namespace runtime
} // namespace alloy

View File

@@ -14,7 +14,6 @@
XEDECLARECLASS1(alloy, Memory);
namespace alloy {
namespace runtime {
@@ -22,9 +21,8 @@ class Function;
class Runtime;
class ThreadState;
class Instrument {
public:
public:
Instrument(Runtime* runtime);
virtual ~Instrument();
@@ -35,15 +33,14 @@ public:
virtual bool Attach();
virtual bool Detach();
private:
Runtime* runtime_;
Memory* memory_;
bool is_attached_;
private:
Runtime* runtime_;
Memory* memory_;
bool is_attached_;
};
class FunctionInstrument : public Instrument {
public:
public:
FunctionInstrument(Runtime* runtime, Function* function);
virtual ~FunctionInstrument() {}
@@ -52,13 +49,13 @@ public:
virtual bool Attach();
virtual bool Detach();
public:
public:
void Enter(ThreadState* thread_state);
void Exit(ThreadState* thread_state);
protected:
protected:
class Instance {
public:
public:
Instance(FunctionInstrument* instrument) : instrument_(instrument) {}
virtual ~Instance() {}
@@ -75,17 +72,16 @@ protected:
// Call(target_fn/address)
// Return(opt_value)
private:
private:
FunctionInstrument* instrument_;
};
private:
private:
Function* target_;
};
class MemoryInstrument : public Instrument {
public:
public:
MemoryInstrument(Runtime* runtime, uint64_t address, uint64_t end_address);
virtual ~MemoryInstrument() {}
@@ -95,34 +91,33 @@ public:
virtual bool Attach();
virtual bool Detach();
public:
public:
enum AccessType {
ACCESS_READ = (1 << 1),
ACCESS_WRITE = (1 << 2),
};
void Access(ThreadState* thread_state, uint64_t address, AccessType type);
protected:
protected:
class Instance {
public:
public:
Instance(MemoryInstrument* instrument) : instrument_(instrument) {}
virtual ~Instance() {}
MemoryInstrument* instrument() const { return instrument_; }
virtual void OnAccess(
ThreadState* thread_state, uint64_t address, AccessType type) = 0;
virtual void OnAccess(ThreadState* thread_state, uint64_t address,
AccessType type) = 0;
private:
private:
MemoryInstrument* instrument_;
};
private:
private:
uint64_t address_;
uint64_t end_address_;
};
// ThreadInstrument
// (v Detach())
// ThreadInstrumentInstance:
@@ -141,9 +136,7 @@ private:
// v OnUnload(context)
// // get proc address?
} // namespace runtime
} // namespace alloy
#endif // ALLOY_RUNTIME_INSTRUMENT_H_

View File

@@ -14,12 +14,11 @@
#include <alloy/runtime/runtime.h>
using namespace alloy;
using namespace alloy::runtime;
namespace alloy {
namespace runtime {
Module::Module(Runtime* runtime) :
runtime_(runtime), memory_(runtime->memory()) {}
Module::Module(Runtime* runtime)
: runtime_(runtime), memory_(runtime->memory()) {}
Module::~Module() {
std::lock_guard<std::mutex> guard(lock_);
@@ -30,9 +29,7 @@ Module::~Module() {
}
}
bool Module::ContainsAddress(uint64_t address) {
return true;
}
bool Module::ContainsAddress(uint64_t address) { return true; }
SymbolInfo* Module::LookupSymbol(uint64_t address, bool wait) {
lock_.lock();
@@ -58,8 +55,9 @@ SymbolInfo* Module::LookupSymbol(uint64_t address, bool wait) {
return symbol_info;
}
SymbolInfo::Status Module::DeclareSymbol(
SymbolInfo::Type type, uint64_t address, SymbolInfo** out_symbol_info) {
SymbolInfo::Status Module::DeclareSymbol(SymbolInfo::Type type,
uint64_t address,
SymbolInfo** out_symbol_info) {
*out_symbol_info = NULL;
lock_.lock();
SymbolMap::const_iterator it = map_.find(address);
@@ -85,12 +83,12 @@ SymbolInfo::Status Module::DeclareSymbol(
} else {
// Create and return for initialization.
switch (type) {
case SymbolInfo::TYPE_FUNCTION:
symbol_info = new FunctionInfo(this, address);
break;
case SymbolInfo::TYPE_VARIABLE:
symbol_info = new VariableInfo(this, address);
break;
case SymbolInfo::TYPE_FUNCTION:
symbol_info = new FunctionInfo(this, address);
break;
case SymbolInfo::TYPE_VARIABLE:
symbol_info = new VariableInfo(this, address);
break;
}
map_[address] = symbol_info;
list_.push_back(symbol_info);
@@ -107,20 +105,20 @@ SymbolInfo::Status Module::DeclareSymbol(
return status;
}
SymbolInfo::Status Module::DeclareFunction(
uint64_t address, FunctionInfo** out_symbol_info) {
SymbolInfo::Status Module::DeclareFunction(uint64_t address,
FunctionInfo** out_symbol_info) {
SymbolInfo* symbol_info;
SymbolInfo::Status status = DeclareSymbol(
SymbolInfo::TYPE_FUNCTION, address, &symbol_info);
SymbolInfo::Status status =
DeclareSymbol(SymbolInfo::TYPE_FUNCTION, address, &symbol_info);
*out_symbol_info = (FunctionInfo*)symbol_info;
return status;
}
SymbolInfo::Status Module::DeclareVariable(
uint64_t address, VariableInfo** out_symbol_info) {
SymbolInfo::Status Module::DeclareVariable(uint64_t address,
VariableInfo** out_symbol_info) {
SymbolInfo* symbol_info;
SymbolInfo::Status status = DeclareSymbol(
SymbolInfo::TYPE_VARIABLE, address, &symbol_info);
SymbolInfo::Status status =
DeclareSymbol(SymbolInfo::TYPE_VARIABLE, address, &symbol_info);
*out_symbol_info = (VariableInfo*)symbol_info;
return status;
}
@@ -156,7 +154,7 @@ SymbolInfo::Status Module::DefineVariable(VariableInfo* symbol_info) {
return DefineSymbol((SymbolInfo*)symbol_info);
}
void Module::ForEachFunction(std::function<void (FunctionInfo*)> callback) {
void Module::ForEachFunction(std::function<void(FunctionInfo*)> callback) {
SCOPE_profile_cpu_f("alloy");
std::lock_guard<std::mutex> guard(lock_);
for (auto it = list_.begin(); it != list_.end(); ++it) {
@@ -169,7 +167,7 @@ void Module::ForEachFunction(std::function<void (FunctionInfo*)> callback) {
}
void Module::ForEachFunction(size_t since, size_t& version,
std::function<void (FunctionInfo*)> callback) {
std::function<void(FunctionInfo*)> callback) {
SCOPE_profile_cpu_f("alloy");
std::lock_guard<std::mutex> guard(lock_);
size_t count = list_.size();
@@ -204,8 +202,7 @@ int Module::ReadMap(const char* file_name) {
while (std::getline(infile, line)) {
// Remove newline.
while (line.size() &&
(line[line.size() - 1] == '\r' ||
line[line.size() - 1] == '\n')) {
(line[line.size() - 1] == '\r' || line[line.size() - 1] == '\n')) {
line.erase(line.end() - 1);
}
@@ -254,3 +251,6 @@ int Module::ReadMap(const char* file_name) {
return 0;
}
} // namespace runtime
} // namespace alloy

View File

@@ -19,16 +19,14 @@
#include <alloy/memory.h>
#include <alloy/runtime/symbol_info.h>
namespace alloy {
namespace runtime {
class Function;
class Runtime;
class Module {
public:
public:
Module(Runtime* runtime);
virtual ~Module();
@@ -39,30 +37,30 @@ public:
virtual bool ContainsAddress(uint64_t address);
SymbolInfo* LookupSymbol(uint64_t address, bool wait = true);
SymbolInfo::Status DeclareFunction(
uint64_t address, FunctionInfo** out_symbol_info);
SymbolInfo::Status DeclareVariable(
uint64_t address, VariableInfo** out_symbol_info);
SymbolInfo::Status DeclareFunction(uint64_t address,
FunctionInfo** out_symbol_info);
SymbolInfo::Status DeclareVariable(uint64_t address,
VariableInfo** out_symbol_info);
SymbolInfo::Status DefineFunction(FunctionInfo* symbol_info);
SymbolInfo::Status DefineVariable(VariableInfo* symbol_info);
void ForEachFunction(std::function<void (FunctionInfo*)> callback);
void ForEachFunction(std::function<void(FunctionInfo*)> callback);
void ForEachFunction(size_t since, size_t& version,
std::function<void (FunctionInfo*)> callback);
std::function<void(FunctionInfo*)> callback);
int ReadMap(const char* file_name);
private:
SymbolInfo::Status DeclareSymbol(
SymbolInfo::Type type, uint64_t address, SymbolInfo** out_symbol_info);
private:
SymbolInfo::Status DeclareSymbol(SymbolInfo::Type type, uint64_t address,
SymbolInfo** out_symbol_info);
SymbolInfo::Status DefineSymbol(SymbolInfo* symbol_info);
protected:
protected:
Runtime* runtime_;
Memory* memory_;
private:
private:
// TODO(benvanik): replace with a better data structure.
std::mutex lock_;
typedef std::unordered_map<uint64_t, SymbolInfo*> SymbolMap;
@@ -71,9 +69,7 @@ private:
SymbolList list_;
};
} // namespace runtime
} // namespace alloy
#endif // ALLOY_RUNTIME_MODULE_H_

View File

@@ -9,15 +9,15 @@
#include <alloy/runtime/raw_module.h>
using namespace alloy;
using namespace alloy::runtime;
namespace alloy {
namespace runtime {
RawModule::RawModule(Runtime* runtime) :
name_(0),
base_address_(0), low_address_(0), high_address_(0),
Module(runtime) {
}
RawModule::RawModule(Runtime* runtime)
: name_(0),
base_address_(0),
low_address_(0),
high_address_(0),
Module(runtime) {}
RawModule::~RawModule() {
if (base_address_) {
@@ -33,8 +33,8 @@ int RawModule::LoadFile(uint64_t base_address, const char* path) {
fseek(file, 0, SEEK_SET);
// Allocate memory.
base_address_ = memory_->HeapAlloc(
base_address, file_length, MEMORY_FLAG_ZERO);
base_address_ =
memory_->HeapAlloc(base_address, file_length, MEMORY_FLAG_ZERO);
if (!base_address_) {
fclose(file);
return 1;
@@ -59,3 +59,6 @@ int RawModule::LoadFile(uint64_t base_address, const char* path) {
bool RawModule::ContainsAddress(uint64_t address) {
return address >= low_address_ && address < high_address_;
}
} // namespace runtime
} // namespace alloy

View File

@@ -12,13 +12,11 @@
#include <alloy/runtime/module.h>
namespace alloy {
namespace runtime {
class RawModule : public Module {
public:
public:
RawModule(Runtime* runtime);
virtual ~RawModule();
@@ -28,16 +26,14 @@ public:
virtual bool ContainsAddress(uint64_t address);
private:
char* name_;
uint64_t base_address_;
uint64_t low_address_;
uint64_t high_address_;
private:
char* name_;
uint64_t base_address_;
uint64_t low_address_;
uint64_t high_address_;
};
} // namespace runtime
} // namespace alloy
#endif // ALLOY_RUNTIME_RAW_MODULE_H_

View File

@@ -14,26 +14,28 @@
#include <alloy/runtime/module.h>
#include <alloy/runtime/tracing.h>
using namespace alloy;
using namespace alloy::backend;
using namespace alloy::frontend;
using namespace alloy::runtime;
// TODO(benvanik): based on compiler support
#include <alloy/backend/ivm/ivm_backend.h>
#include <alloy/backend/x64/x64_backend.h>
DEFINE_string(runtime_backend, "any", "Runtime backend [any, ivm, x64].");
DEFINE_string(runtime_backend, "any",
"Runtime backend [any, ivm, x64].");
namespace alloy {
namespace runtime {
using alloy::backend::Backend;
using alloy::frontend::Frontend;
Runtime::Runtime(Memory* memory) :
memory_(memory), debugger_(0), backend_(0), frontend_(0) {
Runtime::Runtime(Memory* memory)
: memory_(memory), debugger_(0), backend_(0), frontend_(0) {
tracing::Initialize();
}
Runtime::~Runtime() {
{
std::lock_guard<std::mutex> guard(modules_lock_);
for (ModuleList::iterator it = modules_.begin();
it != modules_.end(); ++it) {
for (ModuleList::iterator it = modules_.begin(); it != modules_.end();
++it) {
Module* module = *it;
delete module;
}
@@ -46,10 +48,6 @@ Runtime::~Runtime() {
tracing::Flush();
}
// TODO(benvanik): based on compiler support
#include <alloy/backend/ivm/ivm_backend.h>
#include <alloy/backend/x64/x64_backend.h>
int Runtime::Initialize(Frontend* frontend, Backend* backend) {
// Must be initialized by subclass before calling into this.
XEASSERTNOTNULL(memory_);
@@ -64,27 +62,23 @@ int Runtime::Initialize(Frontend* frontend, Backend* backend) {
if (!backend) {
#if defined(ALLOY_HAS_X64_BACKEND) && ALLOY_HAS_X64_BACKEND
if (FLAGS_runtime_backend == "x64") {
backend = new alloy::backend::x64::X64Backend(
this);
backend = new alloy::backend::x64::X64Backend(this);
}
#endif // ALLOY_HAS_X64_BACKEND
#if defined(ALLOY_HAS_IVM_BACKEND) && ALLOY_HAS_IVM_BACKEND
if (FLAGS_runtime_backend == "ivm") {
backend = new alloy::backend::ivm::IVMBackend(
this);
backend = new alloy::backend::ivm::IVMBackend(this);
}
#endif // ALLOY_HAS_IVM_BACKEND
if (FLAGS_runtime_backend == "any") {
#if defined(ALLOY_HAS_X64_BACKEND) && ALLOY_HAS_X64_BACKEND
if (!backend) {
backend = new alloy::backend::x64::X64Backend(
this);
backend = new alloy::backend::x64::X64Backend(this);
}
#endif // ALLOY_HAS_X64_BACKEND
#if defined(ALLOY_HAS_IVM_BACKEND) && ALLOY_HAS_IVM_BACKEND
if (!backend) {
backend = new alloy::backend::ivm::IVMBackend(
this);
backend = new alloy::backend::ivm::IVMBackend(this);
}
#endif // ALLOY_HAS_IVM_BACKEND
}
@@ -118,8 +112,7 @@ int Runtime::AddModule(Module* module) {
Module* Runtime::GetModule(const char* name) {
std::lock_guard<std::mutex> guard(modules_lock_);
Module* result = NULL;
for (ModuleList::iterator it = modules_.begin();
it != modules_.end(); ++it) {
for (ModuleList::iterator it = modules_.begin(); it != modules_.end(); ++it) {
Module* module = *it;
if (xestrcmpa(module->name(), name) == 0) {
result = module;
@@ -147,7 +140,7 @@ int Runtime::ResolveFunction(uint64_t address, Function** out_function) {
Entry::Status status = entry_table_.GetOrCreate(address, &entry);
if (status == Entry::STATUS_NEW) {
// Needs to be generated. We have the 'lock' on it and must do so now.
// Grab symbol declaration.
FunctionInfo* symbol_info;
int result = LookupFunctionInfo(address, &symbol_info);
@@ -173,8 +166,8 @@ int Runtime::ResolveFunction(uint64_t address, Function** out_function) {
}
}
int Runtime::LookupFunctionInfo(
uint64_t address, FunctionInfo** out_symbol_info) {
int Runtime::LookupFunctionInfo(uint64_t address,
FunctionInfo** out_symbol_info) {
SCOPE_profile_cpu_f("alloy");
*out_symbol_info = NULL;
@@ -187,8 +180,8 @@ int Runtime::LookupFunctionInfo(
std::lock_guard<std::mutex> guard(modules_lock_);
// TODO(benvanik): sort by code address (if contiguous) so can bsearch.
// TODO(benvanik): cache last module low/high, as likely to be in there.
for (ModuleList::const_iterator it = modules_.begin();
it != modules_.end(); ++it) {
for (ModuleList::const_iterator it = modules_.begin(); it != modules_.end();
++it) {
Module* module = *it;
if (module->ContainsAddress(address)) {
code_module = module;
@@ -227,8 +220,8 @@ int Runtime::LookupFunctionInfo(Module* module, uint64_t address,
return 0;
}
int Runtime::DemandFunction(
FunctionInfo* symbol_info, Function** out_function) {
int Runtime::DemandFunction(FunctionInfo* symbol_info,
Function** out_function) {
SCOPE_profile_cpu_f("alloy");
*out_function = NULL;
@@ -240,7 +233,8 @@ int Runtime::DemandFunction(
if (symbol_status == SymbolInfo::STATUS_NEW) {
// Symbol is undefined, so define now.
Function* function = NULL;
int result = frontend_->DefineFunction(symbol_info, DEBUG_INFO_DEFAULT, &function);
int result =
frontend_->DefineFunction(symbol_info, DEBUG_INFO_DEFAULT, &function);
if (result) {
symbol_info->set_status(SymbolInfo::STATUS_FAILED);
return result;
@@ -263,3 +257,6 @@ int Runtime::DemandFunction(
return 0;
}
} // namespace runtime
} // namespace alloy

View File

@@ -23,16 +23,14 @@
#include <alloy/runtime/symbol_info.h>
#include <alloy/runtime/thread_state.h>
namespace alloy {
namespace runtime {
class Runtime {
public:
public:
typedef std::vector<Module*> ModuleList;
public:
public:
Runtime(Memory* memory);
virtual ~Runtime();
@@ -54,27 +52,25 @@ public:
FunctionInfo** out_symbol_info);
int ResolveFunction(uint64_t address, Function** out_function);
//uint32_t CreateCallback(void (*callback)(void* data), void* data);
// uint32_t CreateCallback(void (*callback)(void* data), void* data);
private:
private:
int DemandFunction(FunctionInfo* symbol_info, Function** out_function);
protected:
Memory* memory_;
protected:
Memory* memory_;
Debugger* debugger_;
Debugger* debugger_;
frontend::Frontend* frontend_;
backend::Backend* backend_;
backend::Backend* backend_;
EntryTable entry_table_;
std::mutex modules_lock_;
ModuleList modules_;
EntryTable entry_table_;
std::mutex modules_lock_;
ModuleList modules_;
};
} // namespace runtime
} // namespace alloy
#endif // ALLOY_RUNTIME_RUNTIME_H_

View File

@@ -9,14 +9,15 @@
#include <alloy/runtime/symbol_info.h>
using namespace alloy;
using namespace alloy::runtime;
namespace alloy {
namespace runtime {
SymbolInfo::SymbolInfo(Type type, Module* module, uint64_t address) :
type_(type), status_(STATUS_DEFINING),
module_(module), address_(address), name_(0) {
}
SymbolInfo::SymbolInfo(Type type, Module* module, uint64_t address)
: type_(type),
status_(STATUS_DEFINING),
module_(module),
address_(address),
name_(0) {}
SymbolInfo::~SymbolInfo() {
if (name_) {
@@ -31,14 +32,15 @@ void SymbolInfo::set_name(const char* name) {
name_ = xestrdupa(name);
}
FunctionInfo::FunctionInfo(Module* module, uint64_t address) :
end_address_(0), behavior_(BEHAVIOR_DEFAULT), function_(0),
SymbolInfo(SymbolInfo::TYPE_FUNCTION, module, address) {
FunctionInfo::FunctionInfo(Module* module, uint64_t address)
: end_address_(0),
behavior_(BEHAVIOR_DEFAULT),
function_(0),
SymbolInfo(SymbolInfo::TYPE_FUNCTION, module, address) {
xe_zero_struct(&extern_info_, sizeof(extern_info_));
}
FunctionInfo::~FunctionInfo() {
}
FunctionInfo::~FunctionInfo() {}
void FunctionInfo::SetupExtern(ExternHandler handler, void* arg0, void* arg1) {
behavior_ = BEHAVIOR_EXTERN;
@@ -47,9 +49,10 @@ void FunctionInfo::SetupExtern(ExternHandler handler, void* arg0, void* arg1) {
extern_info_.arg1 = arg1;
}
VariableInfo::VariableInfo(Module* module, uint64_t address) :
SymbolInfo(SymbolInfo::TYPE_VARIABLE, module, address) {
}
VariableInfo::VariableInfo(Module* module, uint64_t address)
: SymbolInfo(SymbolInfo::TYPE_VARIABLE, module, address) {}
VariableInfo::~VariableInfo() {
}
VariableInfo::~VariableInfo() {}
} // namespace runtime
} // namespace alloy

View File

@@ -12,16 +12,14 @@
#include <alloy/core.h>
namespace alloy {
namespace runtime {
class Function;
class Module;
class SymbolInfo {
public:
public:
enum Type {
TYPE_FUNCTION,
TYPE_VARIABLE,
@@ -34,7 +32,8 @@ public:
STATUS_DEFINED,
STATUS_FAILED,
};
public:
public:
SymbolInfo(Type type, Module* module, uint64_t address);
virtual ~SymbolInfo();
@@ -47,26 +46,26 @@ public:
const char* name() const { return name_; }
void set_name(const char* name);
protected:
Type type_;
Module* module_;
Status status_;
uint64_t address_;
protected:
Type type_;
Module* module_;
Status status_;
uint64_t address_;
char* name_;
char* name_;
};
class FunctionInfo : public SymbolInfo {
public:
public:
enum Behavior {
BEHAVIOR_DEFAULT = 0,
BEHAVIOR_DEFAULT = 0,
BEHAVIOR_PROLOG,
BEHAVIOR_EPILOG,
BEHAVIOR_EPILOG_RETURN,
BEHAVIOR_EXTERN,
};
public:
public:
FunctionInfo(Module* module, uint64_t address);
virtual ~FunctionInfo();
@@ -80,15 +79,15 @@ public:
Function* function() const { return function_; }
void set_function(Function* value) { function_ = value; }
typedef void(*ExternHandler)(void* context, void* arg0, void* arg1);
typedef void (*ExternHandler)(void* context, void* arg0, void* arg1);
void SetupExtern(ExternHandler handler, void* arg0, void* arg1);
ExternHandler extern_handler() const { return extern_info_.handler; }
void* extern_arg0() const { return extern_info_.arg0; }
void* extern_arg1() const { return extern_info_.arg1; }
private:
uint64_t end_address_;
Behavior behavior_;
private:
uint64_t end_address_;
Behavior behavior_;
Function* function_;
struct {
ExternHandler handler;
@@ -98,16 +97,14 @@ private:
};
class VariableInfo : public SymbolInfo {
public:
public:
VariableInfo(Module* module, uint64_t address);
virtual ~VariableInfo();
private:
private:
};
} // namespace runtime
} // namespace alloy
#endif // ALLOY_RUNTIME_SYMBOL_INFO_H_

View File

@@ -11,19 +11,18 @@
#include <alloy/runtime/runtime.h>
using namespace alloy;
using namespace alloy::runtime;
namespace alloy {
namespace runtime {
namespace {
__declspec(thread) ThreadState* thread_state_ = NULL;
}
ThreadState::ThreadState(Runtime* runtime, uint32_t thread_id) :
runtime_(runtime), memory_(runtime->memory()),
thread_id_(thread_id), name_(0),
backend_data_(0), raw_context_(0) {
ThreadState::ThreadState(Runtime* runtime, uint32_t thread_id)
: runtime_(runtime),
memory_(runtime->memory()),
thread_id_(thread_id),
name_(0),
backend_data_(0),
raw_context_(0) {
if (thread_id_ == UINT_MAX) {
// System thread. Assign the system thread ID with a high bit
// set so people know what's up.
@@ -59,10 +58,9 @@ void ThreadState::Bind(ThreadState* thread_state) {
thread_state_ = thread_state;
}
ThreadState* ThreadState::Get() {
return thread_state_;
}
ThreadState* ThreadState::Get() { return thread_state_; }
uint32_t ThreadState::GetThreadID() {
return thread_state_->thread_id_;
}
uint32_t ThreadState::GetThreadID() { return thread_state_->thread_id_; }
} // namespace runtime
} // namespace alloy

View File

@@ -14,15 +14,13 @@
#include <alloy/memory.h>
namespace alloy {
namespace runtime {
class Runtime;
class ThreadState {
public:
public:
ThreadState(Runtime* runtime, uint32_t thread_id);
virtual ~ThreadState();
@@ -43,18 +41,16 @@ public:
static ThreadState* Get();
static uint32_t GetThreadID();
protected:
Runtime* runtime_;
Memory* memory_;
uint32_t thread_id_;
char* name_;
void* backend_data_;
void* raw_context_;
protected:
Runtime* runtime_;
Memory* memory_;
uint32_t thread_id_;
char* name_;
void* backend_data_;
void* raw_context_;
};
} // namespace runtime
} // namespace alloy
#endif // ALLOY_RUNTIME_THREAD_STATE_H_

View File

@@ -13,31 +13,27 @@
#include <alloy/tracing/tracing.h>
#include <alloy/tracing/event_type.h>
namespace alloy {
namespace runtime {
const uint32_t ALLOY_RUNTIME = alloy::tracing::EventType::ALLOY_RUNTIME;
class EventType {
public:
public:
enum {
ALLOY_RUNTIME_INIT = ALLOY_RUNTIME | (1),
ALLOY_RUNTIME_DEINIT = ALLOY_RUNTIME | (2),
ALLOY_RUNTIME_THREAD = ALLOY_RUNTIME | (1 << 25),
ALLOY_RUNTIME_THREAD_INIT = ALLOY_RUNTIME_THREAD | (1),
ALLOY_RUNTIME_THREAD_DEINIT = ALLOY_RUNTIME_THREAD | (2),
ALLOY_RUNTIME_MEMORY = ALLOY_RUNTIME | (2 << 25),
ALLOY_RUNTIME_MEMORY_INIT = ALLOY_RUNTIME_MEMORY | (1),
ALLOY_RUNTIME_MEMORY_DEINIT = ALLOY_RUNTIME_MEMORY | (2),
ALLOY_RUNTIME_MEMORY_HEAP = ALLOY_RUNTIME_MEMORY | (1000),
ALLOY_RUNTIME_MEMORY_HEAP_INIT = ALLOY_RUNTIME_MEMORY_HEAP | (1),
ALLOY_RUNTIME_MEMORY_HEAP_DEINIT = ALLOY_RUNTIME_MEMORY | (2),
ALLOY_RUNTIME_MEMORY_HEAP_ALLOC = ALLOY_RUNTIME_MEMORY | (3),
ALLOY_RUNTIME_MEMORY_HEAP_FREE = ALLOY_RUNTIME_MEMORY | (4),
ALLOY_RUNTIME_INIT = ALLOY_RUNTIME | (1),
ALLOY_RUNTIME_DEINIT = ALLOY_RUNTIME | (2),
ALLOY_RUNTIME_THREAD = ALLOY_RUNTIME | (1 << 25),
ALLOY_RUNTIME_THREAD_INIT = ALLOY_RUNTIME_THREAD | (1),
ALLOY_RUNTIME_THREAD_DEINIT = ALLOY_RUNTIME_THREAD | (2),
ALLOY_RUNTIME_MEMORY = ALLOY_RUNTIME | (2 << 25),
ALLOY_RUNTIME_MEMORY_INIT = ALLOY_RUNTIME_MEMORY | (1),
ALLOY_RUNTIME_MEMORY_DEINIT = ALLOY_RUNTIME_MEMORY | (2),
ALLOY_RUNTIME_MEMORY_HEAP = ALLOY_RUNTIME_MEMORY | (1000),
ALLOY_RUNTIME_MEMORY_HEAP_INIT = ALLOY_RUNTIME_MEMORY_HEAP | (1),
ALLOY_RUNTIME_MEMORY_HEAP_DEINIT = ALLOY_RUNTIME_MEMORY | (2),
ALLOY_RUNTIME_MEMORY_HEAP_ALLOC = ALLOY_RUNTIME_MEMORY | (3),
ALLOY_RUNTIME_MEMORY_HEAP_FREE = ALLOY_RUNTIME_MEMORY | (4),
};
typedef struct Init_s {
@@ -63,32 +59,30 @@ public:
} MemoryDeinit;
typedef struct MemoryHeapInit_s {
static const uint32_t event_type = ALLOY_RUNTIME_MEMORY_HEAP_INIT;
uint32_t heap_id;
uint64_t low_address;
uint64_t high_address;
uint32_t is_physical;
uint32_t heap_id;
uint64_t low_address;
uint64_t high_address;
uint32_t is_physical;
} MemoryHeapInit;
typedef struct MemoryHeapDeinit_s {
static const uint32_t event_type = ALLOY_RUNTIME_MEMORY_HEAP_DEINIT;
uint32_t heap_id;
uint32_t heap_id;
} MemoryHeapDeinit;
typedef struct MemoryHeapAlloc_s {
static const uint32_t event_type = ALLOY_RUNTIME_MEMORY_HEAP_ALLOC;
uint32_t heap_id;
uint32_t flags;
uint64_t address;
size_t size;
uint32_t heap_id;
uint32_t flags;
uint64_t address;
size_t size;
} MemoryHeapAlloc;
typedef struct MemoryHeapFree_s {
static const uint32_t event_type = ALLOY_RUNTIME_MEMORY_HEAP_FREE;
uint32_t heap_id;
uint64_t address;
uint32_t heap_id;
uint64_t address;
} MemoryHeapFree;
};
} // namespace runtime
} // namespace alloy
#endif // ALLOY_RUNTIME_TRACING_H_