Add test case for tracepoints with conditions
[external/binutils.git] / gdb / testsuite / gdb.trace / trace-condition.exp
1 # Copyright 2011-2015 Free Software Foundation, Inc.
2 # This program is free software; you can redistribute it and/or modify
3 # it under the terms of the GNU General Public License as published by
4 # the Free Software Foundation; either version 3 of the License, or
5 # (at your option) any later version.
6 #
7 # This program is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 # GNU General Public License for more details.
11 #
12 # You should have received a copy of the GNU General Public License
13 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
14
15 load_lib "trace-support.exp"
16
17 standard_testfile
18 set executable $testfile
19 set expfile $testfile.exp
20
21 # Some targets have leading underscores on assembly symbols.
22 set additional_flags [gdb_target_symbol_prefix_flags]
23
24 if [is_amd64_regs_target] {
25     set pcreg "\$rip"
26 } elseif [is_x86_like_target] {
27     set pcreg "\$eip"
28 } else {
29     set pcreg "\$pc"
30 }
31
32 if [prepare_for_testing $expfile $executable $srcfile \
33         [list debug $additional_flags]] {
34     untested "failed to prepare for trace tests"
35     return -1
36 }
37
38 if ![runto_main] {
39     fail "Can't run to main to check for trace support"
40     return -1
41 }
42
43 if ![gdb_target_supports_trace] {
44     unsupported "target does not support trace"
45     return -1
46 }
47
48 set libipa [get_in_proc_agent]
49 gdb_load_shlibs $libipa
50
51 # Can't use prepare_for_testing, because that splits compiling into
52 # building objects and then linking, and we'd fail with "linker input
53 # file unused because linking not done" when building the object.
54
55 if { [gdb_compile "$srcdir/$subdir/$srcfile" $binfile \
56           executable [list debug $additional_flags shlib=$libipa] ] != "" } {
57     untested "failed to compile ftrace tests"
58     return -1
59 }
60
61 clean_restart ${executable}
62
63 if ![runto_main] {
64     fail "Can't run to main for ftrace tests"
65     return 0
66 }
67
68 if { [gdb_test "info sharedlibrary" ".*${libipa}.*" "IPA loaded"] != 0 } {
69     untested "Could not find IPA lib loaded"
70     return 1
71 }
72
73 proc test_tracepoints { trace_command condition num_frames { kfail_proc 0 } } {
74     global executable gdb_prompt
75
76     clean_restart ${executable}
77
78     if ![runto_main] {
79         fail "Can't run to main for ftrace tests"
80         return 0
81     }
82
83     gdb_test "break begin" ".*" ""
84
85     gdb_test "break end" ".*" ""
86
87     with_test_prefix "${trace_command}: ${condition}" {
88
89         gdb_test "${trace_command} set_point if ${condition}" \
90             "\(Fast t|T\)racepoint .*" \
91             "set tracepoint"
92
93         gdb_test "continue" ".*Breakpoint \[0-9\]+, begin .*" \
94             "advance to trace begin"
95
96         gdb_test_no_output "tstart" "start trace experiment"
97
98         gdb_test_multiple "continue" "advance through tracing" {
99             -re ".*Breakpoint \[0-9\]+, end .*$gdb_prompt $" {
100                 pass "advance through tracing"
101             }
102             -re "Program received signal SIGSEGV, Segmentation fault\\..*$gdb_prompt $" {
103                 if { $kfail_proc != 0 } {
104                     $kfail_proc $trace_command
105                 }
106                 fail "advance through tracing"
107             }
108         }
109
110         if { $kfail_proc != 0 } {
111             $kfail_proc $trace_command
112         }
113         gdb_test "tstatus" \
114             ".*Trace .*Collected $num_frames .*" \
115             "check $num_frames frames were collected."
116
117         gdb_test "tstop" "" ""
118     }
119 }
120
121 # These callbacks identify known failures for certain architectures.  They
122 # are called either if GDBserver crashes or has not traced the correct
123 # number of frames.
124
125 proc 18955_x86_64_failure { trace_command } {
126     if { $trace_command == "ftrace" } {
127         setup_kfail "gdb/18955" "x86_64-*-linux*"
128     }
129 }
130
131 proc 18955_i386_failure { trace_command } {
132     if { $trace_command == "ftrace" } {
133         setup_kfail "gdb/18955" "i\[34567\]86-*-*"
134     }
135 }
136
137 foreach trace_command { "trace" "ftrace" } {
138     # This condition is always true as the PC should be set to the tracepoint
139     # address when hit.
140     test_tracepoints $trace_command "$pcreg == *set_point" 10
141
142     # Can we read local variables?
143     test_tracepoints $trace_command "anarg == 100 || anarg == 200" 2 18955_x86_64_failure
144     # Can we read global variables?
145     test_tracepoints $trace_command "anarg == 100 && globvar == 1" 1 18955_x86_64_failure
146
147     # Test various operations to cover as many opcodes as possible.
148     test_tracepoints $trace_command "21 + 21 == 42" 10
149     test_tracepoints $trace_command "21 - 21 == 0" 10
150     test_tracepoints $trace_command "21 * 2 == 42" 10
151     test_tracepoints $trace_command "21 << 1 == 42" 10
152     test_tracepoints $trace_command "42 >> 1 == 21" 10
153     test_tracepoints $trace_command "-21 << 1 == -42" 10
154     test_tracepoints $trace_command "-42 >> 1 == -21" 10
155     test_tracepoints $trace_command "(0xabababab & 0x0000ffff) == 0xabab" 10
156     test_tracepoints $trace_command "(0xabababab | 0x0000ffff) == 0xababffff" 10
157     test_tracepoints $trace_command "(0xaaaaaaaa ^ 0x55555555) == 0xffffffff" 10
158     test_tracepoints $trace_command "~0xaaaaaaaa == 0x55555555" 10
159     test_tracepoints $trace_command "21 < 42" 10
160     test_tracepoints $trace_command "42 <= 42" 10
161     test_tracepoints $trace_command "42 >= 42" 10
162     test_tracepoints $trace_command "42 > 21" 10
163     test_tracepoints $trace_command "(21 < 42 ? 0 : 1) == 0" 10 18955_i386_failure
164     test_tracepoints $trace_command "(42 <= 42 ? 0 : 1) == 0" 10
165     test_tracepoints $trace_command "(42 >= 42 ? 0 : 1) == 0" 10
166     test_tracepoints $trace_command "(42 > 21 ? 0 : 1) == 0" 10 18955_i386_failure
167     test_tracepoints $trace_command "\$trace_timestamp >= 0" 10
168 }