From patchwork Wed Aug 16 17:40:03 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Buettner X-Patchwork-Id: 22160 Received: (qmail 81491 invoked by alias); 16 Aug 2017 17:40:12 -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 79841 invoked by uid 89); 16 Aug 2017 17:40:09 -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= 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; Wed, 16 Aug 2017 17:40:07 +0000 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C63AAC056794 for ; Wed, 16 Aug 2017 17:40:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com C63AAC056794 Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=kevinb@redhat.com Received: from pinnacle.lan (ovpn-117-126.phx2.redhat.com [10.3.117.126]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9FBA3E9957 for ; Wed, 16 Aug 2017 17:40:04 +0000 (UTC) Date: Wed, 16 Aug 2017 10:40:03 -0700 From: Kevin Buettner To: gdb-patches@sourceware.org Subject: Re: [PATCH v4 4/7] Test case for Inferior.thread_from_thread_handle Message-ID: <20170816104003.293fee9e@pinnacle.lan> In-Reply-To: <20170816092542.6d2deb00@pinnacle.lan> References: <20170816092542.6d2deb00@pinnacle.lan> MIME-Version: 1.0 X-IsSubscribed: yes As the title says, this is a test case for Inferior.thread_from_thread_handle, a python method which will, given a thread library dependent thread handle, find the GDB thread which corresponds to that thread handle (in the inferior under consideration). 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 corresponding thread in GDB's internal thread list. I use barriers to make sure that both threads have actually started; execution will stop when one of the threads breaks at do_something. Thanks to Simon Marchi for suggestions for forcing the thread numbering to be stable. gdb/testsuite/ChangeLog: * gdb.python/py-thrhandle.c, gdb.python/py-thrhandle.exp: New files. --- gdb/testsuite/gdb.python/py-thrhandle.c | 94 +++++++++++++++++++++++++++ gdb/testsuite/gdb.python/py-thrhandle.exp | 102 ++++++++++++++++++++++++++++++ 2 files changed, 196 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..0dd974f --- /dev/null +++ b/gdb/testsuite/gdb.python/py-thrhandle.c @@ -0,0 +1,94 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2017 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 + +#define NTHR 3 +#define NBOGUSTHR 2 + +int thr_data[NTHR]; + +/* Thread handles for each thread plus some "bogus" threads. */ +pthread_t thrs[NTHR + NBOGUSTHR]; + +/* The thread children will meet at this barrier. */ +pthread_barrier_t c_barrier; + +/* The main thread and child thread will meet at this barrier. */ +pthread_barrier_t mc_barrier; + +void +do_something (int n) +{ +} + +void * +do_work (void *data) +{ + int num = * (int *) data; + + /* As the child threads are created, they'll meet the main thread + at this barrier. We do this to ensure that threads end up in + GDB's thread list in the order in which they were created. Having + this ordering makes it easier to write the test. */ + pthread_barrier_wait (&mc_barrier); + + /* All of the child threads will meet at this barrier before proceeding. + This ensures that all threads will be active (not exited) and in + roughly the same state when the first one hits the breakpoint in + do_something(). */ + pthread_barrier_wait (&c_barrier); + + do_something (num); + + pthread_exit (NULL); +} + +void +after_mc_barrier (void) +{ +} + +int +main (int argc, char **argv) +{ + int i; + + pthread_barrier_init (&c_barrier, NULL, NTHR - 1); + pthread_barrier_init (&mc_barrier, NULL, 2); + + thrs[0] = pthread_self (); + thr_data[0] = 1; + + /* 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++) + { + thr_data[i] = i + 1; + + pthread_create (&thrs[i], NULL, do_work, &thr_data[i]); + pthread_barrier_wait (&mc_barrier); + after_mc_barrier (); + } + + for (i = 1; i < NTHR; i++) + pthread_join (thrs[i], NULL); +} diff --git a/gdb/testsuite/gdb.python/py-thrhandle.exp b/gdb/testsuite/gdb.python/py-thrhandle.exp new file mode 100644 index 0000000..66b0472 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-thrhandle.exp @@ -0,0 +1,102 @@ +# Copyright (C) 2017 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.Inferior.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 after_mc_barrier" \ + "Breakpoint 2 at .*: file .*${srcfile}, line .*" \ + "breakpoint on after_mc_barrier" + +gdb_test "break do_something" \ + "Breakpoint 3 at .*: file .*${srcfile}, line .*" \ + "breakpoint on do_something" + +gdb_test "continue" \ + "Breakpoint 2, after_mc_barrier .*" \ + "run to after_mc_barrier" + +gdb_test_no_output "del 2" "delete after_mc_barrier breakpoint" + +gdb_test "continue" \ + "Breakpoint 3, do_something .*" \ + "run to do_something" + +# The test case has been constructed so that the current thread, +# indicated by '*' in the "info threads" output, should be stopped in +# do_something() with a value of n which is the same as the number +# reported in the "Id" column. If it's not, then something went wrong +# with the start up sequence which should cause the main thread to be +# thread 1, the first child thread to be thread 2, and the second +# child thread to be thread 3. +# +# Note that \1 in the RE below is a backreference to the thread id +# reported in the "Id" column. + +gdb_test "info threads" \ + {.*[\r\n]+\* +([0-9]+) +Thread[^\r\n]* do_something \(n=\1\) at.*} + +# Check for expected results when passing a valid thread handle to +# thread_from_thread_handle(). + +gdb_test "python print(gdb.selected_inferior().thread_from_thread_handle(gdb.parse_and_eval('thrs\[0\]')).num)" \ + "1" "print thread id for thrs\[0\]" + +gdb_test "python print(gdb.selected_inferior().thread_from_thread_handle(gdb.parse_and_eval('thrs\[1\]')).num)" \ + "2" "print thread id for thrs\[1\]" + +gdb_test "python print(gdb.selected_inferior().thread_from_thread_handle(gdb.parse_and_eval('thrs\[2\]')).num)" \ + "3" "print thread id for thrs\[2\]" + +# Objects which are of the correct size, but which are bogus thread +# handles should return None. For the first test (using thrs[3]), we +# use 0. For the second (thrs[4]), we use an unlikely bit pattern. + +gdb_test "python print(gdb.selected_inferior().thread_from_thread_handle(gdb.parse_and_eval('thrs\[3\]')))" \ + "None" "print thread for bogus handle thrs\[3\]" + +gdb_test "python print(gdb.selected_inferior().thread_from_thread_handle(gdb.parse_and_eval('thrs\[4\]')))" \ + "None" "print thread for bogus handle thrs\[4\]" + +# We should see an exception when passing an object of the wrong type. + +gdb_test "python print(gdb.selected_inferior().thread_from_thread_handle(gdb.lookup_symbol('main')))" \ + ".*TypeError: Argument 'handle_obj' must be a thread handle object.*" \ + "TypeError when passing a symbol object to thread_from_thread_handle" + +# We should see an exception when passing too large of an object. + +gdb_test "python print(gdb.selected_inferior().thread_from_thread_handle(gdb.parse_and_eval('thrs')))" \ + ".*Thread handle size mismatch.*" \ + "Pass overly large object to thread_from_thread_handle" + +# We should see an exception when passing too small of an object. + +gdb_test "python print(gdb.selected_inferior().thread_from_thread_handle(gdb.parse_and_eval('\"S\"')))" \ + ".*Thread handle size mismatch.*" \ + "Pass too small of an object to thread_from_thread_handle"