Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature pyttd events #11

Merged
merged 4 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ cursor.set_position(first)
print(f"PC: {cursor.get_program_counter():x}")

# Print RCX
ctxt = cursor.get_crossplatform_context()
ctxt = cursor.get_context_x86_64()
print("RCX: %x" % ctxt.rcx)

# Read the memory at RCX on 16 bytes
Expand Down
42 changes: 25 additions & 17 deletions example_api/example_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
last = eng.get_last_position()
print(f"Trace from {first} to {last}")

# Positions can be compared
assert all([first < last, last > first, first == eng.get_first_position()])

# Print a few information on the trace
peb = eng.get_peb_address()
print(f"Peb is at {peb:x}")
Expand All @@ -23,32 +26,37 @@
print(f"PC: {cursor.get_program_counter():x}")
thrdinfo = cursor.get_thread_info()
print(f"Thread ID: {thrdinfo.threadid}")
ctxt = cursor.get_crossplatform_context()
ctxt = cursor.get_context_x86_64()
print("RCX: %x" % ctxt.rcx)

# Print loaded modules
print(f"Modules ({eng.get_module_count()}):")
for mod in sorted(eng.get_module_list(), key=lambda x: x.base_addr):
print(f"\t0x{mod.base_addr:x} - 0x{mod.base_addr + mod.image_size:x}\t{mod.path}")

# Print Thread active zones
# Print an event based timeline
events = sorted(
list((x, "modload") for x in eng.get_module_loaded_event_list())
+ list((x, "modunload") for x in eng.get_module_unloaded_event_list())
+ list((x, "threadcreated") for x in eng.get_thread_created_event_list())
+ list((x, "threadterm") for x in eng.get_thread_terminated_event_list()),
key=lambda event:event[0].position
)
for event, evtype in events:
print(f"[{event.position.major:x}:{event.position.minor:x}]", end=" ")
if evtype == "modload":
print(f"Module {event.info.path} loaded")
elif evtype == "modunload":
print(f"Module {event.info.path} unloaded")
elif evtype == "threadcreated":
print(f"Thread {event.info.threadid:x} created")
elif evtype == "threadterm":
print(f"Thread {event.info.threadid:x} terminated")

# Print threads' future and past active zone
print(f"Threads ({cursor.get_thread_count()}):")
## Save the current position
position_save = cursor.get_position()
## Get Threads' starting position
threads = {} # Thread ID => {"begin": position, "end": position}
cursor.set_position(first)
for thread in cursor.get_thread_list():
threads.setdefault(thread.threadid, {})["begin"] = f"{thread.next_major:x}:{thread.next_minor:x}"
## Get Threads' ending position
cursor.set_position(last)
for thread in cursor.get_thread_list():
threads.setdefault(thread.threadid, {})["end"] = f"{thread.last_major:x}:{thread.last_minor:x}"
## Restore saved position
cursor.set_position(position_save)
## Print out the resulting ranges
for threadid, info in threads.items():
print(f"\t[0x{threadid:x}] {info['begin']} -> {info['end']}")
print(f"\t[0x{thread.threadid:x}] last active: {thread.last_major:x}:{thread.last_minor:x}, next: {thread.next_major:x}:{thread.next_minor:x}")

