|
Can I call a
C++ method from a probe?
To call C++ methods in the target program you will have to create a C
wrapper function and link that with the application. The wrapper will
take the C++ object as an explicit parameter and make the method call.
For example, examine the following class definition:
class MyClass
{
public:
MyClass () {a = 10;}
void Show (int b);
void Show (char *b);
private:
int a;
};
If you want to call the first Show method (the one with an int
parameter) you need to write the following wrapper function:
extern void
WrapperForMyClassShowInt(MyClass *o, int b)
{
o->Show(b);
}
You should pick a unique name for the wrapper, particularly if you
are writing a number of wrappers for overloaded methods in the same
class. This wrapper name includes the class, MyClass, the method name,
Show, and an indication of argument types, Int.
The wrapper function includes an explicit parameter for the C++
object pointer of class MyClass, and all the other parameters for the
method call, in this case just an int. The body of the wrapper function
just makes a C++ method call using the C++ object and the method
parameters. If this method returns a value, the wrapper function should
just return that result.
With this wrapper function compiled and linked with your application,
you can call it from APC code.
Here is an example of calling the wrapper defined above:
probe "main"
{
on_line (31)
{
$WrapperForMyClassShowInt(&$m,
20);
}
}
The target expression $WrapperForMyClassShowInt indicates the wrapper
function we wrote above, and the target expression &$m refers to a
C++ object variable in the target program.
Here is the function, main, that this probe targets:
int main (int argc, char **argv)
{
MyClass m;
return 0; /* line 31 */
}
|