Redirecting cout to buffer causes error

REST version : v2.2.12

Hello,

I am trying to redirect the standard output to a buffer.

I do the following:

std::stringstream buffer;
std::streambuf * old = std::cout.rdbuf(buffer.rdbuf());
std::cout << "testing" << std::endl;
std::string text = buffer.str();
std::cout.rdbuf(old);

cout << "text: " << text << endl;

The code works correctly on a root prompt.

However if I try to run this on a restRoot promp I get a segmentation break, the error I get is the following:

Root > std::string text = buffer.str();
root [4] std::cout.rdbuf( old );

 *** Break *** segmentation violation





===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007f48f210cffa in __GI___waitpid (pid=28688, stat_loc=stat_loc
entry=0x7ffd25a8bfc0, options=options
entry=0) at ../sysdeps/unix/sysv/linux/waitpid.c:29
#1  0x00007f48f20940ab in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:148
#2  0x00007f48f90cc477 in TUnixSystem::Exec (shellcmd=<optimized out>, this=0x556eb8b7f570) at /home/lobis/apps/ROOT/root-6.16.00/core/unix/src/TUnixSystem.cxx:2119
#3  TUnixSystem::StackTrace (this=0x556eb8b7f570) at /home/lobis/apps/ROOT/root-6.16.00/core/unix/src/TUnixSystem.cxx:2413
#4  0x00007f48f90cebc4 in TUnixSystem::DispatchSignals (this=0x556eb8b7f570, sig=kSigSegmentationViolation) at /home/lobis/apps/ROOT/root-6.16.00/core/unix/src/TUnixSystem.cxx:3644
#5  <signal handler called>
#6  0x00007f48fa14002a in ?? ()
#7  0x00007ffd25a8ec00 in ?? ()
#8  0x00007f48e8e6c36a in std::_Function_base::_Base_manager<llvm::orc::LazyEmittingLayer<llvm::orc::IRCompileLayer<cling::IncrementalJIT::RemovableObjectLinkingLayer, llvm::orc::SimpleCompiler> >::EmissionDeferredModule::find(llvm::StringRef, bool, llvm::orc::IRCompileLayer<cling::IncrementalJIT::RemovableObjectLinkingLayer, llvm::orc::SimpleCompiler>&)::{lambda()#1}>::_M_manager(std::_Any_data&, std::_Function_base::_Base_manager<llvm::orc::LazyEmittingLayer<llvm::orc::IRCompileLayer<cling::IncrementalJIT::RemovableObjectLinkingLayer, llvm::orc::SimpleCompiler> >::EmissionDeferredModule::find(llvm::StringRef, bool, llvm::orc::IRCompileLayer<cling::IncrementalJIT::RemovableObjectLinkingLayer, llvm::orc::SimpleCompiler>&)::{lambda()#1}> const&, std::_Manager_operation) () from /home/lobis/apps/ROOT/root-6.16.00/install-gdml/lib/libCling.so
#9  0x00007f48e8e0b0cc in cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) [clone .part.323] () from /home/lobis/apps/ROOT/root-6.16.00/install-gdml/lib/libCling.so
#10 0x00007f48e8e0f8b3 in cling::Interpreter::EvaluateInternal(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) () from /home/lobis/apps/ROOT/root-6.16.00/install-gdml/lib/libCling.so
#11 0x00007f48e8e0fb29 in cling::Interpreter::process(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cling::Value*, cling::Transaction**, bool) () from /home/lobis/apps/ROOT/root-6.16.00/install-gdml/lib/libCling.so
#12 0x00007f48e8eb089f in cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) () from /home/lobis/apps/ROOT/root-6.16.00/install-gdml/lib/libCling.so
#13 0x00007f48e8d7c62a in HandleInterpreterException (metaProcessor=0x556eb9296de0, input_line=0x556ebd59ee10 "#line 1 \"ROOT_prompt_4\"\nstd::cout.rdbuf( old );", compRes=
0x7ffd25a8ead4: cling::Interpreter::kSuccess, result=result
entry=0x7ffd25a8ec00) at /home/lobis/apps/ROOT/root-6.16.00/core/metacling/src/TCling.cxx:2163
#14 0x00007f48e8d9376b in TCling::ProcessLine (this=0x556eb8c92a30, line=<optimized out>, error=0x7ffd25a8efec) at /home/lobis/apps/ROOT/root-6.16.00/core/metacling/src/TCling.cxx:2321
#15 0x00007f48f8f7e81d in TApplication::ProcessLine (this=this
entry=0x7ffd25a91460, line=<optimized out>, sync=sync
entry=false, err=err
entry=0x7ffd25a8efec) at /home/lobis/apps/ROOT/root-6.16.00/core/base/src/TApplication.cxx:1031
#16 0x00007f48f682bc5e in TRint::ProcessLineNr (this=this
entry=0x7ffd25a91460, filestem=filestem
entry=0x7f48f683d2b5 "ROOT_prompt_", line=0x556ebb932550 "std::cout.rdbuf( old );", error=0x7ffd25a8efec, error
entry=0x0) at /home/lobis/apps/ROOT/root-6.16.00/core/rint/src/TRint.cxx:746
#17 0x00007f48f682bfaf in TRint::HandleTermInput (this=0x7ffd25a91460) at /home/lobis/apps/ROOT/root-6.16.00/core/rint/src/TRint.cxx:607
#18 0x00007f48f90ce08d in TUnixSystem::CheckDescriptors (this=this
entry=0x556eb8b7f570) at /home/lobis/apps/ROOT/root-6.16.00/core/unix/src/TUnixSystem.cxx:1322
#19 0x00007f48f90cf48b in TUnixSystem::DispatchOneEvent (this=0x556eb8b7f570, pendingOnly=<optimized out>) at /home/lobis/apps/ROOT/root-6.16.00/core/unix/src/TUnixSystem.cxx:1077
#20 0x00007f48f8febc31 in TSystem::InnerLoop (this=0x556eb8b7f570) at /home/lobis/apps/ROOT/root-6.16.00/core/base/src/TSystem.cxx:412
#21 TSystem::Run (this=0x556eb8b7f570) at /home/lobis/apps/ROOT/root-6.16.00/core/base/src/TSystem.cxx:362
#22 0x00007f48f8f7c90f in TApplication::Run (this=this
entry=0x7ffd25a91460, retrn=retrn
entry=false) at /home/lobis/apps/ROOT/root-6.16.00/core/base/src/TApplication.cxx:1183
#23 0x00007f48f682d501 in TRint::Run (this=0x7ffd25a91460, retrn=<optimized out>) at /home/lobis/apps/ROOT/root-6.16.00/core/rint/src/TRint.cxx:460
#24 0x0000556eb7757af9 in main ()
===========================================================


