about summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authorrajdakin <rajdakin@gmail.com>2022-02-14 13:13:12 +0100
committerrajdakin <rajdakin@gmail.com>2022-02-14 13:13:12 +0100
commit77925f264fbb60d67fb83e9f2d7d27f5898f7982 (patch)
tree655d59df7b98083499875594603b887fb43e13a5 /tests
parent97857d55a02c41ab3b34f9c811ebd7b82f9a1894 (diff)
downloadbox64-77925f264fbb60d67fb83e9f2d7d27f5898f7982.tar.gz
box64-77925f264fbb60d67fb83e9f2d7d27f5898f7982.zip
Fixed the backtrace wrapper (uses eh_frame information only)
Diffstat (limited to 'tests')
-rw-r--r--tests/ref19.txt5
-rwxr-xr-xtests/test19bin0 -> 24408 bytes
-rw-r--r--tests/test19.c73
3 files changed, 78 insertions, 0 deletions
diff --git a/tests/ref19.txt b/tests/ref19.txt
new file mode 100644
index 00000000..85632000
--- /dev/null
+++ b/tests/ref19.txt
@@ -0,0 +1,5 @@
+backtrace() returned 4 addresses
+:myfunc3
+:main
+???
+:_start
diff --git a/tests/test19 b/tests/test19
new file mode 100755
index 00000000..c3ab3033
--- /dev/null
+++ b/tests/test19
Binary files differdiff --git a/tests/test19.c b/tests/test19.c
new file mode 100644
index 00000000..545f8f99
--- /dev/null
+++ b/tests/test19.c
@@ -0,0 +1,73 @@
+#include <execinfo.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+/// build with `gcc -march=core2 -g -O2 test19.c -o test19 -no-pie -rdynamic`
+
+#define BT_BUF_SIZE 100
+
+void myfunc3()
+{
+  int nptrs;
+  void *buffer[BT_BUF_SIZE];
+  char **strings;
+
+  nptrs = backtrace(buffer, BT_BUF_SIZE);
+  printf("backtrace() returned %d addresses\n", nptrs);
+
+ /* The call backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO)
+    would produce similar output to the following: */
+
+  strings = backtrace_symbols(buffer, nptrs);
+  if (strings == NULL) {
+    perror("backtrace_symbols");
+    exit(EXIT_FAILURE);
+  }
+
+  for (int j = 0; j < nptrs; j++) {
+    // clean-up output so it can be compared
+    char* p = strchr(strings[j], '[');
+    if(p)
+      p[-1] = '\0';
+    p = strchr(strings[j], '(');
+    if(p)
+      *p = ':';
+    p = strchr(p?p:strings[j], '+');
+    if(p)
+      *p = '\0';
+    p = strchr(p?p:strings[j], ')');
+    if(p)
+      *p = '\0';
+    p = strchr(strings[j], ':');
+    if(!p)
+      p = strings[j];
+    printf("%s\n", p);
+  }
+  free(strings);
+}
+
+static void   /* "static" means don't export the symbol... */
+myfunc2(void)
+{
+  myfunc3();
+}
+
+void myfunc(int ncalls)
+{
+  if (ncalls > 1)
+    myfunc(ncalls - 1);
+  else
+    myfunc2();
+}
+
+int main(int argc, char *argv[])
+{
+  int ncall = 4;
+  if (argc == 2) {
+    ncall = atoi(argv[1]);
+  }
+
+  myfunc(ncall);
+  exit(EXIT_SUCCESS);
+}