Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to log all invocations of all functions in C++
extern "C" void __attribute__((no_instrument_function))
__cyg_profile_func_enter(void* this_fn, void* call_site)
{
int p =(char*) this_fn - (char *)main;
if(funcs.size()!=0){
if(funcs.count(p)) {
string fname = funcs[p];
if (fname.rfind("cv::",0)==0) return;
if (fname.rfind("FrameWithInfo::Fra",0)==0) return;
if (fname.rfind("FrameWithInfo::~Fra",0)==0) return;
if (fname.rfind("main::{lambda()#3}",0)==0) return;
if (fname.rfind("CommonUtils::getTs",0)==0) return;
if (fname.rfind("FPSMeter::",0)==0) return;
if (fname[0]!='_')
printf("CALL function [%s]\n", fname.c_str());
}
}
}
map<int,string> funcs;
std::string exec(const char* cmd) {
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
map<int,string> loadFunMap(string bin){
map<unsigned long,string> ptrs;
map<int,string> ret;
unsigned long mainPtr;
string myBin =string("nm -C ")+ bin;
string res = exec(myBin.c_str());
auto parts=CommonUtils::split(res,"\n");
for(auto line:parts){
//cout<<line<<endl;
std::regex r("([0-9A-Fa-f]{16}) . (.*)");
std::smatch m;
if (std::regex_search(line, m, r)){
if (m.size()>1) {
string s = m[1];
const char * str = s.c_str();
char *p;
unsigned long z= strtoul( str, & p, 16 );
if (m[2]=="main"){
mainPtr=z;
}
ptrs[z]=m[2];
}
}
}
for(auto&kv:ptrs){
ret[kv.first-mainPtr]=kv.second;
}
return ret;
}
int main(int argc, char **argv) {
funcs = loadFunMap(argv[0]);
...
}
target_compile_options(${ProjectId} PRIVATE -rdynamic -fpermissive -g
-finstrument-functions
-finstrument-functions-exclude-file-list=/usr/include/c++/,/usr/local/include/)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.