From patchwork Tue Dec 10 21:47:17 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 36695 Received: (qmail 75473 invoked by alias); 10 Dec 2019 21:47:22 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 75452 invoked by uid 89); 10 Dec 2019 21:47:22 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-22.1 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=adapter, rebuilding, discrepancies X-HELO: rock.gnat.com Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 10 Dec 2019 21:47:20 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 071AB560C8; Tue, 10 Dec 2019 16:47:19 -0500 (EST) Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 3ufN3rYARFeV; Tue, 10 Dec 2019 16:47:18 -0500 (EST) Received: from murgatroyd.Home (75-166-123-50.hlrn.qwest.net [75.166.123.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by rock.gnat.com (Postfix) with ESMTPSA id B88D6560B8; Tue, 10 Dec 2019 16:47:18 -0500 (EST) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Fix build on macOS Date: Tue, 10 Dec 2019 14:47:17 -0700 Message-Id: <20191210214717.25680-1-tromey@adacore.com> MIME-Version: 1.0 PR build/25268 points out that the build fails on macOS, because on macOS the "pthread_setname_np" function takes a single argument. This patch fixes the problem, by introducing a new template adapter function that handles both styles of pthread_setname_np. This is a technique I learned from Alexandre Oliva, and avoids the need for a complicated autoconf check. This change also meant moving the pthread_setname_np call to the thread function, because macOS only permits setting the name of the current thread. This means that there can be a brief window when gdb will see the wrong name; but I think this is a minor concern. Tested by rebuilding on x86-64 Fedora 30, and on macOS High Sierra. On Linux I also debugged gdb to ensure that the thread names are still set correctly. gdb/ChangeLog 2019-12-10 Tom Tromey PR build/25268: * gdbsupport/thread-pool.c (set_thread_name): New template function. (thread_pool::set_thread_count): Don't call pthread_setname_np. (thread_pool::thread_function): Call set_thread_name. Change-Id: Id7bf28d99ca27a893a9fc87ebb90b15a9c2a9cb4 --- gdb/ChangeLog | 8 ++++++++ gdb/gdbsupport/thread-pool.c | 31 +++++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/gdb/gdbsupport/thread-pool.c b/gdb/gdbsupport/thread-pool.c index d19ae02e3ef..8e24570ee96 100644 --- a/gdb/gdbsupport/thread-pool.c +++ b/gdb/gdbsupport/thread-pool.c @@ -36,8 +36,28 @@ #endif #ifdef USE_PTHREAD_SETNAME_NP + #include -#endif + +/* Handle platform discrepancies in pthread_setname_np: macOS uses a + single-argument form, while Linux uses a two-argument form. This + wrapper template handles the difference. */ + +template +void +set_thread_name (R (*set_name) (A1, A2), const char *name) +{ + set_name (pthread_self (), name); +} + +template +void +set_thread_name (R (*set_name) (A1), const char *name) +{ + set_name (name); +} + +#endif /* USE_PTHREAD_SETNAME_NP */ namespace gdb { @@ -75,9 +95,6 @@ thread_pool::set_thread_count (size_t num_threads) for (size_t i = m_thread_count; i < num_threads; ++i) { std::thread thread (&thread_pool::thread_function, this); -#ifdef USE_PTHREAD_SETNAME_NP - pthread_setname_np (thread.native_handle (), "gdb worker"); -#endif thread.detach (); } } @@ -115,6 +132,12 @@ thread_pool::post_task (std::function func) void thread_pool::thread_function () { +#ifdef USE_PTHREAD_SETNAME_NP + /* This must be done here, because on macOS one can only set the + name of the current thread. */ + set_thread_name (pthread_setname_np, "gdb worker"); +#endif + /* Ensure that SIGSEGV is delivered to an alternate signal stack. */ gdb::alternate_signal_stack signal_stack;