Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

I want to log every function call and parameter value in my C++ app. Is there a generic approach for this task ? (3rd part library, VAR_ARGS etc.)

share|improve this question

put on hold as too broad by MichaelT, durron597, GlenH7, Snowman, gnat yesterday

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs. If this question can be reworded to fit the rules in the help center, please edit the question.

What you describing usually called tracing, I don't know much about C++, someone in comments suggesting that there might be a flag in compiler. But in case that doesn't work you can search for tracing libraries or something along this line. –  David Sergey Jun 30 '13 at 11:59
Sharing your research helps everyone. We can see what you've already tried, and you end up with a more specific answer to your problem. –  GlenH7 Jun 30 '13 at 12:08
@nirth Agree. If you also log every exit from function, then it is usually called instrumentation. May help uahakan to have another word to search for. –  Marjan Venema Jun 30 '13 at 14:24
SystemTap should solve your problem stap -e 'probe process("/your exe/").function("*").call { log (probefunc()." ".$$parms) }' –  Farhan Shariff yesterday

4 Answers 4

When running on Solaris, freeBSD, MacOS or another system where DTrace is available you can trace all function calls of any programm in this easy manner:

$ dtrace -n 'pid$target:::entry' -c '/pth/to/bin/program --parameters'

to trace a complete program run or

$ dtrace -n 'pid$target:::entry' -p $PID

to attach to a program with it's pid.

One might also do a quick statistic on which functions are called how often:

$ dtrace -n 'pid$target:::entry { @[probefunc] = count(); }' -c ls
dtrace: description 'pid$target:::entry ' matched 3923 probes
dtrace: pid 4529 has exited

  ___lwp_private                                                    1
  __cerror                                                          1
  __charmap_init                                                    1
  __collate_init                                                    1
  __ctype_init                                                      1
  __fsr                                                             1
  lookup_sym_direct                                                90
  lookup_sym_interpose                                             90
  elf_lazy_load                                                    93
  _lookup_sym                                                      95
  elf_hash                                                         95
  lookup_sym                                                       95
  elf_find_sym                                                     99
  strcmp                                                          158
  ___errno                                                        183

There is also an option to automatically indent the code

$ cat test.d
#!/usr/sbin/dtrace -s
#pragma D option flowindent
pid$target:::entry {}
pid$target:::return {}
$ dtrace -stest.d -c ls
dtrace: script 'test.d' matched 7817 probes
dtrace: pid 4591 has exited
CPU FUNCTION                                 
   0  <- rtld_db_preinit                       
   0  <- rd_event                              
   0  -> tsort                                 
   0    -> set_environ                         
   0      -> lookup_sym                        
   0        -> elf_hash                        
   0        <- elf_hash                        
   0        -> _lookup_sym                     
   0          -> core_lookup_sym               
   0            -> callable                    
   0            <- callable                    
   0    -> main                                
   0      -> set_program_name                  
   0        -> elf_rtbndr                      
   0          -> elf_bndr                      
   0            -> enter                       
   0              -> rt_bind_guard             
   0                -> _ti_bind_guard          
   0                  -> mutex_lock            
   0                    -> mutex_lock_impl     
   0                    <- mutex_lock_impl     
   0                  <- mutex_lock            
   0                  -> set_cancel_pending_flag 
   0                  <- set_cancel_pending_flag 
   0                <- _ti_bind_guard          
   0              <- rt_bind_guard             
   0            <- enter                       

and then a ton more of options to filter to get the info you actually need, as getting a log of all functions is a lot, even for a relatively simple program like ls in an empty directory like shown here does about 4000 function calls

share|improve this answer

There is a special compiler flag in certain Microsoft compilers to call a "hook function" at the beginning of every function call for profiling purposes. The hook function can be used to do the logging you require.

This technique was used in the MockItNow project. However, I am not sure if MockItNow is still alive and usable.

You have not mentioned the platform your are using. For unix, similar techniques probably do exist.

share|improve this answer

Do a google search with parameters - "C/C++ built-in prprocessor macros". I know that the GNU and Microsoft provides some built-in macros. One I can remember and as a relevant reference is __function__ macro. Also, check this out.

share|improve this answer

Performing an additional action when functions are called is the main feature of aspect oriented programming. You may be able to achieve what you want by using an AOP extension to C++, eg http://www.aspectc.org/

Unlike using debugging tools or nonstandard compiler options, this would be (at least to some extent)a cross-platform solution.

share|improve this answer

protected by gnat yesterday

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.