# Copyright (C) 2009, 2010 Free Software Foundation, Inc. # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # This file is part of the GDB testsuite. It tests the mechanism # of exposing types to Python. if $tracelevel then { strace $tracelevel } set testfile "py-type" set srcfile ${testfile}.c set binfile ${objdir}/${subdir}/${testfile} if [get_compiler_info not-used c++] { return -1; } # Build inferior to language specification. proc build_inferior {lang} { global srcdir subdir srcfile binfile testfile hex if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable "debug $lang"] != "" } { untested "Couldn't compile ${srcfile} in $lang mode" return -1 } } # Restart GDB. proc restart_gdb {} { global srcdir subdir srcfile binfile testfile hex gdb_exit gdb_start gdb_reinitialize_dir $srcdir/$subdir gdb_load ${binfile} if ![runto_main ] then { perror "couldn't run to breakpoint" return } } # Set breakpoint and run to that breakpoint. proc runto_bp {bp} { gdb_breakpoint [gdb_get_line_number $bp] gdb_continue_to_breakpoint $bp } # Run a command in GDB, and report a failure if a Python exception is thrown. # If report_pass is true, report a pass if no exception is thrown. proc gdb_py_test_silent_cmd {cmd name report_pass} { global gdb_prompt gdb_test_multiple $cmd $name { -re "Traceback.*$gdb_prompt $" { fail $name } -re "$gdb_prompt $" { if $report_pass { pass $name } } } } proc test_fields {lang} { global gdb_prompt if {$lang == "c++"} { # Test usage with a class gdb_py_test_silent_cmd "print c" "print value" 1 gdb_py_test_silent_cmd "python c = gdb.history (0)" "get value from history" 1 gdb_py_test_silent_cmd "python fields = c.type.fields()" "get fields" 1 gdb_test "python print len(fields)" "2" "Check number of fields" gdb_test "python print fields\[0\].name" "c" "Check class field c name" gdb_test "python print fields\[1\].name" "d" "Check class field d name" gdb_test "python print c.type == gdb.parse_and_eval('d').type" "False" gdb_test "python print c.type == gdb.parse_and_eval('d').type.fields()\[0\].type" \ "True" } # Test normal fields usage in structs. gdb_py_test_silent_cmd "print st" "print value" 1 gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value from history" 1 gdb_py_test_silent_cmd "python fields = st.type.fields()" "get fields" 1 gdb_test "python print len(fields)" "2" "Check number of fields" gdb_test "python print fields\[0\].name" "a" "Check structure field a name" gdb_test "python print fields\[1\].name" "b" "Check structure field b name" # Test regression PR python/10805 gdb_py_test_silent_cmd "print ar" "print value" 1 gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value from history" 1 gdb_test "python fields = ar.type.fields()" gdb_test "python print len(fields)" "1" "Check the number of fields" gdb_test "python print fields\[0\].type" "" "Check array field type" gdb_test "python print ar\[0\].cast(ar\[0\].type.array(1))" \ ".1, 2." "cast to array with one argument" gdb_test "python print ar\[0\].cast(ar\[0\].type.array(0, 1))" \ ".1, 2." "cast to array with two arguments" gdb_test "python print ar\[0\].type == ar\[0\].type" "True" } proc test_base_class {} { gdb_py_test_silent_cmd "print d" "print value" 1 gdb_py_test_silent_cmd "python d = gdb.history (0)" "get value from history" 1 gdb_py_test_silent_cmd "python fields = d.type.fields()" "get value from history" 1 gdb_test "python print len(fields)" "3" "Check the number of fields" gdb_test "python print fields\[0\].is_base_class" "True" "Check base class" gdb_test "python print fields\[1\].is_base_class" "False" "Check base class" } proc test_range {} { # Test a valid range request. gdb_py_test_silent_cmd "print ar" "print value" 1 gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value from history" 1 gdb_test "python print len(ar.type.range())" "2" "Check correct tuple length" gdb_test "python print ar.type.range()\[0\]" "0" "Check low range" gdb_test "python print ar.type.range()\[1\]" "1" "Check high range" # Test a range request on a ranged type. gdb_py_test_silent_cmd "print ar" "print value" 1 gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value from history" 1 gdb_py_test_silent_cmd "python fields = ar.type.fields()" "get fields" 1 gdb_test "python print fields\[0\].type.range()\[0\]" "0" "Check range type low bound" gdb_test "python print fields\[0\].type.range()\[1\]" "1" "Check range type high bound" # Test where a range does not exist. gdb_py_test_silent_cmd "print st" "print value" 1 gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value from history" 1 gdb_test "python print st.type.range()" "RuntimeError: This type does not have a range.*" "Check range for non ranged type." } # Some tests of template arguments. proc test_template {} { gdb_py_test_silent_cmd \ "python ttype = gdb.parse_and_eval('temvar').type" \ "get type of temvar" \ 1 gdb_test "python print ttype.template_argument(0)" "D" gdb_test "python print isinstance(ttype.template_argument(0), gdb.Type)" \ "True" # The next two tests require a GCC that emits DW_TAG_template_*. # GCC 4.4 does not emit it, 4.5 and 6 do emit it. set have_older_gcc 0 if {[test_compiler_info {gcc-[0-3]-*}] || [test_compiler_info {gcc-4-[0-4]-*}]} { set have_older_gcc 1 } if $have_older_gcc { setup_xfail *-*-* } gdb_test "python print ttype.template_argument(1)" "23" if $have_older_gcc { setup_xfail *-*-* } gdb_test "python print isinstance(ttype.template_argument(1), gdb.Value)" \ "True" setup_kfail "gcc/41736" *-*-* gdb_test "python print ttype.template_argument(2)" "&C::c" } # Perform C Tests. build_inferior "c" restart_gdb # Skip all tests if Python scripting is not enabled. if { [skip_python_tests] } { continue } runto_bp "break to inspect struct and array." test_fields "c" # Perform C++ Tests. build_inferior "c++" restart_gdb runto_bp "break to inspect struct and array." test_fields "c++" test_base_class test_range test_template