From: Justin Seyster Date: Tue, 12 Oct 2010 23:56:33 +0000 (-0400) Subject: Added advice_header.c example plug-in. X-Git-Tag: release-v1.0~28^2~15 X-Git-Url: https://git.fsl.cs.sunysb.edu/?a=commitdiff_plain;h=594bc39fb9d7a527a46b175c8e12ff8c9406a5dc;p=interaspect.git Added advice_header.c example plug-in. --- diff --git a/src/aop-doxy-main.c b/src/aop-doxy-main.c index 2432304..3e97e7d 100644 --- a/src/aop-doxy-main.c +++ b/src/aop-doxy-main.c @@ -78,6 +78,22 @@ * A plug-in adds one or more passes by calling aop_register_pass() * from its main function, which must be named aop_main. * + * See the \ref hello.c "Hello World" sample plug-in for a simple + * example of setting up a pass. + * + * \section advicetype Advice Function Type Safety + * + * When inserting a call to an advice function, InterAspect cannot + * directly check that the arguments passed to the advice match the + * actual advice function's prototype. To avoid type mismatches, it + * is possible to use aop_write_c_header() to generate a header file + * that specifies the prototypes that InterAspect expects for each + * advice function. You can include this header file in the file that + * implements your advice functions to ensure that the types match. + * + * See the \ref advice_header.c "advice_header" sample plug-in for an + * example of automatically generating an advice header. + * * \section memory Memory management * * InterAspect functions return various data structures that are diff --git a/workspace/.gitignore b/workspace/.gitignore index 8d7f82c..3260e01 100644 --- a/workspace/.gitignore +++ b/workspace/.gitignore @@ -1,6 +1,7 @@ *.c *.so -# There is one C file included as an example that should be +# There are some C files included as examples that should be # distributed with InterAspect. !hello.c +!advice_header.c diff --git a/workspace/advice_header.c b/workspace/advice_header.c new file mode 100644 index 0000000..875f4c0 --- /dev/null +++ b/workspace/advice_header.c @@ -0,0 +1,75 @@ +/* This sample plug-in demonstrates how to generate a C header with + prototypes for all the advice functions that InterAspect inserts + call to. + + Whenever you compile a file with this plug-in, it will output an + advice.h header in the current directory. + + Compile this plug-in with: + + make libadvice_header.so +*/ + +#include +#include /* For perror. */ + +AOP_I_AM_GPL_COMPATIBLE(); + +/* Join on a call to printf. */ +static void join_on_printf(struct aop_joinpoint *jp, void *data) +{ + struct aop_dynval *format; + struct aop_dynval *int_arg; + + format = aop_capture_param(jp, 0); + int_arg = aop_capture_param_by_type(jp, 1, aop_t_all_signed()); + + if (int_arg == NULL) { + /* Second argument is not a signed integer. */ + aop_insert_advice(jp, "__printf_advice", AOP_INSERT_BEFORE, + AOP_DYNVAL(format), AOP_TERM_ARG); + } + else { + /* Second argument is a signed integer. */ + aop_insert_advice(jp, "__printf_int_advice", AOP_INSERT_BEFORE, + AOP_DYNVAL(format), AOP_DYNVAL(int_arg), AOP_TERM_ARG); + } +} + +static unsigned int printf_instr_pass() +{ + struct aop_pointcut *pc; + const struct aop_type *char_star; + + char_star = aop_t_pointer_to(aop_t_signed8()); + + pc = aop_match_function_call(); + aop_filter_call_pc_by_name(pc, "printf"); + aop_filter_call_pc_by_param(pc, 0, char_star); + aop_join_on(pc, join_on_printf, NULL); + + return 0; +} + +/* Override aop_finish() for any tasks you want to complete just + before compilation finishes. This is the best place to put a call + to aop_write_c_header(). + + Note that if advice.h already exists, InterAspect will lock it (so + that parallel builds function correctly) and add any prototypes + that it is missing. */ +void aop_finish() +{ + int res; + + /* You may wish to use aop_get_arg_value() to get the name of the + header file from the command line. */ + res = aop_write_c_header("advice.h", "_ADVICE_H_", NULL, NULL); + if (res != 0) + perror("advice.h"); +} + +AOP_MAIN_PROTO aop_main() +{ + aop_register_pass("printf-instr", printf_instr_pass); +}