[RFC,v2,26/27] Add Infinity notes implementing libpthread::thr_get_lwpid

Message ID 1465814311-31470-27-git-send-email-gbenson@redhat.com
State New, archived
Headers

Commit Message

Gary Benson June 13, 2016, 10:38 a.m. UTC
  This commit adds the Infinity function libpthread::thr_get_lwpid.
It has no libthread_db equivalent (in libthread_db the LWPID of
a thread is read from the ti_lid field of the td_thrinfo_t that
td_thr_get_info fills in).
---
 nptl/Makefile                      |    3 +-
 nptl/infinity-thr_get_lwpid.i8     |   38 ++++++++++++++++++++++++++++++++++++
 nptl/tst-infinity-thr_get_lwpid.py |   37 +++++++++++++++++++++++++++++++++++
 3 files changed, 77 insertions(+), 1 deletions(-)
 create mode 100644 nptl/infinity-thr_get_lwpid.i8
 create mode 100644 nptl/tst-infinity-thr_get_lwpid.py
  

Patch

diff --git a/nptl/Makefile b/nptl/Makefile
index 6967940..bdfe656 100644
--- a/nptl/Makefile
+++ b/nptl/Makefile
@@ -139,7 +139,8 @@  libpthread-routines = nptl-init vars events version pt-interp \
 #		      pthread_setresgid
 
 ifeq ($(build-infinity),yes)
-infinity-routines = infinity-map_lwp2thr infinity-thr_iter
+infinity-routines = infinity-map_lwp2thr infinity-thr_iter \
+		    infinity-thr_get_lwpid
 
 libpthread-routines += $(infinity-routines)
 endif
diff --git a/nptl/infinity-thr_get_lwpid.i8 b/nptl/infinity-thr_get_lwpid.i8
new file mode 100644
index 0000000..4755de7
--- /dev/null
+++ b/nptl/infinity-thr_get_lwpid.i8
@@ -0,0 +1,38 @@ 
+/* Get LWPID of a thread.
+   Copyright (C) 2016 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#include "infinity-nptl.i8"
+
+/* Return the LWPID of this thread.  */
+
+define MODULE_NAME::thr_get_lwpid returns td_err_e, lwpid_t
+	argument pthread_t descr
+
+	dup
+	beq NULL, fake_descriptor
+
+real_descriptor:
+	add PTHREAD_TID_OFFSET
+	deref pid_t
+	load TD_OK
+	return
+
+fake_descriptor:
+	call procservice::getpid
+	load TD_OK
+	return
diff --git a/nptl/tst-infinity-thr_get_lwpid.py b/nptl/tst-infinity-thr_get_lwpid.py
new file mode 100644
index 0000000..150b6d1
--- /dev/null
+++ b/nptl/tst-infinity-thr_get_lwpid.py
@@ -0,0 +1,37 @@ 
+from i8c.runtime import TestCase
+
+TestCase.import_builtin_constants()
+TestCase.import_constants_from("infinity-nptl-constants.h")
+TestCase.import_constants_from("infinity-nptl_db-constants.h")
+
+class TestThrGetLWPID(TestCase):
+    TESTFUNC = "libpthread::thr_get_lwpid(p)ii"
+    MAIN_PID = 12345
+
+    def setUp(self):
+        self.ps_getpid_called = False
+
+    def call_procservice_getpid(self):
+        """Implementation of procservice::getpid."""
+        self.ps_getpid_called = True
+        return self.MAIN_PID
+
+    def __do_test(self, descr, expect_lwpid):
+        result = self.i8ctx.call(self.TESTFUNC, descr)
+        self.assertEqual(len(result), 2)
+        self.assertEqual(result[0], TD_OK)
+        self.assertEqual(result[1], expect_lwpid)
+
+    def test_faked(self):
+        """Test thr_get_lwpid with a faked descriptor"""
+        self.__do_test(NULL, self.MAIN_PID)
+        self.assertTrue(self.ps_getpid_called)
+
+    def test_real(self):
+        """Test thr_get_lwpid with a real descriptor"""
+        lwpid = self.MAIN_PID - 5
+        with self.memory.builder() as mem:
+            descr = mem.alloc()
+            descr.store_i32(PTHREAD_TID_OFFSET, lwpid)
+        self.__do_test(descr, lwpid)
+        self.assertFalse(self.ps_getpid_called)