xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.base/stap-probe.exp (revision 70f7362772ba52b749c976fb5e86e39a8b2c9afc)
1# Copyright (C) 2012-2020 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
16standard_testfile
17
18# Run the tests.  We run the tests two different ways: once with a
19# plain probe, and once with a probe that has an associated semaphore.
20# This returns -1 on failure to compile or start, 0 otherwise.
21proc stap_test {exec_name {args ""}} {
22    global testfile hex srcfile
23
24    if {[prepare_for_testing "failed to prepare" ${exec_name} $srcfile \
25	   [concat $args debug]]} {
26	return -1
27    }
28
29    set semaphore_addr_var ""
30    if {[string first "-DUSE_SEMAPHORES" $args] == -1} {
31	gdb_test_no_output "set breakpoint always-inserted on"
32	set semaphore_addr_var [get_hexadecimal_valueof "&relocation_marker" "0"]
33    }
34
35    if ![runto_main] {
36	return -1
37    }
38
39    gdb_test "print \$_probe_argc" "No probe at PC $hex" \
40	"check argument not at probe point"
41
42    if {[string first "-DUSE_SEMAPHORES" $args] != -1} {
43	gdb_test "info probes stap" \
44	    "test *user *$hex *$hex .*"
45    } else {
46	gdb_test "info probes stap" \
47	    "test *user *$hex .*"
48    }
49
50    if {[runto "-pstap test:user"]} {
51	pass "run to -pstap test:user"
52    } else {
53	fail "run to -pstap test:user"
54    }
55
56    if {[string first "-DUSE_SEMAPHORES" $args] == -1} {
57	set relocation_base \
58	   [expr [get_hexadecimal_valueof "&relocation_marker" "0"] - $semaphore_addr_var]
59	if {$relocation_base != 0} {
60	   # Checks that GDB doesn't mistakenly relocate and write to null
61	   # semaphore addresses.  If it were to relocate a zero-valued
62	   # semaphore address and increment the value at that address, we
63	   # would expect to see "\200ELF" here instead.
64	   gdb_test "p (*(char*) $relocation_base)@4" \
65		" = \"\\\\177ELF\"" \
66		"null semaphore relocation"
67	}
68    }
69
70    # Test probe arguments.
71    gdb_test "print \$_probe_argc" " = 1" \
72    "print \$_probe_argc for probe user"
73    gdb_test "print \$_probe_arg0 == x" " = 1" \
74    "check \$_probe_arg0 for probe user"
75    gdb_test "print \$_probe_arg1" \
76    	"Invalid probe argument 1 -- probe has 1 arguments available" \
77    	"check \$_probe_arg1 for probe user"
78
79    # Set a breakpoint with multiple probe locations.
80    gdb_test "break -pstap test:two" \
81	"Breakpoint \[0-9\]+ at $hex.*2 locations.*" \
82	"set multi-location probe breakpoint (probe two)"
83
84    # Reinit GDB, set a breakpoint on probe m4.
85    delete_breakpoints
86    if {[runto "-pstap test:m4"]} {
87      pass "run to -pstap test:m4"
88    } else {
89      fail "run to -pstap test:m4"
90    }
91
92    # Testing probe arguments.
93    gdb_test "print \$_probe_argc" " = 3" \
94    "print \$_probe_argc for probe m4"
95    gdb_test "print \$_probe_arg0" " = 42" \
96    "check \$_probe_arg0 for probe m4"
97    gdb_test "print (const char *) \$_probe_arg1" \
98    " = $hex .This is a test message.*" \
99    "check \$_probe_arg1 for probe m4"
100    gdb_test "print \$_probe_arg2 == v" " = 1" \
101    "check \$_probe_arg2 for probe m4"
102
103    # Reinit GDB, set a breakpoint on probe ps.
104    delete_breakpoints
105    if {[runto "-pstap test:ps"]} {
106      pass "run to -pstap test:m4"
107    } else {
108      fail "run to -pstap test:m4"
109    }
110
111    gdb_test "print \$_probe_argc" " = 3" \
112    "print \$_probe_argc for probe ps"
113    gdb_test "print (const char *) \$_probe_arg1" \
114    " = $hex .This is another test message.*" \
115    "print \$_probe_arg1 for probe ps"
116
117    return 0
118}
119
120proc stap_test_no_debuginfo {exec_name {args ""}} {
121    global testfile hex
122
123    if {[prepare_for_testing "failed to prepare" ${exec_name} ${testfile}.c \
124	   [concat $args nodebug optimize=-O2]]} {
125	return -1
126    }
127
128    if {[runto "-pstap test:user"]} {
129	pass "run to -pstap test:user"
130    } else {
131	fail "run to -pstap test:user"
132    }
133
134    # Test probe arguments.
135    gdb_test "print \$_probe_argc" " = 1" \
136    "print \$_probe_argc for probe user"
137    gdb_test "print \$_probe_arg0 == 23" " = 1" \
138    "check \$_probe_arg0 for probe user"
139    gdb_test "print \$_probe_arg1" \
140    	"Invalid probe argument 1 -- probe has 1 arguments available" \
141    	"check \$_probe_arg1 for probe user"
142
143    # Set a breakpoint with multiple probe locations.
144    # In this scenario, we may expect more than 2 locations because of
145    # the optimizations (inlining, loop unrolling, etc).
146    gdb_test "break -pstap test:two" \
147	"Breakpoint .* at $hex.*\[0-9\]+ locations.*" \
148	"set multi-location probe breakpoint (probe two)"
149
150    # Reinit GDB, set a breakpoint on probe m4.
151    delete_breakpoints
152    if {[runto "-pstap test:m4"]} {
153      pass "run to -pstap test:m4"
154    } else {
155      fail "run to -pstap test:m4"
156    }
157
158    # Testing probe arguments.
159    gdb_test "print \$_probe_argc" " = 3" \
160    "print \$_probe_argc for probe m4"
161    gdb_test "print \$_probe_arg0" " = 42" \
162    "check \$_probe_arg0 for probe m4"
163    gdb_test "print (const char *) \$_probe_arg1" \
164    " = $hex .This is a test message.*" \
165    "check \$_probe_arg1 for probe m4"
166    gdb_test "print \$_probe_arg2 == 0" " = 1" \
167    "check \$_probe_arg2 for probe m4"
168
169    # Reinit GDB, set a breakpoint on probe ps.
170    delete_breakpoints
171    if {[runto "-pstap test:ps"]} {
172      pass "run to -pstap test:m4"
173    } else {
174      fail "run to -pstap test:m4"
175    }
176
177    gdb_test "print \$_probe_argc" " = 3" \
178    "print \$_probe_argc for probe ps"
179    gdb_test "print (const char *) \$_probe_arg1" \
180    " = $hex .This is another test message.*" \
181    "print \$_probe_arg1 for probe ps"
182
183    return 0
184}
185
186with_test_prefix "without semaphore, not optimized" {
187    if {[stap_test "stap-probe-nosem-noopt"] == -1} {
188	untested "stap probe test failed"
189	  return -1
190    }
191
192    foreach_with_prefix pie { "nopie" "pie" } {
193	stap_test "stap-probe-nosem-noopt-$pie" $pie
194    }
195}
196
197with_test_prefix "with semaphore, not optimized" {
198    stap_test "stap-probe-sem-noopt" additional_flags=-DUSE_SEMAPHORES
199}
200
201with_test_prefix "without semaphore, optimized" {
202    stap_test_no_debuginfo "stap-probe-nosem-opt"
203}
204
205with_test_prefix "with semaphore, optimized" {
206    stap_test_no_debuginfo "stap-probe-sem-opt" additional_flags=-DUSE_SEMAPHORES
207}
208