Change in osmo-trx[master]: debug.h: Avoid printing pthread_t type

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

pespin gerrit-no-reply at lists.osmocom.org
Wed Feb 19 17:09:01 UTC 2020


pespin has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-trx/+/17226 )


Change subject: debug.h: Avoid printing pthread_t type
......................................................................

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
---
M CommonLibs/debug.c
M CommonLibs/debug.h
M configure.ac
3 files changed, 37 insertions(+), 4 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-trx refs/changes/26/17226/1

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 @@
 	.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
+}
diff --git a/CommonLibs/debug.h b/CommonLibs/debug.h
index 0dca2ee..9f118b5 100644
--- a/CommonLibs/debug.h
+++ b/CommonLibs/debug.h
@@ -1,7 +1,7 @@
 #pragma once
 
 #include <stdbool.h>
-#include <pthread.h>
+#include <sys/types.h>
 
 #include <osmocom/core/logging.h>
 
@@ -18,10 +18,12 @@
 	DDEVDRV,
 };
 
+pid_t my_gettid(void);
+
 #define CLOGC(category, level, fmt, args...) do { \
-	LOGP(category, level, "[tid=%lu] " fmt, pthread_self(), ##args);  \
+	LOGP(category, level, "[tid=%ld] " fmt, (long int) my_gettid(), ##args);  \
 } while(0)
 
 #define CLOGCHAN(chan, category, level, fmt, args...) do { \
-	LOGP(category, level, "[tid=%lu][chan=%lu] " fmt, pthread_self(), chan, ##args);  \
+	LOGP(category, level, "[tid=%ld][chan=%lu] " fmt, (long int) my_gettid(), chan, ##args);  \
 } while(0)
diff --git a/configure.ac b/configure.ac
index b0be728..76c3515 100644
--- a/configure.ac
+++ b/configure.ac
@@ -75,6 +75,15 @@
 AC_HEADER_TIME
 AC_C_BIGENDIAN
 
+# Check if gettid is available (despite not being documented in glibc doc, it requires __USE_GNU on some systems)
+# C compiler is used since __USE_GNU seems to be always defined for g++.
+save_CPPFLAGS=$CPPFLAGS
+AC_LANG_PUSH(C)
+CPPFLAGS="$CPPFLAGS -D_GNU_SOURCE"
+AC_CHECK_FUNCS([gettid])
+AC_LANG_POP(C)
+CPPFLAGS=$save_CPPFLAGS
+
 PKG_CHECK_MODULES(LIBOSMOCORE, libosmocore >= 1.3.0)
 PKG_CHECK_MODULES(LIBOSMOVTY, libosmovty >= 1.3.0)
 PKG_CHECK_MODULES(LIBOSMOCTRL, libosmoctrl >= 1.3.0)

-- 
To view, visit https://gerrit.osmocom.org/c/osmo-trx/+/17226
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-trx
Gerrit-Branch: master
Gerrit-Change-Id: I40265fd4c62e550014ba3ff3335ca053c5bc01f2
Gerrit-Change-Number: 17226
Gerrit-PatchSet: 1
Gerrit-Owner: pespin <pespin at sysmocom.de>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20200219/5c0446c6/attachment.htm>


More information about the gerrit-log mailing list