Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6216495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:15:33+00:00 2026-05-24T07:15:33+00:00

I am trying to get a backtrace at some point of the execution of

  • 0

I am trying to get a backtrace at some point of the execution of my (c++) program.

for that I am using backtrace and backtrace_symbols. Something along this lines:

std::string stacktrace( unsigned int frames_to_skip )
{
    std::string str;

    void* stack_addrs[50];
    int trace_size = backtrace( stack_addrs, 50 );
    char** stack_strings = backtrace_symbols( stack_addrs, trace_size );

    str += "[bt] backtrace:\n";
    // skip frames_to_skip stack frames
    for( int i = frames_to_skip; i < trace_size; ++i )
    {
        char tmp[4096];
        sprintf( tmp, "[bt] #%d %s\n", i-frames_to_skip, stack_strings[i] );
        str += tmp;
    }

    free( stack_strings );

    return str;
}

It works but some functions names are missing. example:

[bt] #0 /path/to/executable() [0x43e1b5]
[bt] #1 /path/to/executable() [0x43e0cd]
[bt] #2 /path/to/executable() [0x43df51]
[bt] #3 /path/to/executable() [0x43dd44]
[bt] #4 /path/to/executable() [0x43db50]
[bt] #5 /path/to/executable() [0x43d847]
[bt] #6 /path/to/executable() [0x43d216]
[bt] #7 /path/to/executable() [0x43c1e1]
[bt] #8 /path/to/executable() [0x43b293]
[bt] #9 /path/to/executable(_Z29SomeRN5other8symbolE+0x2c) [0x43a6ca]
[bt] #10 /path/to/executable(_Z11SomeIN5_8symbolEPFvRS1_EEvRKT_RKT0_+0x77) [0x441716]
...

the functions 0 to 8 have one common point : they all sit in a namespace…
I tried putting function 9 in an anonymous namespace (without any other modification) and it disapears from the backtrace… which now looks like this:

[bt] #0 /path/to/executable() [0x43e1b5]
[bt] #1 /path/to/executable() [0x43e0cd]
[bt] #2 /path/to/executable() [0x43df51]
[bt] #3 /path/to/executable() [0x43dd44]
[bt] #4 /path/to/executable() [0x43db50]
[bt] #5 /path/to/executable() [0x43d847]
[bt] #6 /path/to/executable() [0x43d216]
[bt] #7 /path/to/executable() [0x43c1e1]
[bt] #8 /path/to/executable() [0x43b293]
[bt] #9 /path/to/executable() [0x43a6ca]
[bt] #10 /path/to/executable(_Z11SomeIN5_8symbolEPFvRS1_EEvRKT_RKT0_+0x77) [0x441716]
...

Is there any way to fix that?

p.s.: version of g++:
g++ (GCC) 4.6.0 20110530 (Red Hat 4.6.0-9)

edit fixed max depth of the backtrace after Code Monkey remark
edit2 added the full code of the function
edit3 the code is compiled with -O0 -g3 and linked with -rdynamic

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:15:34+00:00Added an answer on May 24, 2026 at 7:15 am

    Your problem may be the functions you are using. Your max_depth in backtrace(..) is set to 16. That may be too low. At any rate…

    This blog post on C++ stack traces with GCC explains how you should be performing stack traces. In sum,

    #include <execinfo.h>
    void print_trace(FILE *out, const char *file, int line)
    {
        const size_t max_depth = 100;
        size_t stack_depth;
        void *stack_addrs[max_depth];
        char **stack_strings;
    
        stack_depth = backtrace(stack_addrs, max_depth);
        stack_strings = backtrace_symbols(stack_addrs, stack_depth);
    
        fprintf(out, "Call stack from %s:%d:\n", file, line);
    
        for (size_t i = 1; i < stack_depth; i++) {
            fprintf(out, "    %s\n", stack_strings[i]);
        }
        free(stack_strings); // malloc()ed by backtrace_symbols
        fflush(out);
    }
    

    GCC also provides access to the C++ name (de)mangler. There are some
    pretty hairy details to learn about memory ownership, and interfacing
    with the stack trace output requires a bit of string parsing, but it
    boils down to replacing the above inner loop with this:

    #include <cxxabi.h>
    ...
    for (size_t i = 1; i < stack.depth; i++) {
        size_t sz = 200; // just a guess, template names will go much wider
        char *function = static_cast(malloc(sz));
        char *begin = 0, *end = 0;
        // find the parentheses and address offset surrounding the mangled name
        for (char *j = stack.strings[i]; *j; ++j) {
            if (*j == '(') {
                begin = j;
            }
            else if (*j == '+') {
                end = j;
            }
        }
        if (begin && end) {
            *begin++ = '';
            *end = '';
            // found our mangled name, now in [begin, end)
    
            int status;
            char *ret = abi::__cxa_demangle(begin, function, &sz, &status);
            if (ret) {
                // return value may be a realloc() of the input
                function = ret;
            }
            else {
                // demangling failed, just pretend it's a C function with no args
                std::strncpy(function, begin, sz);
                std::strncat(function, "()", sz);
                function[sz-1] = '';
            }
            fprintf(out, "    %s:%s\n", stack.strings[i], function);
        }
        else
        {
            // didn't find the mangled name, just print the whole line
            fprintf(out, "    %s\n", stack.strings[i]);
        }
        free(function);
    }
    

    There is more information on that site (I didn’t want to copy verbatim) but looking at this code and the above site should get you on the right track.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get a GDB backtrace output from GDB through PHP. I'd like
I am trying get all html links within a string and replace them using
I am trying get all html links within a string and replace them using
I am trying get Struts 2 and Tiles to work and I am using
Trying to get this expression to work, can someone look at it and tell
I'm new to Ruby and trying to get my head around some of its
I am trying to get hash of password using Botan's function generate_bcrypt(). I read
I'm trying to run GnuTLS example on VPS server. I get this error: [root@localhost
I get this error when trying to update the profile in ucp SQL ERROR
I was having some trouble running some Ruby scripts. While trying to get these

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.