Created
June 19, 2020 06:19
-
-
Save stiv-yakovenko/bde250ec6cb24265f28325b3e75ba36d to your computer and use it in GitHub Desktop.
How to log all invocations of all functions in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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