2015-04-16 40 views
0

我有多次的memcpy的正從不同的地方稱爲過程的memcpy。 有沒有辦法統計有多少的memcpy命令執行過程中已經啓動?如何計算一個C++程序

+0

有很多取決於您的平臺的方式,修改代碼的能力等等。 –

回答

0

如果你正在尋找一個工具,我建議callgrind。 Callgrind是一個概要分析工具,可以將程序運行過程中函數的調用歷史記錄爲調用圖。它是valgrind套件中的免費工具。

文檔可以在這裏找到: callgrind manual

0

沒有什麼便攜。 memcpy調用通常由編譯器處理,對於較小的類型內聯,即使<cstring>標頭直接或間接聲明memcpy表示它是extern而不是inline。因此,有可能是沒有辦法知道是否是因爲在源代碼中的memcpy的生成機器碼。你可能會也可能不會在意。

警告:醜陋的黑客如下

FWIW,如果你能重新編譯應用程序,並只是在做此故障排除或興趣 - 而不是用於生產 - 你可以嘗試破解它如下,只是看它是否發生在你的系統上合計持有:

#include <cstring> 

#include <iostream> 
#include <map> 
#include <string> 
#include <utility> 

std::map<std::pair<std::string, int>, int> g_memcpy_calls; 

namespace std 
{ 
    inline void* 
    instrumented_memcpy(void* dest, void* src, std::size_t count, 
         const char file[], int line) 
    { 
     ++g_memcpy_calls[std::make_pair(file, line)]; 
     return std::memcpy(dest, src, count); 
    } 
} 

// may or may not need it at global scope too...  
inline void* 
instrumented_memcpy(void* dest, void* src, std::size_t count, 
        const char file[], int line) 
{ 
    return std::instrumented_memcpy(dest, src, count, file, line); 
} 

#define memcpy(DEST, SRC, COUNT) \ 
    instrumented_memcpy(DEST, SRC, COUNT, __FILE__, __LINE__) 

int main() 
{ 
    char a, b; 
    for (b = 'x'; b != 'z'; ++b) 
     std::memcpy(&a, &b, sizeof a); 
    std::memcpy(&a, &b, sizeof a); 
    for (auto& where : g_memcpy_calls) 
     std::cout << where.first.first << ':' << where.first.second 
       << " called " << where.second << " times\n"; 
} 

注意:g_memcpy_calls,你可能擺脫使用const char*代替std::string,如果你的編譯器將確保__FILE__返回任何給定的源文件中的所有用途的單一const char*值,但是這只是一個可能出問題的地方還有一點....