3

I was searching online for something to help me do assembly line profiling. I searched and found something on http://www.webservertalk.com/message897404.html

There are two parts of to this problem; finding all instructions of a particular type (inc, add, shl, etc) to determine groupings and then figuring out which are getting executed and summing correcty. The first bit is tricky unless grouping by disassembler is sufficient. For figuring which instructions are being executed, Dtrace is of course your friend here( at least in userland).

The nicest way of doing this would be instrument only the begining of each basic block; finding these would be a manual process right now... however, instrumenting each instruction is feasible for small applications. Here's an example:

First, our quite trivial C program under test:

main()
{
    int i;

    for (i = 0; i < 100; i++)
    getpid();
}

Now, our slightly tricky D script:

#pragma D option quiet

pid$target:a.out::entry
/address[probefunc] == 0/
{
    address[probefunc]=uregs[R_PC];
}

pid$target:a.out::
/address[probefunc] != 0/
{
    @a[probefunc,(uregs[R_PC]-address[probefunc]), uregs[R_PC]]=count();
}

END
{
    printa("%s+%#x:\t%d\t%@d\n", @a);
}
main+0x1:           1
main+0x3:           1
main+0x6:           1
main+0x9:           1
main+0xe:           1
main+0x11:           1
main+0x14:           1
main+0x17:           1
main+0x1a:           1
main+0x1c:           1
main+0x23:         101
main+0x27:         101
main+0x29:         100
main+0x2e:         100
main+0x31:         100
main+0x33:         100
main+0x35:           1
main+0x36:           1
main+0x37:           1

From the example given, this is exactly what i need. However I have no idea what it is doing, how to save the DTrace program, how to execute with the code that i want to get the results of. So i opened this hoping some people with good DTrace background could help me understand the code, save it, run it and hopefully get the results shown.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Syntax_Error
  • 5,964
  • 15
  • 53
  • 73

1 Answers1

4

If all you want to do is run this particular DTrace script, simply save it to a .d script file and use a command like the following to run it against your compiled executable:

sudo dtrace -s dtracescript.d -c [Path to executable]

where you replace dtracescript.d with your script file name.

This assumes that you have DTrace as part of your system (I'm running Mac OS X, which has had it since Leopard).

If you're curious about how this works, I wrote a two-part tutorial on using DTrace for MacResearch a while ago, which can be found here and here.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
  • thanks a lot! I am actually very curious to look at the explanation of the code in particular! I have linux Ubuntu... if DTrace isnt part of ubuntu ill install it...does the compiled code need any flags? like gcc -pg or does not necessarily? – Syntax_Error Jan 06 '11 at 19:30
  • @Syntax_Error - DTrace needs to be present at the kernel level, and it looks like it's not in Linux due to licensing issues: http://stackoverflow.com/questions/2059311/whats-an-alternative-for-dtrace-on-linux . Your executable doesn't need any special compiler settings, if I recall correctly. As far as what the script does, I'd direct you to the articles I link to, because I describe some of the syntax there and point out other resources for more detailed information. – Brad Larson Jan 06 '11 at 19:45