From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 23727 invoked by alias); 23 Jun 2018 20:22:34 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 23588 invoked by uid 89); 23 Jun 2018 20:22:34 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-24.7 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_NONE,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=Union, type_name, TYPE_NAME, quit X-HELO: gateway21.websitewelcome.com Received: from gateway21.websitewelcome.com (HELO gateway21.websitewelcome.com) (192.185.46.113) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 23 Jun 2018 20:22:32 +0000 Received: from cm16.websitewelcome.com (cm16.websitewelcome.com [100.42.49.19]) by gateway21.websitewelcome.com (Postfix) with ESMTP id E1C83400C80C6 for ; Sat, 23 Jun 2018 15:22:30 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id Wp3Gf6MOeaSeyWp3Gfmktk; Sat, 23 Jun 2018 15:22:30 -0500 X-Authority-Reason: nr=8 Received: from 75-166-79-120.hlrn.qwest.net ([75.166.79.120]:40170 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.91) (envelope-from ) id 1fWp3G-0012UF-N7; Sat, 23 Jun 2018 15:22:30 -0500 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFA 2/2] Support ptype/o in Rust Date: Sat, 23 Jun 2018 20:22:00 -0000 Message-Id: <20180623202227.17259-3-tom@tromey.com> In-Reply-To: <20180623202227.17259-1-tom@tromey.com> References: <20180623202227.17259-1-tom@tromey.com> X-BWhitelist: no X-Source-L: No X-Exim-ID: 1fWp3G-0012UF-N7 X-Source-Sender: 75-166-79-120.hlrn.qwest.net (bapiya.Home) [75.166.79.120]:40170 X-Source-Auth: tom+tromey.com X-Email-Count: 3 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-SW-Source: 2018-06/txt/msg00556.txt.bz2 This adds support for ptype/o to the Rust language code. By default, the Rust compiler reorders fields to reduce padding. So, the Rust language code sorts the fields by offset before printing. This may yield somewhat odd-looking results, but it is faithful to "what really happens", and might be useful when doing lower-level debugging. The reordering can be disabled using #[repr(c)]; ptype/o might be more useful in this case. gdb/ChangeLog 2018-06-23 Tom Tromey PR rust/22574: * typeprint.c (whatis_exp): Allow ptype/o for Rust. * rust-lang.c (rust_print_struct_def): Add podata parameter. Update. (rust_internal_print_type): Add podata parameter. (rust_print_type): Update. gdb/testsuite/ChangeLog 2018-06-23 Tom Tromey PR rust/22574: * gdb.rust/simple.exp (test_one_slice): Add ptype/o tests. * gdb.rust/simple.rs (struct SimpleLayout): New. --- gdb/ChangeLog | 9 +++++ gdb/rust-lang.c | 82 ++++++++++++++++++++++++++++++--------- gdb/testsuite/ChangeLog | 6 +++ gdb/testsuite/gdb.rust/simple.exp | 18 +++++++++ gdb/testsuite/gdb.rust/simple.rs | 8 ++++ gdb/typeprint.c | 3 +- 6 files changed, 106 insertions(+), 20 deletions(-) diff --git a/gdb/rust-lang.c b/gdb/rust-lang.c index d9807d0ac1a..af1144dde07 100644 --- a/gdb/rust-lang.c +++ b/gdb/rust-lang.c @@ -31,8 +31,10 @@ #include "objfiles.h" #include "psymtab.h" #include "rust-lang.h" +#include "typeprint.h" #include "valprint.h" #include "varobj.h" +#include #include #include @@ -616,14 +618,14 @@ static void rust_internal_print_type (struct type *type, const char *varstring, struct ui_file *stream, int show, int level, const struct type_print_options *flags, - bool for_rust_enum); + bool for_rust_enum, print_offset_data *podata); /* Print a struct or union typedef. */ static void rust_print_struct_def (struct type *type, const char *varstring, struct ui_file *stream, int show, int level, const struct type_print_options *flags, - bool for_rust_enum) + bool for_rust_enum, print_offset_data *podata) { /* Print a tuple type simply. */ if (rust_tuple_type_p (type)) @@ -636,6 +638,13 @@ rust_print_struct_def (struct type *type, const char *varstring, if (TYPE_N_BASECLASSES (type) > 0) c_print_type (type, varstring, stream, show, level, flags); + if (flags->print_offsets) + { + /* Temporarily bump the level so that the output lines up + correctly. */ + level += 2; + } + /* Compute properties of TYPE here because, in the enum case, the rest of the code ends up looking only at the variant part. */ const char *tagname = TYPE_NAME (type); @@ -674,16 +683,41 @@ rust_print_struct_def (struct type *type, const char *varstring, if (TYPE_NFIELDS (type) == 0 && !is_tuple) return; - if (for_rust_enum) + if (for_rust_enum && !flags->print_offsets) fputs_filtered (is_tuple_struct ? "(" : "{", stream); else fputs_filtered (is_tuple_struct ? " (\n" : " {\n", stream); + // When printing offsets, we rearrange the fields into storage + // order. This lets us show holes more clearly. We work using + // field indices here because it simplifies calls to + // print_offset_data::update below. + std::vector fields; for (int i = 0; i < TYPE_NFIELDS (type); ++i) { - QUIT; if (field_is_static (&TYPE_FIELD (type, i))) continue; + if (is_enum && i == enum_discriminant_index) + continue; + fields.push_back (i); + } + if (flags->print_offsets) + std::sort (fields.begin (), fields.end (), + [&] (int a, int b) + { + return (TYPE_FIELD_BITPOS (type, a) + < TYPE_FIELD_BITPOS (type, b)); + }); + + for (int i : fields) + { + QUIT; + + gdb_assert (!field_is_static (&TYPE_FIELD (type, i))); + gdb_assert (! (is_enum && i == enum_discriminant_index)); + + if (flags->print_offsets) + podata->update (type, i, stream); /* We'd like to print "pub" here as needed, but rustc doesn't emit the debuginfo, and our types don't have @@ -691,27 +725,35 @@ rust_print_struct_def (struct type *type, const char *varstring, /* For a tuple struct we print the type but nothing else. */ - if (!for_rust_enum) + if (!for_rust_enum || flags->print_offsets) print_spaces_filtered (level + 2, stream); if (is_enum) - { - if (i == enum_discriminant_index) - continue; - fputs_filtered (TYPE_FIELD_NAME (type, i), stream); - } + fputs_filtered (TYPE_FIELD_NAME (type, i), stream); else if (!is_tuple_struct) fprintf_filtered (stream, "%s: ", TYPE_FIELD_NAME (type, i)); rust_internal_print_type (TYPE_FIELD_TYPE (type, i), NULL, stream, (is_enum ? show : show - 1), - level + 2, flags, is_enum); - if (!for_rust_enum) + level + 2, flags, is_enum, podata); + if (!for_rust_enum || flags->print_offsets) fputs_filtered (",\n", stream); + /* Note that this check of "I" is ok because we only sorted the + fields by offset when print_offsets was set, so we won't take + this branch in that case. */ else if (i + 1 < TYPE_NFIELDS (type)) fputs_filtered (", ", stream); } - if (!for_rust_enum) + if (flags->print_offsets) + { + /* Undo the temporary level increase we did above. */ + level -= 2; + podata->finish (type, level, stream); + print_spaces_filtered (print_offset_data::indentation, stream); + if (level == 0) + print_spaces_filtered (2, stream); + } + if (!for_rust_enum || flags->print_offsets) print_spaces_filtered (level, stream); fputs_filtered (is_tuple_struct ? ")" : "}", stream); } @@ -735,7 +777,7 @@ static void rust_internal_print_type (struct type *type, const char *varstring, struct ui_file *stream, int show, int level, const struct type_print_options *flags, - bool for_rust_enum) + bool for_rust_enum, print_offset_data *podata) { int i; @@ -778,7 +820,7 @@ rust_internal_print_type (struct type *type, const char *varstring, if (i > 0) fputs_filtered (", ", stream); rust_internal_print_type (TYPE_FIELD_TYPE (type, i), "", stream, - -1, 0, flags, false); + -1, 0, flags, false, podata); } fputs_filtered (")", stream); /* If it returns unit, we can omit the return type. */ @@ -786,7 +828,7 @@ rust_internal_print_type (struct type *type, const char *varstring, { fputs_filtered (" -> ", stream); rust_internal_print_type (TYPE_TARGET_TYPE (type), "", stream, - -1, 0, flags, false); + -1, 0, flags, false, podata); } break; @@ -796,7 +838,8 @@ rust_internal_print_type (struct type *type, const char *varstring, fputs_filtered ("[", stream); rust_internal_print_type (TYPE_TARGET_TYPE (type), NULL, - stream, show - 1, level, flags, false); + stream, show - 1, level, flags, false, + podata); if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCEXPR || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCLIST) @@ -811,7 +854,7 @@ rust_internal_print_type (struct type *type, const char *varstring, case TYPE_CODE_UNION: case TYPE_CODE_STRUCT: rust_print_struct_def (type, varstring, stream, show, level, flags, - for_rust_enum); + for_rust_enum, podata); break; case TYPE_CODE_ENUM: @@ -856,8 +899,9 @@ rust_print_type (struct type *type, const char *varstring, struct ui_file *stream, int show, int level, const struct type_print_options *flags) { + print_offset_data podata; rust_internal_print_type (type, varstring, stream, show, level, - flags, false); + flags, false, &podata); } diff --git a/gdb/testsuite/gdb.rust/simple.exp b/gdb/testsuite/gdb.rust/simple.exp index ba90e061ce3..20fe8dd0a88 100644 --- a/gdb/testsuite/gdb.rust/simple.exp +++ b/gdb/testsuite/gdb.rust/simple.exp @@ -285,6 +285,24 @@ gdb_test "print parametrized" \ gdb_test "print u" " = simple::Union {f1: -1, f2: 255}" +gdb_test_sequence "ptype/o Union" "" { + "/\\* offset | size \\*/ type = union simple::Union {" + "/\\* 1 \\*/ f1: i8," + "/\\* 1 \\*/ f2: u8," + "" + " /\\* total size \\(bytes\\): 1 \\*/" + " }" +} + +gdb_test_sequence "ptype/o SimpleLayout" "" { + "/\\* offset | size \\*/ type = struct simple::SimpleLayout {" + "/\\* 0 | 2 \\*/ f1: u16," + "/\\* 2 | 2 \\*/ f2: u16," + "" + " /\\* total size \\(bytes\\): 4 \\*/" + " }" +} + load_lib gdb-python.exp if {[skip_python_tests]} { continue diff --git a/gdb/testsuite/gdb.rust/simple.rs b/gdb/testsuite/gdb.rust/simple.rs index e5bbe521226..9d89361b753 100644 --- a/gdb/testsuite/gdb.rust/simple.rs +++ b/gdb/testsuite/gdb.rust/simple.rs @@ -85,6 +85,13 @@ union Union { f2: u8, } +// A simple structure whose layout won't be changed by the compiler, +// so that ptype/o testing will work on any platform. +struct SimpleLayout { + f1: u16, + f2: u16 +} + fn main () { let a = (); let b : [i32; 0] = []; @@ -159,6 +166,7 @@ fn main () { }; let u = Union { f2: 255 }; + let v = SimpleLayout { f1: 8, f2: 9 }; println!("{}, {}", x.0, x.1); // set breakpoint here println!("{}", diff2(92, 45)); diff --git a/gdb/typeprint.c b/gdb/typeprint.c index 66ba0a87c6a..c6a404665ba 100644 --- a/gdb/typeprint.c +++ b/gdb/typeprint.c @@ -488,7 +488,8 @@ whatis_exp (const char *exp, int show) feature. */ if (show > 0 && (current_language->la_language == language_c - || current_language->la_language == language_cplus)) + || current_language->la_language == language_cplus + || current_language->la_language == language_rust)) { flags.print_offsets = 1; flags.print_typedefs = 0; -- 2.13.6