0

I am trying to use the Boost 1.60.0 library with Intel Pin 2.14-71313-msvc12-windows. The following piece of code is the simple implementation I did to try things out:

#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;
}

When I build the above code, Visual Studio cannot find boost_network::boost::asio::ip and keeps giving error saying asio::ip does not exist. I had previously posted this question myself: Sending data from a boost asio client and after using the provided solution in the same workspace, the code worked fine and I was able to communicate over the network. I am not sure what is going wrong here. For some reason using the different namespace seems to not work out because it says boost must be in the default namespace. However, if I do not add the namespace, in that case the line, KNOB<BOOL> KnobPrintArgs(KNOB_MODE_WRITEONCE, "pintool", "a", "0", "print call arguments "); throws an error saying BOOL is ambiguous. Kindly suggest what should be a viable solution in this situation. I am using Visual Studio 2013. The same piece of code with only Pin also works with out the network part and I can write data generated from Pin into a flat file.

Community
  • 1
  • 1
  • Why do you even have that `namespace boost_network` in there? – Dan Mašek Nov 12 '16 at 13:13
  • Hi,Thank you for the reply. As I mentioned, without the different namespace, there is an error on the line KNOB KnobPrintArgs(KNOB_MODE_WRITEONCE, "pintool", "a", "0", "print call arguments "); saying that BOOL is an ambiguous symbol. This is probably because BOOL is defined in the header files of Pin as well as Boost Asio. So I try to put the asio inside another namespace to use it only when needed. –  Nov 12 '16 at 13:58
  • That comes from [windows headers](https://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx) that asio requires on Windows. Since only `void do_somenetwork(...)` uses asio, wouldn't it make more sense to put this function into a separate compilation unit? Then there's no need for such shinanigans. – Dan Mašek Nov 12 '16 at 14:42
  • Hi,Thanks once again for the comment. I am new to C++ programming. Can you suggest a source from where I can learn what you mentioned in your comment? –  Nov 12 '16 at 16:03
  • http://stackoverflow.com/documentation/c%2b%2b/7211/header-files#t=201611121804325813012 – Dan Mašek Nov 12 '16 at 18:05
  • e.g. make files `foo.cpp` and `foo.hpp`. Put the implementation of your `do_somenetwork` to the cpp file. Put the declaration of that function to the hpp. Add necessary includes to both (string to the hpp, the asio stuff into the cpp). Remove the function and asio includes from your main file, and add `#include "foo.hpp"`. Compile and link. – Dan Mašek Nov 12 '16 at 18:10
  • I doubt the reason network writes are unsuccessful is due to the problem you're describing. It seems much more likely that the culprit is the use of io_service. As far as I know, asio creates an internal thread to manage asynchronous io. This will not fly with Pin, you need to use pin's apis for thread creation (pin_createinternalthread()) – nitzanms Nov 13 '16 at 18:52
  • ... and then call io_service::run() – nitzanms Nov 13 '16 at 18:58
  • @DanMašek I did what you suggested and now I have a lot of unresolved external symbol errors. –  Nov 14 '16 at 10:27
  • @nitzanms I am not sure I understand what you mean by the calling of service thing. Is it possible for you to share an example? Also the do some network function is called from the writed thread function which is called from a new thread created internally from Pin. Isn't that how things should work? –  Nov 14 '16 at 10:28
  • I think the unresolved external symbol errors come because I am on a 64 bit machine and the pin tool I build is 32 bit. It tries to pick up 32 bit dlls from the System32 folder like in a 32 bit machine unlike it should do from the SYSWOW64 folder in the 64 bit machine. @DanMašek –  Nov 14 '16 at 12:46
  • Sorry, I misread your question and understood that you've reached a situation where your code compiles but no network writes happen. It may still happen but this isn't the issue you're currently tackling. – nitzanms Nov 14 '16 at 16:16

0 Answers0