1

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

Alessandro
  • 266
  • 1
  • 2
  • 13
  • 1
    There's no such thing as "the program enter[ing] into a class". What do you mean? Pretty much all executable code is a function of one form or another. Classes can have member functions, including static member functions, but those are still functions. Constructors and destructors are also special kinds of functions. Try `dtrace -l -n 'pid$target:myProgram::entry' -c ./myProgram` to see a list of the function entry points that DTrace knows about. (Since the function name is empty, it matches everything.) – Ken Thomases Mar 24 '15 at 03:13
  • Thank you for this answer, I solved my problem with this. I wanted to know when the program enters in a member function. – Alessandro Mar 24 '15 at 17:07
  • @Alessandro: Please post your solution as an answer to your question. – myaut Mar 26 '15 at 10:28

1 Answers1

3
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

Alessandro
  • 266
  • 1
  • 2
  • 13