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;
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745656162a4638554.html
评论列表(0条)