Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function to mangle/demangle functions

I have previously, here, been shown that C++ functions aren't easily represented in assembly. Now I am interested in reading them one way or another because Callgrind, part of Valgrind, show them demangled while in assembly they are shown mangled.

So I would like to either mangle the Valgrind function output or demangle the assembly names of functions. Anyone ever tried something like that? I was looking at a 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 that? I want to demangle/mangle in C.

My compiler is gcc 4.x.

like image 308
Syntax_Error Avatar asked Feb 08 '11 23:02

Syntax_Error


People also ask

What does Demangle mean?

Transforming C++ ABI identifiers (like RTTI symbols) into the original C++ source identifiers is called “demangling.”

What is mangling and Demangling in C++?

That means that the C++ compiler needs to generate C identifier compatible symbols for C++ constructs. This process is called “mangling”, the resulting symbol is a “mangled symbol”, and reconstructing the original C++ name is “demangling”.

What is C ++ Filt?

The c++filt program does the inverse mapping: it decodes ( demangles ) low-level names into user-level names so that the linker can keep these overloaded functions from clashing. Every alphanumeric word (consisting of letters, digits, underscores, dollars, or periods) seen in the input is a potential label.


2 Answers

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

like image 142
Eugen Constantin Dinca Avatar answered Oct 15 '22 13:10

Eugen Constantin Dinca


Here is my C++11 implementation, derived from 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 make the memory management easy on the returned (char *), I'm using a std::shared_ptr with a custom lambda 'deleter' function that calls free() on the returned memory. Because of this, I don't ever have to worry about deleting the memory on my own, I just use it as needed, and when the shared_ptr goes out of scope, the memory will be free'd.

Here's the macro I use to access the demangled type name as a (const char *). Note that you must have RTTI turned on to have access to 'typeid'

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

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

printf("I am inside of a %s\n",CLASS_NAME(this));
like image 40
Dave Avatar answered Oct 15 '22 15:10

Dave