Bug 17734 - Printing a std::string member hangs after clang compile
Summary: Printing a std::string member hangs after clang compile
Status: NEW
Alias: None
Product: gdb
Classification: Unclassified
Component: c++ (show other bugs)
Version: HEAD
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-12-19 21:29 UTC by Joel
Modified: 2014-12-20 21:19 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Joel 2014-12-19 21:29:56 UTC
When the following code is compiled with clang++, breaking on MyClass::test then printing str hangs GDB. It doesn't respond to sigterms/sigints and consumes 100% cpu. Interestingly, the problem doesn't occur when I compile with g++. In addition, the problem does not occur when printing a local std::string.

#include <iostream>
#include <string>

struct MyClass
{
    MyClass()
        : str("Hello World")
    {}

    void test()
    {
        std::cout << str << std::endl;
    }

    std::string str;
};

int main()
{
    MyClass inst;
    inst.test();
    return 0;
}

Here are some versions. Note that I tested with 2 GDB versions, including the latest HEAD.

joel ~> uname -a
Linux walker11 3.16.0-28-generic #38-Ubuntu SMP Fri Dec 12 17:37:40 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
joel ~> clang++ --version
Ubuntu clang version 3.5.0-4ubuntu2 (tags/RELEASE_350/final) (based on LLVM 3.5.0)
Target: x86_64-pc-linux-gnu
Thread model: posix
joel ~> gdb --version
GNU gdb (Ubuntu 7.8-1ubuntu4) 7.8.0.20141001-cvs
...
joel ~> /tmp/binutils-gdb/gdb/gdb --version
GNU gdb (GDB) 7.8.50.20141219-cvs
...

Compilation command:
clang++ -g -Wall test_string.cpp

