17 #include "xed-interface.h"
20 #define MAX_FILE_PATH (1000)
25 PIN_ERROR(
"CCTLib client Pin tool to gather calling context on each instruction.\n" + KNOB_BASE::StringKnobSummary() +
"\n");
39 char* envPath = getenv(
"CCTLIB_CLIENT_OUTPUT_FILE");
43 snprintf(name,
sizeof(name),
"%s", envPath);
46 gethostname(name + strlen(name),
MAX_FILE_PATH - strlen(name));
48 sprintf(name + strlen(name),
"%d", pid);
49 cerr <<
"\n Creating log file at:" << name <<
"\n";
54 for (
int i = 0; i < argc; i++) {
64 xed_state_init(&
xedState, XED_MACHINE_MODE_LONG_64, XED_ADDRESS_WIDTH_64b, XED_ADDRESS_WIDTH_64b);
69 const CONTEXT* ctxt = (
const CONTEXT*)_ctxt;
76 return PIN_GetContextReg(ctxt, LEVEL_BASE::REG_RAX);
85 return PIN_GetContextReg(ctxt, LEVEL_BASE::REG_RCX);
93 return PIN_GetContextReg(ctxt, LEVEL_BASE::REG_RDX);
101 return PIN_GetContextReg(ctxt, LEVEL_BASE::REG_RBX);
109 return PIN_GetContextReg(ctxt, LEVEL_BASE::REG_RSP);
114 return PIN_GetContextReg(ctxt, LEVEL_BASE::REG_RBP);
119 return PIN_GetContextReg(ctxt, LEVEL_BASE::REG_RSI);
124 return PIN_GetContextReg(ctxt, LEVEL_BASE::REG_RDI);
129 return PIN_GetContextReg(ctxt, LEVEL_BASE::REG_R8);
134 return PIN_GetContextReg(ctxt, LEVEL_BASE::REG_R9);
139 return PIN_GetContextReg(ctxt, LEVEL_BASE::REG_R10);
144 return PIN_GetContextReg(ctxt, LEVEL_BASE::REG_R11);
149 return PIN_GetContextReg(ctxt, LEVEL_BASE::REG_R12);
154 return PIN_GetContextReg(ctxt, LEVEL_BASE::REG_R13);
159 return PIN_GetContextReg(ctxt, LEVEL_BASE::REG_R14);
164 return PIN_GetContextReg(ctxt, LEVEL_BASE::REG_R15);
167 return PIN_GetContextReg(ctxt, LEVEL_BASE::REG_EFLAGS);
172 return PIN_GetContextReg(ctxt, LEVEL_BASE::REG_RIP);
189 return (uint32_t)PIN_GetContextReg(ctxt, LEVEL_BASE::REG_SEG_CS);
191 return (uint32_t)PIN_GetContextReg(ctxt, LEVEL_BASE::REG_SEG_FS);
193 return (uint32_t)PIN_GetContextReg(ctxt, LEVEL_BASE::REG_SEG_GS);
200 #include <unordered_set>
202 std::unordered_set<ADDRINT>
hm;
204 VOID
Checker(ADDRINT addr, ADDRINT
ip,
const CONTEXT* ctxt, UINT32 idx) {
205 xed_decoded_inst_t xedd;
206 xed_decoded_inst_zero_set_mode(&xedd, &
xedState);
208 if (XED_ERROR_NONE != xed_decode(&xedd, (
const xed_uint8_t*)(
ip), 15 )) {
213 if (XED_ERROR_NONE != xed_agen(&xedd, idx , (
void*)ctxt, (xed_uint64_t*)(&
address))) {
216 if (addr != (ADDRINT)(
address)) {
217 if (
hm.find(addr) ==
hm.end()) {
219 if (0 == xed_format_context(XED_SYNTAX_ATT, &xedd,
buf, 200,
ip,
nullptr,
nullptr))
220 strcpy(
buf,
"xed_decoded_inst_dump_att_format failed");
221 printf(
"\n %s, %p, %p",
buf, (
void*)addr, (
void*)
address);
230 if (INS_IsControlFlow(ins) || INS_IsRet(ins)) {
235 UINT32 memOperands = INS_MemoryOperandCount(ins);
236 for (UINT32 i = 0; i < memOperands; i++) {
237 INS_InsertPredicatedCall(ins, IPOINT_AFTER, (AFUNPTR)
Checker, IARG_MEMORYOP_EA, i, IARG_INST_PTR, IARG_CONST_CONTEXT, IARG_UINT32, i, IARG_END);
241 VOID
Fini(INT32 code, VOID* v) {
244 int main(
int argc,
char* argv[]) {
246 if (PIN_Init(argc, argv))
258 PIN_AddFiniFunction(
Fini,
nullptr);
int main(int argc, char *argv[])
VOID Checker(ADDRINT addr, ADDRINT ip, const CONTEXT *ctxt, UINT32 idx)
void ClientInit(int argc, char *argv[])
std::unordered_set< ADDRINT > hm
VOID Fini(INT32 code, VOID *v)
xed_uint64_t watchpointRegisterValueCallback(xed_reg_enum_t reg, void *_ctxt, xed_bool_t *error)
VOID Instruction(INS ins, VOID *v)
static uint64_t buf[ITERS]