Using gdb 11.1, I tried to use this trick : calling operator<< in gdb to print a custom type that has an overloads to print it graphically.
However, I get a segfault calling it (by the way, it works very well when called from the program and not gdb...)
while trying to track this, I found I am actually not even able to call std::cout<<"test"
These are all my tries for "test"...
(gdb) print std::cout<<"test"
Cannot access memory at address 0xffffffffffffffe8
(gdb) print std::cout<<"test"<<std::endl;
Invalid character ';' in expression.
(gdb) print std::cout<<"test"<<std::endl
Cannot access memory at address 0xffffffffffffffe8
(gdb) call "operator<<(ostream &, const char *)"(std::cout, "test")
Invalid data type for function to be called.
(gdb) call "operator<<(std::ostream &, const char *)"(std::cout, "test")
Invalid data type for function to be called.
(gdb) call operator<<(std::cou
Invalid cast.
(gdb) call operator<<(std::cout, "test")
Invalid cast.
(gdb) call operator<<(std::cout, (const char *)"test")
Invalid cast.
(gdb)
And this is what I get with any custom type :
(gdb) printr *this
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7e9048a in std::ostream::sentry::sentry (this=0x7fffffffc3c0, __os=...) at /build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/ostream.tcc:51
The program being debugged was signaled while in a function called from GDB.
GDB remains in the frame where the signal was received.
To change this behavior use "set unwindonsignal on".
Evaluation of the expression containing the function
(operator<<(std::ostream&, gamestate_t const&)) will be abandoned.
When the function is done executing, GDB will silently stop.
with this backtrace :
native process 293516 In: std::ostream::sentry::sentry L51 PC: 0x7ffff7e9048a
#0 0x00007ffff7e9048a in std::ostream::sentry::sentry (this=0x7fffffffc3c0, __os=...) at /build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/ostream.tcc:51
#1 0x00007ffff7e90c9d in std::__ostream_insert<char, std::char_traits<char> > (__out=..., __s=__s@entry=0x55555563607f "GAMESTATE : day: ", __n=18) at /build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/ostream_insert.h:83
#2 0x00007ffff7e911ce in std::operator<< <std::char_traits<char> > (__out=..., __s=0x55555563607f "GAMESTATE : day: ") at /build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/ostream:611
#3 0x0000555555594d04 in operator<< (out=..., gs=...) at tests.cpp:75
#4 <function called from gdb>
#5 gamestate_t::addTree (this=0x7fffffffc710, t=...) at /home/leo/projects/other/codegame/ia2/cpp/main.cpp:769
#6 0x00005555555b1616 in test_main::make_gamestate (in=...) at tests.cpp:587
#7 0x000055555559757a in from (this=0x5555556aacd8 <test_main::param_test_apply_round+504>) at tests.cpp:597
#8 0x000055555559780b in test_main::____C_A_T_C_H____T_E_S_T____19 () at tests.cpp:694
#9 0x000055555557f4d8 in Catch::TestInvokerAsFunction::invoke (this=0x5555556be510) at /usr/include/catch2/catch.hpp:14321
#10 0x000055555557e75b in Catch::TestCase::invoke (this=0x5555556de090) at /usr/include/catch2/catch.hpp:14160
#11 0x0000555555578d60 in Catch::RunContext::invokeActiveTestCase (this=0x7fffffffcf50) at /usr/include/catch2/catch.hpp:13020
#12 0x0000555555578aa1 in Catch::RunContext::runCurrentTest (this=0x7fffffffcf50, redirectedCout="", redirectedCerr="") at /usr/include/catch2/catch.hpp:12993
#13 0x00005555555775b6 in Catch::RunContext::runTest (this=0x7fffffffcf50, testCase=...) at /usr/include/catch2/catch.hpp:12754
#14 0x000055555557a602 in Catch::(anonymous namespace)::TestGroup::execute (this=0x7fffffffcf40) at /usr/include/catch2/catch.hpp:13347
#15 0x000055555557ba12 in Catch::Session::runInternal (this=0x7fffffffd230) at /usr/include/catch2/catch.hpp:13553
#16 0x000055555557b718 in Catch::Session::run (this=0x7fffffffd230) at /usr/include/catch2/catch.hpp:13509
(it is used from a Catch2 test case)
this is my .gdbinit :
define printr
call (void)operator<<(std::cout, $arg0)
printf "\n"
end