]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - gdb/testsuite/gdb.python/py-prettyprint.exp
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.python / py-prettyprint.exp
index 7eda415a2d3b9b962dceb20114445690c09b08ae..bdd92ec4bfd1ac9aa8273b56c305b091ac346f7d 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
+# Copyright (C) 2008-2023 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
 # This file is part of the GDB testsuite.  It tests Python-based
 # pretty-printing for the CLI.
 
-if $tracelevel then {
-    strace $tracelevel
-}
-
 load_lib gdb-python.exp
 
-set testfile "py-prettyprint"
-set srcfile ${testfile}.c
-set binfile ${objdir}/${subdir}/${testfile}
+standard_testfile
 
 # Start with a fresh gdb.
 gdb_exit
@@ -33,10 +27,10 @@ gdb_start
 # Skip all tests if Python scripting is not enabled.
 if { [skip_python_tests] } { continue }
 
-proc run_lang_tests {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"
+proc run_lang_tests {exefile lang} {
+    global srcdir subdir srcfile testfile hex
+    if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${exefile}" executable "debug $lang"] != "" } {
+       untested "failed to compile in $lang mode"
        return -1
     }
 
@@ -46,22 +40,22 @@ proc run_lang_tests {lang} {
     gdb_exit
     gdb_start
     gdb_reinitialize_dir $srcdir/$subdir
-    gdb_load ${binfile}
+    gdb_load ${exefile}
 
-    if ![runto_main ] then {
-       perror "couldn't run to breakpoint"
+    if {![runto_main]} {
        return
     }
 
     gdb_test_no_output "set print pretty on"
 
-    gdb_test "b [gdb_get_line_number {break to inspect} ${testfile}.c ]" \
-       ".*Breakpoint.*"
-    gdb_test "continue" ".*Breakpoint.*"
+    gdb_breakpoint [gdb_get_line_number "break to inspect" ${testfile}.c]
+    gdb_continue_to_breakpoint "break to inspect" ".*break to inspect.*"
 
-    set remote_python_file [remote_download host ${srcdir}/${subdir}/${testfile}.py]
 
-    gdb_test_no_output "python execfile ('${remote_python_file}')"
+    set remote_python_file [gdb_remote_download host \
+                               ${srcdir}/${subdir}/${testfile}.py]
+
+    gdb_test_no_output "source ${remote_python_file}" "load python file"
     
     gdb_test "print ss" " = a=< a=<1> b=<$hex>> b=< a=<2> b=<$hex>>"
     gdb_test "print ssa\[1\]" " = a=< a=<5> b=<$hex>> b=< a=<6> b=<$hex>>"
@@ -69,6 +63,10 @@ proc run_lang_tests {lang} {
     
     gdb_test "print arraystruct" " = {$nl *y = 7, *$nl *x = { a=<23> b=<$hex>,  a=<24> b=<$hex>} *$nl *}"
 
+    # Test that when a pretty-printer returns a gdb.Value in its to_string, we
+    # call the pretty-printer of that value too.
+    gdb_test "print tsrvw" " = Inner to_string 1989"
+
     if {$lang == "c++"} {
        gdb_test "print cps" "=  a=<8> b=<$hex>"
        gdb_test "print cpss" " = {$nl *zss = 9, *$nl *s =  a=<10> b=<$hex>$nl}"
@@ -79,60 +77,142 @@ proc run_lang_tests {lang} {
        gdb_test "print ref" "= a=<15> b=< a=<8> b=<$hex>>"
        gdb_test "print derived" \
            " = \{.*<Vbase1> = pp class name: Vbase1.*<Vbase2> = \{.*<VirtualTest> = pp value variable is: 1,.*members of Vbase2:.*_vptr.Vbase2 = $hex.*<Vbase3> = \{.*members of Vbase3.*members of Derived:.*value = 2.*"
-       gdb_test "print ns " "\"embedded\\\\000null\\\\000string\""
-       gdb_py_test_silent_cmd "set print elements 3" "" 1
-       gdb_test "print ns" "emb\.\.\.."
-       gdb_py_test_silent_cmd "set print elements 10" "" 1
-       gdb_test "print ns" "embedded\\\\000n\.\.\.."
-       gdb_py_test_silent_cmd "set print elements 200" "" 1
+       gdb_test "print ns " "\"embedded\\\\000null\\\\000string\"" \
+           "print ns with default element limit"
+       gdb_test_no_output "set print elements 3"
+       gdb_test "print ns" "emb\.\.\.." \
+           "print ns with element limit of 3"
+       gdb_test_no_output "set print elements 10"
+       gdb_test "print ns" "embedded\\\\000n\.\.\.." \
+           "print ns with element limit of 10"
+       gdb_test_no_output "set print elements 200"
     }
 
-    gdb_test "print ns2" ".error reading variable: Address 0x0 out of bounds."
+    if { ![is_address_zero_readable] } {
+       gdb_test "print ns2" \
+           ".error reading variable: Cannot access memory at address 0x0."
+    }
 
     gdb_test "print x" " = \"this is x\""
     gdb_test "print cstring" " = \"const string\""
 
-    gdb_test "print estring" "\"embedded x\\\\201\\\\202\\\\203\\\\204\""
+    gdb_test "print estring" " = \"embedded x\\\\201\\\\202\\\\203\\\\204\""
+    if { ![is_address_zero_readable] } {
+           gdb_test "print estring3" \
+               " = <error reading variable: Cannot create a lazy string with address 0x0, and a non-zero length.>"
+    }
 
     gdb_test_no_output "python pp_ls_encoding = 'UTF-8'"
     gdb_test "print estring2" "\"embedded \", <incomplete sequence \\\\302>"
 
-    gdb_test "print c" " = container \"container\" with 2 elements = {$nl *.0. = 23,$nl *.1. = 72$nl}"
+    gdb_test_no_output "set python print-stack full"
+    gdb_test "print hint_error" "Exception: hint failed\r\nhint_error_val"
+
+    gdb_test "print c" " = container \"container\" with 2 elements = {$nl *.0. = 23,$nl *.1. = 72$nl}" \
+       "print c, pretty printing on, default display hint"
+
+    gdb_test_no_output "set variable c.is_array_p=1"
+    gdb_test "print c" " = container \"container\" with 2 elements = \\{23, 72\\}" \
+       "print c, pretty printing on, display hint is now array"
+
+    # Setting is_map_p while _is_array_p is also set will cause the
+    # display_hint method to raise an exception (see py-prettyprint.py).
+    gdb_test_no_output "set variable c.is_map_p=1"
+
+    # This test checks what happens when the display hint throws an
+    # error.  GDB just treats this as though the display hint was
+    # None.
+    set py_exception \
+       [multi_line \
+            "Traceback\[^\r\n\]+" \
+            "\\s+File \"\[^\r\n\]+/py-prettyprint.py\", line \[0-9\]+, in display_hint" \
+            "\\s+raise Exception\[^\r\n\]+" \
+            "Exception: invalid object state found in display_hint"]
+    gdb_test "print c" \
+       [multi_line \
+            " = ${py_exception}" \
+            "container \"container\" with 2 elements = {" \
+            "\\s+\\\[0\\\] = 23," \
+            "\\s+\\\[1\\\] = 72" \
+            "}"] \
+       "print c, pretty printing on, exception raised from display_hint"
+
+    # Unset is_array_p so that display_hint no longer raises an
+    # exception.
+    gdb_test_no_output "set variable c.is_array_p=0"
+
+    gdb_test "print c" " = container \"container\" with 2 elements = \{$nl  \\\[23\\\] = 72$nl\}" \
+       "print c, pretty printing on, display hint is now map"
 
     gdb_test "print nstype" " = {$nl *.0. = 7,$nl *.1. = 42$nl}"
-    gdb_test "continue" "Program exited normally\."
 
-    remote_file host delete ${remote_python_file}
-}
+    gdb_test_no_output "set print pretty off"
+    gdb_test "print nstype" " = {.0. = 7, .1. = 42}" \
+       "print nstype on one line"
 
-run_lang_tests "c"
-run_lang_tests "c++"
+    # Now we have pretty printing turned off, try printing 'c' again.
+    gdb_test "print c" " = container \"container\" with 2 elements = \{\\\[23\\\] = 72\}" \
+       "print c, pretty printing off, display hint is now map"
 
-# Run various other tests.
+    gdb_test_no_output "set variable c.is_map_p=0"
+    gdb_test "print c" " = container \"container\" with 2 elements = \{\\\[0\\\] = 23, \\\[1\\\] = 72\}" \
+       "print c, pretty printing off, default display hint"
 
-if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable "debug"] != "" } {
-    untested "Couldn't compile ${srcfile}"
-    return -1
+    # Check that GDB doesn't lose typedefs when looking for a printer.
+    gdb_test "print an_int" " = -1"
+    gdb_test "print (int) an_int" " = -1"
+    gdb_test "print (int_type) an_int" " = type=int_type, val=-1"
+
+    gdb_test "print an_int_type" " = type=int_type, val=1"
+    gdb_test "print (int_type) an_int_type" " = type=int_type, val=1"
+
+    gdb_test "print an_int_type2" " = type=int_type2, val=2"
+    gdb_test "print (int) an_int_type2" " = 2"
+    gdb_test "print (int_type) an_int_type2" " = type=int_type, val=2"
+    gdb_test "print (int_type2) an_int_type2" " = type=int_type2, val=2"
+
+    # PR python/16047: it is ok for a pretty printer not to have a
+    # to_string method.
+    gdb_test "print (int_type3) an_int_type2" " = {s = 27}"
+
+    gdb_continue_to_end
+}
+
+with_test_prefix c {
+    if { [run_lang_tests "${binfile}" "c"] == -1 } {
+       return
+    }
+}
+with_test_prefix c++ {
+    if { [run_lang_tests "${binfile}-cxx" "c++"] == -1 } {
+       return
+    }
 }
 
+# Run various other tests.
+
 # Start with a fresh gdb.
 gdb_exit
 gdb_start
 gdb_reinitialize_dir $srcdir/$subdir
 gdb_load ${binfile}
 
-if ![runto_main ] then {
-    perror "couldn't run to breakpoint"
+if {![runto_main]} {
     return
 }
 
-gdb_test "b [gdb_get_line_number {break to inspect} ${testfile}.c ]" \
-    ".*Breakpoint.*"
-gdb_test "continue" ".*Breakpoint.*"
+set remote_python_file [gdb_remote_download host \
+                           ${srcdir}/${subdir}/${testfile}.py]
+
+gdb_test_no_output "source ${remote_python_file}" "load python file"
+
+gdb_breakpoint [gdb_get_line_number "eval-break"]
+gdb_continue_to_breakpoint "eval-break" ".* eval-break .*"
 
-set remote_python_file [remote_download host ${srcdir}/${subdir}/${testfile}.py]
+gdb_test "info locals" "eval9 = eval=<123456789>"
 
-gdb_test_no_output "python execfile ('${remote_python_file}')"
+gdb_breakpoint [gdb_get_line_number "break to inspect" ${testfile}.c ]
+gdb_continue_to_breakpoint "break to inspect" ".*break to inspect.*"
 
 gdb_test "print ss" " = a=< a=<1> b=<$hex>> b=< a=<2> b=<$hex>>" \
     "print ss enabled #1"
@@ -147,4 +227,23 @@ gdb_test_no_output "python enable_lookup_function ()"
 gdb_test "print ss" " = a=< a=<1> b=<$hex>> b=< a=<2> b=<$hex>>" \
     "print ss enabled #2"
 
-remote_file host delete ${remote_python_file}
+gdb_test "print -raw-values -- ss" " = {a = {a = 1, b = $hex}, b = {a = 2, b = $hex}}" \
+    "print -raw-values -- ss"
+
+gdb_test "print -raw-values on -- ss" " = {a = {a = 1, b = $hex}, b = {a = 2, b = $hex}}" \
+    "print -raw-values on -- ss"
+
+gdb_test "with print raw-values -- print ss" " = {a = {a = 1, b = $hex}, b = {a = 2, b = $hex}}" \
+    "with print raw-values -- print ss"
+
+# Test interaction between /r format and raw-values option:
+#   When /r is not present, raw-values option tells to bypass (or not) the pretty printers.
+#     (these cases are tested above).
+#   When /r is present, it must override the option raw-values off.
+gdb_test "print /r ss" " = {a = {a = 1, b = $hex}, b = {a = 2, b = $hex}}"
+
+gdb_test "with print raw-values off -- print /r ss" " = {a = {a = 1, b = $hex}, b = {a = 2, b = $hex}}"
+
+gdb_test "print -raw-values off -- /r ss" " = {a = {a = 1, b = $hex}, b = {a = 2, b = $hex}}"
+
+