1# Copyright 1998-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
16
17standard_testfile average.c sum.c
18
19if {[build_executable $testfile.exp $testfile \
20	 [list $srcfile $srcfile2] debug] == -1} {
21    untested "failed to compile"
22    return -1
23}
24
25#
26# start gdb -- start gdb running, default procedure
27#
28proc dbx_gdb_start { } {
29    global prompt
30    global timeout
31    global gdb_spawn_id
32
33    save_vars { GDBFLAGS } {
34	append GDBFLAGS " -dbx"
35
36	set res [gdb_spawn]
37	if { $res != 0} {
38	    return $res
39	}
40    }
41
42    set oldtimeout $timeout
43    set timeout [expr "$timeout + 60"]
44    gdb_expect {
45        -re ".*\r\n$gdb_prompt $" {
46            verbose "GDB initialized."
47        }
48        -re "$prompt $" {
49            perror "GDB never initialized."
50	    unset gdb_spawn_id
51            return -1
52        }
53        timeout         {
54            perror "(timeout) GDB never initialized."
55	    unset gdb_spawn_id
56            return -1
57        }
58    }
59    set timeout $oldtimeout
60    # force the height to "unlimited", so no pagers get used
61    send_gdb "set height 0\n"
62    gdb_expect {
63        -re ".*$prompt $" {
64            verbose "Setting height to 0." 2
65        }
66        timeout {
67            warning "Couldn't set the height to 0."
68        }
69    }
70    # force the width to "unlimited", so no wraparound occurs
71    send_gdb "set width 0\n"
72    gdb_expect {
73        -re ".*$prompt $" {
74            verbose "Setting width to 0." 2
75        }
76        timeout {
77            warning "Couldn't set the width to 0."
78        }
79    }
80}
81
82
83proc dbx_reinitialize_dir { subdir } {
84    global gdb_prompt
85
86   send_gdb "use\n"
87    gdb_expect {
88        -re "Reinitialize source path to empty.*y or n. " {
89            send_gdb "y\n"
90            gdb_expect {
91                -re "Source directories searched.*$gdb_prompt $" {
92                    send_gdb "use $subdir\n"
93                    gdb_expect {
94                        -re "Source directories searched.*$gdb_prompt $" {
95                            verbose "Dir set to $subdir"
96                        }
97                        -re ".*$gdb_prompt $" {
98                            perror "Dir \"$subdir\" failed."
99                        }
100                    }
101                }
102                -re ".*$gdb_prompt $" {
103                    perror "Dir \"$subdir\" failed."
104                }
105            }
106        }
107        -re ".*$gdb_prompt $" {
108            perror "Dir \"$subdir\" failed."
109        }
110    }
111}
112
113# In "testsuite/config/unix-gdb.exp", the routine "gdb_load"
114# is defined as "gdb_file_cmd".  The binding of "gdb_file_cmd"
115# is done at invocation time.  Before this file is processed,
116# it binds to the definition in "testsuite/lib/gdb.exp"; after
117# this file is processed, it binds to this definition.
118# TCL lets us overrides a previous routine definition without a
119# warning (isn't that special?).
120#
121# This means that tests before use "file" to load a target, and
122# tests afterwards use the pair "symbol-file" "exec-file".
123#
124# I'm leaving it as it is for now because at the moment it
125# is the only test we have of the use of the combination of
126# "symbol-file" and "exec-file" to load a debugging target (the
127# other definition uses "file".
128#
129# Symbol-file and exec-file should be tested explicitly, not
130# as a side effect of running a particular test (in this case,
131# "testsuite/gdb.compat/dbx.exp").
132#
133# CM: Renamed the procedure so it does not override the orginal file name.
134#     Having the test suite change behavior depending on the tests run makes
135#     it extremely difficult to reproduce errors. I've also added a
136#     "dbx_gdb_load" procedure.  This and only this test will call these
137#     procedures now. I also added an "expect" to the "send exec-file" line.
138#     The "expect" waits for a prompt to appear. Otherwise, if the tests run
139#     too quickly, the caller could send another command before the prompt
140#     of this command returns, causing the test to get out of sync and fail
141#     seemingly randomly or only on a loaded system.
142#
143# Problem is, though, that the testsuite config files can override the definition of
144# gdb_load (without notice, as was mentioned above). Unfortunately, the gdb_load proc
145# that was copied into this test was a copy of the unix native version.
146#
147# The real problem that we're attempting to solve is how to load an exec and symbol
148# file into gdb for a dbx session. So why not just override gdb_file_cmd with the
149# right sequence of events, allowing gdb_load to do its normal thing? This way
150# remotes and simulators will work, too.
151#
152
153proc local_gdb_file_cmd {arg} {
154    global loadpath
155    global loadfile
156    global GDB
157    global gdb_prompt
158    global spawn_id
159    upvar timeout timeout
160    global last_loaded_file
161
162    set last_loaded_file $arg
163
164    if [is_remote host] {
165        set arg [remote_download host $arg]
166        if { $arg == "" } {
167            error "download failed"
168            return -1
169        }
170    }
171
172    send_gdb "symbol-file $arg\n"
173    gdb_expect {
174        -re "Reading symbols from.*$gdb_prompt $" {
175            verbose "\t\tLoaded $arg into the $GDB"
176            send_gdb "exec-file $arg\n"
177            gdb_expect {
178	        -re "A program is being debugged already.*Kill it.*y or n. $" {
179	            send_gdb "y\n"
180	                verbose "\t\tKilling previous program being debugged"
181	            exp_continue
182	        }
183                -re ".*$gdb_prompt $" {
184                    verbose "\t\tLoaded $arg with new symbol table into $GDB"
185                    return 0
186                }
187                timeout {
188                    perror "(timeout) Couldn't load $arg"
189                    return -1
190                }
191            }
192            return 0
193        }
194        -re "has no symbol-table.*$gdb_prompt $" {
195            perror "$arg wasn't compiled with \"-g\""
196            return -1
197        }
198        -re "Load new symbol table from \".*\".*y or n. $" {
199            send_gdb "y\n"
200	    exp_continue
201        }
202        -re ".*No such file or directory.*$gdb_prompt $" {
203            perror "($arg) No such file or directory\n"
204            return -1
205        }
206        -re "$gdb_prompt $" {
207            perror "couldn't load $arg into $GDB."
208            return -1
209            }
210        timeout {
211            perror "couldn't load $arg into $GDB (timed out)."
212            return -1
213        }
214        eof {
215            # This is an attempt to detect a core dump, but seems not to
216            # work.  Perhaps we need to match .* followed by eof, in which
217            # expect does not seem to have a way to do that.
218            perror "couldn't load $arg into $GDB (end of file)."
219            return -1
220        }
221    }
222}
223
224#
225#test_breakpoints
226#
227proc test_breakpoints { } {
228    set stop_line [gdb_get_line_number "stop-in-main"]
229    gdb_test "stop in main" "Breakpoint.*at.*: file.*average\.c, line $stop_line\."
230    gdb_test "status" "Num.*Type.*Disp.*Enb.*Address.*What\r\n1\[ \r\]+breakpoint\[ \r\]+keep y.*in main at.*average\.c:$stop_line.*"
231    set stop_line [gdb_get_line_number "stop-at-call"]
232    gdb_test "stop at average.c:$stop_line" "Breakpoint.*at.*: file.*average\.c, line $stop_line.*"
233    gdb_test "stop in average.c:$stop_line" "Usage: stop in <function . address>"
234    gdb_test "stop at main" "Usage: stop at LINE"
235}
236
237#
238#test_assign
239#
240proc test_assign { } {
241    global decimal
242    global gdb_prompt
243
244    gdb_run_cmd
245    set test "running to main"
246    gdb_test_multiple "" $test {
247        -re "Break.* at .*:$decimal.*$gdb_prompt $" {
248	    pass $test
249	}
250        -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in .*$gdb_prompt $" {
251	    pass $test
252	}
253    }
254    send_gdb "assign first=1\n"
255    gdb_expect {
256      -re "No symbol \"first\" in current context.*$" { fail "assign first" }
257      -re "$gdb_prompt $" { pass "assign first" }
258      timeout { fail "assign first (timeout)" }
259    }
260    gdb_test "print first" ".1 = 1"
261}
262
263#
264#test_whereis
265#
266proc test_whereis { } {
267    gdb_test "whereis my_list" "All variables matching regular expression \"my_list\":\r\n\r\nFile.*average\.c:\r\n.*\tstatic int my_list\\\[10\\\];"
268}
269
270#
271#test_func
272#
273proc test_func { } {
274    global decimal
275    global srcfile2
276    gdb_test "cont" ".*" "cont 1"
277    gdb_test "step" ".*"
278    gdb_test "func sum" "'sum' not within current stack frame\."
279    set stop_line [gdb_get_line_number "stop-in-sum" $srcfile2]
280    gdb_test "stop in sum" "Breakpoint.*at.*: file.*sum\.c, line $stop_line\."
281    gdb_test "cont" ".*" "cont 2"
282    gdb_test "func print_average" ".*in print_average.*\\(list=.*, low=0, high=6\\).*at.*average\.c:${decimal}\r\n${decimal}\[ \t\]+total = sum\\(list, low, high\\);"
283}
284
285# Start with a fresh gdb.
286
287gdb_exit
288
289with_override gdb_file_cmd local_gdb_file_cmd {
290    save_vars GDBFLAGS {
291	set GDBFLAGS "$GDBFLAGS --dbx"
292
293	gdb_start
294	dbx_reinitialize_dir $srcdir/$subdir
295	gdb_load ${binfile}
296
297	test_breakpoints
298	test_assign
299	test_whereis
300	gdb_test "file average.c:1" "1\[ \t\]+/. This is a sample program.*"
301	test_func
302
303	#exit and cleanup
304	gdb_exit
305    }
306}
307
308return 0
309