这个主题几乎涵盖了它:使用Linux Eclipse,我能以编程方式判断我正在调试器(gdb)中执行吗?
您可能不得不求助于晦涩难懂的黑客攻击。
例如,您可以检查/proc/
如果你真的想知道你是否被gdb跟踪,你可以尝试查看该过程的exe链接(但这并不可靠)。
//=====================================================================
// effectively performs `cat /proc/$pid/status | grep TracerPid`
//=====================================================================
bool RunningInDebugger( pid_t pid )
{
std::string line = "";
std::string pidFileName = "";
size_t y = 0;
size_t x = 0;
bool rc = FALSE;
pidFileName = "/proc/";
pidFileName = pidFileName.append( NumToStr( pid ).c_str() );
pidFileName = pidFileName.append( "/status" );
std::ifstream pidFile (pidFileName.c_str() );
if ( pidFile.is_open() )
{
while ( pidFile.good() )
{
getline (pidFile,line);
x = line.find( "TracerPid:" );
if ( x != std::string::npos )
{
line = line.substr( x+11 ); // length of "TracerPid:" + 1
x = line.find_first_not_of( " t" ); // find first non whitespace character
y = line.find_first_of( " ", x ); // next whitespace
if ( std::string::npos == y ) // can't find trailing spaces that aren't there
y = line.size() - 1;
rc = atoi( line.substr( x, y-x+1 ).c_str() );
pidFile.close(); // pidFile will no longer be "good"
}
}
pidFile.close();
}
else // File open failed
rc = FALSE;
return rc;
}