From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 45811 invoked by alias); 5 May 2016 21:19:05 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 45796 invoked by uid 89); 5 May 2016 21:19:04 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-4.0 required=5.0 tests=BAYES_00,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=U*bug-gdb, buggdbgnuorg, bug-gdb@gnu.org 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 (AES256-GCM-SHA384 encrypted) ESMTPS; Thu, 05 May 2016 21:18:54 +0000 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id AA1A861A0E for ; Thu, 5 May 2016 21:18:53 +0000 (UTC) Received: from pinnacle.lan (ovpn-113-39.phx2.redhat.com [10.3.113.39]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u45LIqU5027718 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA256 bits=256 verify=NO) for ; Thu, 5 May 2016 17:18:53 -0400 Date: Thu, 05 May 2016 21:19:00 -0000 From: Kevin Buettner To: gdb-patches@sourceware.org Subject: [PATCH 4/4] Test case for gdb.thread_from_thread_handle Message-ID: <20160505141851.5f5ce5af@pinnacle.lan> In-Reply-To: <20160505140938.26a084ed@pinnacle.lan> References: <20160505140938.26a084ed@pinnacle.lan> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-IsSubscribed: yes X-SW-Source: 2016-05/txt/msg00081.txt.bz2 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 | 71 +++++++++++++++++++++++++++++++ gdb/testsuite/gdb.python/py-thrhandle.exp | 46 ++++++++++++++++++++ 2 files changed, 117 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..ec148c9 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-thrhandle.c @@ -0,0 +1,71 @@ +/* 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 + +#define NTHR 3 +pthread_t thrs[NTHR]; +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); + } + + 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..a324d4e --- /dev/null +++ b/gdb/testsuite/gdb.python/py-thrhandle.exp @@ -0,0 +1,46 @@ +# 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"