Add TraceMe() to main()

On POSIX systems, you may set CQUERY_TRACEME=1 before running your editor.
cquery will inherit that environment variable and stop itself at the start of main().
This commit is contained in:
Fangrui Song 2018-01-06 23:42:42 -08:00
parent 32d37ede49
commit 0a7b9209df
4 changed files with 20 additions and 1 deletions

View File

@ -383,6 +383,8 @@ void LanguageServerMain(const std::string& bin_name,
// MAIN ////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
int main(int argc, char** argv) {
TraceMe();
std::unordered_map<std::string, std::string> options =
ParseOptions(argc, argv);

View File

@ -48,3 +48,5 @@ void FreeUnusedMemory();
// If true objective-c index tests will be run.
bool RunObjectiveCIndexTests();
void TraceMe();

View File

@ -37,8 +37,11 @@
#if defined(__FreeBSD__)
#include <sys/param.h> // MAXPATHLEN
#include <sys/sysctl.h> // sysctl
#elif defined(__APPLE__) || defined(__BSD__)
#include <sys/ptrace.h>
#elif defined(__linux__)
#include <malloc.h>
#include <sys/ptrace.h>
#endif
#include <iostream>
@ -272,4 +275,12 @@ bool RunObjectiveCIndexTests() {
#endif
}
void TraceMe() {
// If the environment variable is defined, wait for a debugger.
// In gdb, you need to invoke `signal SIGCONT` if you want cquery to continue
// after detaching.
if (getenv("CQUERY_TRACEME"))
raise(SIGTSTP);
}
#endif

View File

@ -146,4 +146,8 @@ bool RunObjectiveCIndexTests() {
return false;
}
// TODO Wait for debugger to attach
void TraceMe() {
}
#endif