1 # Copyright 2022-2024 Free Software Foundation, Inc.
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.
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.
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/>.
18 require allow_dap_tests
20 load_lib dap-support.exp
24 if {[build_executable ${testfile}.exp $testfile] == -1} {
28 if {[dap_initialize] == ""} {
32 set launch_id [dap_launch $testfile]
34 set obj [dap_check_request_and_response "set breakpoint on two functions" \
35 setFunctionBreakpoints \
36 {o breakpoints [a [o name [s function_breakpoint_here]] \
37 [o name [s do_not_stop_here]]]}]
38 set fn_bpno [dap_get_breakpoint_number $obj]
40 # This also tests that the previous do_not_stop_here breakpoint is
42 set obj [dap_check_request_and_response "set breakpoint on function" \
43 setFunctionBreakpoints \
44 {o breakpoints [a [o name [s function_breakpoint_here]]]}]
45 set fn_bpno [dap_get_breakpoint_number $obj]
47 set obj [dap_check_request_and_response "set breakpoint with invalid filename" \
49 [format {o source [o path [s nosuchfilename.c]] breakpoints [a [o line [i 29]]]}]]
51 set line [gdb_get_line_number "BREAK"]
52 set obj [dap_check_request_and_response "set breakpoint by line number" \
54 [format {o source [o path [%s]] breakpoints [a [o line [i %d]]]} \
55 [list s $srcfile] $line]]
56 set line_bpno [dap_get_breakpoint_number $obj]
58 # Check the new breakpoint event.
60 foreach d [lindex $obj 1] {
61 if {[dict get $d type] != "event"
62 || [dict get $d event] != "breakpoint"} {
65 if {[dict get $d body reason] == "new"
66 && [dict get $d body breakpoint verified] == "true"} {
72 pass "check lack of new breakpoint event"
74 fail "check lack of new breakpoint event"
77 # Note that in this request, we add a 'source' field to the
78 # SourceBreakpoint object. This isn't in the spec but it once caused
79 # an incorrect exception in the Python code. See PR dap/30820.
80 set obj [dap_check_request_and_response "reset breakpoint by line number" \
82 [format {o source [o path [%s]] \
83 breakpoints [a [o source [o path [%s]] \
85 [list s $srcfile] [list s $srcfile] $line]]
86 set new_line_bpno [dap_get_breakpoint_number $obj]
87 gdb_assert {$new_line_bpno == $line_bpno} "re-setting kept same breakpoint number"
89 dap_check_request_and_response "configurationDone" configurationDone
91 dap_check_response "launch response" launch $launch_id
93 dap_wait_for_event_and_check "inferior started" thread "body reason" started
95 # While waiting for the stopped event, we might receive breakpoint changed
96 # events indicating some breakpoint addresses were relocated.
97 lassign [dap_wait_for_event_and_check "stopped at function breakpoint" stopped \
98 "body reason" breakpoint \
99 "body hitBreakpointIds" $fn_bpno] unused objs
101 if { [dict get $obj "type"] != "event" } {
105 if { [dict get $obj "event"] != "breakpoint" } {
109 set body [dict get $obj "body"]
111 if { [dict get $body "reason"] != "changed" } {
115 set breakpoint [dict get $body "breakpoint"]
116 set breakpoint_id [dict get $breakpoint "id"]
119 # This uses "&address_breakpoint_here" as the address -- this is a
120 # hack because we know how this is implemented under the hood.
121 set obj [dap_check_request_and_response "set breakpoint by address" \
122 setInstructionBreakpoints \
123 {o breakpoints [a [o instructionReference [s &address_breakpoint_here]]]}]
124 set insn_bpno [dap_get_breakpoint_number $obj]
126 set response [lindex $obj 0]
127 set bplist [dict get $response body breakpoints]
128 set insn_pc [dict get [lindex $bplist 0] instructionReference]
130 # Check that there are breakpoint locations on each line between FIRST
132 set first_line [gdb_get_line_number "FIRST"]
133 set last_line [expr {$line - 1}]
134 set obj [dap_check_request_and_response "breakpoint locations" \
135 breakpointLocations \
136 [format {o source [o path [%s]] line [i %d] endLine [i %d]} \
137 [list s $srcfile] $first_line $last_line]]
138 # We know gdb returns the lines in sorted order.
139 foreach entry [dict get [lindex $obj 0] body breakpoints] {
140 gdb_assert {[dict get $entry line] == $first_line} \
141 "line $first_line in result"
145 set obj [dap_check_request_and_response "evaluate global in function" \
146 evaluate {o expression [s global_variable]}]
147 dap_match_values "global value in function" [lindex $obj 0] \
150 dap_check_request_and_response step stepIn {o threadId [i 1]}
151 dap_wait_for_event_and_check "stopped after step" stopped "body reason" step
153 set obj [dap_check_request_and_response "evaluate global second time" \
154 evaluate {o expression [s global_variable]}]
155 dap_match_values "global value after step" [lindex $obj 0] \
158 dap_check_request_and_response "continue to address" continue \
160 dap_wait_for_event_and_check "stopped at address breakpoint" stopped \
161 "body reason" breakpoint \
162 "body hitBreakpointIds" $insn_bpno
164 dap_check_request_and_response "continue to line" continue \
166 dap_wait_for_event_and_check "stopped at line breakpoint" stopped \
167 "body reason" breakpoint \
168 "body hitBreakpointIds" $line_bpno \
169 "body allThreadsStopped" true
171 dap_check_request_and_response "return from function" stepOut \
173 dap_wait_for_event_and_check "stopped after return" stopped \
176 set obj [dap_check_request_and_response "evaluate global in main" \
177 evaluate {o expression [s global_variable]}]
178 dap_match_values "global value in main" [lindex $obj 0] \
181 set obj [dap_check_request_and_response "set global in main" \
182 setExpression {o expression [s global_variable] value [s 23]}]
183 dap_match_values "global value in main after set" [lindex $obj 0] \
187 set obj [dap_request_and_response \
188 evaluate {o expression [s nosuchvariable]}]
189 set response [lindex $obj 0]
190 gdb_assert { [dict get $response success] == "false" } "result of invalid request"
192 set obj [dap_check_request_and_response "disassemble one instruction" \
194 [format {o memoryReference [s %s] instructionCount [i 1]} \
196 set response [lindex $obj 0]
197 gdb_assert { [dict exists $response body instructions] } "instructions in disassemble output"
198 foreach insn [dict get $response body instructions] {
199 gdb_assert {[dict exists $insn instructionBytes]} \
200 "instruction bytes in disassemble output"
201 set bytes [dict get $insn instructionBytes]
202 gdb_assert {[string length $bytes] % 2 == 0} \
203 "even number of digits"
204 gdb_assert {[regexp "^\[0-9A-Fa-f\]+\$" $bytes]} \
205 "instructionBytes is hex"
208 set obj [dap_check_request_and_response "command repl" \
209 evaluate {o expression [s {output 23}] context [s repl]}]
210 set response [lindex $obj 0]
211 gdb_assert {[dict get $response body result] == 23}