how to print execution trace

Hi everyone

I am a newbie to llvm. I want to record the execution (statement/instruction) trace of a program. For example, a C program is like this:

1: sum = 0;
2: for(i = 0; i < 2; i++)
3: sum += i;
4: printf("%d", sum);

The execution trace would be something like 1->2->3->2->3->2->4.

So how to achieve this goal? by writing a llvm pass?

Is there an example about this in the codebase of llvm?

Thank you in advance

Ting Chen