binutils-gdb modified for the FreeChainXenon project
Find a file
Andrew Burgess bde240e7f8 gdb: fix printf of wchar_t early in a gdb session
Given this test program:

  #include <wchar.h>

  const wchar_t wide_str[] = L"wide string";

  int
  main (void)
  {
    return 0;
  }

I observed this GDB behaviour:

  $ gdb -q /tmp/printf-wchar_t
  Reading symbols from /tmp/printf-wchar_t...
  (gdb) start
  Temporary breakpoint 1 at 0x40110a: file /tmp/printf-wchar_t.c, line 8.
  Starting program: /tmp/printf-wchar_t

  Temporary breakpoint 1, main () at /tmp/printf-wchar_t.c:8
  25	  return 0;
  (gdb) printf "%ls\n", wide_str

  (gdb)

Notice that the printf results in a blank line rather than the
expected 'wide string' output.

I tracked the problem down to printf_wide_c_string (in printcmd.c), in
this function we do this:

  struct type *wctype = lookup_typename (current_language,
					 "wchar_t", NULL, 0);
  int wcwidth = wctype->length ();

the problem here is that 'wchar_t' is a typedef.  If we look at the
comment on type::length() we see this:

  /* Note that if thistype is a TYPEDEF type, you have to call check_typedef.
     But check_typedef does set the TYPE_LENGTH of the TYPEDEF type,
     so you only have to call check_typedef once.  Since value::allocate
     calls check_typedef, X->type ()->length () is safe.  */

What this means is that after calling lookup_typename we should call
check_typedef in order to ensure that the length of the typedef has
been setup correctly.  We are not doing this in printf_wide_c_string,
and so wcwidth is incorrectly calculated as 0.  This is what leads GDB
to print an empty string.

We can see in c_string_operation::evaluate (in c-lang.c) an example of
calling check_typedef specifically to fix this exact issue.

Initially I did fix this problem by adding a check_typedef call into
printf_wide_c_string, but then I figured why not move the
check_typedef call up into lookup_typename itself, that feels like it
should be harmless when looking up a non-typedef type, but will avoid
bugs like this when looking up a typedef.  So that's what I did.

I can then remove the extra check_typedef call from c-lang.c, I don't
see any other places where we had extra check_typedef calls.  This
doesn't mean we definitely had bugs -- so long as we never checked the
length, or, if we knew that check_typedef had already been called,
then we would be fine.

I don't see any test regressions after this change, and my new test
case is now passing.

