From: "Marc-André Lureau" <marcandre.lureau@redhat.com>
To: Bin Meng <bmeng.cn@gmail.com>
Cc: qemu-devel@nongnu.org, bin.meng@windriver.com,
richard.henderson@linaro.org, Stefan Weil <sw@weilnetz.de>
Subject: Re: [PATCH v4] win32: set threads name
Date: Tue, 11 Oct 2022 15:59:20 +0400 [thread overview]
Message-ID: <CAMxuvayCds1Mzf64JCSawkZADcBrMw4z=2KD8MnQzu-+iq5guQ@mail.gmail.com> (raw)
In-Reply-To: <CAEUhbmWNkLbt7ik2REQ4w+2FRCAr9NwFm+aGw85LHAzbQ9-n6Q@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 3942 bytes --]
Hi
On Tue, Oct 11, 2022 at 2:33 PM Bin Meng <bmeng.cn@gmail.com> wrote:
> On Tue, Oct 11, 2022 at 5:29 PM <marcandre.lureau@redhat.com> wrote:
> >
> > From: Marc-André 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=vs-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é Lureau <marcandre.lureau@redhat.com>
> > Acked-by: Richard Henderson <richard.henderson@linaro.org>
> > ---
> > 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 = 0;
> > +
> > + if (g_once_init_enter(&_init_once)) {
> > + kernel32_module = LoadLibrary("kernel32.dll");
> > + if (kernel32_module) {
> > + SetThreadDescriptionFunc =
> > + (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 = 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 = 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 = NULL;
> > +
> > + if (!load_set_thread_description()) {
> > + return false;
> > + }
> > +
> > + namew = g_utf8_to_utf16(name, -1, NULL, NULL, NULL);
> > + if (!namew) {
> > + return false;
> > + }
> > +
> > + hr = 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 = data;
> > }
> >
>
> Regards,
> Bin
>
>
[-- Attachment #2: Type: text/html, Size: 5717 bytes --]
prev parent reply other threads:[~2022-10-11 13:15 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-11 9:08 [PATCH v4] win32: set threads name marcandre.lureau
2022-10-11 10:33 ` Bin Meng
2022-10-11 11:59 ` Marc-André Lureau [this message]
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAMxuvayCds1Mzf64JCSawkZADcBrMw4z=2KD8MnQzu-+iq5guQ@mail.gmail.com' \
--to=marcandre.lureau@redhat.com \
--cc=bin.meng@windriver.com \
--cc=bmeng.cn@gmail.com \
--cc=qemu-devel@nongnu.org \
--cc=richard.henderson@linaro.org \
--cc=sw@weilnetz.de \
/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.
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 NNTP newsgroup(s).