2023-10-26 22:29:05 +02:00
|
|
|
#include <fmt/format.h>
|
2024-02-23 21:57:57 +01:00
|
|
|
#include "common/disassembler.h"
|
2023-05-30 12:33:52 +02:00
|
|
|
|
2023-11-05 15:56:28 +01:00
|
|
|
namespace Common {
|
|
|
|
|
|
|
|
Disassembler::Disassembler() {
|
2024-02-23 21:57:57 +01:00
|
|
|
ZydisDecoderInit(&m_decoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_STACK_WIDTH_64);
|
|
|
|
ZydisFormatterInit(&m_formatter, ZYDIS_FORMATTER_STYLE_INTEL);
|
2023-05-30 12:33:52 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 15:56:28 +01:00
|
|
|
Disassembler::~Disassembler() = default;
|
2023-05-30 12:33:52 +02:00
|
|
|
|
2023-11-05 15:56:28 +01:00
|
|
|
void Disassembler::printInstruction(void* code, u64 address) {
|
2024-02-23 21:57:57 +01:00
|
|
|
ZydisDecodedInstruction instruction;
|
|
|
|
ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT_VISIBLE];
|
|
|
|
ZyanStatus status =
|
|
|
|
ZydisDecoderDecodeFull(&m_decoder, code, sizeof(code), &instruction, operands);
|
2023-11-05 15:56:28 +01:00
|
|
|
if (!ZYAN_SUCCESS(status)) {
|
2024-02-23 21:57:57 +01:00
|
|
|
fmt::print("decode instruction failed at {}\n", fmt::ptr(code));
|
2023-11-05 15:56:28 +01:00
|
|
|
} else {
|
2024-02-23 21:57:57 +01:00
|
|
|
printInst(instruction, operands, address);
|
|
|
|
}
|
2023-05-30 12:33:52 +02:00
|
|
|
}
|
|
|
|
|
2024-02-23 21:57:57 +01:00
|
|
|
void Disassembler::printInst(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands,
|
|
|
|
u64 address) {
|
|
|
|
const int bufLen = 256;
|
|
|
|
char szBuffer[bufLen];
|
|
|
|
ZydisFormatterFormatInstruction(&m_formatter, &inst, operands, inst.operand_count_visible,
|
2023-11-05 15:56:28 +01:00
|
|
|
szBuffer, sizeof(szBuffer), address, ZYAN_NULL);
|
2023-10-26 22:29:05 +02:00
|
|
|
fmt::print("instruction: {}\n", szBuffer);
|
|
|
|
}
|
2023-11-05 15:56:28 +01:00
|
|
|
|
|
|
|
} // namespace Common
|