From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7C50DC433FE for ; Tue, 11 Oct 2022 13:15:32 +0000 (UTC) Received: from localhost ([::1]:34684 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oiF6Z-0004u5-I0 for qemu-devel@archiver.kernel.org; Tue, 11 Oct 2022 09:15:31 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:60322) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oiDvE-0003ga-JP for qemu-devel@nongnu.org; Tue, 11 Oct 2022 07:59:45 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:58674) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oiDv8-0001kK-HN for qemu-devel@nongnu.org; Tue, 11 Oct 2022 07:59:44 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1665489574; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=JSnNY3G0vdAXeoyDXzDef85QUislhBw0H0GwiRkSw8E=; b=Bi5Lscl9PfxjpoZ6odf8d63958fkXDR99oMKH+s2BRg0zUIaquikcP/pcUm6PznrjSCaJj W7NcJ/OEoDcLQoqevIYo3mNGV+5uAbHHJoOnkprwWEGhJVuFMhE3rCHZtcJLuoGu5V4RWF IUwURhs7TPGH0DADyTcWLG89Y5DYjmA= Received: from mail-oo1-f69.google.com (mail-oo1-f69.google.com [209.85.161.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-665-QBJS-nAqPLumpMtRoIaR0A-1; Tue, 11 Oct 2022 07:59:32 -0400 X-MC-Unique: QBJS-nAqPLumpMtRoIaR0A-1 Received: by mail-oo1-f69.google.com with SMTP id z3-20020a4a6543000000b004805f23e3b1so4136807oog.12 for ; Tue, 11 Oct 2022 04:59:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=JSnNY3G0vdAXeoyDXzDef85QUislhBw0H0GwiRkSw8E=; b=axX18oFerk0p03r3DHOMA1lrLrD49Hrb0kwvsKeyigMrSlXGou2t0OHPO053I8qQ2Y tS50R/MNTjQ8riDGPnGOmccMWmyqdcGpa3O6NQtR3uDW3HiuF9OJ7iYwP9ccgsueq8OG Y8qcsPLm0X+ijAjTHWRyvuxFLDirBlXx/iQilnB4UXy+e7+8ArmaosEglbskpXq4qHE6 WIB0lNtQZiqvXfmx5G7/zYDWNq543r87aWEKyBRcoMCCtC9b0E+pjHr5kDB1hjL7AJUD 59azfeUzJpA5iEVsOE5eMmt8dF5OC5SG3O6aciMt+6lLD8VW1Oe4uD+9lUzAGbjCc20R 6uKA== X-Gm-Message-State: ACrzQf3yMc4WhUbQycKnK6xs2OtECBb9Z8+WiszlAfswZn8tV5mWMzD5 Hx2xxIkoP6T6EflyaaIfeSMKctHp6omT52NYDpAgHAPJVB2x4b5VB7wIOFHH1DxmsHNRoiI55mD 5dmi/lcv4GC/EB3kQvDY3FPLmDy1kUgI= X-Received: by 2002:a05:6830:4a6:b0:661:8587:7000 with SMTP id l6-20020a05683004a600b0066185877000mr8679893otd.378.1665489572265; Tue, 11 Oct 2022 04:59:32 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5P67UcyqiQlqOQkPROf/rXbH0gx49X10G16WzHjo3Ow3jzFFysQHnf9IruVjmNQOFWd881EMVidDkriEg/NsQ= X-Received: by 2002:a05:6830:4a6:b0:661:8587:7000 with SMTP id l6-20020a05683004a600b0066185877000mr8679884otd.378.1665489572061; Tue, 11 Oct 2022 04:59:32 -0700 (PDT) MIME-Version: 1.0 References: <20221011090835.3534934-1-marcandre.lureau@redhat.com> In-Reply-To: From: =?UTF-8?B?TWFyYy1BbmRyw6kgTHVyZWF1?= Date: Tue, 11 Oct 2022 15:59:20 +0400 Message-ID: Subject: Re: [PATCH v4] win32: set threads name To: Bin Meng Cc: qemu-devel@nongnu.org, bin.meng@windriver.com, richard.henderson@linaro.org, Stefan Weil Content-Type: multipart/alternative; boundary="00000000000051f72905eac10230" Received-SPF: pass client-ip=170.10.129.124; envelope-from=mlureau@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" --00000000000051f72905eac10230 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi On Tue, Oct 11, 2022 at 2:33 PM Bin Meng wrote: > On Tue, Oct 11, 2022 at 5:29 PM wrote: > > > > From: Marc-Andr=C3=A9 Lureau > > > > As described in: > > > https://learn.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thre= ad-name-in-native-code?view=3Dvs-2022 > > > > SetThreadDescription() is available since Windows 10, version 1607 and > > in some versions only by "Run Time Dynamic Linking". Its declaration is > > not yet in mingw, so we lookup the function the same way glib does. > > > > Tested with Visual Studio Community 2022 debugger. > > > > Signed-off-by: Marc-Andr=C3=A9 Lureau > > Acked-by: Richard Henderson > > --- > > util/qemu-thread-win32.c | 55 ++++++++++++++++++++++++++++++++++++++-- > > 1 file changed, 53 insertions(+), 2 deletions(-) > > > > diff --git a/util/qemu-thread-win32.c b/util/qemu-thread-win32.c > > index a2d5a6e825..b20bfa9c1f 100644 > > --- a/util/qemu-thread-win32.c > > +++ b/util/qemu-thread-win32.c > > @@ -19,12 +19,39 @@ > > > > static bool name_threads; > > > > +typedef HRESULT (WINAPI *pSetThreadDescription) (HANDLE hThread, > > + PCWSTR > lpThreadDescription); > > +static pSetThreadDescription SetThreadDescriptionFunc; > > +static HMODULE kernel32_module; > > + > > +static bool load_set_thread_description(void) > > +{ > > + static gsize _init_once =3D 0; > > + > > + if (g_once_init_enter(&_init_once)) { > > + kernel32_module =3D LoadLibrary("kernel32.dll"); > > + if (kernel32_module) { > > + SetThreadDescriptionFunc =3D > > + (pSetThreadDescription)GetProcAddress(kernel32_module, > > + > "SetThreadDescription"); > > + if (!SetThreadDescriptionFunc) { > > + FreeLibrary(kernel32_module); > > + } > > + } > > + g_once_init_leave(&_init_once, 1); > > + } > > + > > + return !!SetThreadDescriptionFunc; > > +} > > + > > void qemu_thread_naming(bool enable) > > { > > - /* But note we don't actually name them on Windows yet */ > > name_threads =3D enable; > > > > - fprintf(stderr, "qemu: thread naming not supported on this host\n"= ); > > + if (enable && !load_set_thread_description()) { > > + fprintf(stderr, "qemu: thread naming not supported on this > host\n"); > > + name_threads =3D false; > > + } > > } > > > > static void error_exit(int err, const char *msg) > > @@ -400,6 +427,26 @@ void *qemu_thread_join(QemuThread *thread) > > return ret; > > } > > > > +static bool > > This is still not fixed > > > +set_thread_description(HANDLE h, const char *name) > > +{ > > + HRESULT hr; > > and the 4 spaces here ... > good catch, thanks > > > + g_autofree wchar_t *namew =3D NULL; > > + > > + if (!load_set_thread_description()) { > > + return false; > > + } > > + > > + namew =3D g_utf8_to_utf16(name, -1, NULL, NULL, NULL); > > + if (!namew) { > > + return false; > > + } > > + > > + hr =3D SetThreadDescriptionFunc(h, namew); > > + > > + return SUCCEEDED(hr); > > +} > > + > > void qemu_thread_create(QemuThread *thread, const char *name, > > void *(*start_routine)(void *), > > void *arg, int mode) > > @@ -423,7 +470,11 @@ void qemu_thread_create(QemuThread *thread, const > char *name, > > if (!hThread) { > > error_exit(GetLastError(), __func__); > > } > > + if (name_threads && name && !set_thread_description(hThread, name)= ) > { > > + fprintf(stderr, "qemu: failed to set thread description: %s\n"= , > name); > > + } > > CloseHandle(hThread); > > + > > thread->data =3D data; > > } > > > > Regards, > Bin > > --00000000000051f72905eac10230 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi

On Tue, Oct 11, 2022 at 2:33 PM Bin= Meng <bmeng.cn@gmail.com> = wrote:
On Tue, O= ct 11, 2022 at 5:29 PM <marcandre.lureau@redhat.com> wrote:
>
> From: Marc-Andr=C3=A9 Lureau <marcandre.lureau@redhat.com>
>
> As described in:
> https://learn.microsoft.com/en-us/visualstudio/debugger/how-= to-set-a-thread-name-in-native-code?view=3Dvs-2022
>
> SetThreadDescription() is available since Windows 10, version 1607 and=
> in some versions only by "Run Time Dynamic Linking". Its dec= laration is
> not yet in mingw, so we lookup the function the same way glib does. >
> Tested with Visual Studio Community 2022 debugger.
>
> Signed-off-by: Marc-Andr=C3=A9 Lureau <marcandre.lureau@redhat.com> > Acked-by: Richard Henderson <richard.henderson@linaro.org>
> ---
>=C2=A0 util/qemu-thread-win32.c | 55 ++++++++++++++++++++++++++++++++++= ++++--
>=C2=A0 1 file changed, 53 insertions(+), 2 deletions(-)
>
> diff --git a/util/qemu-thread-win32.c b/util/qemu-thread-win32.c
> index a2d5a6e825..b20bfa9c1f 100644
> --- a/util/qemu-thread-win32.c
> +++ b/util/qemu-thread-win32.c
> @@ -19,12 +19,39 @@
>
>=C2=A0 static bool name_threads;
>
> +typedef HRESULT (WINAPI *pSetThreadDescription) (HANDLE hThread,
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0PCWSTR lpThreadDescription);
> +static pSetThreadDescription SetThreadDescriptionFunc;
> +static HMODULE kernel32_module;
> +
> +static bool load_set_thread_description(void)
> +{
> +=C2=A0 =C2=A0 static gsize _init_once =3D 0;
> +
> +=C2=A0 =C2=A0 if (g_once_init_enter(&_init_once)) {
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 kernel32_module =3D LoadLibrary("ker= nel32.dll");
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (kernel32_module) {
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 SetThreadDescriptionFunc = =3D
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (pSetThreadDe= scription)GetProcAddress(kernel32_module,
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "SetThreadDescription&qu= ot;);
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (!SetThreadDescriptionFu= nc) {
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 FreeLibrary(k= ernel32_module);
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 }
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 }
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 g_once_init_leave(&_init_once, 1); > +=C2=A0 =C2=A0 }
> +
> +=C2=A0 =C2=A0 return !!SetThreadDescriptionFunc;
> +}
> +
>=C2=A0 void qemu_thread_naming(bool enable)
>=C2=A0 {
> -=C2=A0 =C2=A0 /* But note we don't actually name them on Windows = yet */
>=C2=A0 =C2=A0 =C2=A0 name_threads =3D enable;
>
> -=C2=A0 =C2=A0 fprintf(stderr, "qemu: thread naming not supported= on this host\n");
> +=C2=A0 =C2=A0 if (enable && !load_set_thread_description()) {=
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 fprintf(stderr, "qemu: thread naming= not supported on this host\n");
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 name_threads =3D false;
> +=C2=A0 =C2=A0 }
>=C2=A0 }
>
>=C2=A0 static void error_exit(int err, const char *msg)
> @@ -400,6 +427,26 @@ void *qemu_thread_join(QemuThread *thread)
>=C2=A0 =C2=A0 =C2=A0 return ret;
>=C2=A0 }
>
> +static bool

This is still not fixed

> +set_thread_description(HANDLE h, const char *name)
> +{
> +=C2=A0 HRESULT hr;

and the 4 spaces here ...

good catch, t= hanks

> +=C2=A0 g_autofree wchar_t *namew =3D NULL;
> +
> +=C2=A0 if (!load_set_thread_description()) {
> +=C2=A0 =C2=A0 =C2=A0 return false;
> +=C2=A0 }
> +
> +=C2=A0 namew =3D g_utf8_to_utf16(name, -1, NULL, NULL, NULL);
> +=C2=A0 if (!namew) {
> +=C2=A0 =C2=A0 =C2=A0 return false;
> +=C2=A0 }
> +
> +=C2=A0 hr =3D SetThreadDescriptionFunc(h, namew);
> +
> +=C2=A0 return SUCCEEDED(hr);
> +}
> +
>=C2=A0 void qemu_thread_create(QemuThread *thread, const char *name, >=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0void *(*start_routine)(void *),
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0void *arg, int mode)
> @@ -423,7 +470,11 @@ void qemu_thread_create(QemuThread *thread, const= char *name,
>=C2=A0 =C2=A0 =C2=A0 if (!hThread) {
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 error_exit(GetLastError(), __func__)= ;
>=C2=A0 =C2=A0 =C2=A0 }
> +=C2=A0 =C2=A0 if (name_threads && name && !set_thread= _description(hThread, name)) {
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 fprintf(stderr, "qemu: failed to set= thread description: %s\n", name);
> +=C2=A0 =C2=A0 }
>=C2=A0 =C2=A0 =C2=A0 CloseHandle(hThread);
> +
>=C2=A0 =C2=A0 =C2=A0 thread->data =3D data;
>=C2=A0 }
>

Regards,
Bin

--00000000000051f72905eac10230--