# Read the memory
print("@128[RCX]: %s" % cursor.read_mem(ctxt.rcx, 16))
Expand Down
56 changes: 54 additions & 2 deletions python-bindings/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ PYBIND11_MODULE(pyTTD, m) {
py::class_<TTD::TTD_Replay_ThreadInfo, std::unique_ptr<TTD::TTD_Replay_ThreadInfo, py::nodelete>>(m, "ThreadInfo")
.def_readonly("threadid", &TTD::TTD_Replay_ThreadInfo::threadid);


py::class_<TTD::TTD_Replay_Event<TTD::TTD_Replay_Module>, std::unique_ptr<TTD::TTD_Replay_Event<TTD::TTD_Replay_Module>, py::nodelete>>(m, "ModuleEvent")
.def_readonly("position", &TTD::TTD_Replay_Event<TTD::TTD_Replay_Module>::pos)
.def_readonly("info", &TTD::TTD_Replay_Event<TTD::TTD_Replay_Module>::info);

py::class_<TTD::TTD_Replay_Event<TTD::TTD_Replay_ThreadInfo>, std::unique_ptr<TTD::TTD_Replay_Event<TTD::TTD_Replay_ThreadInfo>, py::nodelete>>(m, "ThreadEvent")
.def_readonly("position", &TTD::TTD_Replay_Event<TTD::TTD_Replay_ThreadInfo>::pos)
.def_readonly("info", &TTD::TTD_Replay_Event<TTD::TTD_Replay_ThreadInfo>::info);

py::class_<TTD::Position, std::unique_ptr<TTD::Position, py::nodelete>>(m, "Position")
.def_readwrite("major", &TTD::Position::Major)
.def_readwrite("minor", &TTD::Position::Minor)
Expand All @@ -40,7 +47,16 @@ PYBIND11_MODULE(pyTTD, m) {
sprintf_s(out, "<Position %llx:%llx>", pos->Major, pos->Minor);
return std::string(out);
}
);
)
.def("__lt__", [](TTD::Position &self, const TTD::Position &b) {
return self < b;
}, py::is_operator())
.def("__gt__", [](TTD::Position& self, const TTD::Position& b) {
return self > b;
}, py::is_operator())
.def("__eq__", [](TTD::Position& self, const TTD::Position& b) {
return (self.Major == b.Major) && (self.Minor == b.Minor);
}, py::is_operator());

py::class_<TTD::TTD_Replay_ActiveThreadInfo, std::unique_ptr<TTD::TTD_Replay_ActiveThreadInfo, py::nodelete>>(m, "ActiveThreadInfo")
.def_property("threadid", [](TTD::TTD_Replay_ActiveThreadInfo& self) {
Expand Down Expand Up @@ -166,6 +182,42 @@ PYBIND11_MODULE(pyTTD, m) {
.def("get_last_position", &TTD::ReplayEngine::GetLastPosition)
.def("get_peb_address", &TTD::ReplayEngine::GetPebAddress)
.def("new_cursor", &TTD::ReplayEngine::NewCursor)
.def("get_module_loaded_event_count", &TTD::ReplayEngine::GetModuleLoadedEventCount)
.def("get_module_loaded_event_list", [](TTD::ReplayEngine& self) {
std::vector<TTD::TTD_Replay_ModuleLoadedEvent> mods;
const TTD::TTD_Replay_ModuleLoadedEvent* mod_list = self.GetModuleLoadedEventList();
for (int i = 0; i < self.GetModuleLoadedEventCount(); i++) {
mods.push_back(mod_list[i]);
}
return mods;
})
.def("get_module_unloaded_event_count", &TTD::ReplayEngine::GetModuleUnloadedEventCount)
.def("get_module_unloaded_event_list", [](TTD::ReplayEngine& self) {
std::vector<TTD::TTD_Replay_ModuleUnloadedEvent> mods;
const TTD::TTD_Replay_ModuleUnloadedEvent* mod_list = self.GetModuleUnloadedEventList();
for (int i = 0; i < self.GetModuleUnloadedEventCount(); i++) {
mods.push_back(mod_list[i]);
}
return mods;
})
.def("get_thread_created_event_count", &TTD::ReplayEngine::GetThreadCreatedEventCount)
.def("get_thread_created_event_list", [](TTD::ReplayEngine& self) {
std::vector<TTD::TTD_Replay_ThreadCreatedEvent> thrds;
const TTD::TTD_Replay_ThreadCreatedEvent* thrd_list = self.GetThreadCreatedEventList();
for (int i = 0; i < self.GetThreadCreatedEventCount(); i++) {
thrds.push_back(thrd_list[i]);
}
return thrds;
})
.def("get_thread_terminated_event_count", &TTD::ReplayEngine::GetThreadTerminatedEventCount)
.def("get_thread_terminated_event_list", [](TTD::ReplayEngine& self) {
std::vector<TTD::TTD_Replay_ThreadTerminatedEvent> thrds;
const TTD::TTD_Replay_ThreadTerminatedEvent* thrd_list = self.GetThreadTerminatedEventList();
for (int i = 0; i < self.GetThreadTerminatedEventCount(); i++) {
thrds.push_back(thrd_list[i]);
}
return thrds;
})
.def("get_module_count", &TTD::ReplayEngine::GetModuleCount)
.def("get_module_list", [](TTD::ReplayEngine& self) {
std::vector<TTD::TTD_Replay_Module> mods;
Expand Down