c++classdtracedynamic-analysis

Is it possible to probe the entry in a C++ class using Dtrace?


I would like to see when the program enters in a class using Dtrace.

For instance:

dtrace -c './myProgram' -n 'pid$target:myProgram:function:entry'

it fires when the program myProgram enters in the function function, now how can I write a probe that fires when the program enters into a class rather than a function?

I tried: dtrace -c './myProgram' -n 'pid$target:myProgram:className:entry' but it doesn't work


Solution

  • dtrace -c './main' -n 'pid$target:main::entry' -n 'pid$target:main::return'
    

    In this way I can have in output all the functions called at runtime, it will fire at the enter of a function and at its return.

    The code I'm probing is this:

    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    
    using namespace std;
    
    class Polygon {
      protected:
        int width, height;
      public:
        void set_values (int a, int b)
          { width=a; height=b; }
        virtual int area ()
          { return 0; }
    };
    
    class Rectangle: public Polygon {
      public:
        int area()
          { 
            foo();
            return width*height; 
         }
        void foo(){}
    };
    
    class Triangle: public Polygon {
      public:
        int area()
        { 
            foo();
            return width*height/2; 
          }
        void foo(){}
    };
    
    int main () {
    
        //initialize random seed
        srand(time(NULL));
    
        if(rand() % 2)
            {
                Rectangle rect;
                Polygon * ppoly = &rect;
                ppoly->set_values (4,5);
                ppoly->area();
            }
        else
            {
                Triangle trgl;
                Polygon * ppoly = &trgl;
                ppoly->set_values (4,5);
                ppoly->area();
            }
        return 0;
    }
    

    and the dtrace output I get is this:

    CPU     ID                    FUNCTION:NAME
      3 109401                       main:entry 
      3 109404       Triangle::Triangle():entry 
      3 109405         Polygon::Polygon():entry 
      3 109415        Polygon::Polygon():return 
      3 109414      Triangle::Triangle():return 
      3 109403 Polygon::set_values(int, int):entry 
      3 109413 Polygon::set_values(int, int):return 
      3 109406           Triangle::area():entry 
      3 109407            Triangle::foo():entry 
      3 109417           Triangle::foo():return 
      3 109416          Triangle::area():return 
      3 109411                      main:return 
    

    now I'm trying to parse it with a Python script and make an xml of the call tree