Skip to content

Commit b455d1f

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.
1 parent 6e19544 commit b455d1f

File tree

4 files changed

+75
-13
lines changed

4 files changed

+75
-13
lines changed

gdb/c-lang.c

+1-1
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

+1-1
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

+4-11
Original file line numberDiff line numberDiff line change
@@ -18256,16 +18256,7 @@ read_base_type (struct die_info *die, struct dwarf2_cu *cu)
1825618256
break;
1825718257
case DW_ATE_UTF:
1825818258
{
18259-
if (bits == 16)
18260-
type = builtin_type (arch)->builtin_char16;
18261-
else if (bits == 32)
18262-
type = builtin_type (arch)->builtin_char32;
18263-
else
18264-
{
18265-
complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18266-
bits);
18267-
type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18268-
}
18259+
type = init_character_type (objfile, bits, 1, name);
1826918260
return set_die_type (die, type, cu);
1827018261
}
1827118262
break;
@@ -18285,7 +18276,9 @@ read_base_type (struct die_info *die, struct dwarf2_cu *cu)
1828518276
break;
1828618277
}
1828718278

18288-
if (name && strcmp (name, "char") == 0)
18279+
if (type->code () == TYPE_CODE_INT
18280+
&& name != nullptr
18281+
&& strcmp (name, "char") == 0)
1828918282
type->set_has_no_signedness (true);
1829018283

1829118284
maybe_set_alignment (cu, die, type);

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

+69
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)