From a95e0617d919eab6c4700b0d2e84de58cff4691c Mon Sep 17 00:00:00 2001 From: David CARLIER Date: Sun, 27 Aug 2023 12:49:46 +0100 Subject: [PATCH 1/2] hdbg arm64 implementation proposal. x29 is the frame pointer register on arm64, we store them respectively using temporary vars. note arm32 needs its own version. --- heap.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/heap.c b/heap.c index ea9272d..13b256c 100644 --- a/heap.c +++ b/heap.c @@ -344,13 +344,7 @@ getframe(void ***bp, void ***ip) { "ret;" ); } -#elif defined(__aarch64__) || defined(__arm__) -void -getframe(void ***bp, void ***ip) { - //XXX: TODO: support aarch64 -} -#else - +#elif defined(__i386__) static void __attribute__((naked,noinline,optimize("O0"))) getframe(void ***bp, void ***ip) { asm("mov (%esp), %ecx;" @@ -360,6 +354,25 @@ getframe(void ***bp, void ***ip) { "mov %ebp, (%edx);" "ret;"); } +#elif defined(__aarch64__) +static void __attribute__((noinline,optimize("O0"))) +getframe(void ***bp, void ***ip) { + void *stackp, *framep; + asm( + "mov %0, sp;" + "mov %1, x29;" + "str %0, [%2];" + "str %1, [%3]" + : "=r"(stackp), "=r"(framep) + : "r"(ip), "r"(bp) + : "memory" + ); +} +#else +void +getframe(void ***bp, void ***ip) { + //XXX: TODO: support other relevant archs +} #endif static void getstacktrace(void **ents, int max_ents) { From 9294a9fb2fb163ddc0b279da2b6efd8951daeddf Mon Sep 17 00:00:00 2001 From: David CARLIER Date: Wed, 30 Aug 2023 22:26:53 +0100 Subject: [PATCH 2/2] changes from feedback. also avoiding to apply c++ flags to c. --- CMakeLists.txt | 2 +- heap.c | 14 ++++---------- tests/CMakeLists.txt | 3 ++- 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cc3c253..71e236c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,7 +44,7 @@ message(STATUS "Version: ${VERSION_TAG}") add_definitions("-DVERSION=${VERSION_TAG}") set(CMAKE_CXX_STANDARD 17) -add_definitions("-std=c++17") # the above does not work for clang. +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17") # the above does not work for clang. add_definitions("-Wall -Wextra -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE") # Adding frame pointers makes things easy to run performance measurements with, diff --git a/heap.c b/heap.c index 13b256c..b88b299 100644 --- a/heap.c +++ b/heap.c @@ -355,18 +355,12 @@ getframe(void ***bp, void ***ip) { "ret;"); } #elif defined(__aarch64__) -static void __attribute__((noinline,optimize("O0"))) +static void __attribute__((naked,noinline,optimize("O0"))) getframe(void ***bp, void ***ip) { - void *stackp, *framep; asm( - "mov %0, sp;" - "mov %1, x29;" - "str %0, [%2];" - "str %1, [%3]" - : "=r"(stackp), "=r"(framep) - : "r"(ip), "r"(bp) - : "memory" - ); + "mov x0, x29;" + "mov x1, x30;" + "ret;"); } #else void diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 7c7b040..a525fed 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -6,8 +6,9 @@ project(pstack-tests C CXX) set(CMAKE_BUILD_TYPE Debug) set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17") -add_definitions("-std=c++17 -O0 -D_FORTIFY_SOURCE=0 -I${CMAKE_SOURCE_DIR}") +add_definitions("-O0 -D_FORTIFY_SOURCE=0 -I${CMAKE_SOURCE_DIR}") add_library(testhelper STATIC abort.c)