The lines below might hint at the cause of the crash.
You may get help by asking at the ROOT forum http://root.cern.ch/forum
Only if you are really convinced it is a bug in ROOT then please submit a
report at http://root.cern.ch/bugs Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#6  0x00007f48fa14002a in ?? ()
#7  0x00007ffd25a8ec00 in ?? ()
#8  0x00007f48e8e6c36a in std::_Function_base::_Base_manager<llvm::orc::LazyEmittingLayer<llvm::orc::IRCompileLayer<cling::IncrementalJIT::RemovableObjectLinkingLayer, llvm::orc::SimpleCompiler> >::EmissionDeferredModule::find(llvm::StringRef, bool, llvm::orc::IRCompileLayer<cling::IncrementalJIT::RemovableObjectLinkingLayer, llvm::orc::SimpleCompiler>&)::{lambda()#1}>::_M_manager(std::_Any_data&, std::_Function_base::_Base_manager<llvm::orc::LazyEmittingLayer<llvm::orc::IRCompileLayer<cling::IncrementalJIT::RemovableObjectLinkingLayer, llvm::orc::SimpleCompiler> >::EmissionDeferredModule::find(llvm::StringRef, bool, llvm::orc::IRCompileLayer<cling::IncrementalJIT::RemovableObjectLinkingLayer, llvm::orc::SimpleCompiler>&)::{lambda()#1}> const&, std::_Manager_operation) () from /home/lobis/apps/ROOT/root-6.16.00/install-gdml/lib/libCling.so
#9  0x00007f48e8e0b0cc in cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) [clone .part.323] () from /home/lobis/apps/ROOT/root-6.16.00/install-gdml/lib/libCling.so
#10 0x00007f48e8e0f8b3 in cling::Interpreter::EvaluateInternal(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) () from /home/lobis/apps/ROOT/root-6.16.00/install-gdml/lib/libCling.so
#11 0x00007f48e8e0fb29 in cling::Interpreter::process(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cling::Value*, cling::Transaction**, bool) () from /home/lobis/apps/ROOT/root-6.16.00/install-gdml/lib/libCling.so
#12 0x00007f48e8eb089f in cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) () from /home/lobis/apps/ROOT/root-6.16.00/install-gdml/lib/libCling.so
#13 0x00007f48e8d7c62a in HandleInterpreterException (metaProcessor=0x556eb9296de0, input_line=0x556ebd59ee10 "#line 1 "ROOT_prompt_4"nstd::cout.rdbuf( old );", compRes=
0x7ffd25a8ead4: cling::Interpreter::kSuccess, result=result
entry=0x7ffd25a8ec00) at /home/lobis/apps/ROOT/root-6.16.00/core/metacling/src/TCling.cxx:2163
===========================================================


