26 #include <system_error> 32 void redirect_stream_to_fd(
int fd,
int stream)
34 auto rc = ::dup2(fd, stream);
36 throw std::system_error(errno, std::system_category());
39 void print_backtrace(std::ostream& out,
const std::string& line_prefix)
43 out << line_prefix << std::dec << std::setw(2) << frame.
depth() <<
"@" << std::hex << std::setw(14) << frame.
frame_pointer() <<
": " 60 ChildProcess::Pipe stdin_pipe{ChildProcess::Pipe::invalid()};
61 ChildProcess::Pipe stdout_pipe{ChildProcess::Pipe::invalid()};
62 ChildProcess::Pipe stderr_pipe{ChildProcess::Pipe::invalid()};
65 stdin_pipe = ChildProcess::Pipe();
67 stdout_pipe = ChildProcess::Pipe();
69 stderr_pipe = ChildProcess::Pipe();
74 throw std::system_error(errno, std::system_category());
82 stdin_pipe.close_write_fd();
83 stdout_pipe.close_read_fd();
84 stderr_pipe.close_read_fd();
87 redirect_stream_to_fd(stdin_pipe.read_fd(), STDIN_FILENO);
89 redirect_stream_to_fd(stdout_pipe.write_fd(), STDOUT_FILENO);
91 redirect_stream_to_fd(stderr_pipe.write_fd(), STDERR_FILENO);
94 }
catch(
const std::exception& e)
96 std::cerr <<
"core::posix::fork(): An unhandled std::exception occured in the child process:" << std::endl
97 <<
" what(): " << e.what() << std::endl;
101 std::cerr <<
"core::posix::fork(): An unhandled exception occured in the child process." << std::endl;
107 ::exit(static_cast<int>(result));
112 stdin_pipe.close_read_fd();
113 stdout_pipe.close_write_fd();
114 stderr_pipe.close_write_fd();
125 ChildProcess::Pipe stdin_pipe, stdout_pipe, stderr_pipe;
130 throw std::system_error(errno, std::system_category());
139 stdin_pipe.close_write_fd();
140 stdout_pipe.close_read_fd();
141 stderr_pipe.close_read_fd();
144 redirect_stream_to_fd(stdin_pipe.read_fd(), STDIN_FILENO);
146 redirect_stream_to_fd(stdout_pipe.write_fd(), STDOUT_FILENO);
148 redirect_stream_to_fd(stderr_pipe.write_fd(), STDERR_FILENO);
151 }
catch(
const std::exception& e)
153 std::cerr <<
"core::posix::fork(): An unhandled std::exception occured in the child process:" << std::endl
154 <<
" what(): " << e.what() << std::endl;
158 std::cerr <<
"core::posix::fork(): An unhandled exception occured in the child process." << std::endl;
164 ::exit(static_cast<int>(result));
170 stdin_pipe.close_read_fd();
171 stdout_pipe.close_write_fd();
172 stderr_pipe.close_write_fd();
virtual std::size_t depth() const =0
depth returns the depth of this frame in the overall backtrace.
CORE_POSIX_DLL_PUBLIC ChildProcess vfork(const std::function< posix::exit::Status()> &main, const StandardStream &flags)
fork vforks a new process and executes the provided main function in the newly forked process...
Status
The Status enum wrap's the posix exit status.
The Frame class models an individual frame of a backtrace.
StandardStream
The StandardStream enum wraps the POSIX standard streams.
void visit_with_handler(const FrameHandler &handler)
visit_with_handler iterates the backtrace of the calling program, invoking the handler for every fram...
virtual const Symbol & symbol() const =0
symbol returns the symbolic representation of this frame.
virtual std::string raw() const =0
raw The raw symbolic representation of a frame pointer.
CORE_POSIX_DLL_PUBLIC ChildProcess fork(const std::function< posix::exit::Status()> &main, const StandardStream &flags)
fork forks a new process and executes the provided main function in the newly forked process...
virtual std::string demangled() const =0
demangled returns the demangled C++ symbol name or raw.
CORE_POSIX_DLL_PUBLIC std::ostream & cerr() noexcept(true)
Access this process's stderr.
int main(int argc, char *argv[])
virtual void * frame_pointer() const =0
frame_pointer returns the the raw frame pointer of this frame.
The Process class models a child process of this process.
virtual bool is_cxx() const =0
is_cxx checks whether the symbol refers to a mangled C++ symbol.