GDB session:
joel /tmp> ./binutils-gdb/gdb/gdb ./a.out 
Python Exception <type 'exceptions.ImportError'> No module named gdb: 
./binutils-gdb/gdb/gdb: warning: 
Could not load the Python gdb module from `/usr/local/share/gdb/python'.
Limited Python support is available from the _gdb module.
Suggest passing --data-directory=/path/to/gdb/data-directory.

GNU gdb (GDB) 7.8.50.20141219-cvs
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-unknown-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
/home/joel/.gdbinit:1: Error in sourced command file:
Undefined command: "".  Try "help".
Reading symbols from ./a.out...done.
(gdb) break MyClass::test() 
Breakpoint 1 at 0x400cea: file test_string.cpp, line 12.
(gdb) r
Starting program: /tmp/a.out 

Breakpoint 1, MyClass::test (this=0x7fffffffe3b0) at test_string.cpp:12
12              std::cout << str << std::endl;
(gdb) print str
fish: Job 1, “./binutils-gdb/gdb/gdb ./a.out ” terminated by signal SIGKILL (Forced quit)
Comment 1 Joel 2014-12-20 02:10:17 UTC
I just had the bright idea to debug GDB with GDB to see what's happening. I re-compiled GDB with "-g -O0", and here's the backtrace:

(gdb) print str

Program received signal SIGFPE, Arithmetic exception.
0x00000000005b37b0 in value_primitive_field (arg1=0xfef6b0, offset=0, fieldno=0, arg_type=0xf57a70) at value.c:2984
2984          if ((bitpos % container_bitsize) + v->bitsize <= container_bitsize
(gdb) bt
Python Exception <type 'exceptions.ImportError'> No module named gdb.frames: 
#0  0x00000000005b37b0 in value_primitive_field (arg1=0xfef6b0, offset=0, fieldno=0, arg_type=0xf57a70) at value.c:2984
#1  0x00000000005c28ad in do_search_struct_field (name=0x1076780 "str", arg1=0xfef6b0, offset=0, type=0xf57a70, looking_for_baseclass=0, result_ptr=0x7fffffffd710, last_boffset=0x7fffffffd70c, 
    outermost_type=0xf57a70) at valops.c:1822
#2  0x00000000005c2e91 in search_struct_field (name=0x1076780 "str", arg1=0xfef6b0, offset=0, type=0xf57a70, looking_for_baseclass=0) at valops.c:1966
#3  0x00000000005c38fa in value_struct_elt (argp=0x7fffffffd838, args=0x0, name=0x1076780 "str", static_memfuncp=0x0, err=0x91cd49 "structure pointer") at valops.c:2165
#4  0x00000000005ba643 in evaluate_subexp_standard (expect_type=0x0, exp=0x1076740, pos=0x7fffffffdd74, noside=EVAL_NORMAL) at eval.c:1904
#5  0x00000000006f9d7d in evaluate_subexp_c (expect_type=0x0, exp=0x1076740, pos=0x7fffffffdd74, noside=EVAL_NORMAL) at c-lang.c:717
#6  0x00000000005b594d in evaluate_subexp (expect_type=0x0, exp=0x1076740, pos=0x7fffffffdd74, noside=EVAL_NORMAL) at eval.c:79
#7  0x00000000005b5bd6 in evaluate_expression (exp=0x1076740) at eval.c:163
#8  0x00000000005d3d43 in print_command_1 (exp=0xd71196 "str", voidprint=1) at ./printcmd.c:974
#9  0x00000000005d3e7b in print_command (exp=0xd71196 "str", from_tty=1) at ./printcmd.c:1006
#10 0x00000000004fc94d in do_cfunc (c=0xe164e0, args=0xd71196 "str", from_tty=1) at ./cli/cli-decode.c:105
#11 0x00000000004ffcda in cmd_func (cmd=0xe164e0, args=0xd71196 "str", from_tty=1) at ./cli/cli-decode.c:1893
#12 0x000000000073fc1b in execute_command (p=0xd71198 "r", from_tty=1) at top.c:476
#13 0x000000000062c047 in command_handler (command=0xd71190 "print str") at event-top.c:494
#14 0x000000000062c613 in command_line_handler (rl=0xf0bef0 "") at event-top.c:692
#15 0x00000000007aa0c4 in rl_callback_read_char () at callback.c:220
#16 0x000000000062baa9 in rl_callback_read_char_wrapper (client_data=0x0) at event-top.c:171
#17 0x000000000062bf58 in stdin_event_handler (error=0, client_data=0x0) at event-top.c:432
#18 0x000000000062a9e8 in handle_file_event (data=...) at event-loop.c:762
#19 0x0000000000629e76 in process_event () at event-loop.c:339
#20 0x0000000000629f3d in gdb_do_one_event () at event-loop.c:403
#21 0x0000000000629f9c in start_event_loop () at event-loop.c:428
#22 0x000000000062badb in cli_command_loop (data=0x0) at event-top.c:186
#23 0x00000000006215ed in current_interp_command_loop () at interps.c:317
#24 0x00000000006229e8 in captured_command_loop (data=0x0) at main.c:321
#25 0x000000000061e2a1 in catch_errors (func=0x6229cd <captured_command_loop>, func_args=0x0, errstring=0x935ae5 "", mask=RETURN_MASK_ALL) at exceptions.c:237
#26 0x0000000000623f90 in captured_main (data=0x7fffffffe350) at main.c:1149
#27 0x000000000061e2a1 in catch_errors (func=0x622e2e <captured_main>, func_args=0x7fffffffe350, errstring=0x935ae5 "", mask=RETURN_MASK_ALL) at exceptions.c:237
#28 0x0000000000623fb9 in gdb_main (args=0x7fffffffe350) at main.c:1157
#29 0x000000000045e3bc in main (argc=2, argv=0x7fffffffe458) at gdb.c:32
Comment 2 Siva Chandra 2014-12-20 02:27:31 UTC
FWIW, this is triggered by a Clang bug. I tried with Clang 3.4 and Clang ToT and found that it only occurs with Clang ToT. The problem is that the 'str' fields bit offset is too high:

< 1><0x000003d4>    DW_TAG_structure_type
                      DW_AT_name                  "MyClass"
                      DW_AT_byte_size             0x00000008
                      DW_AT_decl_file             0x00000006 
                      DW_AT_decl_line             0x00000004
< 2><0x000003dc>      DW_TAG_member
                        DW_AT_name                  "str"
                        DW_AT_type                  <0x00000090>
                        DW_AT_decl_file             0x00000006 
                        DW_AT_decl_line             0x0000000f
                        DW_AT_byte_size             0x00000000
                        DW_AT_bit_size              0x00000040
                        DW_AT_bit_offset            0xffffffffffffffc0
                        DW_AT_data_member_location  0

GDB should not hang (or, appear to hang) and hence it is a GDB bug also.