Add some more casts (1/2)
[external/binutils.git] / gdb / testsuite / gdb.gdb / selftest.exp
1 # Copyright 1988-2015 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 # This file was written by Rob Savoye. (rob@cygnus.com)
17
18 load_lib selftest-support.exp
19
20 # are we on a target board
21 if { [is_remote target] || ![isnative] } then {
22     return
23 }
24
25 # Not all of the lines of code near the start of main are executed for
26 # every machine.  Also, optimization may reorder some of the lines.
27 # So all we do is try to step or next over everything until we get
28 # to a line that we know is always executed.
29
30 proc do_steps_and_nexts {} {
31     global gdb_prompt
32     global srcdir
33  
34     gdb_reinitialize_dir $srcdir/..
35
36     set unlikely_line 0
37     for {set count 0} {$count < 32} {incr count} {
38         # NOTE: carlton/2002-12-11: The "initial brace" and
39         # "current_directory initialization" possibilities happen to
40         # me with GCC 3.1 on i686-pc-linux-gnu when I compile with
41         # optimization.
42         gdb_test_multiple "list" "list" {
43             -re ".*context = \\(struct captured_main_args \\*\\) data.*$gdb_prompt $" {
44                 set description "step over context initialization"
45                 set command "step"
46             }
47             -re ".*argc = context->argc.*$gdb_prompt $" {
48                 set description "step over argc initialization"
49                 set command "step"
50             }
51             -re ".*argv = context->argv.*$gdb_prompt $" {
52                 set description "step over argv initialization"
53                 set command "step"
54             }
55             -re ".*quiet = 0.*$gdb_prompt $" {
56                 set description "step over quiet initialization"
57                 set command "step"
58             }
59             -re ".*batch = 0.*$gdb_prompt $" {
60                 set description "step over batch initialization"
61                 set command "step"
62             }
63             -re ".*symarg = NULL.*$gdb_prompt $" {
64                 set description "step over symarg initialization"
65                 set command "step"
66             }
67             -re ".*execarg = NULL.*$gdb_prompt $" {
68                 set description "step over execarg initialization"
69                 set command "step"
70             }
71             -re ".*pidarg = NULL.*$gdb_prompt $" {
72                 set description "step over pidarg initialization"
73                 set command "step"
74             }
75             -re ".*corearg = NULL.*$gdb_prompt $" {
76                 set description "step over corearg initialization"
77                 set command "step"
78             }
79             -re ".*pid_or_core_arg = NULL.*$gdb_prompt $" {
80                 set description "step over pid_or_core_arg initialization"
81                 set command "step"
82             }
83             -re ".*cdarg = NULL.*$gdb_prompt $" {
84                 set description "step over cdarg initialization"
85                 set command "step"
86             }
87             -re ".*ttyarg = NULL.*$gdb_prompt $" {
88                 set description "step over ttyarg initialization"
89                 set command "step"
90             }
91             -re ".*cmdarg_vec = NULL.*$gdb_prompt $" {
92                 set description "step over cmdarg_vec initialization"
93                 set command "step"
94             }
95             -re ".*pre_stat_chain = make_command_stats_cleanup.*$gdb_prompt $" {
96                 set description "next over make_command_stats_cleanup and everything it calls"
97                 set command "next"
98             }
99             -re ".*START_PROGRESS.*$gdb_prompt $" {
100                 # Note: ezannoni/2004/02/17: This check should be
101                 # removed, since as of today that source line is not
102                 # in gdb anymore.
103                 set description "next over START_PROGRESS and everything it calls"
104                 set command "next"
105             }
106             -re ".*mac_init.*$gdb_prompt $" {
107                 set description "next over mac_init and everything it calls"
108                 set command "next"
109             }
110             -re ".*init_malloc.*$gdb_prompt $" {
111                 # gdb 6.2.X is the last gdb which called init_malloc
112                 set description "next over init_malloc and everything it calls"
113                 set command "next"
114             }
115             -re ".*lim_at_start.*$gdb_prompt $" {
116                 set description "next over lim_at_start initialization"
117                 set command "next"
118             }
119             -re ".*count . 0x3.*$gdb_prompt $" {
120                 set description "next over conditional stack alignment code 1"
121                 set command "next"
122             }
123             -re ".*if .i != 0.*$gdb_prompt $" {
124                 set description "next over conditional stack alignment code 2"
125                 set command "next"
126             }
127             -re ".*alloca .i - 4.*$gdb_prompt $" {
128                 set description "next over conditional stack alignment alloca"
129                 set command "next"
130             }
131             -re ".*dirsize = 1.*$gdb_prompt $" {
132                 set description "next over dirsize initialization"
133                 set command "next"
134             }
135             -re ".*dirarg = .* xmalloc.*$gdb_prompt $" {
136                 return
137             }
138             -re ".*setlocale .LC_MESSAGES,.*$gdb_prompt $" {
139                 set description "next over setlocale LC_MESSAGES"
140                 set command "next"
141             }
142             -re ".*setlocale .LC_CTYPE,.*$gdb_prompt $" {
143                 set description "next over setlocale LC_CTYPE"
144                 set command "next"
145             }
146             -re ".*bindtextdomain .PACKAGE, LOCALEDIR.;.*$gdb_prompt $" {
147                 set description "next over bindtextdomain"
148                 set command "next"
149             }
150             -re ".*textdomain .PACKAGE.;.*$gdb_prompt $" {
151                 set description "next over textdomain PACKAGE"
152                 set command "next"
153             }
154             -re ".*bfd_init ..;.*$gdb_prompt $" {
155                 set description "next over bfd_init"
156                 set command "next"
157             }
158             -re ".*notice_open_fds ..;.*$gdb_prompt $" {
159                 set description "next over notice_open_fds"
160                 set command "next"
161             }
162             -re ".*VEC_cleanup .cmdarg_s.*$gdb_prompt $" {
163                 set description "next over cmdarg_s VEC_cleanup"
164                 set command "next"
165             }
166             -re "\[0-9\]+\[\t \]+\{\r\n$gdb_prompt $" {
167                 set description "step over initial brace"
168                 set command "step"
169             }
170             -re ".*current_directory = gdb_dirbuf.*$gdb_prompt $" {
171                 set description "step over current_directory initialization"
172                 set command "step"
173             }
174             -re ".*gdb_sysroot = .*$gdb_prompt $" {
175                 # NOTE: carlton/2003-01-15: More optimization reordering,
176                 # observed on GCC 3.1.
177                 set description "step over gdb_sysroot initialization"
178                 set command "step"
179             }
180             -re ".*ndir = 0.*$gdb_prompt $" {
181                 set description "step over ndir initialization"
182                 set command "step"
183             }
184             -re ".*instream = stdin.*$gdb_prompt $" {
185                 set description "step over instream initialization"
186                 set command "step"
187             }
188             -re ".*getcwd .gdb_dirbuf, sizeof .gdb_dirbuf.*$gdb_prompt $" {
189                 set description "next over getcwd"
190                 set command "next"
191             }
192             -re ".*gdb_program_name = xstrdup.*$gdb_prompt $" {
193                 set description "next over xstrdup"
194                 set command "next"
195             } 
196             -re ".*quit_flag = 0.*$gdb_prompt $" {
197                 set description "step over quit_flag initialization"
198                 set command "step"
199             }
200             -re ".*gdb_stdout = stdio_fileopen .stdout.;.*$gdb_prompt $" {
201                 set description "step over gdb_stdout initialization"
202                 set command "step"
203             }
204             -re ".*gdb_stderr = stdio_fileopen .stderr.;.*$gdb_prompt $" {
205                 set description "step over gdb_stderr initialization"
206                 set command "step"
207             }
208             -re ".*main.c.*No such file or directory.*$gdb_prompt $" {
209                 setup_xfail "rs6000-*-aix3*"
210                 fail "must be able to list source lines"
211                 return
212             }
213             -re ".*interpreter_p = xstrdup.*$gdb_prompt $" {
214                 if { $unlikely_line == 0 } {
215                     # This is a GCC optimization bug; a constant has been
216                     # associated with the wrong line number.
217                     setup_xfail "*-*-*" gcc/26475
218                     fail "$description (unlikely line from gcc)"
219                     set unlikely_line 1
220                 }
221                 set description "next over xstrdup"
222                 set command "next"
223             }
224             -re ".*$gdb_prompt $" {
225                 # If $description hasn't been set yet, it's likely the file
226                 # has been edited but gdb hasn't been recompiled.
227                 if [info exists description] {
228                     fail "unknown source line (after $description)"
229                 } else {
230                     fail "unknown source line"
231                 }
232                 return
233             }
234             default {
235                 fail "unknown source line near main"
236                 return
237             }
238         }
239         gdb_test_multiple "$command" "$description" {
240             -re ".*No such file or directory.\r\n$gdb_prompt $" {
241                 fail "$description (no source available)"
242             }
243             -re ".*A file or directory .* does not exist..\r\n$gdb_prompt $" {
244                 fail "$description (no source available)"
245             }
246             -re ".*$gdb_prompt $" {
247                 pass "$description"
248             }
249         }
250     }
251 }
252
253 proc test_with_self { executable } {
254     global gdb_prompt
255     global tool
256     global det_file
257     global decimal
258     global timeout
259     global INTERNAL_GDBFLAGS
260     global inferior_spawn_id gdb_spawn_id
261
262     # load yourself into the debugger
263     # This can take a relatively long time, particularly for testing where
264     # the executable is being accessed over a network, or where gdb does not
265     # support partial symbols for a particular target and has to load the
266     # entire symbol table.  Set the timeout to 10 minutes, which should be
267     # adequate for most environments (it *has* timed out with 5 min on a
268     # SPARCstation SLC under moderate load, so this isn't unreasonable).
269     # After gdb is started, set the timeout to 30 seconds for the duration
270     # of this test, and then back to the original value.
271
272     set oldtimeout $timeout
273     set timeout 600
274     verbose "Timeout is now $timeout seconds" 2
275
276     global gdb_file_cmd_debug_info
277     set gdb_file_cmd_debug_info "unset"
278
279     set result [gdb_load $executable]
280     set timeout $oldtimeout
281     verbose "Timeout is now $timeout seconds" 2
282
283     if { $result != 0 } then {
284         return -1
285     }
286
287     if { $gdb_file_cmd_debug_info != "debug" } then {
288         untested "No debug information, skipping testcase."
289         return -1
290     }
291
292     # When GDB is built as a C++ program, disassemble shows the full
293     # prototype.
294     set cxx_main_args_re [string_to_regexp "(int, char**)"]
295
296     # disassemble yourself
297     gdb_test "x/10i main" \
298             "x/10i.*main.*main($cxx_main_args_re)?.$decimal.*main($cxx_main_args_re)?.$decimal.*" \
299             "Disassemble main"
300
301     # Set a breakpoint at main
302     gdb_test "break captured_main" \
303             "Breakpoint.*at.* file.*, line.*" \
304             "breakpoint in captured_main"
305
306     # We'll need this when we send a ^C to GDB.  Need to do it before we
307     # run the program and gdb starts saving and restoring tty states.
308     gdb_test "shell stty intr '^C'" ".*" \
309         "set interrupt character in test_with_self"
310
311     # FIXME: If we put this after the run to main, the first list
312     # command doesn't print the same line as the current line where
313     # gdb is stopped.
314     gdb_test_no_output "set listsize 1" "set listsize to 1"
315
316     # run yourself
317     # It may take a very long time for the inferior gdb to start (lynx),
318     # so we bump it back up for the duration of this command.
319     set timeout 600
320
321     set description "run until breakpoint at captured_main"
322     gdb_test_multiple "run $INTERNAL_GDBFLAGS" "$description" {
323         -re "Starting program.*Breakpoint \[0-9\]+,.*captured_main .data.* at .*main.c:.*$gdb_prompt $" {
324             pass "$description"
325         }
326         -re "Starting program.*Breakpoint \[0-9\]+,.*captured_main .data.*$gdb_prompt $" {
327             xfail "$description (line numbers scrambled?)"
328         }
329         -re "vfork: No more processes.*$gdb_prompt $" {
330             fail "$description (out of virtual memory)"
331             set timeout $oldtimeout
332             verbose "Timeout is now $timeout seconds" 2
333             return -1
334         }
335         -re ".*$gdb_prompt $" {
336             fail "$description"
337             set timeout $oldtimeout
338             verbose "Timeout is now $timeout seconds" 2
339             return -1
340         }
341     }
342
343     set timeout $oldtimeout
344     verbose "Timeout is now $timeout seconds" 2
345
346     # do we have a version number ?
347     gdb_test_multiple "print version" "printed version" {
348         -re ".\[0-9\]+ = .\[0-9.\]+.*$gdb_prompt $" {
349             pass "printed version as string"
350         }
351         -re ".\[0-9\]+ = +0x.*\[0-9.\]+.*$gdb_prompt $" {
352             pass "printed version as pointer"
353         }
354         -re ".\[0-9\]+ = +.+ +0x.*\[0-9.\]+.*$gdb_prompt $" {
355             pass "printed version with cast"
356         }
357     }
358
359     do_steps_and_nexts
360
361     gdb_test "print \"foo\"" ".\[0-9\]+ = \"foo\"" "print a string"
362
363     # do_steps_and_nexts left us ready to execute an xmalloc call,
364     # so give that a try.
365     # If we don't actually enter the xmalloc call when we give a
366     # step command that seems like a genuine bug.  It seems to happen
367     # on most RISC processors.
368     # NOTE drow/2003-06-22: However, if we step back to the preceding two
369     # lines, just keep stepping until we enter.
370     set stepped_back 0
371     setup_xfail "alpha-*-*" "mips-*-*"
372     set description "step into xmalloc call"
373     gdb_test_multiple "step" "$description" {
374         -re "ncmd = 0;.*$gdb_prompt $" {
375             set stepped_back 1
376             send_gdb "step\n"
377             exp_continue
378         }
379         -re ".*cmdarg = .* xmalloc.*$gdb_prompt $" {
380             set stepped_back 1
381             send_gdb "step\n"
382             exp_continue
383         }
384         -re "dirsize = 1;.*$gdb_prompt $" {
385             set stepped_back 1
386             send_gdb "step\n"
387             exp_continue
388         }
389         -re ".*dirarg = .* xmalloc.*$gdb_prompt $" {
390             if { $stepped_back == 1 } {
391                 send_gdb "step\n"
392                 exp_continue
393             } else {
394                 fail "$description"
395             }
396         }
397         -re "xmalloc.*size=.*at.*utils.c.*$gdb_prompt $" {
398             pass "$description"
399         }
400         -re ".*No such file or directory.\r\n$gdb_prompt $" {
401             pass "$description (no source available)"
402         }
403         -re "A file or directory .* does not exist..\r\n$gdb_prompt $" {
404             pass "$description (no source available)"
405         }
406     }
407
408     # start the "xgdb" process
409     if [target_info exists gdb,noinferiorio] {
410         # With no way to interact with the inferior GDB, all we can do
411         # is let it run.
412         send_gdb "continue\n"
413         # Wait a bit while the inferior gdb gets to its prompt.
414         sleep 1
415     } else {
416         set test "xgdb is at prompt"
417         gdb_test_multiple "continue" $test {
418             -i "$inferior_spawn_id"
419             -re "GNU gdb \[0-9\.\]*.*Copyright \\(C\\) \[0-9\]* Free Software Foundation, Inc.*License GPLv3\\+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.*This is free software: you are free to change and redistribute it.*There is NO WARRANTY, to the extent permitted by law.  Type \"show copying\".*and \"show warranty\" for details.*This GDB was configured as .*$gdb_prompt $" {
420                 pass $test
421             }
422         }
423
424         # set xgdb prompt so we can tell which is which
425         send_inferior "set prompt (xgdb) \n"
426         set msg "Set xgdb_prompt"
427         gdb_test_multiple "" $msg {
428             -i "$inferior_spawn_id"
429             -re "\[(\]xgdb\[)\].*\[(\]xgdb\[)\] $" {
430                 pass $msg
431             }
432         }
433     }
434
435     # kill the xgdb process
436     set description "send ^C to child process"
437     send_gdb "\003"
438     gdb_expect {
439         -re "Program received signal SIGINT.*$gdb_prompt $" {
440             pass "$description"
441         }
442         -re ".*$gdb_prompt $" {
443             fail "$description"
444         }
445         timeout {
446             fail "$description (timeout)"
447         }
448     }
449     
450     set description "send SIGINT signal to child process"
451     gdb_test "signal SIGINT" \
452         "Continuing with signal SIGINT.*" \
453         "$description"
454     
455     # get a stack trace
456     #
457     # This fails on some linux systems for unknown reasons.  On the
458     # systems where it fails, sometimes it works fine when run manually.
459     # The testsuite failures may not be limited to just aout systems.
460     setup_xfail "i*86-pc-linuxaout-gnu"
461     set description "backtrace through signal handler"
462     gdb_test_multiple "backtrace" "$description" {
463         -re "#0.*(read|poll).*in main \\(.*\\) at .*gdb\\.c.*$gdb_prompt $" {
464             pass "$description"
465         }
466     }
467
468
469     # Set the timeout back to the value it had when we were called.
470     set timeout $oldtimeout
471     verbose "Timeout is now $timeout seconds" 2
472
473     # Restart gdb in case next test expects it to be started already.
474     return 0
475 }
476
477 # Run the test with self.
478 # Copy the file executable file in case this OS doesn't like to edit its own
479 # text space.
480
481 set GDB_FULLPATH [find_gdb $GDB]
482
483 if {[is_remote host]} {
484     set xgdb x$tool
485 } else {
486     set xgdb [standard_output_file x$tool]
487 }
488
489 # Remove any old copy lying around.
490 remote_file host delete $xgdb
491
492 gdb_start
493 set file [remote_download host $GDB_FULLPATH $xgdb]
494 set result [test_with_self $file]
495 gdb_exit
496 catch "remote_file host delete $file"
497
498 if {$result <0} then {
499     warning "Couldn't test self"
500     return -1
501 }