Debugging C++ using instrumentation.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Gurus,
 
I was wondering if there is a method that allows a call to an arbitrary
function to be intercepted and replaced by a new function at runtime /
compile-time / link-time.
 
I am actually writing unit test cases for some classes in my project (C++).
Currently I am forced to write too many stubs for all dependencies and use a
lot of preprocessor directives to get my job done.
This makes testing messy and error prone. 
 
What I want to achieve is provide testing functionality by not changing
anything in the code that is being tested, but if necessary the code could
be instrumented.
 
For example:
 
The project contains two classes A and B:
========================
struct A_a_dependency
{
    int dfunc(int);
};
 
struct B_under_test
{
   A_a_dependency a1;
   int somefunc(int i, int j)
   {
      if(a1.dfunc(i))
      {
        return i + j
      }
      else return 0;
   }
};
 
 
For testing "B_under_test", I would like a1.dfunc(i) to be replaced by
A_stub::dfunc1(int);
 
struct A_stub
{
  int dfunc1(int)
  {
    return 1;
  }
  
  int dfunc2(int)
  {
    return 0;
  }
};
 
============
test_func1()
{
  A_stub mystub;
 
  //Set up call interceptors and preconditions.
  Interceptor Inter(&A_a_dependency::dfunc, &A_stub::dfunc1, &mystub )
 
  B_under_test b;
  b.somefunc(10, 20);
  
  //ASSERT postconditions.
}
 
test_func2()
{
  //Set up call interceptors and preconditions.
  Interceptor Inter(&A_a_dependency::dfunc, &A_stub::dfunc2, &mystub)
 
  B_under_test b;
  b.somefunc(10, 20);
  
  //ASSERT postconditions.
}
 
===========
I stumbled upon "-finstrument-function" option which would allow me to trace
function calls entry and exit.
But I was not able to use it to modify the execution path.
 
I also saw "-fall-virtual" option, that would have allowed me to write stub
classes without modifying anything else.
But this option is no longer available with the newer compiler.
I use "g++ (GCC) 3.3.2"
 
 
Can anybody help,
 
 
Regards,
Shriram.
 



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux