/*BEGIN_LEGAL Intel Open Source License Copyright (c) 2002-2005 Intel Corporation All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the Intel Corporation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. END_LEGAL */ // // ORIGINAL_AUTHOR: Kim Hazelwood // // This tool watches various code cache activities related to traces // and prints them out for the user // Sample usage: // pin -t trace_insertions -- /bin/ls #include "pin.H" #include "xed-interface.H" using namespace XED; #include "utils.H" #include "portability.H" #include #include using namespace std; /* ================================================================== */ /* Global Variables */ /* ================================================================== */ static int insertions; static int numLinks; static int numUnlinks; static ofstream TraceFile; static int PrintInstruction(ADDRINT pc, bool expandcalls, bool &isRet); static VOID PrintInstructionSequence(ADDRINT pc, size_t length, bool expandcalls = false); /* ===================================================================== */ /* Commandline Switches */ /* ===================================================================== */ static KNOB KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool", "o", "codecachelib.out", "specify trace file name"); /* ================================================================== */ static VOID InitCounters() { string logFileName = KnobOutputFile.Value(); TraceFile.open(logFileName.c_str()); insertions = 0; numLinks = 0; numUnlinks = 0; } /* ================================================================== */ /* Print details of links patched */ static VOID WatchLinks(ADDRINT branch_pc, ADDRINT target_pc) { numLinks++; TraceFile << "LINK #" << dec << numLinks; TraceFile << "\tcache pc=0x" << hex << branch_pc; TraceFile << " \tpatched to pc=0x" << hex << target_pc << endl; TraceFile << " \tshowing first few instructions at target:" << endl; PrintInstructionSequence(target_pc, 40, true); TraceFile << "LINK #" << dec << numLinks << " end of target preview <<<< ---- " << endl; } /* ================================================================== */ /* Print details of links unpatched */ static VOID WatchUnlinks(ADDRINT branch_pc, ADDRINT stub_pc) { numUnlinks++; TraceFile << "UNLINK #" << dec << numUnlinks; TraceFile << "\tcache pc=0x" << hex << branch_pc; TraceFile << " \trestored to stub pc=0x" << hex << stub_pc << endl; } static VOID PrintInstructionSequence(ADDRINT pc, size_t length, bool expandcalls) { bool isRet = false; ADDRINT endpc = pc + length; while (pc < endpc && (!expandcalls || isRet == false)) { pc += PrintInstruction(pc, expandcalls, isRet); } } /* ================================================================== */ /* Print details of traces as they are inserted */ static VOID WatchTraces(TRACE trace, VOID *v) { insertions++; TraceFile << "Code cache insertion #" << dec << insertions; TraceFile << "\torig pc=0x" << hex << TRACE_Address(trace); TraceFile << "\tcache pc=0x" << hex << TRACE_CodeCacheAddress(trace); TraceFile << "\tapplication trace size=" << dec << TRACE_OriginalSize(trace); TraceFile << "\tcode cache trace size=" << dec << TRACE_CodeCacheSize(trace); TraceFile << " (BBL=" << dec << TRACE_NumBbl(trace); TraceFile << " Ins=" << dec << TRACE_NumIns(trace) << ")" << endl; /* added - gback */ TraceFile << "original code was:" << endl; PrintInstructionSequence(TRACE_Address(trace), TRACE_OriginalSize(trace)); TraceFile << "translated code is:" << endl; PrintInstructionSequence(TRACE_CodeCacheAddress(trace), TRACE_CodeCacheSize(trace)); } // print instruction, return length static int PrintInstruction(ADDRINT pc, bool expandcalls, bool & isRet) { const xed_state_t dstate( XED_MACHINE_MODE_LEGACY_32, ADDR_WIDTH_32b, ADDR_WIDTH_32b); xed_decoded_inst_t xedd(dstate); const int max_inst_len = 15; xed_error_enum_t xed_code; xed_code = xed_decode(&xedd, reinterpret_cast(pc), max_inst_len); if (xed_code != XED_ERROR_NONE) { TraceFile << "error decoding instruction @" << hex << pc << " " << xed_error_enum_t2str(xed_code) << endl; } char buffer[100]; xed_syntax_t syntax = XED_SYNTAX_ATT; // XED_SYNTAX_INTEL, XED_SYNTAX_XED BOOL xed_format_ok = xed_format(syntax, &xedd, buffer, sizeof buffer, static_cast(pc)); if (xed_format_ok) { TraceFile << "\t" << hex << std::setw(8) << pc << " "; //xedd.print_short( TraceFile, pc); TraceFile << buffer; TraceFile << endl; } else { TraceFile << "error formatting " << hex << xedd << endl; } // hack to print out first 10 instructions of calls if asked if (!strncmp(buffer, "call", 4) && expandcalls) { // read as 64bit, cast to 32bit to work around bug in xed. unsigned long long taddr; sscanf(buffer, "call %llx", &taddr); ADDRINT addr = taddr; int n = 15; TraceFile << "\t\t" << ">>> expanding call ... " << hex << addr << endl; PrintInstructionSequence(addr, n, false); TraceFile << "<<<" << endl; } isRet = strstr(buffer, "ret") != NULL; return xedd.get_length(); } /* ================================================================== */ /* Print details at the end of the run */ static VOID PrintDetailsOnExit(INT32 code, VOID *v) { int traces = CODECACHE_NumTracesInCache(); int stubs = CODECACHE_NumExitStubsInCache(); int compensation = traces - stubs; traces = traces - compensation; TraceFile << endl << "------------------------" << endl; TraceFile << dec << traces << " traces" << endl; TraceFile << dec << stubs << " exit stubs" << endl; TraceFile << dec << compensation << " compensations" << endl; TraceFile << dec << (traces+stubs+compensation) << " total regions in the code cache" << endl; TraceFile << numLinks << " links set in the code cache" << endl; TraceFile << numUnlinks << " links removed from the code cache" << endl; TraceFile << "#eof" << endl; TraceFile.close(); } void CodeCacheLibInit(void) { // Initialize some counters we're using in this tool InitCounters(); // Register a routine that gets called when the cache is first initialized CODECACHE_AddCacheInitFunction(PrintInitInfo, 0); // Register a routine that gets called when a trace is // inserted into the codecache CODECACHE_AddTraceInsertedFunction(WatchTraces, 0); // Register a routine that gets called when patch a link in // the code cache CODECACHE_AddTraceLinkedFunction(WatchLinks, 0); // Register a routine that gets called when we unpatch a link in // the code cache CODECACHE_AddTraceUnlinkedFunction(WatchUnlinks, 0); // Register a routine that gets called when the program ends PIN_AddFiniFunction(PrintDetailsOnExit, 0); }