Root >
root [5] cout << "text: " << text << endl;

 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007f48f210cffa in __GI___waitpid (pid=28767, stat_loc=stat_loc
entry=0x7ffd25a8bf00, options=options
entry=0) at ../sysdeps/unix/sysv/linux/waitpid.c:29
#1  0x00007f48f20940ab in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:148
#2  0x00007f48f90cc477 in TUnixSystem::Exec (shellcmd=<optimized out>, this=0x556eb8b7f570) at /home/lobis/apps/ROOT/root-6.16.00/core/unix/src/TUnixSystem.cxx:2119
#3  TUnixSystem::StackTrace (this=0x556eb8b7f570) at /home/lobis/apps/ROOT/root-6.16.00/core/unix/src/TUnixSystem.cxx:2413
#4  0x00007f48f90cebc4 in TUnixSystem::DispatchSignals (this=0x556eb8b7f570, sig=kSigSegmentationViolation) at /home/lobis/apps/ROOT/root-6.16.00/core/unix/src/TUnixSystem.cxx:3644
#5  <signal handler called>
#6  0x00007f48f2934d36 in std::ostream::sentry::sentry(std::ostream&) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#7  0x00007f48f2935439 in std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#8  0x00007f48f2935897 in std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#9  0x00007f48fa13e04b in ?? ()
#10 0x00007ffd25a8ec00 in ?? ()
#11 0x00007f48e8d912cf in std::vector<TCling::MutexStateAndRecurseCount, std::allocator<TCling::MutexStateAndRecurseCount> >::emplace_back<>() (this=0x7f48fa13e310) at /usr/include/c++/6/bits/vector.tcc:101
#12 TCling::RewindInterpreterMutex (this=0x7f48fa13e000) at /home/lobis/apps/ROOT/root-6.16.00/core/metacling/src/TCling.cxx:8789
#13 TCling__ResetInterpreterMutex () at /home/lobis/apps/ROOT/root-6.16.00/core/metacling/src/TCling.cxx:385
#14 0x00007ffd25a8ec00 in ?? ()
#15 0x0000556eb8c93460 in ?? ()
#16 0x00007ffd25a8ec00 in ?? ()
#17 0x0000556eb90c04d0 in ?? ()
#18 0x00007f48e8e0b0cc in cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) [clone .part.323] () from /home/lobis/apps/ROOT/root-6.16.00/install-gdml/lib/libCling.so
#19 0x00007f48e8e0f8b3 in cling::Interpreter::EvaluateInternal(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) () from /home/lobis/apps/ROOT/root-6.16.00/install-gdml/lib/libCling.so
#20 0x00007f48e8e0fb29 in cling::Interpreter::process(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cling::Value*, cling::Transaction**, bool) () from /home/lobis/apps/ROOT/root-6.16.00/install-gdml/lib/libCling.so
#21 0x00007f48e8eb089f in cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) () from /home/lobis/apps/ROOT/root-6.16.00/install-gdml/lib/libCling.so
#22 0x00007f48e8d7c62a in HandleInterpreterException (metaProcessor=0x556eb9296de0, input_line=0x556ebd5edf80 "#line 1 \"ROOT_prompt_5\"\ncout << \"text: \" << text << endl;", compRes=
0x7ffd25a8ead4: cling::Interpreter::kSuccess, result=result
entry=0x7ffd25a8ec00) at /home/lobis/apps/ROOT/root-6.16.00/core/metacling/src/TCling.cxx:2163
#23 0x00007f48e8d9376b in TCling::ProcessLine (this=0x556eb8c92a30, line=<optimized out>, error=0x7ffd25a8efec) at /home/lobis/apps/ROOT/root-6.16.00/core/metacling/src/TCling.cxx:2321
#24 0x00007f48f8f7e81d in TApplication::ProcessLine (this=this
entry=0x7ffd25a91460, line=<optimized out>, sync=sync
entry=false, err=err
entry=0x7ffd25a8efec) at /home/lobis/apps/ROOT/root-6.16.00/core/base/src/TApplication.cxx:1031
#25 0x00007f48f682bc5e in TRint::ProcessLineNr (this=this
entry=0x7ffd25a91460, filestem=filestem
entry=0x7f48f683d2b5 "ROOT_prompt_", line=0x556ebd5b6ce0 "cout << \"text: \" << text << endl;", error=0x7ffd25a8efec, error
entry=0x0) at /home/lobis/apps/ROOT/root-6.16.00/core/rint/src/TRint.cxx:746
#26 0x00007f48f682bfaf in TRint::HandleTermInput (this=0x7ffd25a91460) at /home/lobis/apps/ROOT/root-6.16.00/core/rint/src/TRint.cxx:607
#27 0x00007f48f90ce08d in TUnixSystem::CheckDescriptors (this=this
entry=0x556eb8b7f570) at /home/lobis/apps/ROOT/root-6.16.00/core/unix/src/TUnixSystem.cxx:1322
#28 0x00007f48f90cf48b in TUnixSystem::DispatchOneEvent (this=0x556eb8b7f570, pendingOnly=<optimized out>) at /home/lobis/apps/ROOT/root-6.16.00/core/unix/src/TUnixSystem.cxx:1077
#29 0x00007f48f8febc31 in TSystem::InnerLoop (this=0x556eb8b7f570) at /home/lobis/apps/ROOT/root-6.16.00/core/base/src/TSystem.cxx:412
#30 TSystem::Run (this=0x556eb8b7f570) at /home/lobis/apps/ROOT/root-6.16.00/core/base/src/TSystem.cxx:362
#31 0x00007f48f8f7c90f in TApplication::Run (this=this
entry=0x7ffd25a91460, retrn=retrn
entry=false) at /home/lobis/apps/ROOT/root-6.16.00/core/base/src/TApplication.cxx:1183
#32 0x00007f48f682d501 in TRint::Run (this=0x7ffd25a91460, retrn=<optimized out>) at /home/lobis/apps/ROOT/root-6.16.00/core/rint/src/TRint.cxx:460
#33 0x0000556eb7757af9 in main ()
===========================================================


