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, no registration required.

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
    
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. –  nirth Jun 30 '13 at 11:59
1  
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
add comment

3 Answers

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
add comment

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
add comment

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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