0

The subject pretty much covers it: Using Linux Eclipse, can I programatically tell I am executing in the debugger (gdb)?

Wes Miller
  • 2,191
  • 2
  • 38
  • 64
  • Just curious, what have you tried so far? (ex: arg[0]) And what platform are you on? – MartyE Aug 15 '12 at 11:59
  • Tried nothing except google. And "Using **Linux** Eclipse, can I ... " – Wes Miller Aug 15 '12 at 12:25
  • See this question - Detect if gdb is running http://stackoverflow.com/questions/3596781/detect-if-gdb-is-running – Tom Aug 15 '12 at 12:37
  • @WesMiller I'm more likely to give an upvote if you tried a few things. For instance if main is defined as `int main(int argc, char **argv)` attempt to see the output of `argv[0]`. – MartyE Aug 15 '12 at 13:48
  • But @MartyE, I couldn't think of anything to try! That's why I asked. T'is rather the point, isn't it. – Wes Miller Aug 15 '12 at 15:13

2 Answers2

2

You'll probably have to resort to obscure hacking.

For example, you could examine the "TracerPid" in your /proc/<pid>/status file to determine if you are being ptraced.

If you actually want to know whether you're ptraced by a gdb, you could try looking at the exe link of that process (but that's not reliable).

Christian Stieber
  • 9,954
  • 24
  • 23
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;
}
Wes Miller
  • 2,191
  • 2
  • 38
  • 64
  • Thanks this helped a bunch. Just a note though, if you don't already have `pid` and do not want to go through the trouble to get it, you can simply read from `/proc/self/status` as `self` will map to the `pid` of the process – kalelien Sep 04 '13 at 19:31