From patchwork Sat Jul 23 09:25:15 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: LRN X-Patchwork-Id: 13941 Received: (qmail 104248 invoked by alias); 23 Jul 2016 09:25:41 -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 104233 invoked by uid 89); 23 Jul 2016 09:25:40 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: =?ISO-8859-1?Q?No, score=0.8 required=5.0 tests=AWL, BAYES_00, BODY_8BITS, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GARBLED_BODY, KAM_LOTSOFHASH, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=no version=3.3.2 spammy=browse, =d0=be=d0=b2, ribbon, =d0=bb=d0=b0=d1?= X-HELO: mail-lf0-f65.google.com Received: from mail-lf0-f65.google.com (HELO mail-lf0-f65.google.com) (209.85.215.65) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Sat, 23 Jul 2016 09:25:30 +0000 Received: by mail-lf0-f65.google.com with SMTP id l69so8672659lfg.1 for ; Sat, 23 Jul 2016 02:25:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:to:from:subject:message-id :disposition-notification-to:date:user-agent:mime-version; bh=07s29K0gSoB18nZS7YOv4i+PDt6rpaqTfPZgsG56T/w=; b=H9iKatq665xY2+TqHExIHQfnqfEbjZ1fT89vmFIGTWzRnP6D9aY/hP4XyPe8eKomQO R9NNc+4LN4d0++Gnn2Stsa1mmGTYfug2et3Kqcgld4LszYlEPRQTKeUtD7+hkDRR3hIW TTT+WVo1Ynf3yXv1jcvGolINpTmJ59/esO+dG538DWvymMZQypu1rhu2DBQRAM2+rjyg clcEv4YKCFgxUS3ELV6TuNcv+oQ3Rwoo8NAkdX4o93KjiV16AvkJlDfsTUdWlf75j6PD IN4GsUVJZxPv2mw0hGeLcU7ND5vD6mmAHYwmMhNBNpp54Njm7DZ7xcXsENZ2V7/t2Ff6 DELA== X-Gm-Message-State: AEkoouuxvXm8Lf+EtxD1PxsytlQcFGNOTsQZQhECcAgeQoNSM6tOsOU2YcsvYZVZ3uNOmw== X-Received: by 10.25.161.76 with SMTP id k73mr4415236lfe.26.1469265926610; Sat, 23 Jul 2016 02:25:26 -0700 (PDT) Received: from [192.168.4.39] (broadband-95-84-200-6.nationalcablenetworks.ru. [95.84.200.6]) by smtp.gmail.com with ESMTPSA id 78sm3699028lfv.39.2016.07.23.02.25.25 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 23 Jul 2016 02:25:25 -0700 (PDT) To: gdb-patches@sourceware.org From: LRN Subject: Program-assigned thread names on Windows Message-ID: <5052d495-ea40-b364-96ea-9e68c90bd747@gmail.com> Date: Sat, 23 Jul 2016 12:25:15 +0300 User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Thunderbird/50.0a1 MIME-Version: 1.0 X-IsSubscribed: yes The attached patch adds thread naming support on Windows. This works as documented[1] on MSDN - by catching a specific exception that the program throws. Setting thread name this way is supported by glib[2] and winpthreads[3] at least, as well as any program developed with MS toolchain (because WinDbg supported this for a long time). [1] https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx [2] https://git.gnome.org/browse/glib/commit/glib/gthread-win32.c?id=e118856430a798bbc529691ad235fd0b0684439d [3] https://sourceforge.net/p/mingw-w64/mingw-w64/ci/0d95c795b44b76e1b60dfc119fd93cfd0cb35816/ diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c index 3f67486..01e7954 100644 --- a/gdb/windows-nat.c +++ b/gdb/windows-nat.c @@ -174,6 +174,9 @@ static int debug_registers_used; static int windows_initialization_done; #define DR6_CLEAR_VALUE 0xffff0ff0 +#define WINDOWS_THREADNAME_EXCEPTION 0x406D1388 +#define WINDOWS_THREADNAME_EXCEPTION_S "0x406D1388" + /* The string sent by cygwin when it processes a signal. FIXME: This should be in a cygwin include file. */ #ifndef _CYGWIN_SIGNAL_STRING @@ -1035,6 +1038,7 @@ static int handle_exception (struct target_waitstatus *ourstatus) { DWORD code = current_event.u.Exception.ExceptionRecord.ExceptionCode; + int result = 1; ourstatus->kind = TARGET_WAITKIND_STOPPED; @@ -1140,6 +1144,38 @@ handle_exception (struct target_waitstatus *ourstatus) DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_NONCONTINUABLE_EXCEPTION"); ourstatus->value.sig = GDB_SIGNAL_ILL; break; + case WINDOWS_THREADNAME_EXCEPTION: + DEBUG_EXCEPTION_SIMPLE (WINDOWS_THREADNAME_EXCEPTION_S); + ourstatus->value.sig = GDB_SIGNAL_TRAP; + if (current_event.u.Exception.ExceptionRecord.NumberParameters == 4) + { + DWORD named_thread_id; + ptid_t named_thread_ptid; + struct thread_info *named_thread; + uintptr_t thread_name_target; + char *thread_name; + + named_thread_id = (DWORD) current_event.u.Exception.ExceptionRecord.ExceptionInformation[2]; + thread_name_target = (uintptr_t) current_event.u.Exception.ExceptionRecord.ExceptionInformation[1]; + + if (named_thread_id == (DWORD) -1) + named_thread_id = current_event.dwThreadId; + + named_thread_ptid = ptid_build (current_event.dwProcessId, 0, named_thread_id), + named_thread = find_thread_ptid (named_thread_ptid); + + thread_name = NULL; + if (!target_read_string ((CORE_ADDR) thread_name_target, &thread_name, 1024, 0) + || !thread_name || !*thread_name) + /* nothing to do */; + else + { + xfree (named_thread->name); + named_thread->name = thread_name; + } + result = 2; + } + break; default: /* Treat unhandled first chance exceptions specially. */ if (current_event.u.Exception.dwFirstChance) @@ -1153,7 +1189,7 @@ handle_exception (struct target_waitstatus *ourstatus) } exception_count++; last_sig = ourstatus->value.sig; - return 1; + return result; } /* Resume thread specified by ID, or all artificially suspended @@ -1510,10 +1546,19 @@ get_windows_debug_event (struct target_ops *ops, "EXCEPTION_DEBUG_EVENT")); if (saw_create != 1) break; - if (handle_exception (ourstatus)) - thread_id = current_event.dwThreadId; - else - continue_status = DBG_EXCEPTION_NOT_HANDLED; + switch (handle_exception (ourstatus)) + { + case 0: + default: + continue_status = DBG_EXCEPTION_NOT_HANDLED; + break; + case 1: + thread_id = current_event.dwThreadId; + break; + case 2: + continue_status = DBG_CONTINUE; + break; + } break; case OUTPUT_DEBUG_STRING_EVENT: /* Message from the kernel. */