unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Robert Pluim <rpluim@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 38632@debbugs.gnu.org, yantar92@gmail.com
Subject: bug#38632: 27.0.50; Emacs process name is changed permanently upon creating a named thread
Date: Thu, 19 Dec 2019 17:42:41 +0100	[thread overview]
Message-ID: <m2zhfo9shq.fsf@gmail.com> (raw)
In-Reply-To: <8336dg2vvn.fsf@gnu.org> (Eli Zaretskii's message of "Thu, 19 Dec 2019 17:11:24 +0200")

[-- Attachment #1: Type: text/plain, Size: 1041 bytes --]

>>>>> On Thu, 19 Dec 2019 17:11:24 +0200, Eli Zaretskii <eliz@gnu.org> said:
    Eli> We need to truncate the names we store in the thread object (and
    Eli> document that).

    Eli> I think I'm going to change my mind on that.  The complication here is
    Eli> that the name should be encoded by ENCODE_SYSTEM before we pass it to
    Eli> pthread_setname_np etc., and if the locale-coding-system is not UTF-8
    Eli> and not single-byte, we don't really know where a character will end
    Eli> after encoding.  And encoding one character at a time sounds too
    Eli> gross.

    Eli> So perhaps we should just truncate the bytes, and leave this to the
    Eli> application to make sure the result makes sense, and also disregard
    Eli> the difference between list-threads and the thread name as the OS and
    Eli> debuggers see it.  Doing that will also avoid the complication of
    Eli> having the thread name return to the caller different from what the
    Eli> caller used.

Hereʼs what it looks like.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Use-pthread_setname_np-to-set-thread-name.patch --]
[-- Type: text/x-patch, Size: 2496 bytes --]

From bbb7e7837fcad3bac10aef69a1b331243c2b1c4c Mon Sep 17 00:00:00 2001
From: Robert Pluim <rpluim@gmail.com>
Date: Thu, 19 Dec 2019 17:33:16 +0100
Subject: [PATCH] Use pthread_setname_np to set thread name

* configure.ac: Remove check for sys/prctl.h and prctl, check for
pthread_setname_np instead.

* systhread.c: Remove sys/prctl.h include.
(sys_thread_create) [HAVE_PTHREAD_SETNAME_NP]: Use pthread_setname_np
to set the name of the newly created thread (Bug#38632).
---
 configure.ac    |  4 ++--
 src/systhread.c | 17 +++++++++++------
 2 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/configure.ac b/configure.ac
index 3b6a2a6d16..34a2654494 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1767,7 +1767,7 @@ AC_DEFUN
   sys/sysinfo.h
   coff.h pty.h
   sys/resource.h
-  sys/utsname.h pwd.h utmp.h util.h sys/prctl.h)
+  sys/utsname.h pwd.h utmp.h util.h)
 
 AC_CACHE_CHECK([for ADDR_NO_RANDOMIZE],
   [emacs_cv_personality_addr_no_randomize],
@@ -4180,7 +4180,7 @@ AC_DEFUN
 sendto recvfrom getsockname getifaddrs freeifaddrs \
 gai_strerror sync \
 getpwent endpwent getgrent endgrent \
-cfmakeraw cfsetspeed __executable_start log2 prctl)
+cfmakeraw cfsetspeed __executable_start log2 pthread_setname_np)
 LIBS=$OLD_LIBS
 
 dnl No need to check for posix_memalign if aligned_alloc works.
diff --git a/src/systhread.c b/src/systhread.c
index 6f4de536fb..55bde837e3 100644
--- a/src/systhread.c
+++ b/src/systhread.c
@@ -98,10 +98,6 @@ sys_thread_yield (void)
 
 #include <sched.h>
 
-#ifdef HAVE_SYS_PRCTL_H
-#include <sys/prctl.h>
-#endif
-
 void
 sys_mutex_init (sys_mutex_t *mutex)
 {
@@ -227,9 +223,18 @@ sys_thread_create (sys_thread_t *thread_ptr, const char *name,
   if (!pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED))
     {
       result = pthread_create (thread_ptr, &attr, func, arg) == 0;
-#if defined (HAVE_SYS_PRCTL_H) && defined (HAVE_PRCTL) && defined (PR_SET_NAME)
+#ifdef HAVE_PTHREAD_SETNAME_NP
       if (result && name != NULL)
-	prctl (PR_SET_NAME, name);
+        {
+          /* We need to truncate here otherwise pthread_setname_np
+             fails to set the name.  TASK_COMM_LEN is what the length
+             is called in the Linux kernel headers (Bug#38632).  */
+#define TASK_COMM_LEN 16
+          char p_name[TASK_COMM_LEN];
+          strncpy (p_name, name, TASK_COMM_LEN - 1);
+          p_name[TASK_COMM_LEN - 1] = '\0';
+          pthread_setname_np (*thread_ptr, p_name);
+        }
 #endif
     }
 
-- 
2.23.0


  reply	other threads:[~2019-12-19 16:42 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-16  6:42 bug#38632: 27.0.50; Emacs process name is changed permanently upon creating a named thread Ihor Radchenko
2019-12-17 20:05 ` Eli Zaretskii
2019-12-18  9:05   ` Robert Pluim
2019-12-18 15:53     ` Eli Zaretskii
2019-12-18 17:04       ` Robert Pluim
2019-12-18 17:18         ` Eli Zaretskii
2019-12-18 21:30           ` Robert Pluim
2019-12-19 15:11             ` Eli Zaretskii
2019-12-19 16:42               ` Robert Pluim [this message]
2019-12-19 18:56                 ` Eli Zaretskii
2019-12-20 19:13               ` Eli Zaretskii
2020-01-06 14:43                 ` Robert Pluim
2020-01-06 16:22                   ` Eli Zaretskii
2020-01-06 19:50 ` Mattias Engdegård
2020-01-06 21:58   ` Mattias Engdegård
2020-01-06 23:06     ` Robert Pluim
2020-01-07 15:45     ` Eli Zaretskii
2020-01-07 16:46       ` Mattias Engdegård
2020-01-07 17:05         ` Eli Zaretskii
2020-01-07 17:07         ` Eli Zaretskii
2020-01-07 17:19           ` Mattias Engdegård
2020-01-08 18:26         ` Glenn Morris
2020-01-08 18:54           ` Eli Zaretskii
2020-01-08 19:34             ` Glenn Morris
2020-01-08 20:01               ` Eli Zaretskii
2020-01-06 22:21   ` Robert Pluim

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2zhfo9shq.fsf@gmail.com \
    --to=rpluim@gmail.com \
    --cc=38632@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=yantar92@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).