该主题几乎涵盖了它:使用 Linux Eclipse,我可以通过编程方式告诉我正在调试器 (gdb) 中执行吗?
问问题
133 次
2 回答
2
您可能不得不求助于晦涩难懂的黑客攻击。
例如,您可以检查 /proc/<pid>/status 文件中的“TracerPid”以确定您是否正在被跟踪。
如果您真的想知道您是否被 gdb 跟踪,您可以尝试查看该进程的 exe 链接(但这并不可靠)。
于 2012-08-15T11:56:36.243 回答
2
//=====================================================================
// 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;
}
于 2012-08-15T15:11:38.860 回答