C4 Syntax
An advice can be applied to a function, introduce new attributes to a struct or union (introduction), introduce a global variable only visible to an aspect (introduction). For readability, though, all aspect code in the C4 woven view use the similar syntax. For example, an before advice starts with the keyword advice_before, followed by the name of the aspect, and then a block of code containing either C statements or declarations. By the same token, after advice uses the keyword advice_after, introduction uses the keyword advice_intro and finally, the around advice users advice_around. In example 1, aspect test has a before advice instrumenting the main() function because the advice is defined before the function body. Advice for struct/union and global are defined the same way, but they have different meanings. When we complete the C4 unweaver/weaver tools, we expect that introductions to structs and unions will always appear at the end of these aggregate types. In the C4 unwoven view, we will leverage the AspectC syntax, which uses keywords like before, after, and around to define advice. For introductions we will need to define new keywords and syntax for the C4 unwoven view, as AspectC did not support those.
Around Advice
In C4, an around advice is explicitly defined by using the advice_around keyword. In this context, the optional proceed(...) call is supported to invoke the function body. An example of around advice can be found in example 4. We would greatly appreciate feedback on the use of around as currently defined.
Example 1
Before and after advice instrumenting on the main() function
int main(void) { advice_before (test) { printf("inside test aspect\n"); int a = 10; }; advice_before (foo) { int foo; foo = 10; }; printf("Not inside an aspect\n"); int i=0; for(; i<10; ++i) { printf("i: %d\n", i); } return 0; advice_after(bar) { printf("Inside bar\n"); printf("bye\n"); }; }
Example 2
Introduction advice on struct/union
struct system { int cpus; float cpuSpeed; advice_intro (mem) { float mem; void *io; }; };
Example 3
Advice introducing new variables, types, and/or functions
advice_intro (network) { /** New type */ typedef struct eth { char *ethName; int type; } ether; /** Global variable */ float speed; /** New function */ void foobarFunction(void *d) { int localVar = 1; char localVar1 = 'A'; printf("Inside foobarFunction: %d, %c\n", localVar, localVar1); } };
Note: that all of the type names, variables names, and functions names defined in an introduction advice will be mangled by C4. But the mangling process is completely invisible to developers, C4 will mangle and demangle the names automatically. Moreover, to make debugging easier for developers, we will write a gdb plugin that can understand the mangled names.
Example 4
Around advice
int func(int var, char *str) { advice_around (test) { printf("Inside before advice\n"); proceed(20,"Testing"); /** go to function body */ printf("Finished proceed\n"); return 10; /** Return value for func */ }; advice_before (test) { printf("Inside test before advice\n"); }; printf("var: %d\n", var); var += 10; return var; advice_after (foo) { printf("Inside after advice\n"); }; }
