Hey,
This task confused me a lot, I've work on it for three days
A compiler is a very complicated and complex piece of software. So, don't expect too much after just three days of work. :-)
As for as I know the purpose of GENERIC is simply to provide a way of representing an entire function in trees.
Right, GENERIC is a tree-based, language-independent intermediate-representation used e.g. by the C front-end to represent functions and variable declarations/definitions.
so in my opinion, adding my codes(example printf("the function begins")) as a number of tree nodes into the original source tree could achieve my task
Yes, that would work! But there might be much easier approaches than modifying the C parser. As Ian already suggested, the -finstrument-functions option exactly does what you want to achieve. However, the -finstrument-functions logic doesn't work on the GENERIC trees, but on GIMPLE tuples (GIMPLE is another, simplified intermediate representation used by GCC). For a reference, please check the source file - based on GCC 4.5.0 sources - gcc-4.5.0/gcc/gimplify.c:7631-7657.
But I do not know how, how GCC achieve the relationship between the source code and generic tree?
It would take too long to explain all the details of parsing/scanning, but this "relationship" is established by the C front-end parser in source file c-parser.c.
I hope that helps, Andi