From 0246b130560755661ba9fddeb5f92fcbb96d966f Mon Sep 17 00:00:00 2001 From: William Cohen Date: Mon, 20 Sep 2021 09:46:04 -0400 Subject: [PATCH] Show the liveness information at points being probed This code is work-in-progress and to check whether the liveness analysis is working. Currently, the location context information for the variable isn't being passed in and the setup of that is disabled. --- analysis.cxx | 78 ++++++++++++++++++++++++++++++++++++++++++++++++---- analysis.h | 4 ++- tapsets.cxx | 8 ++++++ 3 files changed, 83 insertions(+), 7 deletions(-) diff --git a/analysis.cxx b/analysis.cxx index 6b1145972..5e8701efb 100644 --- a/analysis.cxx +++ b/analysis.cxx @@ -10,7 +10,7 @@ #ifdef HAVE_DYNINST -#include +#include "loc2stap.h" #include "analysis.h" #include #include @@ -39,25 +39,91 @@ analysis::analysis(char *name) // If not seen before // Create a new binary code object from the filename argument sts = new SymtabCodeSource(name); - return; + if(!sts) return; co = new CodeObject(sts); if(!co) return; } +// FIXME: Currently only support x86_64 will need to set up for powerpc and aarch64 +static const MachRegister dyninst_register[] = { + x86_64::rax, + x86_64::rdx, + x86_64::rcx, + x86_64::rbx, + x86_64::rsi, + x86_64::rdi, + x86_64::rbp, + x86_64::rsp, + x86_64::r8, + x86_64::r9, + x86_64::r10, + x86_64::r11, + x86_64::r12, + x86_64::r13, + x86_64::r14, + x86_64::r15, + x86_64::rip +}; + -int liveness(char *executable, - Dwarf_Addr addr __attribute__ ((unused)) - /*, variable */) +int liveness(const char *executable, + Dwarf_Addr addr, + location_context ctx __attribute__ ((unused))) { - analysis func_to_analyze(executable); + // should cache the executable names like the other things + char *exe = strdup(executable); + analysis func_to_analyze(exe); + + #if 0 // Find where the variable is located + location *loc = ctx.locations.back (); + // If variable isn't in a register, punt (return 0) + if (loc->type != loc_register) return 0; + + // Map dwarf number to dyninst number, skip if out of range + if (loc->offset >= sizeof(dyninst_register)/sizeof(MachRegister)) return 0; + MachRegister r = dyninst_register[loc->offset]; + #endif + // Find the function containing address + cout << "liveness analysis " << executable << " " << hex << addr << endl; + vector rr = func_to_analyze.co->cs()->regions(); + std::set ff; + if(func_to_analyze.co->findFuncs(rr[0], addr, ff) <= 0) return 0; + ParseAPI::Function *func = *ff.begin(); + cout << "<" << func->addr() << ">:" << func->name() << "[" << addr << "]" << endl; // Check to see if a previous liveness information exists for function to reuse // Otherwise create new liveness analysis + LivenessAnalyzer la(func->obj()->cs()->getAddressWidth()); + la.analyze(func); + // Construct a liveness query location for the function entry. + // Get basic block containing the instruction + set bb_s; + if (func_to_analyze.co->findBlocks(rr[0], addr, bb_s) != 1 ) + return 0; // problem find basic block, punt + Block *bb = *bb_s.begin(); + Instruction curInsn = bb->getInsn(addr); + + // Construct a liveness query location for the function entry. + InsnLoc i(bb, addr, curInsn); + Location iloc(func, i); + + #if 0 // Query to see if whether the register is live at that point + bool used; + la.query(iloc, LivenessAnalyzer::Before, r, used); + cout << r << (used ? " used" : "unused") << endl; + return (used ? 1 : -1); + #else + // Query to list out the registers that are live at that point + // FIXME: This is because not currently getting the actual register + bitArray liveRegs; + la.query(iloc, LivenessAnalyzer::Before, liveRegs); + cout << "liveRegs " << liveRegs << endl; return 0; + #endif } #endif // HAVE_DYNINST diff --git a/analysis.h b/analysis.h index 7ddf1927b..470bca57d 100644 --- a/analysis.h +++ b/analysis.h @@ -17,7 +17,9 @@ #ifdef HAVE_DYNINST -extern int liveness(char *executable, Dwarf_Addr location __attribute__ ((unused)) /*, varaccess */); +extern int liveness(const char *executable, + Dwarf_Addr location, + location_context ctx); #else diff --git a/tapsets.cxx b/tapsets.cxx index 1c82aaf69..348d9c613 100644 --- a/tapsets.cxx +++ b/tapsets.cxx @@ -23,6 +23,7 @@ #include "dwflpp.h" #include "setupdwfl.h" #include "loc2stap.h" +#include "analysis.h" #include #include "sdt_types.h" @@ -4720,6 +4721,13 @@ dwarf_var_expanding_visitor::visit_target_symbol (target_symbol *e) ctx.pc = addr; ctx.userspace_p = userspace_p; + // Check if change to variable has any effect + if (lvalue) { + if (liveness(q.dw.module_name.c_str(), addr, ctx) < 0) { + // warn that the write has no effect + } + } + // NB: pass the ctx.e (copied/rewritten veraion e, not orig_e), // so [x] index expressions have their intra-synthetic-function names Dwarf_Die endtype; -- 2.43.5