aboutsummaryrefslogtreecommitdiffstats
path: root/CommonLibs/debug.c
diff options
context:
space:
mode:
authorPau Espin Pedrol <pespin@sysmocom.de>2020-02-19 16:31:57 +0100
committerPau Espin Pedrol <pespin@sysmocom.de>2020-02-19 18:08:20 +0100
commit7a07de1efd4eb7cc11c33d3ad25cb2df70aa1ef1 (patch)
treeb18342affc780b9385cb8a9ef2f1e008c832241c /CommonLibs/debug.c
parentfd67262df85c367d44547131e129997b90aa8320 (diff)
debug.h: Avoid printing pthread_t type
Using %lu for pthread_t was wrong on armv7l arch. Even worse, according to pthread_self() man page, pthread_t cannot be assumed to be of a simple type and hence printable: """ POSIX.1 allows an implementation wide freedom in choosing the type used to represent a thread ID; for example, representation using either an arithmetic type or a structure is permitted. """ Let's use gettid() instead. According to glibc documentation: """ The pid_t data type is a signed integer type which is capable of representing a process ID. In the GNU C Library, this is an int. """ It may not be the same on other libc's though, so let's better cast to a long int just in case. Accordign to gettid() man, the libc function was only added recently during glibc 2.30, however the system call has been around for quite some time (linux 2.4.11). Let's accomodate use udner non-glibc or older versions of it by having a direct syscall fallback. Change-Id: I40265fd4c62e550014ba3ff3335ca053c5bc01f2
Diffstat (limited to 'CommonLibs/debug.c')
-rw-r--r--CommonLibs/debug.c24
1 files changed, 23 insertions, 1 deletions
diff --git a/CommonLibs/debug.c b/CommonLibs/debug.c
index c227435..5e09079 100644
--- a/CommonLibs/debug.c
+++ b/CommonLibs/debug.c
@@ -21,7 +21,17 @@
* See the COPYING file in the main directory for details.
*/
-#include <pthread.h>
+#include "config.h"
+
+/* If HAVE_GETTID, then "_GNU_SOURCE" may need to be defined to use gettid() */
+#if HAVE_GETTID
+#define _GNU_SOURCE
+#endif
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <sys/syscall.h>
+#include "config.h"
#include <osmocom/core/logging.h>
#include <osmocom/core/utils.h>
@@ -77,3 +87,15 @@ const struct log_info log_info = {
.cat = default_categories,
.num_cat = ARRAY_SIZE(default_categories),
};
+
+pid_t my_gettid(void)
+{
+#if HAVE_GETTID
+ return gettid();
+#elif defined(LINUX) && defined(__NR_gettid)
+ return (pid_t) syscall(__NR_gettid);
+#else
+ #pragma message ("use pid as tid")
+ return getpid();
+#endif
+}