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;
110 ::_exit(static_cast<int>(result));
115 stdin_pipe.close_read_fd();
116 stdout_pipe.close_write_fd();
117 stderr_pipe.close_write_fd();
128 ChildProcess::Pipe stdin_pipe, stdout_pipe, stderr_pipe;
133 throw std::system_error(errno, std::system_category());
142 stdin_pipe.close_write_fd();
143 stdout_pipe.close_read_fd();
144 stderr_pipe.close_read_fd();
147 redirect_stream_to_fd(stdin_pipe.read_fd(), STDIN_FILENO);
149 redirect_stream_to_fd(stdout_pipe.write_fd(), STDOUT_FILENO);
151 redirect_stream_to_fd(stderr_pipe.write_fd(), STDERR_FILENO);
154 }
catch(
const std::exception& e)
156 std::cerr <<
"core::posix::fork(): An unhandled std::exception occured in the child process:" << std::endl
157 <<
" what(): " << e.what() << std::endl;
161 std::cerr <<
"core::posix::fork(): An unhandled exception occured in the child process." << std::endl;
170 ::_exit(static_cast<int>(result));
176 stdin_pipe.close_read_fd();
177 stdout_pipe.close_write_fd();
178 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.
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.
int main(int argc, char *argv[])
virtual bool is_cxx() const =0
is_cxx checks whether the symbol refers to a mangled C++ symbol.