[DynInst_API:] Deleting the snippets after reattaching


Date: Mon, 10 Nov 2014 10:08:49 +0000 (UTC)
From: shruti <tv.shruthi@xxxxxxxxx>
Subject: [DynInst_API:] Deleting the snippets after reattaching
Hi,

We are developing the dynamic instrumentation tool using dyninst API's.

Please help me on this.
 
In the following code I am attaching to process,inserting the printf 
snippet and detaching from it. Again I am reattaching to the same 
process for the sake of deleting the snippet inserted previously.
But I am not able to delete the snippet. Is there any possible ways to 
delete the snippet after reattaching to it.

void usage() {
        cout<<"Please enter the pid and function name"<<endl;
}

int main(int argc , char *argv[])
{
        BPatch bpatch;
        int pid;
        bool ret;
        if (argc != 3) {
                usage();
                exit(1);
        }

        pid=atoi(argv[1]);

        cout<<"The attached pid is "<<pid<<endl;
        cout<<"the function name is "<<argv[2]<<endl;
        BPatch_process *appProc = bpatch.processAttach("",pid);
        BPatch_image *img = appProc->getImage();
        bpatch.setTrampRecursive(true);
        std::vector<BPatch_function *> printFuncs;
        img->findFunction(argv[2],printFuncs);
        if (printFuncs.size() == 0) {
                cout<<"Unable to find function "<<argv[2]<<endl;
                return -1;
        } else {
                 std::vector<BPatch_point *> *points;

                std::vector<BPatch_snippet *> printArgs;
                BPatch_snippet *fmt = new BPatch_constExpr("hello 
world\n");
                printArgs.push_back(fmt);

                std::vector<BPatch_function *> printfFuncs;
                img->findFunction("printf", printfFuncs);
                BPatch_funcCallExpr printfCall(*(printfFuncs[0]), 
printArgs);
                points = printFuncs[0]->findPoint(BPatch_entry);
                if ((*points).size() == 0) {
                        return false;
                } else {
                        cout<<"Got the points "<<endl;
                }
                BPatchSnippetHandle *ptr;
                ptr=appProc->insertSnippet(printfCall, *points);
                if(!ptr)
                {
                        cout<<"Failed to insert the snippet"<<endl;
                } else {
                        cout<<"Snippet is inserted successfully"<<endl;
                }
      }
        appProc->detach(true);
        cout<<"Done."<<endl;

        cout<<"Reattaching will be done again for deletion"<<endl;
        BPatch_process *appProc1 = bpatch.processAttach("", pid);
        BPatch_image *img1 = appProc1->getImage();
        if(img1) {
                cout<<"Successfully got the image again"<<endl;
        } else {
                cout<<"failed to get the image"<<endl;
        }

        std::vector<BPatch_function *> printFuncs1;
        img->findFunction(argv[2],printFuncs1);
        if (printFuncs1.size() == 0) {
                cout<<"Unable to find function "<<argv[2]<<endl;
                return -1;
        } else {
                 std::vector<BPatch_point *> *points1;

                std::vector<BPatch_function *> printfFuncs1;
                img->findFunction("printf", printfFuncs1);
                points1 = printFuncs1[0]->findPoint(BPatch_entry);
                if ((*points1).size() == 0) {
                        return false;
                } else {
                        cout<<"Got the points 2nd time"<<endl;
                }

                BPatch_point pt=*(*points1)[0];
                BPatch_Vector<BPatchSnippetHandle *> childSnippets = 
pt.getCurrentSnippets(BPatch_callBefore);
                if(childSnippets.size()==0){
                        cout<<" No snippets were found "<<endl;
                } else {
                        cout<<"The snippet size is "
<<childSnippets.size()<<endl;
                }
                sleep(50);
                 bool ret=appProc->deleteSnippet(childSnippets[0]);
              if(ret)
              {
                        cout<<"The snippet is deleted"<<endl;
              } else {
                      cout<<"Failed to delete the snippet"<<endl;
              }


                BPatch_point po=*(*points1)[0];


                bool check=appProc1->removeFunctionCall(po);
                if(check) {
                        cout<<"success"<<endl;
                } else {
                        cout<<"failure"<<endl;
                }

        }
        appProc1->detach(true);
        cout<<"Done for second time."<<endl;
        return 0;
}


Thanks,
Shruthi

[← Prev in Thread] Current Thread [Next in Thread→]