From patchwork Sun Apr 9 06:07:23 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Buettner X-Patchwork-Id: 19923 Received: (qmail 90741 invoked by alias); 9 Apr 2017 06:07:30 -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 90672 invoked by uid 89); 9 Apr 2017 06:07:28 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=buggdbgnuorg, bug-gdb@gnu.org, U*bug-gdb X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 09 Apr 2017 06:07:24 +0000 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 36A75C04B30E for ; Sun, 9 Apr 2017 06:07:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 36A75C04B30E Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=kevinb@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 36A75C04B30E Received: from pinnacle.lan (ovpn-116-88.phx2.redhat.com [10.3.116.88]) by smtp.corp.redhat.com (Postfix) with ESMTP id EBAF37B551 for ; Sun, 9 Apr 2017 06:07:24 +0000 (UTC) Date: Sat, 8 Apr 2017 23:07:23 -0700 From: Kevin Buettner To: gdb-patches@sourceware.org Subject: [PATCH v2 4/7] Test case for gdb.thread_from_thread_handle Message-ID: <20170408230723.17aa99b8@pinnacle.lan> In-Reply-To: <20170408224959.67164a27@pinnacle.lan> References: <20170408224959.67164a27@pinnacle.lan> MIME-Version: 1.0 X-IsSubscribed: yes As the title says, this is a test case for gdb.thread_from_thread_handle, a python function which will, given a thread library dependent thread handle, find the GDB thread which corresponds to that thread handle. The C file for this test case causes the thread handles for the main thread and two child threads to be placed into an array. The test case runs to one of the functions (do_something()) at which point, it retrieves the thread handles from the array and attempts to find the correponding thread in GDB's internal thread list. I use a barrier to make sure that both threads have actually started; execution will stop when one of the threads breaks at do_something. The one concern I have about what I've written is with the last three invocations of gdb_test. I don't know that we can be certain that thrs[1] will always map to GDB thread 2 and that thrs[2] will map to GDB thread 3. It seems likely, but some perverse pthreads implementation could change the order in which newly created threads are actually started. If anyone thinks this is a problem, I can tweak it so that the test case simply verifies that reasonable output is produced and another test can verify that the two child thread numbers are actually different. gdb/testsuite/ChangeLog: * gdb.python/py-thrhandle.c, gdb.python/py-thrhandle.exp: New files. --- gdb/testsuite/gdb.python/py-thrhandle.c | 76 +++++++++++++++++++++++++++++++ gdb/testsuite/gdb.python/py-thrhandle.exp | 52 +++++++++++++++++++++ 2 files changed, 128 insertions(+) diff --git a/gdb/testsuite/gdb.python/py-thrhandle.c b/gdb/testsuite/gdb.python/py-thrhandle.c new file mode 100644 index 0000000..93d7dee --- /dev/null +++ b/gdb/testsuite/gdb.python/py-thrhandle.c @@ -0,0 +1,76 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2016 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include +#include +#include +#include +#include + +#define NTHR 3 +pthread_t thrs[NTHR+2]; +pthread_barrier_t barrier; +pthread_mutex_t mutex; + +void +do_something (int n) +{ + pthread_mutex_lock (&mutex); + printf ("%d\n", n); + pthread_mutex_unlock (&mutex); +} + +void * +do_work (void *data) +{ + int num = * (int *) data; + + pthread_barrier_wait (&barrier); + + do_something (num); + + pthread_exit (NULL); +} + +int +main (int argc, char **argv) +{ + int i; + + pthread_barrier_init (&barrier, NULL, NTHR-1); + pthread_mutex_init (&mutex, NULL); + + thrs[0] = pthread_self (); + + for (i=1; i< NTHR; i++) + { + int *iptr = alloca (sizeof (int)); + + *iptr = i; + pthread_create (&thrs[i], NULL, do_work, iptr); + } + + /* Create two bogus thread handles. */ + memset (&thrs[NTHR], 0, sizeof (pthread_t)); + memset (&thrs[NTHR+1], 0xaa, sizeof (pthread_t)); + + for (i=1; i< NTHR; i++) + { + pthread_join (thrs[i], NULL); + } + printf ("Done!"); +} diff --git a/gdb/testsuite/gdb.python/py-thrhandle.exp b/gdb/testsuite/gdb.python/py-thrhandle.exp new file mode 100644 index 0000000..d542734 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-thrhandle.exp @@ -0,0 +1,52 @@ +# Copyright (C) 2016 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Please email any bugs, comments, and/or additions to this file to: +# bug-gdb@gnu.org + +# This file verifies that gdb.thread_from_thread_handle works as expected. + +standard_testfile + + +if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable debug] != "" } { + return -1 +} + +clean_restart ${binfile} +runto_main + +gdb_test "break do_something" \ + "Breakpoint 2 at .*: file .*${srcfile}, line .*" \ + "breakpoint on do_something" + +gdb_test "continue" \ + "Breakpoint 2, do_something .*" \ + "run to do_something" + +gdb_test "python print gdb.thread_from_thread_handle(gdb.parse_and_eval('thrs\[0\]')).num" \ + "1" + +gdb_test "python print gdb.thread_from_thread_handle(gdb.parse_and_eval('thrs\[1\]')).num" \ + "2" + +gdb_test "python print gdb.thread_from_thread_handle(gdb.parse_and_eval('thrs\[2\]')).num" \ + "3" + +gdb_test "python print gdb.thread_from_thread_handle(gdb.parse_and_eval('thrs\[3\]'))" \ + "None" + +gdb_test "python print gdb.thread_from_thread_handle(gdb.parse_and_eval('thrs\[4\]'))" \ + "None"