Printing Variables

From OC Systems Wiki!
Jump to: navigation, search

Printing Variables

Sometimes you just want to print the value of a (complex) variable at runtime. You can do this using the logging facility of Aprobe using immediate formatting. This will allow you to use all the automatic formatting of Aprobe rather than having to manually create printfs to do this.

Normally you log a target program variable using the log() function of Aprobe, and then format the logged data using apformat. You can use immediate formatting to have Aprobe format the data to stdout rather than logging it to the APD file. There are two ways to do this.

Immediate Format Flag

Aprobe provides a command line option, -if, which will cause all logged data to be formatted to stdout instead. This is all or nothing.

For this example program:

package xxx is

  type enum is (a, b, c, d, e, f);
  type enum_arr is array (integer range 1..4) of enum;

  type rec is
    record
      f1 : integer;
      f2 : integer;
      f3 : enum_arr;
      f4 : integer;
    end record;

   procedure p(var : in out rec);

end xxx;

we can put a probe on xxx.p() to log the value of the parameters var like this:

probe thread 
{
  probe "xxx.p"
  {
    on_entry 
    { 
       log("var = ", $var);
    }
  }
}

When we run this with Aprobe and then format data we get:

aprobé -u t.ual main.exe
apformat main.apd
var = {
  f1 = 1
  f2 = 2
  f3 = {
    [1] =    c
    [2] =    c
    [3] =    c
    [4] =    c
  }
  f4 = 4
}

Using the -if option we get the data formatted by Aprobe to stdout:

aprobé -if -u t.ual main.exe
var = {
  f1 = 1
  f2 = 2
  f3 = {
    [1] =    c
    [2] =    c
    [3] =    c
    [4] =    c
  }
  f4 = 4
}

This example is very simple because we had only one probe. If we had many probes logging data we can hurt performance and drown in output if we use the immediate formatting flag.

Selective Immediate Format

To use immediate formatting selectively you can call Aprobe runtime functions to turn immediate formatting on and off as needed.

In the following probe we use ap_ImmediateFormat() and ap_SetImmediateFormat() to query and set the immediate formatting mode.

probe thread 
{
  probe "xxx.p"
  {
    on_entry 
    { 
       ap_BooleanT IF = ap_ImmediateFormat();
       ap_SetImmediateFormat(TRUE);
       log("var = ", $var);
       ap_SetImmediateFormat(IF);
    }
  }
}

This will immediately format the value of the parameter var as follows:

aprobé -u t.ual main.exe
var = {
  f1 = 1
  f2 = 2
  f3 = {
    [1] =    c
    [2] =    c
    [3] =    c
    [4] =    c
  }
  f4 = 4
}