way better backtrace !

This commit is contained in:
Maieul BOYER 2024-05-08 16:07:23 +02:00
parent 99f1e58812
commit 09b2f62fc3
No known key found for this signature in database
6 changed files with 33 additions and 20 deletions

View file

@ -6,16 +6,20 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 11:08:03 by maiboyer #+# #+# */
/* Updated: 2024/05/08 15:22:04 by maiboyer ### ########.fr */
/* Updated: 2024/05/08 16:06:58 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/printf/printf.h"
#include "me/types.h"
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#define PRINT_BACKTRACE
#ifndef BASE_PATH
# define BASE_PATH "/no_base_path_defined/"
#endif
#if defined(PRINT_BACKTRACE) || defined(BACKTRACE_DEEP)
@ -29,13 +33,19 @@ static void print_trace_inner(void **trace, t_str *messages, t_usize i)
t_i32 p;
p = 0;
printf("[bt] #%zu\t", i);
fprintf(stderr, "[bt] #%zu\t", i);
while (messages[i][p] != '(' && messages[i][p] != ' '
&& messages[i][p] != 0)
++p;
fflush(stdout);
snprintf(syscom, 1024, "addr2line %p -e %.*s -sipf", trace[i], p,
messages[i]);
fflush(stderr);
snprintf(syscom, \
1024, \
"addr2line %p -e %.*s -ipf | 1>&2 rg \"^(.*) at %s(.*)\"'$' " \
"--replace '$1 at $2' --color never", \
trace[i], \
p, \
messages[i], \
BASE_PATH);
(void)system(syscom);
}
@ -49,7 +59,7 @@ void print_trace(void)
size = backtrace(trace, BACKTRACE_DEEP);
messages = backtrace_symbols(trace, size);
i = 1;
printf("[bt] Execution path:\n");
fprintf(stderr, "[bt] Execution path:\n");
if (size >= 3)
size -= 3;
while (i < size)
@ -65,6 +75,7 @@ static void print_trace(void)
void me_abort(void)
{
me_eprintf("Abort:\n");
print_trace();
me_exit(1);
}