wip: tests: show more debug information

This commit is contained in:
Khaïs COLIN 2025-02-26 14:07:55 +01:00 committed by Khaïs COLIN
parent c57a4a69a7
commit d303f22b73
7 changed files with 99 additions and 5 deletions

View file

@ -1,5 +1,5 @@
NAME = minishell
DEBUG = -g
DEBUG = -g -O0
# -fno-omit-frame-pointer is to prevent malloc stacktraces from being truncated,
# see "My malloc stacktraces are too short" here:
# https://github.com/google/sanitizers/wiki/AddressSanitizer
@ -19,6 +19,7 @@ ifeq ($(CFLAGS),)
endif
export CFLAGS
srcs = \
src/print_backtrace.c \
src/buffer/buffer.c \
src/env/env.c \
src/env/env_convert.c \

View file

@ -6,7 +6,7 @@
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 16:06:09 by jguelen #+# #+# */
/* Updated: 2025/02/20 14:59:22 by khais ### ########.fr */
/* Updated: 2025/03/03 13:02:45 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -173,4 +173,42 @@ t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
/*frees two pointers and returns NULL*/
void *free2(void *p1, void *p2);
# include <stdio.h>
# include "../src/print_backtrace.h"
# define malloc(size) \
({ \
void * ret = malloc(size); \
if (!ret) \
fprintf(stderr, "malloc fail at %s:%d failed\n", \
__FILE__, __LINE__); \
else \
fprintf(stderr, "malloc at(%p) at %s:%d\n", \
ret, __FILE__, __LINE__); \
print_backtrace(); \
ret; \
})
# define ft_calloc(elmt_nbr, elmt_size) \
({ \
void * ret = ft_calloc(elmt_nbr, elmt_size); \
if (!ret) \
fprintf(stderr, "malloc fail at %s:%d failed\n", \
__FILE__, __LINE__); \
else \
fprintf(stderr, "ft_calloc at(%p) at %s:%d\n", \
ret, __FILE__, __LINE__); \
print_backtrace(); \
ret; \
})
# define free(ptr) \
({ \
fprintf(stderr, "free(%p) at %s:%d\n", \
ptr, __FILE__, __LINE__); \
print_backtrace(); \
free(ptr); \
})
#endif

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/13 17:20:36 by khais #+# #+# */
/* Updated: 2025/03/06 16:52:44 by khais ### ########.fr */
/* Updated: 2025/03/09 12:35:25 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -45,6 +45,7 @@ t_worddesc *worddesc_create(char *word, char flags, char *marker)
*/
void worddesc_destroy(t_worddesc *worddesc)
{
ft_dprintf(STDERR_FILENO, "WORDDESC_destroy: %p\n", worddesc);
if (worddesc == NULL)
return ;
free(worddesc->word);

View file

@ -6,12 +6,14 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/13 17:07:01 by khais #+# #+# */
/* Updated: 2025/02/24 18:20:18 by khais ### ########.fr */
/* Updated: 2025/02/26 16:57:59 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "wordlist.h"
#include "ft_printf.h"
#include "libft.h"
#include "unistd.h"
#include <stdlib.h>
/*
@ -41,6 +43,7 @@ void wordlist_destroy(t_wordlist *wordlist)
while (wordlist != NULL)
{
ft_dprintf(STDERR_FILENO, "wordlist_destroy: %p\n", wordlist);
worddesc_destroy(wordlist->word);
prev = wordlist;
wordlist = wordlist->next;
@ -108,5 +111,6 @@ t_worddesc *wordlist_pop(t_wordlist **wordlist)
(*wordlist) = first->next;
word = first->word;
free(first);
ft_dprintf(STDERR_FILENO, "freed a wordlist in wordlist_pop: %p\n", first);
return (word);
}

32
src/print_backtrace.c Normal file
View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_backtrace.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/03 11:56:35 by khais #+# #+# */
/* Updated: 2025/03/03 13:02:28 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#define _GNU_SOURCE // Important for dladdr
#include <stdio.h>
#include <stdlib.h>
#include <execinfo.h>
#include <dlfcn.h>
#include <string.h>
void print_backtrace() {
void* buffer[100];
int num_addresses = backtrace(buffer, 100);
Dl_info info;
printf("Backtrace:\n");
for (int i = 0; i < num_addresses; i++) {
if (dladdr(buffer[i], &info)) {
printf("\t%s (%s:%p)\n", info.dli_sname, info.dli_fname, buffer[i]);
} else {
printf("0x%p\n", buffer[i]);
}
}
}

18
src/print_backtrace.h Normal file
View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_backtrace.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/03 11:56:54 by khais #+# #+# */
/* Updated: 2025/03/03 12:15:50 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef BACKTRACE_H
#define BACKTRACE_H
void print_backtrace();
#endif // BACKTRACE_H

View file

@ -39,7 +39,7 @@ run: $(run_tests)
$(CC) -MM $(CFLAGS) $(IFLAGS) $*.c > $*.d
test_%: %.o $(objs)
$(CC) $(CFLAGS) -o $@ $*.o $(objs) $(LINCLUDE) $(LDLIBS)
$(CC) $(CFLAGS) -rdynamic -o $@ $*.o $(objs) $(LINCLUDE) $(LDLIBS)
run_test_%: test_%
@echo