Skip to main content

LM_DemangleSymbol

Demangles a symbol name from its mangled form to a human-readable format.

Signature

LM_API lm_char_t * LM_CALL
LM_DemangleSymbol(lm_string_t symbol_name,
                  lm_char_t  *demangled_buf,
                  lm_size_t   maxsize);

Parameters

symbol_name
lm_string_t
required
The mangled symbol name to demangle (e.g., "_ZN3foo3barEv").
demangled_buf
lm_char_t*
The buffer where the demangled symbol name will be stored. If this is NULL, the symbol will be dynamically allocated and maxsize is ignored.
maxsize
lm_size_t
The maximum size of the buffer where the demangled symbol name will be stored. This parameter is ignored if demangled_buf is NULL.

Return Value

Returns a pointer to the demangled symbol string, or NULL if it fails. If the symbol was dynamically allocated (when demangled_buf is NULL), you need to free it with LM_FreeDemangledSymbol.

Description

This function converts mangled C++ symbol names (as generated by the compiler’s name mangling scheme) into their human-readable form. C++ compilers mangle names to encode additional information like namespaces, class names, and parameter types.

Example 1: Using a User-Provided Buffer

#include <libmem/libmem.h>
#include <stdio.h>

int main()
{
    char buffer[256];
    lm_char_t *demangled;
    
    // Demangle a C++ symbol using a fixed buffer
    demangled = LM_DemangleSymbol("_ZN6Player10takeDamageEi", buffer, sizeof(buffer));
    
    if (demangled) {
        printf("Mangled:   _ZN6Player10takeDamageEi\n");
        printf("Demangled: %s\n", demangled);
        // Output: Player::takeDamage(int)
    } else {
        printf("Failed to demangle symbol\n");
    }
    
    return 0;
}

Example 2: Using Dynamic Allocation

#include <libmem/libmem.h>
#include <stdio.h>

int main()
{
    lm_char_t *demangled;
    
    // Demangle a C++ symbol with dynamic allocation
    demangled = LM_DemangleSymbol("_ZNSt6vectorIiE9push_backERKi", NULL, 0);
    
    if (demangled) {
        printf("Demangled symbol: %s\n", demangled);
        // Output: std::vector<int>::push_back(int const&)
        
        // Free the dynamically allocated memory
        LM_FreeDemangledSymbol(demangled);
    } else {
        printf("Failed to demangle symbol\n");
    }
    
    return 0;
}

Example 3: Demangling Symbols from a Module

#include <libmem/libmem.h>
#include <stdio.h>

typedef struct {
    int count;
} callback_data_t;

lm_bool_t symbol_callback(lm_symbol_t *symbol, lm_void_t *arg)
{
    callback_data_t *data = (callback_data_t *)arg;
    lm_char_t *demangled;
    
    // Try to demangle the symbol
    demangled = LM_DemangleSymbol(symbol->name, NULL, 0);
    
    if (demangled) {
        printf("%4d. %s -> %p\n", ++data->count, demangled, (void *)symbol->address);
        LM_FreeDemangledSymbol(demangled);
    } else {
        // If demangling fails, print the original name
        printf("%4d. %s -> %p\n", ++data->count, symbol->name, (void *)symbol->address);
    }
    
    return LM_TRUE;
}

int main()
{
    lm_module_t module;
    callback_data_t data = { 0 };
    
    if (!LM_FindModule("libstdc++.so.6", &module)) {
        printf("Failed to find module\n");
        return 1;
    }
    
    printf("Demangled symbols in %s:\n", module.name);
    LM_EnumSymbols(&module, symbol_callback, &data);
    
    printf("\nTotal symbols: %d\n", data.count);
    
    return 0;
}

Common Mangled Name Examples

Mangled NameDemangled Name
_ZN3foo3barEvfoo::bar()
_ZN6Player10takeDamageEiPlayer::takeDamage(int)
_ZNSt6vectorIiE4sizeEvstd::vector<int>::size()
_ZN9MyClass15myTemplateMethodIiEEvT_void MyClass::myTemplateMethod<int>(int)

Notes

  • When using dynamic allocation (demangled_buf is NULL), always free the result with LM_FreeDemangledSymbol.
  • When using a fixed buffer, ensure the buffer is large enough to hold the demangled name.
  • If the symbol is not a mangled C++ name, the function may fail and return NULL.
  • Different compilers use different mangling schemes (Itanium ABI for GCC/Clang, MSVC for Visual Studio).

LM_FreeDemangledSymbol

Frees the memory allocated for a demangled symbol name.

Signature

LM_API lm_void_t LM_CALL
LM_FreeDemangledSymbol(lm_char_t *symbol_name);

Parameters

symbol_name
lm_char_t*
required
The demangled symbol name to free. This should be a pointer that was returned by LM_DemangleSymbol when using dynamic allocation (when demangled_buf was NULL).

Return Value

This function does not return a value.

Description

This function deallocates memory that was dynamically allocated by LM_DemangleSymbol. You should only call this function on pointers returned by LM_DemangleSymbol when you passed NULL as the demangled_buf parameter.

Example

#include <libmem/libmem.h>
#include <stdio.h>

int main()
{
    lm_char_t *demangled;
    
    // Dynamically allocate demangled symbol
    demangled = LM_DemangleSymbol("_ZN6Player4moveEii", NULL, 0);
    
    if (demangled) {
        printf("Demangled: %s\n", demangled);
        // Output: Player::move(int, int)
        
        // Free the allocated memory
        LM_FreeDemangledSymbol(demangled);
    }
    
    return 0;
}

Important Notes

  • Only use this function for dynamically allocated symbols: Do not call this on symbols that were demangled into a user-provided buffer.
  • Memory Management: Always pair LM_DemangleSymbol (with NULL buffer) with LM_FreeDemangledSymbol to avoid memory leaks.
  • NULL Safety: It is safe to call this function with a NULL pointer (it will do nothing).

Correct vs Incorrect Usage

Correct Usage (Dynamic Allocation)

lm_char_t *demangled = LM_DemangleSymbol("_ZN3foo3barEv", NULL, 0);
if (demangled) {
    printf("%s\n", demangled);
    LM_FreeDemangledSymbol(demangled); // Correct: Free dynamically allocated memory
}

Incorrect Usage (User Buffer)

char buffer[256];
lm_char_t *demangled = LM_DemangleSymbol("_ZN3foo3barEv", buffer, sizeof(buffer));
if (demangled) {
    printf("%s\n", demangled);
    // LM_FreeDemangledSymbol(demangled); // WRONG: Do not free user-provided buffer!
}

See Also

  • LM_DemangleSymbol - Demangle a symbol name
  • LM_EnumSymbolsDemangled - Enumerate symbols with automatic demangling
  • LM_FindSymbolAddressDemangled - Find a symbol by its demangled name

Build docs developers (and LLMs) love