Skip to content

Commit

Permalink
change P_cplus_demangle interface
Browse files Browse the repository at this point in the history
* now takes and returns a std::string instead of a char*
  • Loading branch information
kupsch committed Oct 14, 2020
1 parent e4817fe commit 64b85e6
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 105 deletions.
2 changes: 1 addition & 1 deletion common/src/freebsdHeaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ inline int P_rexec(char **ahost, u_short inport, char *user,
}
#endif

extern char * COMMON_EXPORT P_cplus_demangle( const char * symbol,
extern std::string COMMON_EXPORT P_cplus_demangle( const std::string &symbol,
bool includeTypes = false );

inline int P_mkdir(const char *pathname, mode_t mode) {
Expand Down
4 changes: 2 additions & 2 deletions common/src/freebsdKludges.C
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ using std::map;

#include "symbolDemangleWithCache.h"

char * P_cplus_demangle( const char * symbol, bool includeTypes )
std::string P_cplus_demangle( const std::string &symbol, bool includeTypes )
{
char *demangled = symbol_demangle_with_cache(symbol, includeTypes);
std::string demangled = symbol_demangle_with_cache(symbol, includeTypes);
return demangled;
} /* end P_cplus_demangle() */

Expand Down
8 changes: 1 addition & 7 deletions common/src/linuxHeaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,7 @@ inline int P_rexec(char **ahost, u_short inport, char *user,
char *passwd, char *cmd, int *fd2p) {
return (rexec(ahost, inport, user, passwd, cmd, fd2p));}

/* The following values are taken from demangle.h in binutils */
#define DMGL_PARAMS (1 << 0) /* Include function args */
#define DMGL_ANSI (1 << 1) /* Include const, volatile, etc */

#define DMGL_ARM (1 << 11) /* Use C++ ARM name mangling */

extern COMMON_EXPORT char * P_cplus_demangle( const char * symbol,
extern COMMON_EXPORT std::string P_cplus_demangle( const std::string &symbol,
bool includeTypes = false );

inline int P_mkdir(const char *pathname, mode_t mode) {
Expand Down
4 changes: 2 additions & 2 deletions common/src/linuxKludges.C
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ unsigned long long PDYN_mulMillion(unsigned long long in) {

#include "symbolDemangleWithCache.h"

char * P_cplus_demangle( const char * symbol, bool includeTypes )
std::string P_cplus_demangle( const std::string &symbol, bool includeTypes )
{
char *demangled = symbol_demangle_with_cache(symbol, includeTypes);
std::string demangled = symbol_demangle_with_cache(symbol, includeTypes);
return demangled;
} /* end P_cplus_demangle() */

Expand Down
10 changes: 8 additions & 2 deletions common/src/ntHeaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,14 @@ inline int P_unlink(const char *pathname) {
}
extern char *cplus_demangle(char *, int, bool );
/* We can't export this, it's inline. */
inline char * P_cplus_demangle( const char * symbol, bool includeTypes = false ) {
return cplus_demangle( (char *)symbol, 0, includeTypes );
inline std::string P_cplus_demangle( const std::string &symbol, bool includeTypes = false ) {
char *demangled = cplus_demangle( symbol.c_str(), 0, includeTypes );
if (demangled) {
std::string s = std::string(demangled);
free(demangled)
return s;
} else {
return symbol;
}

#ifndef BPATCH_LIBRARY
Expand Down
29 changes: 13 additions & 16 deletions common/src/symbolDemangleWithCache.C
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,35 @@
*/


#include <string.h>
#include <string>
#include <stdlib.h>
#include "symbolDemangle.h"
#include "symbolDemangleWithCache.h"

static thread_local char* lastSymName = nullptr;
static thread_local std::string lastSymName;
static thread_local bool lastIncludeParams = false;
static thread_local char* lastDemangled = nullptr;
static thread_local std::string lastDemangled;



// Returns a demangled symbol using symbol_demangle, but includes a per thread
// cache of the previous demangling.
//
char *symbol_demangle_with_cache(const char *symName, bool includeParams)
std::string symbol_demangle_with_cache(const std::string &symName, bool includeParams)
{
if (lastSymName && lastDemangled && includeParams == lastIncludeParams && !strcmp(symName, lastSymName)) {
if (includeParams == lastIncludeParams && symName == lastSymName) {
// found hit, return a copy of cached demangling
return strdup(lastDemangled);
return lastDemangled;
}

char *demangled = symbol_demangle(symName, includeParams);
char *demangled = symbol_demangle(symName.c_str(), includeParams);

// update cache
if (lastSymName) {
free(lastSymName);
}
if (lastDemangled) {
free(lastDemangled);
}
lastSymName = strdup(symName);
lastSymName = symName;
lastIncludeParams = includeParams;
lastDemangled = strdup(demangled);
lastDemangled = demangled;

free(demangled);

return demangled;
return lastDemangled;
}
3 changes: 2 additions & 1 deletion common/src/symbolDemangleWithCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <string>

char *symbol_demangle_with_cache(const char *symName, bool includeParams);
std::string symbol_demangle_with_cache(const std::string &symName, bool includeParams);
5 changes: 3 additions & 2 deletions symlite/src/SymLite-elf.C
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,10 @@ std::string SymElf::getDemangledName(const Symbol_t &sym)

if (cache[cache_index].demangled_name)
return std::string(cache[cache_index].demangled_name);
char *res = P_cplus_demangle(name, true);
//char *res = P_cplus_demangle(name, true);
std::string res = P_cplus_demangle(name, true);

cache[cache_index].demangled_name = res ? res : name;
cache[cache_index].demangled_name = strdup(res.c_str());
return cache[cache_index].demangled_name;
}

Expand Down
30 changes: 4 additions & 26 deletions symtabAPI/src/Symbol.C
Original file line number Diff line number Diff line change
Expand Up @@ -79,36 +79,14 @@ SYMTAB_EXPORT string Symbol::getMangledName() const

SYMTAB_EXPORT string Symbol::getPrettyName() const
{
std::string working_name = mangledName_;
#ifdef os_windows
// Accoring to Itanium C++ ABI, all mangled names start with _Z
if (mangledName_.size() < 2 || mangledName_[0] != '_' || mangledName_[1] != 'Z') return working_name;
#endif

char *prettyName = P_cplus_demangle(working_name.c_str(), false);
if (prettyName) {
working_name = std::string(prettyName);
// XXX caller-freed
free(prettyName);
}
return working_name;
std::string prettyName = P_cplus_demangle(mangledName_, false);
return prettyName;
}

SYMTAB_EXPORT string Symbol::getTypedName() const
{
std::string working_name = mangledName_;
#ifdef os_windows
// Accoring to Itanium C++ ABI, all mangled names start with _Z
if (mangledName_.size() < 2 || mangledName_[0] != '_' || mangledName_[1] != 'Z') return working_name;
#endif

char *prettyName = P_cplus_demangle(working_name.c_str(), true);
if (prettyName) {
working_name = std::string(prettyName);
// XXX caller-freed
free(prettyName);
}
return working_name;
std::string typedName = P_cplus_demangle(mangledName_, true);
return typedName;
}

bool Symbol::setOffset(Offset newOffset)
Expand Down
26 changes: 9 additions & 17 deletions symtabAPI/src/dwarfWalker.C
Original file line number Diff line number Diff line change
Expand Up @@ -1562,26 +1562,18 @@ bool DwarfWalker::addFuncToContainer(boost::shared_ptr<Type> returnType) {
functions, but confuses the tests. Since Type uses vectors
to hold field names, however, duplicate -- demangled names -- are OK. */

char * demangledName = P_cplus_demangle( curName().c_str() );
std::string toUse;
std::string demangledName = P_cplus_demangle( curName() );

if (!demangledName) {
dwarf_printf("(0x%lx) Unable to demangle %s, using it as mangled\n", id(), curName().c_str());
toUse = curName();
}
else {
// Strip everything left of the rightmost ':' off to get rid of the class names
toUse = demangledName;
// rfind finds the last occurrence of ':'; add 1 to get past it.
size_t offset = toUse.rfind(':');
if (offset != toUse.npos) {
toUse = toUse.substr(offset+1);
}
// Strip everything left of the rightmost ':' off to get rid of the class names
// rfind finds the last occurrence of ':'; add 1 to get past it.
size_t offset = demangledName.rfind(':');
if (offset != demangledName.npos) {
demangledName.erase(offset+1);
}

curEnclosure()->asFieldListType().addField( toUse, Type::make_shared<typeFunction>(
type_id(), returnType, toUse));
free( demangledName );
curEnclosure()->asFieldListType().addField( demangledName, Type::make_shared<typeFunction>(
type_id(), returnType, demangledName));

return true;
}

Expand Down
36 changes: 7 additions & 29 deletions symtabAPI/src/parseStab.C
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@

#include <ctype.h>
#include <iostream>
// from libiberty's demangle.h
#define DMGL_PARAMS (1 << 0)
#define DMGL_ANSI (1 << 1)
#define DMGL_VERBOSE (1 << 3)

#include "symutil.h"
#include "Symtab.h" // For looking up compiler type
Expand Down Expand Up @@ -106,15 +102,11 @@ void vectorNameMatchKLUDGE(Module *mod, char *demangled_sym, std::vector<Functio
if (syms.size()) {
l_mangled = syms[0]->getMangledName();

char * l_demangled_raw = P_cplus_demangle(l_mangled.c_str());
if( l_demangled_raw == NULL ) {
l_demangled_raw = strdup(l_mangled.c_str());
}
std::string l_demangled_raw = P_cplus_demangle(l_mangled);

if (!strcmp(l_demangled_raw, demangled_sym)) {
if (l_demangled_raw == demangled_sym) {
matches.push_back(i);
}
free(l_demangled_raw);
}
} /* end iteration over function vector */
}
Expand Down Expand Up @@ -143,11 +135,8 @@ Function *mangledNameMatchKLUDGE(const char *pretty, const char *mangled,
}

// demangle name with extra parameters
char * demangled_sym = P_cplus_demangle( mangled, true );
if( demangled_sym == NULL ) {
demangled_sym = strdup( mangled );
assert( demangled_sym != NULL );
}
std::string demangled = P_cplus_demangle( mangled, true );
char *demangled_sym = strdup(demangled.c_str());

std::vector<int> matches;

Expand Down Expand Up @@ -220,17 +209,7 @@ std::string Dyninst::SymtabAPI::parseStabString(Module *mod, int linenum, char *
std::string mangledname = getIdentifier( stabstr, cnt );

currentRawSymbolName = mangledname;
char * demangled = P_cplus_demangle( mangledname.c_str() );
std::string name;

if ( demangled == NULL )
{
name = mangledname;
}
else
{
name = demangled;
}
std::string name = P_cplus_demangle( mangledname );

if ( name[0] != '\0' && stabstr[cnt] != ':' )
{
Expand Down Expand Up @@ -2015,7 +1994,8 @@ static char *parseCPlusPlusInfo(Module *mod,
className[3] = 'c';
className[strlen(className)-1] = '\0'; // remove tailing "_"
std::string methodName = std::string(className) + std::string(funcName) + std::string("_");
char * name = P_cplus_demangle( methodName.c_str() );

std::string fName = P_cplus_demangle( methodName );
if( name != NULL ) {
funcName = strrchr( name, ':' );
if( funcName ) { funcName++; }
Expand All @@ -2025,8 +2005,6 @@ static char *parseCPlusPlusInfo(Module *mod,
// should include position for virtual methods
auto fieldType = tc->findType("void", Type::share);

std::string fName = convertCharToString(funcName);

newType->asFieldListType().addField( fName, Type::make_shared<typeFunction>( ID, fieldType, fName));

free(name);
Expand Down

0 comments on commit 64b85e6

Please sign in to comment.