Fix previous check-in.
[external/binutils.git] / gdb / testsuite / lib / cell.exp
1 # Copyright 2009 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 # Contributed by Markus Deuling <deuling@de.ibm.com>.
17 #
18 # Support library for testing the combined debugger for Linux
19 # on the Cell Broadband Engine.
20
21 # Compile SPU objects.
22 proc gdb_compile_cell_spu {source dest type options} {
23   global board
24
25   # Save and unset multilib flags; these are not appropriate
26   # for the SPU compiler.
27   set board [target_info name]
28   set save_multilib_flag [board_info $board multilib_flags]
29   unset_board_info "multilib_flags"
30
31   set options_spu [concat $options [list compiler=spu-gcc]]
32   set ccout [gdb_compile $source $dest $type $options_spu]
33
34   set_board_info multilib_flags $save_multilib_flag
35   return $ccout
36 }
37
38 # Compile PPU objects.  This is just like gdb_compile_pthreads, except that we
39 # always add the libspe2 library for compiling Cell/B.E. programs.
40 proc gdb_compile_cell_ppu {source dest type options} {
41     # We do not need to try multiple names for the pthread library
42     # -lpthread works on all Cell/B.E. systems
43     set lib "-lspe2 -lpthread"
44     set options_ppu [concat $options [list libs=$lib]]
45     return [gdb_compile $source $dest $type $options_ppu]
46 }
47
48 # Embed SPU executable into a PPU object.
49 proc gdb_cell_embedspu {source dest options} {
50     global CC_FOR_TARGET
51
52     if [info exists CC_FOR_TARGET] {
53         set compiler $CC_FOR_TARGET
54     } else {
55         set compiler [board_info [target_info name] compiler]
56     }
57
58     # We assume the PPU compiler is called gcc or ppu-gcc,
59     # and find the appropriate embedspu based on that.
60     regsub gcc "$compiler" embedspu embedspu
61
62     # Determine default embedded symbol name from source filename.
63     set path [split "$source" /]
64     set filename [lindex $path [expr [llength $path] - 1]]
65     regsub -all -- "\[-\.\]" "$filename" "_" symbol
66
67     set options_embed [concat $options [list compiler=$embedspu]]
68     return [gdb_compile "$symbol $source $dest" "" none $options_embed]
69 }
70
71 # Run a test on the target to see if it supports Cell/B.E. hardware.
72 # Return 0 if so, 1 if it does not.
73 proc skip_cell_tests {} {
74     global skip_cell_tests_saved
75     global srcdir subdir gdb_prompt
76
77     # Use the cached value, if it exists.
78     set me "skip_cell_tests"
79     if [info exists skip_cell_tests_saved] {
80         verbose "$me:  returning saved $skip_cell_tests_saved" 2
81         return $skip_cell_tests_saved
82     }
83
84     # Set up, compile, and execute a combined Cell/B.E. test program.
85     # Include the current process ID in the file names to prevent conflicts
86     # with invocations for multiple testsuites.
87     set src cell[pid].c
88     set exe cell[pid].x
89     set src_spu cell[pid]-spu.c
90     set exe_spu cell[pid]-spu.x
91
92     set f [open $src "w"]
93     puts $f "#include <libspe2.h>"
94     puts $f "extern spe_program_handle_t cell[pid]_spu_x;"
95     puts $f "int main (void) {"
96     puts $f "unsigned int entry = SPE_DEFAULT_ENTRY;"
97     puts $f "spe_context_ptr_t ctx = spe_context_create (0, NULL);"
98     puts $f "spe_program_load (ctx, &cell[pid]_spu_x);"
99     puts $f "return spe_context_run (ctx, &entry, 0, NULL, NULL, NULL); }"
100     close $f
101
102     set f [open $src_spu "w"]
103     puts $f "int main (void) { return 0; }"
104     close $f
105
106     verbose "$me:  compiling testfile $src" 2
107     set compile_flags {debug nowarnings quiet}
108
109     if { [gdb_compile_cell_spu $src_spu $exe_spu executable $compile_flags] != "" } {
110         verbose "$me:  compiling spu binary failed, returning 1" 2
111         return [set skip_cell_tests_saved 1]
112     }
113     if { [gdb_cell_embedspu $exe_spu $exe_spu-embed.o $compile_flags]  != "" } {
114         verbose "$me:  embedding spu binary failed, returning 1" 2
115         return [set skip_cell_tests_saved 1]
116     }
117     if { [gdb_compile_cell_ppu [list $src $exe_spu-embed.o] $exe executable $compile_flags] != "" } {
118         verbose "$me:  compiling ppu binary failed, returning 1" 2
119         return [set skip_cell_tests_saved 1]
120     }
121     file delete $src
122     file delete $src_spu
123     file delete $exe_spu
124     file delete $exe_spu-embed.o
125
126     # Compilation succeeded so now run it via gdb.
127
128     gdb_exit
129     gdb_start
130     gdb_reinitialize_dir $srcdir/$subdir
131     gdb_load "$exe"
132     gdb_run_cmd
133     gdb_expect {
134         -re ".*Program exited normally.*${gdb_prompt} $" {
135             verbose -log "\n$me: Cell/B.E. hardware detected"
136             set skip_cell_tests_saved 0
137         }
138         -re ".*Program exited with code.*${gdb_prompt} $" {
139             verbose -log "\n$me: Cell/B.E. hardware not detected"
140             set skip_cell_tests_saved 1
141         }
142         default {
143             verbose -log "\n$me Cell/B.E. hardware not detected (default case)"
144             set skip_cell_tests_saved 1
145         }
146     }
147     gdb_exit
148     remote_file build delete $exe
149
150     verbose "$me:  returning $skip_cell_tests_saved" 2
151     return $skip_cell_tests_saved
152 }
153
154 # Delete all breakpoints and stop on the next new SPU thread
155 proc cont_spu_main { } {
156   delete_breakpoints
157   gdb_test "set spu stop-on-load on"
158   gdb_test "continue" \
159            "Continuing.*Temporary breakpoint .*main .*" \
160            "continue to SPU main"
161 }
162
163 # Continue to MARKER
164 proc c_to { marker srcfile } {
165   set line [gdb_get_line_number $marker $srcfile]
166   gdb_test "break $line" \
167            "Breakpoint.*at.*file.*$srcfile.*line $line.*" \
168            "break $line"
169   gdb_test "continue" \
170            "Continuing.*Breakpoint.*at.*$srcfile.*$line.*" \
171            "continue to $line"
172 }
173
174 # Check if the current thread has SPU architecture
175 proc check_spu_arch { msg } {
176   if { $msg == "" } {
177     set msg "spu architecture is spu256K"
178   }
179   gdb_test "show architecture" \
180            "The target architecture is set automatically.*currently spu:256K.*" \
181            $msg
182 }