2013-09-30 23 views
1

打印我想寫一個DTrace探測器,將匹配函數std::string參數並打印字符串的內容:怎樣的libstdC++字符串內容使用DTrace

void func(std::string some) { 
    /* some code here */ 
} 

我試圖執行探頭like this

pid$target::func(std??string):entry 
{ 
    this->str = *(uintptr_t*)copyin(arg1, sizeof(char*)); 
    printf("arg1 %s", copyinstr(this->str)); 
} 

不幸的是,這並不適用於我,dtrace報告它檢測到無效地址。另外,這裏還有另一個問題 - libstdC++中的字符串在寫入時使用拷貝,所以僅僅在這裏處理指針是不夠的。有人知道該怎麼做嗎?我在mac os x上使用dtrace。

回答

0

我能夠自己找到一個工作解決方案。我的問題中的探針有一個愚蠢的錯誤 - arg0應該被複制而不是arg1。因此,工作探頭:

pid$target::func(*):entry 
{ 
    this->str = *((uintptr_t*)copyin(arg0, sizeof(void*))); 
    printf("darg %s", copyinstr(this->str)); 
} 

在另一方面成員函數ARG1應使用:

class some { 
    public: 
     void func(const std::string arg) { 
      std::cout << "arg " << arg << std::endl; 
     } 
}; 

探針功能some::func應該是這樣的:

pid$target::some??func(*):entry 
{ 
    this->str = *((uintptr_t*)copyin(arg1, sizeof(void*))); 
    printf("darg %s", copyinstr(this->str)); 
} 

該作品用於libC++和libstdC++ std::string類。如果使用引用字符串,它甚至可以工作。