admin管理员组文章数量:1435859
I'm trying to print a backtrace after an exception occurs in my code. I acquire the backtrace like:
constexpr size_t MAX_STACK_FRAMES{ 64 };
static void* stack_traces[MAX_STACK_FRAMES];
void posix_print_stack_trace()
{
int i, trace_size = 0;
char** messages = (char**)NULL;
char buffer[1024]{}; // buffer for error message
trace_size = backtrace(stack_traces, MAX_STACK_FRAMES);
messages = backtrace_symbols(stack_traces, trace_size);
/* skip the first couple stack frames (as they are this function and
our handler) and also skip the last frame as it's (always?) junk. */
// for (i = 3; i < (trace_size - 1); ++i)
// we'll use this for now so you can see what's going on
for (i = 0; i < trace_size; ++i)
{
if (addr2line(global_program_name, stack_traces[i]) != 0)
And use a function called addr2line to attempt to format the backtrace by invoking the addr2line command:
/* have addr2line map the address to the relevant line in the code */
#ifdef __APPLE__
/* apple does things differently... */
sprintf(addr2line_cmd, "atos -o %.256s %p", program_name, addr);
#else
sprintf(addr2line_cmd, "addr2line -f -p -e %.256s %p", program_name, addr);
#endif
The problem I have is that output I get looks like:
?? ??:0
?? ??:0
?? ??:0
?? ??:0
?? ??:0
?? ??:0
?? ??:0
I'm sure it's how I'm invoking the addr2line
command. If I need to adjust the address I pass to it, what do I need to do please?
I'm trying to print a backtrace after an exception occurs in my code. I acquire the backtrace like:
constexpr size_t MAX_STACK_FRAMES{ 64 };
static void* stack_traces[MAX_STACK_FRAMES];
void posix_print_stack_trace()
{
int i, trace_size = 0;
char** messages = (char**)NULL;
char buffer[1024]{}; // buffer for error message
trace_size = backtrace(stack_traces, MAX_STACK_FRAMES);
messages = backtrace_symbols(stack_traces, trace_size);
/* skip the first couple stack frames (as they are this function and
our handler) and also skip the last frame as it's (always?) junk. */
// for (i = 3; i < (trace_size - 1); ++i)
// we'll use this for now so you can see what's going on
for (i = 0; i < trace_size; ++i)
{
if (addr2line(global_program_name, stack_traces[i]) != 0)
And use a function called addr2line to attempt to format the backtrace by invoking the addr2line command:
/* have addr2line map the address to the relevant line in the code */
#ifdef __APPLE__
/* apple does things differently... */
sprintf(addr2line_cmd, "atos -o %.256s %p", program_name, addr);
#else
sprintf(addr2line_cmd, "addr2line -f -p -e %.256s %p", program_name, addr);
#endif
The problem I have is that output I get looks like:
?? ??:0
?? ??:0
?? ??:0
?? ??:0
?? ??:0
?? ??:0
?? ??:0
I'm sure it's how I'm invoking the addr2line
command. If I need to adjust the address I pass to it, what do I need to do please?
- 1 Are you sure you compile your program with debug symbols? – dumbass Commented Nov 18, 2024 at 16:57
1 Answer
Reset to default 0Needed to convert absolute address to offset using:
std::uint64_t convertToVMA(void* addr)
{
Dl_info info;
struct link_map* link_map;
dladdr1((void*)addr, &info, (void**)&link_map, RTLD_DL_LINKMAP);
return reinterpret_cast<std::uint64_t>(addr) - link_map->l_addr;
}
本文标签: cWhat39s wrong with this code that39s attempting to print a backtrace on LinuxStack Overflow
版权声明:本文标题:c++ - What's wrong with this code that's attempting to print a backtrace on Linux - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745656162a2668719.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论