lib

morphux C utility library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/lib

commit d669289ca641ddeba99af37c586e727c95ef0e9d
parent edfedc482750d0430e0be89ef8fef0b1e82159d8
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Mon, 24 Apr 2017 22:29:59 +0200

Add(Profiler): Add intern profiler inside libmorphux:

In order to activate it, you must compile with -DDEBUG_FULL, and the flag -finstrument-functions
The callback print to stdout every function called, with a timestamp, at runtime.

Diffstat:
MMakefile | 2+-
Minc/fail_test.h | 2++
Msrc/m_print.c | 28++++++++++++++++++++++++++++
Msrc/test.c | 1-
Mtests/Makefile | 6+++---
5 files changed, 34 insertions(+), 5 deletions(-)

diff --git a/Makefile b/Makefile @@ -35,7 +35,7 @@ $(NAME): $(OBJS) $(LIB) $(LFLAGS) $(NAME) $(OBJS) check: all - $(MAKE) fclean all CFLAGS="$(CFLAGS) -Wno-error -DCOMPILE_WITH_TEST -DDEBUG" + $(MAKE) fclean all CFLAGS="$(CFLAGS) -Wno-error -DCOMPILE_WITH_TEST -DDEBUG -DDEBUG_FULL -ldl" make -C tests re check doc: diff --git a/inc/fail_test.h b/inc/fail_test.h @@ -18,6 +18,8 @@ # ifndef M_FAIL_TEST_H # define M_FAIL_TEST_H +# undef strdup + # define malloc(x) fl_malloc(x) # define write(fd, ptr, len) fl_write(fd, ptr, len) # define read(fd, ptr, len) fl_read(fd, ptr, len) diff --git a/src/m_print.c b/src/m_print.c @@ -159,3 +159,31 @@ bool m_log(const char *str, ...) { va_end(ap); return ret; } + +#ifdef DEBUG_FULL +# define __USE_GNU +# include <dlfcn.h> +# include <time.h> + +void __cyg_profile_func_enter(void *fn, void *call) __attribute__((no_instrument_function)); +void __cyg_profile_func_exit(void *fn, void *call) __attribute__((no_instrument_function)); + +void __cyg_profile_func_enter(void *fn, void *call) { + Dl_info info; + + (void)call; + if (dladdr(fn, &info) != 0) + { + printf("[%lu]: %s:%s %p\n", time(NULL), info.dli_fname, + info.dli_sname == NULL ? "???" : info.dli_sname, fn); + return ; + } +} + +void __cyg_profile_func_exit(void *fn, void *call) { + (void)fn; + (void)call; + return ; +} + +#endif diff --git a/src/test.c b/src/test.c @@ -40,7 +40,6 @@ static int g_strdup_fail = -1; static int g_fstat_fail = -1; void *fl_malloc(size_t alloc) { -static char *(*real_strdup)(const char *) = &(strdup); if (g_malloc_fail == -1) return real_malloc(alloc); if (g_malloc_fail == 0) { diff --git a/tests/Makefile b/tests/Makefile @@ -1,7 +1,7 @@ NAME = test CC = gcc -CFLAGS = -Wall -Wextra -Wno-unused-result -g -O3 -I../inc -DDEBUG -LDFLAGS = -lmorphux +CFLAGS = -Wall -Wextra -Wno-unused-result -g -O3 -I../inc -DDEBUG #-finstrument-functions -export-dynamic +LDFLAGS = -lmorphux -ldl SRCS = $(wildcard *.c) OBJS = $(SRCS:%.c=%.o) @@ -15,7 +15,7 @@ endif all: $(NAME) $(NAME): $(OBJS) - $(CC) $(CFLAGS) $(OBJS) -o $(NAME) -L../ -lmorphux + $(CC) $(CFLAGS) $(OBJS) -o $(NAME) -L../ $(LDFLAGS) check: $(NAME) ./$(NAME)