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