From 8ef02d3b9df213dd872f77998c5157985066ca57 Mon Sep 17 00:00:00 2001 From: Ludwig Ortmann Date: Wed, 26 Feb 2014 10:43:07 +0100 Subject: [PATCH 1/5] native profiling support only works with Linux for now --- boards/native/Makefile.include | 11 ++++++++--- cpu/native/startup.c | 1 - cpu/native/syscalls.c | 16 +++++++++++++--- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/boards/native/Makefile.include b/boards/native/Makefile.include index 753b118b24..c9125838ff 100644 --- a/boards/native/Makefile.include +++ b/boards/native/Makefile.include @@ -26,9 +26,9 @@ export ASFLAGS = export DEBUGGER_FLAGS = $(ELF) export VALGRIND_FLAGS ?= --track-origins=yes all-valgrind: export CFLAGS += -DHAVE_VALGRIND_H -g -all-valgrind: export NATIVEINCLUDES += $(shell pkg-config valgrind --cflags) - -export INCLUDES += $(NATIVEINCLUDES) +all-valgrind: export INCLUDES += $(shell pkg-config valgrind --cflags) +all-profile: export CFLAGS += -pg +all-profile: export LINKFLAGS += -pg # backward compatability with glibc <= 2.17 for native ifeq ($(CPU),native) @@ -56,6 +56,8 @@ endif all: # do not override first target +all-profile: all + all-valgrind: all valgrind: @@ -63,3 +65,6 @@ valgrind: # echo 0 > /proc/sys/kernel/yama/ptrace_scope # VALGRIND_FLAGS += --db-attach=yes $(VALGRIND) $(VALGRIND_FLAGS) $(ELF) $(PORT) + +profile: + gprof $(ELF) diff --git a/cpu/native/startup.c b/cpu/native/startup.c index f9a893ab3a..8620f8329f 100644 --- a/cpu/native/startup.c +++ b/cpu/native/startup.c @@ -186,7 +186,6 @@ __attribute__((constructor)) static void startup(int argc, char **argv) *(void **)(&real_read) = dlsym(RTLD_NEXT, "read"); *(void **)(&real_write) = dlsym(RTLD_NEXT, "write"); *(void **)(&real_malloc) = dlsym(RTLD_NEXT, "malloc"); - *(void **)(&real_calloc) = dlsym(RTLD_NEXT, "calloc"); *(void **)(&real_realloc) = dlsym(RTLD_NEXT, "realloc"); *(void **)(&real_free) = dlsym(RTLD_NEXT, "free"); diff --git a/cpu/native/syscalls.c b/cpu/native/syscalls.c index 032396a0d2..3fbc979678 100644 --- a/cpu/native/syscalls.c +++ b/cpu/native/syscalls.c @@ -107,12 +107,22 @@ void free(void *ptr) _native_syscall_leave(); } +int _native_in_calloc; void *calloc(size_t nmemb, size_t size) { - /* XXX: This is a dirty hack to enable old dlsym versions to run. - * Throw it out when Ubuntu 12.04 support runs out (in 2017-04)! */ + /* dynamically load calloc when it's needed - this is necessary to + * support profiling as it uses calloc before startup runs */ if (!real_calloc) { - return NULL; + if (_native_in_calloc) { + /* XXX: This is a dirty hack to enable old dlsym versions to run. + * Throw it out when Ubuntu 12.04 support runs out (in 2017-04)! */ + return NULL; + } + else { + _native_in_calloc = 1; + *(void **)(&real_calloc) = dlsym(RTLD_NEXT, "calloc"); + _native_in_calloc = 0; + } } void *r; From ad1d8df0803d6ba71b3ac669045f9b79226c3929 Mon Sep 17 00:00:00 2001 From: Ludwig Ortmann Date: Sat, 1 Mar 2014 16:48:36 +0100 Subject: [PATCH 2/5] change target name, add cachegrind target --- boards/native/Makefile.include | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/boards/native/Makefile.include b/boards/native/Makefile.include index c9125838ff..20b2c6b38a 100644 --- a/boards/native/Makefile.include +++ b/boards/native/Makefile.include @@ -24,11 +24,12 @@ export CFLAGS += -Wall -Wextra -pedantic -m32 export LINKFLAGS += -m32 -gc -ldl export ASFLAGS = export DEBUGGER_FLAGS = $(ELF) -export VALGRIND_FLAGS ?= --track-origins=yes +valgrind: export VALGRIND_FLAGS ?= --track-origins=yes +cachegrind: export CACHEGRIND_FLAGS += --tool=cachegrind all-valgrind: export CFLAGS += -DHAVE_VALGRIND_H -g all-valgrind: export INCLUDES += $(shell pkg-config valgrind --cflags) -all-profile: export CFLAGS += -pg -all-profile: export LINKFLAGS += -pg +all-gprof: export CFLAGS += -pg +all-gprof: export LINKFLAGS += -pg # backward compatability with glibc <= 2.17 for native ifeq ($(CPU),native) @@ -56,7 +57,7 @@ endif all: # do not override first target -all-profile: all +all-gprof: all all-valgrind: all @@ -66,5 +67,8 @@ valgrind: # VALGRIND_FLAGS += --db-attach=yes $(VALGRIND) $(VALGRIND_FLAGS) $(ELF) $(PORT) -profile: +cachegrind: + $(VALGRIND) $(CACHEGRIND_FLAGS) $(ELF) $(PORT) + +gprof: gprof $(ELF) From ae898ef9eb12d1e24987621983eca04a026c5a9d Mon Sep 17 00:00:00 2001 From: Ludwig Ortmann Date: Wed, 5 Mar 2014 10:53:15 +0100 Subject: [PATCH 3/5] rebase_fixup --- boards/native/Makefile.include | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/boards/native/Makefile.include b/boards/native/Makefile.include index 20b2c6b38a..ce3bd7ec64 100644 --- a/boards/native/Makefile.include +++ b/boards/native/Makefile.include @@ -27,10 +27,12 @@ export DEBUGGER_FLAGS = $(ELF) valgrind: export VALGRIND_FLAGS ?= --track-origins=yes cachegrind: export CACHEGRIND_FLAGS += --tool=cachegrind all-valgrind: export CFLAGS += -DHAVE_VALGRIND_H -g -all-valgrind: export INCLUDES += $(shell pkg-config valgrind --cflags) +all-valgrind: export NATIVEINCLUDES += $(shell pkg-config valgrind --cflags) all-gprof: export CFLAGS += -pg all-gprof: export LINKFLAGS += -pg +export INCLUDES += $(NATIVEINCLUDES) + # backward compatability with glibc <= 2.17 for native ifeq ($(CPU),native) ifeq ($(shell uname -s),Linux) From a62559de623e3826f27f3247d6440302e9995779 Mon Sep 17 00:00:00 2001 From: Ludwig Ortmann Date: Fri, 14 Mar 2014 12:48:19 +0100 Subject: [PATCH 4/5] make: streamline native targets The new target names are easier to remember / decipher: term term-cachegrind term-gprof term-valgrind all all-cachegrind all-gprof all-valgrind all-debug eval-gprof eval-cachegrind --- boards/native/Makefile.include | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/boards/native/Makefile.include b/boards/native/Makefile.include index ce3bd7ec64..d8683c7e41 100644 --- a/boards/native/Makefile.include +++ b/boards/native/Makefile.include @@ -24,10 +24,13 @@ export CFLAGS += -Wall -Wextra -pedantic -m32 export LINKFLAGS += -m32 -gc -ldl export ASFLAGS = export DEBUGGER_FLAGS = $(ELF) -valgrind: export VALGRIND_FLAGS ?= --track-origins=yes -cachegrind: export CACHEGRIND_FLAGS += --tool=cachegrind +term-memcheck: export VALGRIND_FLAGS ?= --track-origins=yes +term-cachegrind: export CACHEGRIND_FLAGS += --tool=cachegrind +term-gprof: export TERMPROG = GMON_OUT_PREFIX=gmon.out $(ELF) all-valgrind: export CFLAGS += -DHAVE_VALGRIND_H -g all-valgrind: export NATIVEINCLUDES += $(shell pkg-config valgrind --cflags) +all-debug: export CFLAGS += -g +all-cachegrind: export CFLAGS += -g all-gprof: export CFLAGS += -pg all-gprof: export LINKFLAGS += -pg @@ -63,14 +66,21 @@ all-gprof: all all-valgrind: all -valgrind: +all-cachegrind: all + +term-valgrind: # use this if you want to attach gdb from valgrind: # echo 0 > /proc/sys/kernel/yama/ptrace_scope # VALGRIND_FLAGS += --db-attach=yes $(VALGRIND) $(VALGRIND_FLAGS) $(ELF) $(PORT) -cachegrind: +term-cachegrind: $(VALGRIND) $(CACHEGRIND_FLAGS) $(ELF) $(PORT) -gprof: - gprof $(ELF) +term-gprof: term + +eval-gprof: + gprof $(ELF) $(shell ls -rt gmon.out* | tail -1) + +eval-cachegrind: + cg_annotate $(shell ls -rt cachegrind.out* | tail -1) From dfd0b43cbe23d45bdef7e9f5543206e47bcfa2ef Mon Sep 17 00:00:00 2001 From: Ludwig Ortmann Date: Fri, 14 Mar 2014 12:56:44 +0100 Subject: [PATCH 5/5] add variables for gprof, cg_annotate --- boards/native/Makefile.include | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/boards/native/Makefile.include b/boards/native/Makefile.include index d8683c7e41..37e3b11117 100644 --- a/boards/native/Makefile.include +++ b/boards/native/Makefile.include @@ -18,6 +18,8 @@ export DEBUGGER = gdb export TERMPROG = $(ELF) export FLASHER = true export VALGRIND ?= valgrind +export CGANNOTATE ?= cg_annotate +export GPROF ?= gprof # flags: export CFLAGS += -Wall -Wextra -pedantic -m32 @@ -80,7 +82,7 @@ term-cachegrind: term-gprof: term eval-gprof: - gprof $(ELF) $(shell ls -rt gmon.out* | tail -1) + $(GPROF) $(ELF) $(shell ls -rt gmon.out* | tail -1) eval-cachegrind: - cg_annotate $(shell ls -rt cachegrind.out* | tail -1) + $(CGANNOTATE) $(shell ls -rt cachegrind.out* | tail -1)