The lines below might hint at the cause of the crash.
You may get help by asking at the ROOT forum http://root.cern.ch/forum
Only if you are really convinced it is a bug in ROOT then please submit a
report at http://root.cern.ch/bugs Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#6  0x00007f48f2934d36 in std::ostream::sentry::sentry(std::ostream&) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#7  0x00007f48f2935439 in std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#8  0x00007f48f2935897 in std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#9  0x00007f48fa13e04b in ?? ()
#10 0x00007ffd25a8ec00 in ?? ()
#11 0x00007f48e8d912cf in std::vector<TCling::MutexStateAndRecurseCount, std::allocator<TCling::MutexStateAndRecurseCount> >::emplace_back<>() (this=0x7f48fa13e310) at /usr/include/c++/6/bits/vector.tcc:101
#12 TCling::RewindInterpreterMutex (this=0x7f48fa13e000) at /home/lobis/apps/ROOT/root-6.16.00/core/metacling/src/TCling.cxx:8789
#13 TCling__ResetInterpreterMutex () at /home/lobis/apps/ROOT/root-6.16.00/core/metacling/src/TCling.cxx:385
#14 0x00007ffd25a8ec00 in ?? ()
#15 0x0000556eb8c93460 in ?? ()
#16 0x00007ffd25a8ec00 in ?? ()
#17 0x0000556eb90c04d0 in ?? ()
#18 0x00007f48e8e0b0cc in cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) [clone .part.323] () from /home/lobis/apps/ROOT/root-6.16.00/install-gdml/lib/libCling.so
#19 0x00007f48e8e0f8b3 in cling::Interpreter::EvaluateInternal(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) () from /home/lobis/apps/ROOT/root-6.16.00/install-gdml/lib/libCling.so
#20 0x00007f48e8e0fb29 in cling::Interpreter::process(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cling::Value*, cling::Transaction**, bool) () from /home/lobis/apps/ROOT/root-6.16.00/install-gdml/lib/libCling.so
#21 0x00007f48e8eb089f in cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) () from /home/lobis/apps/ROOT/root-6.16.00/install-gdml/lib/libCling.so
#22 0x00007f48e8d7c62a in HandleInterpreterException (metaProcessor=0x556eb9296de0, input_line=0x556ebd5edf80 "#line 1 "ROOT_prompt_5"ncout << "text: " << text << endl;", compRes=
0x7ffd25a8ead4: cling::Interpreter::kSuccess, result=result
entry=0x7ffd25a8ec00) at /home/lobis/apps/ROOT/root-6.16.00/core/metacling/src/TCling.cxx:2163
===========================================================

