Here are some reasons why you may want to use user-defined log formats:
Your format subprogram's parameter profile must conform to the types of the logged data items. This means that, for each logged data item, the format subprogram must have a corresponding formal parameter declared as a pointer to the actual type of the object being logged. We'll discuss more on this later.
Examples\Advanced\User_Formats\Hello.apc
void EnterSubprogramFormat(char *s, int *Year)
{
static int Count = 0;
printf("#%d Enter %s(%d)\n", ++Count, s, *Year);
}
probe thread
{
probe "SayHello"
{
on_entry
log("SayHello", $1) with EnterSubprogramFormat;
}
}
Notice that the log statement has an extra "with EnterSubprogramFormat" part appended to it. This tells aprobe and apformat which subprogram to call when formatting the data that came from this (and only this) log statement. Any other log statements are unaffected by this. Every log statement can have its own format subprogram.
The format subprogram's parameters must conform to the data types being logged by the log statement itself, not to the subprogram being probed. And, if a log statement happens to log, say, 5 items of types A, B, C, D, and E, then the format subprogram must have 5 parameters of types *A, *B, *C, *D, and *E. Notice that these are all pointers to the actual logged data types, not exactly the same as the logged data types. The exception to this rule is arrays (incl. strings), because of the way the C language treats arrays, so a logged array is matched by a parameter of exactly that same array type, not by a pointer to the array.
The log statement in this probe always logs 2 items: a null-terminated string literal, and $1 which denotes the first parameter of SayHello(). In Hello.c, we see that SayHello's first parameter is named Year and is of type int.
Therefore, EnterSubprogramFormat must have 2 parameters of the appropriate types: first a string, secondly a pointer to an int. Recall that a string is a special case, and therefore we pass in a string, not a pointer to a string.
EnterSubprogramFormat may do anything it wants with that data, including printing it out, like this. The more typical scenario, though, is to combine it with data from other format subprograms, and build a large report at the end of formatting.