• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

GNU Binutils with patches for OS216


Commit MetaInfo

Revisión757bf54bb48a8417154cfdd7128c1775d43478d8 (tree)
Tiempo2017-09-22 03:49:48
AutorKevin Buettner <kevinb@redh...>
CommiterKevin Buettner

Log Message

Test case for Inferior.thread_from_thread_handle

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.

Cambiar Resumen

Diferencia incremental

--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,8 @@
1+2017-09-21 Kevin Buettner <kevinb@redhat.com>
2+
3+ * gdb.python/py-thrhandle.c, gdb.python/py-thrhandle.exp: New
4+ files.
5+
16 2017-09-20 Pedro Alves <palves@redhat.com>
27
38 * gdb.base/list-ambiguous.exp (test_list_ambiguous_symbol): Expect
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-thrhandle.c
@@ -0,0 +1,94 @@
1+/* This testcase is part of GDB, the GNU debugger.
2+
3+ Copyright 2017 Free Software Foundation, Inc.
4+
5+ This program is free software; you can redistribute it and/or modify
6+ it under the terms of the GNU General Public License as published by
7+ the Free Software Foundation; either version 3 of the License, or
8+ (at your option) any later version.
9+
10+ This program is distributed in the hope that it will be useful,
11+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+ GNU General Public License for more details.
14+
15+ You should have received a copy of the GNU General Public License
16+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
17+
18+#include <pthread.h>
19+#include <unistd.h>
20+#include <memory.h>
21+
22+#define NTHR 3
23+#define NBOGUSTHR 2
24+
25+int thr_data[NTHR];
26+
27+/* Thread handles for each thread plus some "bogus" threads. */
28+pthread_t thrs[NTHR + NBOGUSTHR];
29+
30+/* The thread children will meet at this barrier. */
31+pthread_barrier_t c_barrier;
32+
33+/* The main thread and child thread will meet at this barrier. */
34+pthread_barrier_t mc_barrier;
35+
36+void
37+do_something (int n)
38+{
39+}
40+
41+void *
42+do_work (void *data)
43+{
44+ int num = * (int *) data;
45+
46+ /* As the child threads are created, they'll meet the main thread
47+ at this barrier. We do this to ensure that threads end up in
48+ GDB's thread list in the order in which they were created. Having
49+ this ordering makes it easier to write the test. */
50+ pthread_barrier_wait (&mc_barrier);
51+
52+ /* All of the child threads will meet at this barrier before proceeding.
53+ This ensures that all threads will be active (not exited) and in
54+ roughly the same state when the first one hits the breakpoint in
55+ do_something(). */
56+ pthread_barrier_wait (&c_barrier);
57+
58+ do_something (num);
59+
60+ pthread_exit (NULL);
61+}
62+
63+void
64+after_mc_barrier (void)
65+{
66+}
67+
68+int
69+main (int argc, char **argv)
70+{
71+ int i;
72+
73+ pthread_barrier_init (&c_barrier, NULL, NTHR - 1);
74+ pthread_barrier_init (&mc_barrier, NULL, 2);
75+
76+ thrs[0] = pthread_self ();
77+ thr_data[0] = 1;
78+
79+ /* Create two bogus thread handles. */
80+ memset (&thrs[NTHR], 0, sizeof (pthread_t));
81+ memset (&thrs[NTHR + 1], 0xaa, sizeof (pthread_t));
82+
83+ for (i = 1; i < NTHR; i++)
84+ {
85+ thr_data[i] = i + 1;
86+
87+ pthread_create (&thrs[i], NULL, do_work, &thr_data[i]);
88+ pthread_barrier_wait (&mc_barrier);
89+ after_mc_barrier ();
90+ }
91+
92+ for (i = 1; i < NTHR; i++)
93+ pthread_join (thrs[i], NULL);
94+}
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-thrhandle.exp
@@ -0,0 +1,102 @@
1+# Copyright (C) 2017 Free Software Foundation, Inc.
2+
3+# This program is free software; you can redistribute it and/or modify
4+# it under the terms of the GNU General Public License as published by
5+# the Free Software Foundation; either version 3 of the License, or
6+# (at your option) any later version.
7+#
8+# This program is distributed in the hope that it will be useful,
9+# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+# GNU General Public License for more details.
12+#
13+# You should have received a copy of the GNU General Public License
14+# along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+# Please email any bugs, comments, and/or additions to this file to:
17+# bug-gdb@gnu.org
18+
19+# This file verifies that gdb.Inferior.thread_from_thread_handle works
20+# as expected.
21+
22+standard_testfile
23+
24+
25+if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable debug] != "" } {
26+ return -1
27+}
28+
29+clean_restart ${binfile}
30+runto_main
31+
32+gdb_test "break after_mc_barrier" \
33+ "Breakpoint 2 at .*: file .*${srcfile}, line .*" \
34+ "breakpoint on after_mc_barrier"
35+
36+gdb_test "break do_something" \
37+ "Breakpoint 3 at .*: file .*${srcfile}, line .*" \
38+ "breakpoint on do_something"
39+
40+gdb_test "continue" \
41+ "Breakpoint 2, after_mc_barrier .*" \
42+ "run to after_mc_barrier"
43+
44+gdb_test_no_output "del 2" "delete after_mc_barrier breakpoint"
45+
46+gdb_test "continue" \
47+ "Breakpoint 3, do_something .*" \
48+ "run to do_something"
49+
50+# The test case has been constructed so that the current thread,
51+# indicated by '*' in the "info threads" output, should be stopped in
52+# do_something() with a value of n which is the same as the number
53+# reported in the "Id" column. If it's not, then something went wrong
54+# with the start up sequence which should cause the main thread to be
55+# thread 1, the first child thread to be thread 2, and the second
56+# child thread to be thread 3.
57+#
58+# Note that \1 in the RE below is a backreference to the thread id
59+# reported in the "Id" column.
60+
61+gdb_test "info threads" \
62+ {.*[\r\n]+\* +([0-9]+) +Thread[^\r\n]* do_something \(n=\1\) at.*}
63+
64+# Check for expected results when passing a valid thread handle to
65+# thread_from_thread_handle().
66+
67+gdb_test "python print(gdb.selected_inferior().thread_from_thread_handle(gdb.parse_and_eval('thrs\[0\]')).num)" \
68+ "1" "print thread id for thrs\[0\]"
69+
70+gdb_test "python print(gdb.selected_inferior().thread_from_thread_handle(gdb.parse_and_eval('thrs\[1\]')).num)" \
71+ "2" "print thread id for thrs\[1\]"
72+
73+gdb_test "python print(gdb.selected_inferior().thread_from_thread_handle(gdb.parse_and_eval('thrs\[2\]')).num)" \
74+ "3" "print thread id for thrs\[2\]"
75+
76+# Objects which are of the correct size, but which are bogus thread
77+# handles should return None. For the first test (using thrs[3]), we
78+# use 0. For the second (thrs[4]), we use an unlikely bit pattern.
79+
80+gdb_test "python print(gdb.selected_inferior().thread_from_thread_handle(gdb.parse_and_eval('thrs\[3\]')))" \
81+ "None" "print thread for bogus handle thrs\[3\]"
82+
83+gdb_test "python print(gdb.selected_inferior().thread_from_thread_handle(gdb.parse_and_eval('thrs\[4\]')))" \
84+ "None" "print thread for bogus handle thrs\[4\]"
85+
86+# We should see an exception when passing an object of the wrong type.
87+
88+gdb_test "python print(gdb.selected_inferior().thread_from_thread_handle(gdb.lookup_symbol('main')))" \
89+ ".*TypeError: Argument 'handle_obj' must be a thread handle object.*" \
90+ "TypeError when passing a symbol object to thread_from_thread_handle"
91+
92+# We should see an exception when passing too large of an object.
93+
94+gdb_test "python print(gdb.selected_inferior().thread_from_thread_handle(gdb.parse_and_eval('thrs')))" \
95+ ".*Thread handle size mismatch.*" \
96+ "Pass overly large object to thread_from_thread_handle"
97+
98+# We should see an exception when passing too small of an object.
99+
100+gdb_test "python print(gdb.selected_inferior().thread_from_thread_handle(gdb.parse_and_eval('\"S\"')))" \
101+ ".*Thread handle size mismatch.*" \
102+ "Pass too small of an object to thread_from_thread_handle"