summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2012-07-14 12:30:53 +0200
committerHarald Welte <laforge@gnumonks.org>2012-07-14 12:30:53 +0200
commit45ecd04e541e47f51bc4cfc239c1c59e8d59155e (patch)
treed2e3d7d6ef88e38169765dca8459cc83bf143097
parent2fe684755a3c8181a4dde0f5dcabac9c0c5a809c (diff)
backtrace: Add a variant that uses the libosmocore logging framework
I recently discovered that we can only print backtraces to stdout, which is of course useless in a daemon environment. We'd rather want to use the libosmocore logging framework instead.
-rw-r--r--include/osmocom/core/backtrace.h1
-rw-r--r--src/backtrace.c46
2 files changed, 38 insertions, 9 deletions
diff --git a/include/osmocom/core/backtrace.h b/include/osmocom/core/backtrace.h
index 1ed089ad..a24290c5 100644
--- a/include/osmocom/core/backtrace.h
+++ b/include/osmocom/core/backtrace.h
@@ -2,5 +2,6 @@
#define _OSMO_BACKTRACE_H_
void osmo_generate_backtrace(void);
+void osmo_log_backtrace(int subsys, int level);
#endif
diff --git a/src/backtrace.c b/src/backtrace.c
index 023671c2..f0c3489a 100644
--- a/src/backtrace.c
+++ b/src/backtrace.c
@@ -29,36 +29,64 @@
#include <stdio.h>
#include <stdlib.h>
#include <osmocom/core/utils.h>
+#include <osmocom/core/logging.h>
#include "config.h"
#ifdef HAVE_EXECINFO_H
#include <execinfo.h>
-/*! \brief Generate and print a call back-trace
- *
- * This function will generate a function call back-trace of the
- * current process and print it to stdout
- */
-void osmo_generate_backtrace(void)
+static void _osmo_backtrace(int use_printf, int subsys, int level)
{
int i, nptrs;
void *buffer[100];
char **strings;
nptrs = backtrace(buffer, ARRAY_SIZE(buffer));
- printf("backtrace() returned %d addresses\n", nptrs);
+ if (use_printf)
+ printf("backtrace() returned %d addresses\n", nptrs);
+ else
+ LOGP(subsys, level, "backtrace() returned %d addresses\n",
+ nptrs);
strings = backtrace_symbols(buffer, nptrs);
if (!strings)
return;
- for (i = 1; i < nptrs; i++)
- printf("%s\n", strings[i]);
+ for (i = 1; i < nptrs; i++) {
+ if (use_printf)
+ printf("%s\n", strings[i]);
+ else
+ LOGP(subsys, level, "\t%s\n", strings[i]);
+ }
free(strings);
}
+
+/*! \brief Generate and print a call back-trace
+ *
+ * This function will generate a function call back-trace of the
+ * current process and print it to stdout. */
+void osmo_generate_backtrace(void)
+{
+ _osmo_backtrace(1, 0, 0);
+}
+
+/*! \brief Generate and log a call back-trace
+ *
+ * This function will generate a function call back-trace of the
+ * current process and log it to the specified subsystem and
+ * level using the libosmocore logging subsystem */
+void osmo_log_backtrace(int subsys, int level)
+{
+ _osmo_backtrace(0, subsys, level);
+}
#else
void osmo_generate_backtrace(void)
{
+ fprintf(stderr, "This platform has no backtrace function\n");
+}
+void osmo_log_backtrace(int subsys, int level)
+{
+ LOGP(subsys, level, "This platform has no backtrace function\n");
}
#endif