Bug 24012 - GDB crashes on -var-list-children of std::variant
Summary: GDB crashes on -var-list-children of std::variant
Status: NEW
Alias: None
Product: gdb
Classification: Unclassified
Component: varobj (show other bugs)
Version: 8.2
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-12-20 12:56 UTC by Eldar Abusalimov
Modified: 2023-08-31 18:27 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed: 2022-05-25 00:00:00


Attachments
GDB MI log (2.35 KB, text/x-log)
2018-12-20 12:56 UTC, Eldar Abusalimov
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Eldar Abusalimov 2018-12-20 12:56:31 UTC
Created attachment 11478 [details]
GDB MI log

Reproducer code (C++17):

#include <iostream>
#include <variant>
#include <string>

int main()
{
    std::variant<int,double,std::string> varIntDoubleStr;
    varIntDoubleStr = "Hello"; // Set breakpoint here
    varIntDoubleStr = 1.2;
    varIntDoubleStr = 1;
    varIntDoubleStr = "Hello";

    return 0;
}


The full GDB MI log is attached. The interesting part is:


# run:

>-exec-run
...
<=breakpoint-modified,bkpt={number="1",type="breakpoint",disp="keep",enabled="y",addr="0x0000563fed671d8e",func="main()",file="/home/eldar/CLionProjects/CPP-14915/main.cpp",fullname="/home/eldar/CLionProjects/CPP-14915/main.cpp",line="8",thread-groups=["i1"],times="1",original-location="/home/eldar/CLionProjects/CPP-14915/main.cpp:8"}
<~"\n"
<~"Breakpoint 1, main () at /home/eldar/CLionProjects/CPP-14915/main.cpp:8\n"
<~"8\t    varIntDoubleStr = \"Hello\"; // Set breakpoint here\n"
<*stopped,reason="breakpoint-hit",disp="keep",bkptno="1",frame={addr="0x0000563fed671d8e",func="main",args=[],file="/home/eldar/CLionProjects/CPP-14915/main.cpp",fullname="/home/eldar/CLionProjects/CPP-14915/main.cpp",line="8"},thread-id="1",stopped-threads="all",core="0"
...
>-stack-list-variables --thread 1 --frame 0 --no-values
<^done,variables=[{name="varIntDoubleStr"}]
...

# create varobj and list children (works ok for the first time):

>0-interpreter-exec --thread 1 --frame 0 mi2 "-var-create var1_varIntDoubleStr * \"varIntDoubleStr\""
<^done,name="var1_varIntDoubleStr",numchild="0",value="{...}",type="std::variant<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >",thread-id="1",displayhint="array",dynamic="1",has_more="1"
<0^done
<(gdb)
>-var-list-children --all-values "var1_varIntDoubleStr" 0 50
<^done,numchild="1",displayhint="array",children=[child={name="var1_varIntDoubleStr.[contained value]",exp="[contained value]",numchild="0",value="0",type="int",thread-id="1"}],has_more="0"
<(gdb)

# step over:

>-exec-next --thread 1 --frame 0
<^running
<*running,thread-id="all"
<(gdb)
<*stopped,reason="end-stepping-range",frame={addr="0x0000563fed671da1",func="main",args=[],file="/home/eldar/CLionProjects/CPP-14915/main.cpp",fullname="/home/eldar/CLionProjects/CPP-14915/main.cpp",line="9"},thread-id="1",stopped-threads="all",core="2"

# update variables

>-var-update --all-values *
<^done,changelist=[{name="var1_varIntDoubleStr.[contained value]",value="\"Hello\"",in_scope="true",type_changed="true",new_type="std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >",new_num_children="0",displayhint="string",dynamic="1",has_more="0"}]
<(gdb)
>-var-delete "var1_varIntDoubleStr.[contained value]"
<^done,ndeleted="1"
<(gdb)
>-stack-list-variables --thread 1 --frame 0 --no-values
<^done,variables=[{name="varIntDoubleStr"}]
<(gdb)
...
>-var-list-children --all-values "var1_varIntDoubleStr" 0 50
Debugger exited with code 139# run:

>-exec-run
...
<=breakpoint-modified,bkpt={number="1",type="breakpoint",disp="keep",enabled="y",addr="0x0000563fed671d8e",func="main()",file="/home/eldar/CLionProjects/CPP-14915/main.cpp",fullname="/home/eldar/CLionProjects/CPP-14915/main.cpp",line="8",thread-groups=["i1"],times="1",original-location="/home/eldar/CLionProjects/CPP-14915/main.cpp:8"}
<~"\n"
<~"Breakpoint 1, main () at /home/eldar/CLionProjects/CPP-14915/main.cpp:8\n"
<~"8\t    varIntDoubleStr = \"Hello\"; // Set breakpoint here\n"
<*stopped,reason="breakpoint-hit",disp="keep",bkptno="1",frame={addr="0x0000563fed671d8e",func="main",args=[],file="/home/eldar/CLionProjects/CPP-14915/main.cpp",fullname="/home/eldar/CLionProjects/CPP-14915/main.cpp",line="8"},thread-id="1",stopped-threads="all",core="0"
...
>-stack-list-variables --thread 1 --frame 0 --no-values
<^done,variables=[{name="varIntDoubleStr"}]
...