What would be the recommended way to redirect the standard output? I believe the code I tried is the standard way to do it.

Thanks.

This problem seems to come from restRoot’s main function. It turns out that we shouldn’t use any “cout” inside the code, otherwise we cannot use it in the command line. As a hypothesis, cout (a static ostream object) maybe instantiated both by the restRoot executabe and by the cling Interpreter.

I have fixed this problem:

1 Like

I tested, it works for me now.

The lines given by @lobis seem to work now.

However, what we want to achieve is to write the output of a method (::Dump) into the buffer. Dump uses std::cout to output the contents of the class. And we wanted to retrieve any info we want directly from that output and parsing it for the member we are interested in.

Using something like.

std::stringstream dumpOutput;
std::streambuf * old = std::cout.rdbuf(dumpOutput.rdbuf());
anyMetadataClass->Dump();
std::cout.rdbuf(old);

cout << "Dump output: " << dumpOutput << endl;

So that we can build a method TRestMetadata::Get( "memberName" ); using that approach.

It is important that we can access/read the members of the class in that way. At some point that was working with TRestRun with REST_Reflection, but not anymore.

1 Like

Previously I defined TRestRun::Get() to do so. Now I extended this method to be used for any TObject inherited classes.

The usage is:

TRestRun r;
any a = REST_Reflection::GetDataMember(r,"fExperimentName");
string expName = a.ToString();
string memberType = a.type;
char* memberAddr = a.address;

Ok, that’s awesome.

But why I cannot implement it directly in TRestMetadata?

I.e. using something like:

string TRestMetadata::GetDataMember(string memberName) {
        return REST_Reflection::GetDataMember(this, memberName).ToString();
    }

I understand we should implement (repeat) that method in any/all specific metadata classes. There is a way to do that?

You can do it like that. There is no problem.

The problem is that I need to re-implement that method in all specific metadata classes where I want to use the method.

If I implement

string TRestMetadata::GetDataMember(string memberName) {
        return REST_Reflection::GetDataMember(this, memberName).ToString();
    }

It will only return valid values from members from TRestMetadata, as fVersion or fCommit.

But I need to re-implement it again in TRestG4Metadata so that it returns valid values from members inside TRestG4Metadata, as fGenType, etc.

string TRestG4Metadata::GetDataMember(string memberName) {
        return REST_Reflection::GetDataMember(this, memberName).ToString();
    }

I mean, there is an easy way to avoid re-implementing GetDataMember in all the TRestMetadata inherited classes where I want to use it?

string TRestMetadata::GetDataMember(string memberName) {
        return REST_Reflection::GetDataMember(any((char*)this, this->ClassName()), memberName).ToString();
    }

This should be a solution.

1 Like

Thanks, that’s working.