diff --git a/gdb/testsuite/gdb.cp/nested-types.exp b/gdb/testsuite/gdb.cp/nested-types.exp
index 2b540e9907..6d387dd6be 100644
--- a/gdb/testsuite/gdb.cp/nested-types.exp
+++ b/gdb/testsuite/gdb.cp/nested-types.exp
@@ -86,6 +86,29 @@ proc qual_name {child parent_list} {
     }
 }
 
+# Output the test source to the log.
+
+proc make_source {} {
+    # Output the structure.
+    test_nested_limit 10 true
+
+    # Output main().
+    send_log "int\nmain \(\)\n\{\n"
+    set plist {}
+    for {set i 10} {$i < 60} {incr i} {
+	if {$i > 10 && [expr {$i % 10}] == 0} {
+	    incr i
+	    set plist {"S10"}
+	    send_log "\n"
+	}
+	send_log "  [qual_name S$i $plist] s$i;\n"
+	lappend plist "S$i"
+    }
+
+    send_log "  return 0;\n"
+    send_log "\}\n"
+}
+
 # Output to the log and/or create the result list for the fields of node ID.
 
 proc make_fields {result_var id parent_list indent_lvl log} {
@@ -159,25 +182,47 @@ proc node_result {result_var id limit parent_list indent_lvl log} {
 
     if {$log} {
 	indent $indent_lvl
-	send_log "struct $s \{\n"
+	send_log "struct $my_name \{\n"
+    } else {
+	# Add this node to the parent list so that its name appears in
+	# qualified names, but only if we are not logging.  Again, ptype and
+	# actual source code are not exactly interchangeable.
+	lappend parent_list "$my_name"
     }
 
-    # Add this node to the parent list so that its name appears in
-    # qualified names.
-    lappend parent_list "$my_name"
-
-    # Output field list to a local children list.
-    set children_list {}
-    make_fields children_list $id $parent_list [expr {$indent_lvl + 1}] $log
-
-    # Output type definitions to the local children list.
-    # The first number of ID gives us the depth of the node.
-    if {[string index $id 1] < $limit || $limit < 0} {
-	if {$log} {
+    # ptype outputs fields before type definitions, but in order to
+    # properly log the struct, we must output these in reverse.
+
+    if {!$log} {
+	# Output field list to a local children list.
+	set children_list {}
+	make_fields children_list $id $parent_list \
+	    [expr {$indent_lvl + 1}] $log
+
+	# Output type definitions to the local children list.
+	# The first number of ID gives us the depth of the node.
+	if {[string index $id 1] < $limit || $limit < 0} {
+	    make_enum children_list $id $parent_list \
+		[expr {$indent_lvl + 1}] $log
+	    make_union children_list $id $parent_list \
+		[expr {$indent_lvl + 1}] $log
+	}
+    } else {
+	# Output type definitions to the local children list.
+	# The first number of ID gives us the depth of the node.
+	if {[string index $id 1] < $limit || $limit < 0} {
+	    make_enum children_list $id $parent_list \
+		[expr {$indent_lvl + 1}] $log
+	    make_union children_list $id $parent_list \
+		[expr {$indent_lvl + 1}] $log
 	    send_log "\n"
 	}
-	make_enum children_list $id $parent_list [expr {$indent_lvl + 1}] $log
-	make_union children_list $id $parent_list [expr {$indent_lvl + 1}] $log
+
+	# Output field list to a local children list.
+	set children_list {}
+	make_fields children_list $id $parent_list \
+	    [expr {$indent_lvl + 1}] $log
+	send_log "\n"
     }
 
     # Output the children to the local children list.
@@ -205,35 +250,42 @@ proc node_result {result_var id limit parent_list indent_lvl log} {
 # Test nested type definitions.  LIMIT specifies how many nested levels
 # of definitions to test.  If LOG is true, output the tree to the log in
 # a human-readable format mimicing the source code.
+#
+# Only test when not logging.  Generating source code usable by the
+# test is not quite the same as how GDB outputs it.
 
 proc test_nested_limit {limit log} {
     set result {}
 
-    # Set the number of nested definitions to print.
-    gdb_test_no_output "set print type nested-type-limit $limit"
+    if {!$log} {
+	# Set the number of nested definitions to print.
+	gdb_test_no_output "set print type nested-type-limit $limit"
 
-    # Check the output of "show type print nested-type-limit"
-    if {$limit < 0} {
-	set lstr "unlimited"
+	# Check the output of "show type print nested-type-limit"
+	if {$limit < 0} {
+	    set lstr "unlimited"
+	} else {
+	    set lstr $limit
+	}
+	gdb_test "show print type nested-type-limit" \
+	    "Will print $lstr nested types defined in a class" \
+	    "show print type nested-type-limit ($limit)"
     } else {
-	set lstr $limit
-    }
-    gdb_test "show print type nested-type-limit" \
-	"Will print $lstr nested types defined in a class" \
-	"show print type nested-type-limit ($limit)"
-
-    # Generate the result list and test it.
-    if {$log} {
 	send_log "Tree to $limit levels:\n"
     }
+
+    # Generate the result list.
     node_result result 10 $limit {} 0 $log
 
-    # The only output we check for is the contents of the struct, ignoring
-    # the leading "type = struct S10 {" and trailing "}" of the outermost node.
-    set result [lindex $result 0]
-    lassign $result type access key name children
-    cp_test_ptype_class $name "ptype $name (limit = $limit)" $key \
-	$name $children
+    if {!$log} {
+	# The only output we check for is the contents of the struct,
+	# ignoring the leading "type = struct S10 {" and trailing "}" of
+	# the outermost node.
+	set result [lindex $result 0]
+	lassign $result type access key name children
+	cp_test_ptype_class $name "ptype $name (limit = $limit)" $key \
+	    $name $children
+    }
 }
 
 # Build a tree of nodes describing the structures in the source file.
@@ -264,4 +316,7 @@ for {set i 1} {$i < 9} {incr i} {
     test_nested_limit $i false
 }
 
+# To output the test code to the log, uncomment the following line:
+#make_source
+
 unset -nocomplain nodes result
