使用Linux Eclipse,我可以以编程方式告诉我在调试器(gdb)中执行吗?

时间:2012-08-15 11:36:24

标签: c++ eclipse gdb

主题几乎涵盖了它:使用Linux Eclipse,我可以以编程方式告诉我在调试器(gdb)中执行吗?

2 个答案:

答案 0 :(得分:2)

你可能不得不求助于模糊的黑客行为。

例如,您可以检查/ proc /< pid> / status文件中的“TracerPid”,以确定您是否被ptraced。

如果你真的想知道你是否被gdb所取代,你可以尝试查看该进程的exe链接(但这不可靠)。

答案 1 :(得分:1)

//=====================================================================
// 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;
}