Especially when working with core dumps it proved useful to have
charon's log messages prefixed with the thread id instead of the
human readable thread number.
---
 configure.in                             |   12 ++++++++++++
 src/charon/charon.c                      |    5 +++++
 src/libcharon/bus/bus.c                  |    4 ++++
 src/libstrongswan/processing/processor.c |   10 ++++++++++
 src/libstrongswan/threading/thread.c     |   12 ++++++++++++
 src/libstrongswan/threading/thread.h     |    9 +++++++++
 6 files changed, 52 insertions(+), 0 deletions(-)


diff --git a/configure.in b/configure.in
index a0b1f40..45bb318 100644
--- a/configure.in
+++ b/configure.in
@@ -473,6 +473,18 @@ AC_TRY_COMPILE(
 	[AC_MSG_RESULT([no])]
 )
 
+AC_MSG_CHECKING([for gettid])
+AC_TRY_COMPILE(
+	[#define _GNU_SOURCE
+	#include <unistd.h>
+	#include <sys/syscall.h>],
+	[int main(void) {
+		return syscall(SYS_gettid);
+	}],
+	[AC_MSG_RESULT([yes]); AC_DEFINE([HAVE_GETTID])],
+	[AC_MSG_RESULT([no])]
+)
+
 AC_MSG_CHECKING([for gcc atomic operations])
 AC_TRY_RUN(
 [
diff --git a/src/charon/charon.c b/src/charon/charon.c
index 141403b..a7a097b 100644
--- a/src/charon/charon.c
+++ b/src/charon/charon.c
@@ -215,7 +215,12 @@ static void segv_handler(int signal)
 {
 	backtrace_t *backtrace;
 
+#ifdef HAVE_GETTID
+	DBG1(DBG_DMN, "thread %u/%d received %d", thread_current_id(),
+			thread_current_tid(), signal);
+#else	/* #ifdef HAVE_GETTID */
 	DBG1(DBG_DMN, "thread %u received %d", thread_current_id(), signal);
+#endif	/* #ifdef HAVE_GETTID */
 	backtrace = backtrace_create(2);
 	backtrace->log(backtrace, stderr, TRUE);
 	backtrace->destroy(backtrace);
diff --git a/src/libcharon/bus/bus.c b/src/libcharon/bus/bus.c
index bf0ab22..1487a2e 100644
--- a/src/libcharon/bus/bus.c
+++ b/src/libcharon/bus/bus.c
@@ -271,7 +271,11 @@ METHOD(bus_t, vlog, void,
 	log_data_t data;
 
 	data.ike_sa = this->thread_sa->get(this->thread_sa);
+#ifdef HAVE_GETTID
+	data.thread = thread_current_tid();
+#else	/* #ifdef HAVE_GETTID */
 	data.thread = thread_current_id();
+#endif	/* #ifdef HAVE_GETTID */
 	data.group = group;
 	data.level = level;
 	data.format = format;
diff --git a/src/libstrongswan/processing/processor.c b/src/libstrongswan/processing/processor.c
index be33fcd..7c6a823 100644
--- a/src/libstrongswan/processing/processor.c
+++ b/src/libstrongswan/processing/processor.c
@@ -102,7 +102,12 @@ static void restart(private_processor_t *this)
 {
 	thread_t *thread;
 
+#ifdef HAVE_GETTID
+	DBG2(DBG_JOB, "terminated worker thread, ID/TID: %u/%d", thread_current_id(),
+			thread_current_tid());
+#else	/* #ifdef HAVE_GETTID */
 	DBG2(DBG_JOB, "terminated worker thread, ID: %u", thread_current_id());
+#endif	/* #ifdef HAVE_GETTID */
 
 	/* respawn thread if required */
 	this->mutex->lock(this->mutex);
@@ -152,7 +157,12 @@ static void process_jobs(private_processor_t *this)
 	/* worker threads are not cancellable by default */
 	thread_cancelability(FALSE);
 
+#ifdef HAVE_GETTID
+	DBG2(DBG_JOB, "started worker thread, ID/TID: %u/%d", thread_current_id(),
+			thread_current_tid());
+#else	/* #ifdef HAVE_GETTID */
 	DBG2(DBG_JOB, "started worker thread, ID: %u", thread_current_id());
+#endif	/* #ifdef HAVE_GETTID */
 
 	this->mutex->lock(this->mutex);
 	while (this->desired_threads >= this->total_threads)
diff --git a/src/libstrongswan/threading/thread.c b/src/libstrongswan/threading/thread.c
index 5b6f0d2..090a5ef 100644
--- a/src/libstrongswan/threading/thread.c
+++ b/src/libstrongswan/threading/thread.c
@@ -25,6 +25,11 @@
 #include <threading/mutex.h>
 #include <utils/linked_list.h>
 
+#ifdef HAVE_GETTID
+#include <unistd.h>
+#include <sys/syscall.h>
+#endif	/* #ifdef HAVE_GETTID */
+
 #include "thread.h"
 
 typedef struct private_thread_t private_thread_t;
@@ -326,6 +331,13 @@ u_int thread_current_id()
 	return this ? this->id : 0;
 }
 
+#ifdef HAVE_GETTID
+pid_t thread_current_tid()
+{
+	return syscall(SYS_gettid);
+}
+#endif	/* #ifdef HAVE_GETTID */
+
 /**
  * Described in header.
  */
diff --git a/src/libstrongswan/threading/thread.h b/src/libstrongswan/threading/thread.h
index 31b9e1b..cfd3671 100644
--- a/src/libstrongswan/threading/thread.h
+++ b/src/libstrongswan/threading/thread.h
@@ -139,6 +139,15 @@ thread_t *thread_current();
  */
 u_int thread_current_id();
 
+#ifdef HAVE_GETTID
+/**
+ * Get the value a returned by gettid()
+ *
+ * @return				thread's internal id
+ */
+pid_t thread_current_tid(void);
+#endif	/* #ifdef HAVE_GETTID */
+
 /**
  * Push a function onto the current thread's cleanup handler stack.
  * The callback function is called whenever the thread is cancelled, exits or

_______________________________________________
Dev mailing list
[email protected]
https://lists.strongswan.org/mailman/listinfo/dev

Reply via email to