0

我正在尝试将 Boost 1.60.0 库与 Intel Pin 2.14-71313-msvc12-windows 一起使用。以下代码是我尝试的简单实现:

#define _CRT_SECURE_NO_WARNINGS
#include "pin.H"
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>

#include <time.h>

#include <boost/lockfree/spsc_queue.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

   namespace boost_network{
    #include <boost/asio.hpp>
    #include <boost/array.hpp>
}

//Buffersize of lockfree queue to use
const int BUFFERSIZE = 1000;

//Tracefiles for error / debug purpose
std::ofstream TraceFile;

//String wrapper for boost queue
class statement {
public:
    statement(){ s = ""; }
    statement(const std::string &n) : s(n) {}
    std::string s;
};

//string queue to store inserts
boost::lockfree::spsc_queue<statement,     boost::lockfree::capacity<BUFFERSIZE>> buffer; // need lockfree queue for     multithreading

//Pin Lock to synchronize buffer pushes between threads
PIN_LOCK lock;

KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool", "o", "calltrace.txt", "specify trace file name");
KNOB<BOOL>   KnobPrintArgs(KNOB_MODE_WRITEONCE, "pintool", "a", "0", "print call arguments ");

INT32 Usage()
{
    cerr << "This tool produces a call trace." << endl << endl;
    cerr << KNOB_BASE::StringKnobSummary() << endl;
    return -1;
}

VOID ImageLoad(IMG img, VOID *)
{
    //save module informations
    buffer.push(statement("" + IMG_Name(img) + "'; '" + IMG_Name(img).c_str() + "'; " + IMG_LowAddress(img) + ";"));
}

VOID Fini(INT32 code, VOID *v)
{

}

void do_somenetwork(std::string host, int port, std::string message)
{
    boost_network::boost::asio::io_service ios;
    boost_network::boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::address::from_string(host), port);
    boost_network::boost::asio::ip::tcp::socket socket(ios);
socket.connect(endpoint);

boost_network::boost::system::error_code error;
socket.write_some(boost_network::boost::asio::buffer(message.data(), message.size()), error);

socket.close();
}

void WriteData(void * arg)
{
    int popped; //actual amount of popped objects
    const int pop_amount = 10000;
    statement curr[pop_amount];
    string statement = "";

    while (1) {
        //pop more objects from buffer
        while (popped = buffer.pop(curr, pop_amount))
        {
            //got new statements in buffer to insert into db: clean up statement
            statement.clear();

            //concat into one statement
            for (int i = 0; i < popped; i++){
                statement += curr[i].s;
            }

            do_somenetwork(std::string("127.0.0.1"), 50000,     sql_statement.c_str());
        }
        PIN_Sleep(1);
    }
}

int  main(int argc, char *argv[])
{
    PIN_InitSymbols();

    //write address of label to TraceFile
    TraceFile.open(KnobOutputFile.Value().c_str());
    TraceFile << &label << endl;
    TraceFile.close();

    // Initialize the lock
    PIN_InitLock(&lock);

    // Initialize pin
    if (PIN_Init(argc, argv)) return Usage();

    // Register ImageLoad to be called when an image is loaded
    IMG_AddInstrumentFunction(ImageLoad, 0);

    //Start writer thread
    PIN_SpawnInternalThread(WriteData, 0, 0, 0);

    PIN_AddFiniFunction(Fini, 0);

    // Never returns

    PIN_StartProgram();

    return 0;
}

当我构建上述代码时,Visual Studio 找不到 boost_network::boost::asio::ip 并不断给出错误说 asio::ip 不存在。我之前自己发布过这个问题: Sending data from a boost asio client and after using the provided solution in the same workspace,代码运行良好,我能够通过网络进行通信。我不确定这里出了什么问题。由于某种原因,使用不同的命名空间似乎行不通,因为它说 boost 必须在默认命名空间中。但是,如果我不添加命名空间,在这种情况下,该行, KNOB<BOOL> KnobPrintArgs(KNOB_MODE_WRITEONCE, "pintool", "a", "0", "print call arguments "); 抛出一个错误,说 BOOL 不明确。请建议在这种情况下应该是一个可行的解决方案。我正在使用 Visual Studio 2013。只有 Pin 的同一段代码也可以在没有网络部分的情况下工作,我可以将 Pin 生成的数据写入平面文件。

4

0 回答 0