Function for mangle / demangle function

Earlier, here , it was shown that C ++ functions are not easily represented in the assembly. Now I am interested in reading anyway, because callgrind, part of valgrind, shows them demangled, while in the assembly they are shown distorted, so I would like to mutilate the output of the valgrind function, or demanize the names of function assemblies. Has anyone ever tried something like this? I looked at the website and found out the following:

Code to implement demangling is part of the GNU Binutils package; see libiberty/cplus-dem.c and include/demangle.h. 

Has anyone ever tried something like this, I want to untie / maim in C? my compiler is gcc 4.x

+7
source share
2 answers

Use the c++filt command line tool to expand the name.

+13
source

Here is my C ++ 11 implementation, obtained on the following page: http://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_demangling.html

 #include <cxxabi.h> // needed for abi::__cxa_demangle std::shared_ptr<char> cppDemangle(const char *abiName) { int status; char *ret = abi::__cxa_demangle(abiName, 0, 0, &status); /* NOTE: must free() the returned char when done with it! */ std::shared_ptr<char> retval; retval.reset( (char *)ret, [](char *mem) { if (mem) free((void*)mem); } ); return retval; } 

To simplify memory management on returned (char *), I use std :: shared_ptr with the lambda 'deleter' function, which calls free () in the returned memory. Because of this, I don’t have to worry about deleting the memory myself, I just use it as needed, and when shared_ptr goes out of scope, the memory will be free.

Here's the macro that I use to access the name of a demangled name of type (const char *). Please note that you must enable RTTI to access "typeid"

 #define CLASS_NAME(somePointer) ((const char *) cppDemangle(typeid(*somePointer).name()).get() ) 

So, from within the C ++ class, I can say:

 printf("I am inside of a %s\n",CLASS_NAME(this)); 
+11
source

All Articles