#include <cstdlib>
#include <iostream>
#include <vector>
#include <string>
using namespace std;

// Command line parsing
#include <getopt.h>

// DyninstAPI includes
#include "BPatch.h"
#include "BPatch_function.h"
#include "BPatch_point.h"

#include "BPatch_object.h"

#include "Type.h"

#ifdef HAVE_SELINUX
#include <selinux/selinux.h>
#endif

using namespace Dyninst;

/* Every Dyninst mutator needs to declare one instance of BPatch */
BPatch bpatch;
//InstSpec is_init;

int main (int argc, char *argv[])
{
    char *inputProgram = NULL;
    BPatch_process* appProc = NULL;
    pid_t pid ;
    char line[512];
    FILE * command = NULL;
    char strPID[100];
    inputProgram = "influxd";

    sprintf(strPID,"pidof -s %s",inputProgram);
    command = popen(strPID,"r");
    fgets(line,512,command);
    pid = strtoul(line,NULL,10);
    pclose(command);

    cerr << "pid of process " << pid << endl;
    BPatch_addressSpace* app2 = bpatch.processAttach (NULL, pid);
    if (app2 == NULL) {
        cerr << "Failed to open process" << endl;
        return EXIT_FAILURE;
    }

    appProc = dynamic_cast<BPatch_process*>(app2);   
    if (appProc == NULL ) {
        cerr << "Failed to open binary" << endl;
        return EXIT_FAILURE;
    }

    cout<<"end mainfunc num " <<endl;
    
    appProc->detach(true);
    appProc->continueExecution();

    return EXIT_SUCCESS;
}
