2012-08-15 50 views
0

這個主題非常涵蓋它:使用Linux Eclipse,我可以編程地告訴我在調試器(gdb)中執行嗎?使用Linux Eclipse,我可以編程式告訴我正在執行調試器(gdb)嗎?

+0

只是好奇,你到目前爲止嘗試過什麼? (例如:arg [0])你在哪個平臺上? – MartyE 2012-08-15 11:59:04

+0

除谷歌以外什麼也沒試過。和「使用** Linux ** Eclipse,我可以...」 – 2012-08-15 12:25:04

+0

看到這個問題 - 檢測如果gdb正在運行http://stackoverflow.com/questions/3596781/detect-if-gdb-is-running – Tom 2012-08-15 12:37:02

回答

2

你可能不得不求助於隱晦的黑客行爲。

例如,您可以檢查/ proc/<pid>/status文件中的「TracerPid」以確定您是否被盜版。

如果你真的想知道你是否被gdb佔用,你可以試着看看那個進程的exe鏈接(但那不可靠)。

+0

是的!我在下面用我的代碼回答了我自己的問題,對於任何想使用它的人 – 2012-08-15 15:09:31

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; 
} 
+0

謝謝這幫了一大堆忙。儘管如此,如果你還沒有'pid'並且不想經歷麻煩來獲取它,你可以簡單地從'/ proc/self/status'中讀取'self'映射到'過程的pid – 2013-09-04 19:31:54