citrun

watch C/C++ source code execute
Log | Files | Refs | LICENSE

commit ab0ac8f6eae21ebb43eb069778636bf626ee443c
parent 33859664c2d4ff096058d7f4a9a268c1682171e2
Author: Kyle Milz <kyle@0x30.net>
Date:   Sun,  1 Jan 2017 12:39:31 -0700

src: move citrun_inst detection to main()

Diffstat:
Msrc/inst_frontend.cc | 25+++----------------------
Msrc/inst_frontend.h | 2+-
Msrc/inst_log.h | 42++++++++++++++++++------------------------
Msrc/inst_main.cc | 23+++++++++++++++++------
4 files changed, 39 insertions(+), 53 deletions(-)

diff --git a/src/inst_frontend.cc b/src/inst_frontend.cc @@ -28,32 +28,20 @@ #include <cstring> // strcmp #include <err.h> #include <fstream> // ifstream, ofstream -#include <libgen.h> // basename #include <sstream> // ostringstream #include <unistd.h> // execvp, fork, getpid, unlink static llvm::cl::OptionCategory ToolingCategory("citrun_inst options"); -InstFrontend::InstFrontend(int argc, char *argv[]) : +InstFrontend::InstFrontend(int argc, char *argv[], bool is_citrun_inst) : m_args(argv, argv + argc), - m_is_citruninst(false), + m_log(is_citrun_inst), + m_is_citruninst(is_citrun_inst), m_start_time(std::chrono::high_resolution_clock::now()) { - char *base_name; struct utsname utsname; - // Protect against argv[0] being an absolute path. - if ((base_name = basename(m_args[0])) == NULL) - err(1, "basename"); - - // Switch tool mode if we're called as 'citrun_inst'. - if (std::strcmp(base_name, "citrun_inst") == 0) { - m_is_citruninst = true; - // Enable logging to stdout. - m_log.set_citruninst(); - } - m_log << ">> citrun_inst v" << citrun_major << "." << citrun_minor; if (uname(&utsname) == -1) m_log << " Unknown OS" << std::endl; @@ -63,13 +51,6 @@ InstFrontend::InstFrontend(int argc, char *argv[]) : m_log << "CITRUN_SHARE = '" << CITRUN_SHARE << "'" << std::endl; - // Always re-search PATH for binary name (in non citrun_inst case). - if (std::strcmp(m_args[0], base_name) != 0) { - m_log << "Switching argv[0] '" << m_args[0] << "' -> '" - << base_name << "'" << std::endl; - m_args[0] = base_name; - } - // Sometimes we're not called as citrun_inst so force that here. setprogname("citrun_inst"); diff --git a/src/inst_frontend.h b/src/inst_frontend.h @@ -6,7 +6,7 @@ class InstFrontend { public: - InstFrontend(int, char *argv[]); + InstFrontend(int, char *argv[], bool); void process_cmdline(); void instrument(); diff --git a/src/inst_log.h b/src/inst_log.h @@ -19,47 +19,41 @@ class InstrumentLogger : public std::ostream class LogBuffer : public std::stringbuf { int m_pid; - std::error_code m_ec; - llvm::raw_fd_ostream m_outfile; + llvm::raw_ostream *m_out; public: - bool m_iscitruninst; - LogBuffer() : - m_pid(getpid()), - m_ec(), - m_outfile("citrun.log", m_ec, llvm::sys::fs::F_Append), - m_iscitruninst(false) + LogBuffer(bool is_citrun_inst) : + m_pid(getpid()) { - if (m_ec.value()) + if (is_citrun_inst) { + m_out = &llvm::outs(); + return; + } + + std::error_code m_ec; + m_out = new llvm::raw_fd_ostream("citrun.log", m_ec, llvm::sys::fs::F_Append); + if (m_ec.value()) { std::cerr << "Can't open citrun.log: " << m_ec.message(); + m_out = &llvm::errs(); + } } virtual int sync() { - if (!m_ec.value()) { - m_outfile << m_pid << ": " << str(); - m_outfile.flush(); - } - - if (m_iscitruninst) - llvm::outs() << str(); - + *m_out << m_pid << ": " << str(); + m_out->flush(); str(""); + return 0; } }; LogBuffer buffer; public: - InstrumentLogger() : + InstrumentLogger(bool is_citrun_inst) : std::ostream(&buffer), - buffer() - { - } - - void set_citruninst() + buffer(is_citrun_inst) { - buffer.m_iscitruninst = true; } }; diff --git a/src/inst_main.cc b/src/inst_main.cc @@ -14,20 +14,31 @@ // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. // #include <cstring> // strcmp +#include <err.h> +#include <libgen.h> // basename #include "inst_frontend.h" // InstFrontend int main(int argc, char *argv[]) { - int ret; + int ret; + char *base_name; + bool is_citrun_inst = false; - if (argc == 2 && (std::strcmp(argv[1], "--print-share") == 0)) { - std::cout << CITRUN_SHARE; - return 0; - } + // Protect against argv[0] being an absolute path. + if ((base_name = basename(argv[0])) == NULL) + err(1, "basename"); - InstFrontend main(argc, argv); + // Switch tool mode if we're called as 'citrun_inst'. + if (std::strcmp(base_name, "citrun_inst") == 0) + is_citrun_inst = true; + + // Always re-search PATH for binary name (in non citrun_inst case). + if (std::strcmp(argv[0], base_name) != 0) + argv[0] = base_name; + + InstFrontend main(argc, argv, is_citrun_inst); main.process_cmdline(); main.instrument();