From 02a06b6e1cc52ea1e07d517efa572956817b0389 Mon Sep 17 00:00:00 2001 From: Samuel Powell Date: Thu, 29 Aug 2024 16:12:51 -0400 Subject: [PATCH] Initial steps, min. working to attach debugger externally --- src/TiledArray/util/bug.cpp | 48 +++++++++++++++++++++++++++++++++++++ src/TiledArray/util/bug.h | 3 +++ 2 files changed, 51 insertions(+) diff --git a/src/TiledArray/util/bug.cpp b/src/TiledArray/util/bug.cpp index 5e58ba667c..22b61843c7 100644 --- a/src/TiledArray/util/bug.cpp +++ b/src/TiledArray/util/bug.cpp @@ -180,6 +180,11 @@ void Debugger::default_cmd() { } } +const std::string Debugger::gdb_cmd_ = + "gdb -ex \"set variable debugger_ready_=1\" --pid=$(PID) $(EXEC)"; +const std::string Debugger::lldb_cmd_ = + "lldb -p $(PID) -o \"expr debugger_ready_=1\""; + void Debugger::resolve_cmd_alias() { if (cmd_ == "gdb_xterm") { cmd_ = @@ -192,6 +197,28 @@ void Debugger::resolve_cmd_alias() { } } +std::string Debugger::replace_macros(std::string str) { + if (!str.empty()) { + int pid = getpid(); + std::string::size_type pos; + std::string pidvar("$(PID)"); + while ((pos = str.find(pidvar)) != std::string::npos) { + std::string pidstr; + pidstr += std::to_string(pid); + str.replace(pos, pidvar.size(), pidstr); + } + std::string execvar("$(EXEC)"); + while ((pos = str.find(execvar)) != std::string::npos) { + str.replace(pos, execvar.size(), exec_); + } + std::string prefixvar("$(PREFIX)"); + while ((pos = str.find(prefixvar)) != std::string::npos) { + str.replace(pos, prefixvar.size(), prefix_); + } + } + return str; +} + void Debugger::set_cmd(const char *cmd) { if (cmd) { cmd_ = cmd; @@ -262,6 +289,27 @@ void Debugger::debug(const char *reason) { ; } } + } // Here, need handling of cmd_ empty + if (sleep_) { + std::cout << prefix_ << "Debugger: sleeping " << sleep_ + << " seconds to wait for debugger ..." << std::endl; + sleep(sleep_); + } + if (wait_for_debugger_) { + std::cout << prefix_ << "Debugger: waiting for the user ..."; + if (cmd_.empty()) { + std::cout << " attach debugger to process " + << std::to_string(getpid()) + << " as follows:" << std::endl + << prefix_ << "Debugger: - if using gdb: " + << replace_macros(gdb_cmd_) << std::endl + << prefix_ << "Debugger: - if using lldb: " + << replace_macros(lldb_cmd_); + } + + std::cout << prefix_ << ": waiting for the user ..." << std::endl; + while (!debugger_ready_) + ; } } diff --git a/src/TiledArray/util/bug.h b/src/TiledArray/util/bug.h index 2d217ceaee..0aadd3927e 100644 --- a/src/TiledArray/util/bug.h +++ b/src/TiledArray/util/bug.h @@ -384,6 +384,9 @@ class Debugger { private: /// Replaces alias in cmd_ with its full form void resolve_cmd_alias(); + std::string replace_macros(std::string cmd); + static const std::string gdb_cmd_; + static const std::string lldb_cmd_; }; /// Use this to create a Debugger object and make it the default