Line Probes

From OC Systems Wiki!
Revision as of 00:21, 31 March 2017 by Swn (talk | contribs) (How to create lines probes.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

You can probe source lines in your program using the on_line() directive.

You can use the following expressions in an on_line() directive to indicate the line or lines:

  • an absolute source line number, say 10,
  • a relative line number expression using the first and last keywords, or
  • the all keyword.

When using line probes you should consider the effect of changes to the source code. Probing on_entry and on_exit are safe because they are relative to the function/procedure entry and exit and are "immune" to source file changes. Line probes that use absolute line numbers are more fragile because when the source file changes the lines can move "underneath" the one_line probe.

Aprobe supports the “first” and “last” keywords for on_line probes. You can use these in simple expressions to get line numbers relative to the start of the function/procedure. This will protect you from changes elsewhere in the source file, but not in the probed function/procedure itself.

You may have to fiddle a bit to find where the first line in your function/procedure is. Generally, it's the first line that generates code, but in some cases the actual function/procedure signature may be the first line.

Examples=

Here is an example of an absolute line probe:

probe thread
{
   probe "func"
   {
      on_line(100)
      {
         printf("Line 100 reached.\n");
      }
   }
}

Here is an example of on_line(all):

probe thread
{
   probe "func"
   {
      // CAREFUL:  this can slow things down!
      on_line(all)
      {
         printf("on_line:\n");
      }
   }
}


Here is an example of using first and last in on_line probes.

#include <stdio.h>

int proc(int i)
{
   printf("proc(%d)\n", i);
   return i * 5;
}
   
int test ()
{
   int result = 0;   /* first */

   result += proc(1);  /* first + 2 */

   result += proc(2);  /* first + 4 */

   result += proc(3);  /* first + 6 */

   result += proc(4);  /* first + 8 */

   return result;  /* last */
}

int main (int argc, char **argv)
{
   test();
   return 0;
}

Here is the line probe:

probe thread
{
   probe "test"
   {
      on_line (first) printf ("on_line(first)\n");
      on_line (first + 2) printf ("on_line(first+2): ressult =  %d\n", $result);
      on_line (first + 4) printf ("on_line(first+4: ressult =  %d\n", $result);
      on_line (first + 6) printf ("on_line(first+6): ressult =  %d\n", $result);
      on_line (first + 8) printf ("on_line(first+8): ressult =  %d\n", $result);
      on_line (last) printf ("on_line(last): ressult =  %d\n", $result);
   }
}

This will produce the following output:

 
on_line(first)
on_line(first+2): ressult =  0
proc(1)
on_line(first+4: ressult =  5
proc(2)
on_line(first+6): ressult =  15
proc(3)
on_line(first+8): ressult =  30
proc(4)
on_line(last): ressult =  50