1

Given any function like

void f(int a, int b, char c) {


---macro here----

}

now DBG_ARGS should have output like
f(a = 1, b = 2, c = A)

Can we use VA_LIST, #x and __FUNCTION__ to accomplish this?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    Probably not, unless you think `DBG_ARGS(a, b, c)` is ok. – Bo Persson Oct 04 '15 at 15:33
  • @BoPersson Thanks. how close can I come to this sort of debug print. Generally while debugging recursive functions, I want to see the arguments being passed and return value being sent. How to solve this problem if not the one mentioned in the question. – Code_Complete Oct 04 '15 at 15:44
  • 1
    You can look at [C `#define` macro for debug printing](http://stackoverflow.com/questions/1644868/) for a lot of ideas. It is for C rather than C++, but most of it transfers over. You need to decide how you'll handle the types. If you handle a fixed number of values of the same type (3 `int`), then it's straight-forward; if you handle a variable number of values of the same type, you have to work harder; if you handle a fixed number of values of different types, you have to work harder again; and if you have a variable number of values of different types, you have to work hardest of all. – Jonathan Leffler Oct 04 '15 at 17:04
  • 1
    You should probably look at the Boost preprocessor 'library'; it has code that allows you to iterate over the variable length argument list. – Jonathan Leffler Oct 04 '15 at 17:05

1 Answers1

1

I don't think you can do this with a straight macro alone in the general case, but you could do it using a call to another function.

If you look at the manual page for backtrace, you will find a glibc specific routine to dump the stack. If you call another function Y from your current function X, and that function dumps the stack, then the second line of the stack dump will be the arguments to function X (make sure the compiler does not inline the function).

Sadly this method isn't particularly portable. OS-X and Linux have different backtrace type implementations, and I'm sure Windows does too.

The va_list-esque macros only work with variadic functions. The example you gave is not a variadic function. I'm not exactly aware what VA_LIST (upper case) does (perhaps that's compiler specific), but google suggests it's pretty much the same.

abligh
  • 24,573
  • 4
  • 47
  • 84