Calculate the number of variadic parameters for a cloned function

Calculate the number of variadic parameters for a cloned function … here is a solution to the problem.

Calculate the number of variadic parameters for a cloned function

I’m trying LD_PRELOAD linux’s clone function. In my LD_PRELOADed version, I need to record the input parameters before calling the original clone function. However, the problem is that clone requires a variable number of arguments. It states so.

int clone(int (*fn)(void *), void *child_stack, int flags, void *arg, ...
/* pid_t *pid, struct user_desc *tls, pid_t *ctid */ );

Now to pass these parameters to the original clone function, I have to know the number of arguments passed in. What should I do?

Solution

Use the va_* function, which are the methods for handling variadic lists.

This is man page with an example at the end.

Related Problems and Solutions