# create varobj and list children (works ok for the first time):

>0-interpreter-exec --thread 1 --frame 0 mi2 "-var-create var1_varIntDoubleStr * \"varIntDoubleStr\""
<^done,name="var1_varIntDoubleStr",numchild="0",value="{...}",type="std::variant<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >",thread-id="1",displayhint="array",dynamic="1",has_more="1"
<0^done
<(gdb)
>-var-list-children --all-values "var1_varIntDoubleStr" 0 50
<^done,numchild="1",displayhint="array",children=[child={name="var1_varIntDoubleStr.[contained value]",exp="[contained value]",numchild="0",value="0",type="int",thread-id="1"}],has_more="0"
<(gdb)

# step over:

>-exec-next --thread 1 --frame 0
<^running
<*running,thread-id="all"
<(gdb)
<*stopped,reason="end-stepping-range",frame={addr="0x0000563fed671da1",func="main",args=[],file="/home/eldar/CLionProjects/CPP-14915/main.cpp",fullname="/home/eldar/CLionProjects/CPP-14915/main.cpp",line="9"},thread-id="1",stopped-threads="all",core="2"

# update variables

>-var-update --all-values *
<^done,changelist=[{name="var1_varIntDoubleStr.[contained value]",value="\"Hello\"",in_scope="true",type_changed="true",new_type="std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >",new_num_children="0",displayhint="string",dynamic="1",has_more="0"}]
<(gdb)
>-var-delete "var1_varIntDoubleStr.[contained value]"
<^done,ndeleted="1"
<(gdb)
>-stack-list-variables --thread 1 --frame 0 --no-values
<^done,variables=[{name="varIntDoubleStr"}]
<(gdb)
...
>-var-list-children --all-values "var1_varIntDoubleStr" 0 50
Debugger exited with code 139
Comment 1 Eldar Abusalimov 2018-12-20 12:57:12 UTC
Originally reported as https://youtrack.jetbrains.com/issue/CPP-14915
Comment 2 Simon Marchi 2018-12-24 19:30:55 UTC
Thanks for the report.  Could you extract the required MI commands from the log (removing the unrelated ones, like -environment-cd, most probably), so people can just copy paste them?  If possible, without paths that are local to your system.  Thanks!
Comment 3 m101010a 2019-05-14 00:50:38 UTC
I found this bug independently and have a minimized repro case:

Create x.cpp with the following 4 lines:

#include <variant>
int main() {
	std::variant<int, int *> v=0;
}

Compile it with "g++ x.cpp -std=c++17 -g"

run "gdb --interpreter=mi2" and execute the following commands:

-enable-pretty-printing
-file-exec-and-symbols "a.out"
-break-insert -f   "x.cpp:4"
-exec-run
-var-create var1_v * "v"
-var-list-children --all-values "var1_v" 0 50
-var-delete "var1_v.[contained value]"
-var-list-children --all-values "var1_v" 0 50

This crashes gdb 8.2.1 on Arch Linux.  x.cpp was compiled with GCC 8.3.0.
Comment 4 Tom Tromey 2022-05-25 18:44:34 UTC
Confirmed with head:

#0  0x00000000009c585c in install_new_value (var=0x0, value=0x29e47e0, 
    initial=false) at ../../binutils-gdb/gdb/varobj.c:1196
#1  0x00000000009c454d in install_dynamic_child (var=0x239aa90, changed=0x0, 
    type_changed=0x0, newobj=0x0, unchanged=0x0, cchanged=0x7fffffffde0f, 
    index=0, item=0x249d240) at ../../binutils-gdb/gdb/varobj.c:628
#2  0x00000000009c4933 in update_dynamic_varobj_children (var=0x239aa90, 
    changed=0x0, type_changed=0x0, newobj=0x0, unchanged=0x0, 
    cchanged=0x7fffffffde0f, update_children=false, from=0, to=50)
    at ../../binutils-gdb/gdb/varobj.c:718
#3  0x00000000009c4ba1 in varobj_list_children (var=0x239aa90, 
    from=0x7fffffffdea4, to=0x7fffffffdea0)
    at ../../binutils-gdb/gdb/varobj.c:790
#4  0x000000000072e454 in mi_cmd_var_list_children (
    command=0x238b780 "var-list-children", argv=0x24bd6c0, argc=4)
    at ../../binutils-gdb/gdb/mi/mi-cmd-var.c:379
Comment 5 Tom Tromey 2023-08-31 18:27:47 UTC
I didn't debug this but there are some known issues with
variadic templates, so that is my first suspect.