Reviewed-By: Tom Tromey <tom@tromey.com>
2023-07-07 15:20:28 +01:00
bfd Udated Freach and Romainian translations for various sub-directories 2023-07-07 09:40:20 +01:00
binutils arc: Update/Add ARCv3 support. 2023-07-07 13:08:04 +03:00
config Merge config/picflag.m4 from gcc 2023-01-04 13:23:54 +10:30
contrib
cpu Add markers for the 2.41 branch 2023-07-03 11:12:15 +01:00
elfcpp Add markers for the 2.41 branch 2023-07-03 11:12:15 +01:00
etc Update year range in gprofng copyright notices 2023-01-01 23:26:30 +10:30
gas Udated Freach and Romainian translations for various sub-directories 2023-07-07 09:40:20 +01:00
gdb gdb: fix printf of wchar_t early in a gdb session 2023-07-07 15:20:28 +01:00
gdbserver Linux: Avoid pread64/pwrite64 for high memory addresses (PR gdb/30525) 2023-07-06 17:57:48 +01:00
gdbsupport Move unrelocated_addr to common-types.h 2023-06-05 09:59:18 -06:00
gnulib Add missing backslash to update-gnulib.sh 2023-06-21 08:47:05 -06:00
gold Udated Freach and Romainian translations for various sub-directories 2023-07-07 09:40:20 +01:00
gprof Change version number to 2.41.50 and regenerate files 2023-07-03 11:54:06 +01:00
gprofng Change version number to 2.41.50 and regenerate files 2023-07-03 11:54:06 +01:00
include RISC-V: Zvkh[a,b]: Remove individual instruction class 2023-07-03 18:17:59 +08:00
intl egrep in binutils 2022-09-28 13:37:31 +09:30
ld ld: fix build with old glibc / gcc 2023-07-07 14:10:21 +02:00
libbacktrace Ensure that libbacktrace/allocfail.sh is not deleted when creating release tarballs. 2023-01-12 13:39:03 +00:00
libctf Add markers for the 2.41 branch 2023-07-03 11:12:15 +01:00
libdecnumber
libiberty Synchromize libiberty sources with master version in gcc repository 2023-06-26 15:47:15 +01:00
libsframe doc: sframe: update specification for SFRAME_VERSION_2 2023-06-29 16:32:26 -07:00
opcodes x86: optimize 128-bit VPBROADCASTQ to VPUNPCKLQDQ 2023-07-04 17:07:26 +02:00
readline [readline] Fix double free in _rl_scxt_dispose 2023-05-28 10:17:57 +02:00
sim sim: bpf: update to new BPF relocations 2023-04-26 19:22:14 +02:00
texinfo
zlib
.cvsignore
.editorconfig
.gitattributes binutils-gdb/git: highlight whitespace errors in source files 2022-07-25 14:35:41 +01:00
.gitignore
ar-lib
ChangeLog Add markers for the 2.41 branch 2023-07-03 11:12:15 +01:00
compile
config-ml.in
config.guess Sync config.guess and config.sub with upstream master versions. 2023-06-26 14:11:30 +01:00
config.rpath
config.sub Sync config.guess and config.sub with upstream master versions. 2023-06-26 14:11:30 +01:00
configure configure: remove dependencies on gmp and mpfr when gdb is disabled 2023-01-06 09:32:07 +01:00
configure.ac configure: remove dependencies on gmp and mpfr when gdb is disabled 2023-01-06 09:32:07 +01:00
COPYING
COPYING.LIB
COPYING.LIBGLOSS
COPYING.NEWLIB
COPYING3
COPYING3.LIB
depcomp
djunpack.bat
install-sh
libtool.m4
ltgcc.m4
ltmain.sh
ltoptions.m4
ltsugar.m4
ltversion.m4
lt~obsolete.m4
MAINTAINERS
Makefile.def toplevel: Makefile.def: add install-strip dependency on libsframe 2023-01-18 23:17:49 -08:00
Makefile.in toplevel: Makefile.def: add install-strip dependency on libsframe 2023-01-18 23:17:49 -08:00
Makefile.tpl
makefile.vms
missing
mkdep
mkinstalldirs
move-if-change
multilib.am
README
README-maintainer-mode Note that at least dejagnu version 1.5.3 is required in order to be ale to run the testsuites. 2022-10-04 10:54:19 +01:00
SECURITY.txt Add a SECURITY.txt file describing the GNU Binutils' project's stance on security related bugs. 2023-04-20 16:52:11 +01:00
setup.com
src-release.sh Add a SECURITY.txt file describing the GNU Binutils' project's stance on security related bugs. 2023-04-20 16:52:11 +01:00
symlink-tree
test-driver
ylwrap

		   README for GNU development tools

This directory contains various GNU compilers, assemblers, linkers, 
debuggers, etc., plus their support routines, definitions, and documentation.

If you are receiving this as part of a GDB release, see the file gdb/README.
If with a binutils release, see binutils/README;  if with a libg++ release,
see libg++/README, etc.  That'll give you info about this
package -- supported targets, how to use it, how to report bugs, etc.

It is now possible to automatically configure and build a variety of
tools with one command.  To build all of the tools contained herein,
run the ``configure'' script here, e.g.:

	./configure 
	make

To install them (by default in /usr/local/bin, /usr/local/lib, etc),
then do:
	make install

(If the configure script can't determine your type of computer, give it
the name as an argument, for instance ``./configure sun4''.  You can
use the script ``config.sub'' to test whether a name is recognized; if
it is, config.sub translates it to a triplet specifying CPU, vendor,
and OS.)

If you have more than one compiler on your system, it is often best to
explicitly set CC in the environment before running configure, and to
also set CC when running make.  For example (assuming sh/bash/ksh):

	CC=gcc ./configure
	make

A similar example using csh:

	setenv CC gcc
	./configure
	make

Much of the code and documentation enclosed is copyright by
the Free Software Foundation, Inc.  See the file COPYING or
COPYING.LIB in the various directories, for a description of the
GNU General Public License terms under which you can copy the files.

REPORTING BUGS: Again, see gdb/README, binutils/README, etc., for info
on where and how to report problems.