Skip to content

Commit 29b161c

Browse files
committed
Allow DW_ATE_UTF for Rust characters
The Rust compiler plans to change the encoding of a Rust 'char' type to use DW_ATE_UTF. You can see the discussion here: rust-lang/rust#89887 However, this fails in gdb. I looked into this, and it turns out that the handling of DW_ATE_UTF is currently fairly specific to C++. In particular, the code here assumes the C++ type names, and it creates an integer type. This comes from commit 53e710a ("GDB thinks char16_t and char32_t are signed in C++"). The message says: Both places need fixing. But since I couldn't tell why dwarf2read.c needs to create a new type, I've made it use the per-arch built-in types instead, so that the types are only created once per arch instead of once per objfile. That seems to work fine. ... which is fine, but it seems to me that it's also correct to make a new character type; and this approach is better because it preserves the type name as well. This does use more memory, but first we shouldn't be too concerned about the memory use of types coming from debuginfo; and second, if we are, we should implement type interning anyway. Changing this code to use a character type revealed a couple of oddities in the C/C++ handling of TYPE_CODE_CHAR. This patch fixes these as well. I filed PR rust/28637 for this issue, so that this patch can be backported to the gdb 11 branch. (cherry picked from commit 1c0e436)
1 parent a15b332 commit 29b161c

File tree

4 files changed

+75
-13
lines changed

4 files changed

+75
-13
lines changed

gdb/c-lang.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ classify_type (struct type *elttype, struct gdbarch *gdbarch,
8888
{
8989
const char *name = elttype->name ();
9090

91-
if (elttype->code () == TYPE_CODE_CHAR || !name)
91+
if (name == nullptr)
9292
{
9393
result = C_CHAR;
9494
goto done;

gdb/c-valprint.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ c_value_print_inner (struct value *val, struct ui_file *stream, int recurse,
438438
c_value_print_struct (val, stream, recurse, options);
439439
break;
440440

441+
case TYPE_CODE_CHAR:
441442
case TYPE_CODE_INT:
442443
c_value_print_int (val, stream, options);
443444
break;
@@ -458,7 +459,6 @@ c_value_print_inner (struct value *val, struct ui_file *stream, int recurse,
458459
case TYPE_CODE_ERROR:
459460
case TYPE_CODE_UNDEF:
460461
case TYPE_CODE_COMPLEX:
461-
case TYPE_CODE_CHAR:
462462
default:
463463
generic_value_print (val, stream, recurse, options, &c_decorations);
464464
break;

gdb/dwarf2/read.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18177,16 +18177,7 @@ read_base_type (struct die_info *die, struct dwarf2_cu *cu)
1817718177
break;
1817818178
case DW_ATE_UTF:
1817918179
{
18180-
if (bits == 16)
18181-
type = builtin_type (arch)->builtin_char16;
18182-
else if (bits == 32)
18183-
type = builtin_type (arch)->builtin_char32;
18184-
else
18185-
{
18186-
complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18187-
bits);
18188-
type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18189-
}
18180+
type = init_character_type (objfile, bits, 1, name);
1819018181
return set_die_type (die, type, cu);
1819118182
}
1819218183
break;
@@ -18206,7 +18197,9 @@ read_base_type (struct die_info *die, struct dwarf2_cu *cu)
1820618197
break;
1820718198
}
1820818199

18209-
if (name && strcmp (name, "char") == 0)
18200+
if (type->code () == TYPE_CODE_INT
18201+
&& name != nullptr
18202+
&& strcmp (name, "char") == 0)
1821018203
type->set_has_no_signedness (true);
1821118204

1821218205
maybe_set_alignment (cu, die, type);

gdb/testsuite/gdb.dwarf2/utf-rust.exp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Copyright 2021 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+
# Test DW_ATE_UTF for Rust.
17+
18+
load_lib dwarf.exp
19+
20+
# This test can only be run on targets which support DWARF-2 and use
21+
# gas.
22+
if {![dwarf2_support]} {
23+
return 0
24+
}
25+
26+
standard_testfile main.c .S
27+
28+
# Make some DWARF for the test.
29+
set asm_file [standard_output_file $srcfile2]
30+
Dwarf::assemble $asm_file {
31+
upvar cu_lang cu_lang
32+
33+
declare_labels char_label
34+
35+
# Creating a CU with 4-byte addresses lets this test link on
36+
# both 32- and 64-bit machines.
37+
cu { addr_size 4 } {
38+
compile_unit {
39+
{name file1.txt}
40+
{language @DW_LANG_Rust}
41+
} {
42+
char_label: DW_TAG_base_type {
43+
{DW_AT_byte_size 4 DW_FORM_sdata}
44+
{DW_AT_encoding @DW_ATE_UTF}
45+
{DW_AT_name char}
46+
}
47+
48+
DW_TAG_variable {
49+
{name cvalue}
50+
{type :$char_label}
51+
{const_value 97 DW_FORM_udata}
52+
}
53+
}
54+
}
55+
}
56+
57+
if {[prepare_for_testing "failed to prepare" ${testfile} \
58+
[list $srcfile $asm_file] debug]} {
59+
return -1
60+
}
61+
62+
if {![runto main]} {
63+
return -1
64+
}
65+
66+
gdb_test "set language rust" \
67+
"Warning: the current language does not match this frame."
68+
# Get the values into history so we can use it from Rust.
69+
gdb_test "print cvalue" "\\\$1 = 97 'a'"

0 commit comments

Comments
 (0)