Skip to content

Commit

Permalink
Initial steps, min. working to attach debugger externally
Browse files Browse the repository at this point in the history
  • Loading branch information
powellsr committed Aug 29, 2024
1 parent c10f8d5 commit 02a06b6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/TiledArray/util/bug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_ =
Expand All @@ -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;
Expand Down Expand Up @@ -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_)
;
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/TiledArray/util/bug.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 02a06b6

Please sign in to comment.