summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEmmanuel Dreyfus <manu@netbsd.org>2013-11-17 15:05:42 +0100
committerAnand Avati <avati@redhat.com>2013-11-19 13:30:39 -0800
commit5d2439657f7f81f1dcdc45e960c91ce4196ae118 (patch)
treeb1193802a60de983348cac503ec5a5bade70a187
parentbf6f49a309f1894073f208b4a97a7baa6ec3e136 (diff)
NetBSD missing backtrace(3) portability fix
Implement backtrace(3) and backtrace_symbols(3) which do not exist in NetBSD While there, remove duplicate #include <stdio.h> BUG: 764655 Change-Id: Iccd695765906e085c3f8fcb670506d4fea68fa39 Signed-off-by: Emmanuel Dreyfus <manu@netbsd.org> Reviewed-on: http://review.gluster.org/6285 Tested-by: Gluster Build System <jenkins@build.gluster.com> Reviewed-by: Anand Avati <avati@redhat.com>
-rw-r--r--libglusterfs/src/common-utils.c55
-rw-r--r--libglusterfs/src/common-utils.h4
-rw-r--r--xlators/features/protect/src/prot_client.c2
3 files changed, 60 insertions, 1 deletions
diff --git a/libglusterfs/src/common-utils.c b/libglusterfs/src/common-utils.c
index 8274752822d..2b700af4e11 100644
--- a/libglusterfs/src/common-utils.c
+++ b/libglusterfs/src/common-utils.c
@@ -31,7 +31,7 @@
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
-#include <stdlib.h>
+#include <assert.h>
#if defined GF_BSD_HOST_OS || defined GF_DARWIN_HOST_OS
#include <sys/sysctl.h>
@@ -2906,3 +2906,56 @@ gf_thread_create (pthread_t *thread, const pthread_attr_t *attr,
return ret;
}
+
+#ifdef __NetBSD__
+#ifdef __MACHINE_STACK_GROWS_UP
+#define BELOW >
+#else
+#define BELOW <
+#endif
+
+struct frameinfo {
+ struct frameinfo *next;
+ void *return_address;
+};
+
+size_t
+backtrace(void **trace, size_t len)
+{
+ const struct frameinfo *frame = __builtin_frame_address(0);
+ void *stack = &stack;
+ size_t i;
+
+ for (i = 0; i < len; i++) {
+ if ((void *)frame BELOW stack)
+ return i;
+ trace[i] = frame->return_address;
+ frame = frame->next;
+ }
+
+ return len;
+}
+
+char **
+backtrace_symbols(void *const *trace, size_t len)
+{
+ static const size_t slen = sizeof("0x123456789abcdef");
+ char **ptr = calloc(len, sizeof(*ptr) + slen);
+ size_t i;
+
+ if (ptr == NULL)
+ return NULL;
+
+ char *str = (void *)(ptr + len);
+ size_t cur = 0, left = len * slen;
+
+ for (i = 0; i < len; i++) {
+ ptr[i] = str + cur;
+ cur += snprintf(str + cur, left - cur, "%p", trace[i]) + 1;
+ assert(cur < left);
+ }
+
+ return ptr;
+}
+#undef BELOW
+#endif /* __NetBSD__ */
diff --git a/libglusterfs/src/common-utils.h b/libglusterfs/src/common-utils.h
index 3c99a4212d0..e762a86eaf4 100644
--- a/libglusterfs/src/common-utils.h
+++ b/libglusterfs/src/common-utils.h
@@ -590,5 +590,9 @@ void md5_wrapper(const unsigned char *data, size_t len, char *md5);
int gf_thread_create (pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg);
+#ifdef __NetBSD__
+size_t backtrace(void **, size_t);
+char **backtrace_symbols(void *const *, size_t);
+#endif
#endif /* _COMMON_UTILS_H */
diff --git a/xlators/features/protect/src/prot_client.c b/xlators/features/protect/src/prot_client.c
index a27216d0aff..d09715067bd 100644
--- a/xlators/features/protect/src/prot_client.c
+++ b/xlators/features/protect/src/prot_client.c
@@ -15,7 +15,9 @@
#include "xlator.h"
#include "defaults.h"
+#ifndef __NetBSD__
#include <execinfo.h>
+#endif
#define NUM_FRAMES 20