GDB break on object function call - c++

I'm debugging an issue, and I want to break on every method call that has a specific object as the 'this' parameter. Is this possible in GDB?

It's easy. You can use command like b A::a if (this==0x28ff1e).

The this parameter should only be the methods that are included in the class itself. So you should just need to set breakpoints for all Of the methods of the class you are looking at. I'm not sure there is a simple way to do that though.

I want to break on every method call that has a specific object as the 'this' parameter
This means that you want to break on every member function of a particular class for which the object has been instantiated.
Let's say for convenience that all the member functions are defined in a particular cpp file such as myclass_implementation.cpp
You can use gdb to apply breakpoint on every function inside myclass_implementation.cpp this way:
rbreak myclass_implementation.cpp:.
Let's say you want to break on some specific functions such as getter functions which start with Get, then you can use gdb to apply breakpoints this way:
rbreak myclass_implementation.cpp:Get*

Related

Get current module inside FunctionPass llvm

I'm writing a function pass in LLVM and need to call the method Module::getOrInsertFunction. I need to access the module of the current function. How do I get it?
You can use getParent() function: http://llvm.org/docs/doxygen/html/classllvm_1_1GlobalValue.html#a9e1fc23a17e97d2d1732e753ae9251ac
Please refer to: http://llvm.org/docs/WritingAnLLVMPass.html
According to the documentation here,
To be explicit, FunctionPass subclasses are not allowed to:
1. Inspect or modify a Function other than the one currently being processed.
2. Add or remove Functions from the current Module.
3. Add or remove global variables from the current Module.
4. Maintain state across invocations of runOnFunction (including global data).
So, you cannot call getOrInsertFunction from inside a FunctionPass. You will need a ModulePass

How to know if function in c++ are unused if they are in a class?

Is there a method to know if a private function isn't used within the definition class?
Also, I need to know if there some public function of the class which is not used outside the class. How can achieve this?
The solution depends heavily on the situation.
In many cases, you can simply grep or ag the source for calls to the function. Also, there are tools like cppcheck that can analyze a source base and tell you this.
However, if you don't have source then there are other methods.
Add code to the functions that will output somehow visibly (perhaps to a logfile) when called. This is what gcc's Code Coverage functions do.
Remove the function and see whether your project still compiles and links.
You should remove the definition (in the cpp file) only, and leave the declaration in the header. Otherwise overload resolution could mask the places where your function is called.

Force error (compile time) if anyone calls a method in C++

DISCLAIMER: CCNode class is part of the cocos2d-x framework, which i didn't desing.
Base class CCNode has a init method:
virtual bool init();
My derived class needs two arguments, so I declare a new init method:
virtual bool init(int, int);
I'd like to enforce the use of the new init(int a, int) instead of the original one.
I know I have the option to call the new one with default parameters, but it doesn't feel right in the context.
I'm searching for a way to tell the user "Call init(int, int) instead" if anyone tries to call that one. I'd rather get that at compile time that at runtime.
I've tried C++11's static_assert(false, "message"), but fails without calling it...
If your really want to prevent someone calling the standard node method I think you should inherit it privately. However, the more cocosy way of doing this would simply be to call the new init from your create method, which is the only one that should be called by outside code when constructing your object anyway.
Sounds like you have source code access, since you tried sticking a static assert in there? The only way I think you can do exactly what you want is to templatize the function in question. Placing a static assert in a templatize function is a good way to ensure it doesn't compile.
Another option would be to hide the declaration in the private section of your class.
Lastly, a run-time assertion is the most common way I ever achieve what you're asking to do.
If you don't have source code access to that init function, then I really don't think you can do what you're asking.

Executable Code in C++ Class

I need to execute code in a class definition in C++. I have a macro to add a method to a function pointer after the definition of the method. It expands to a call to std::map.insert that it needs to execute in the definition (think of how Ruby executes code in class definitions).
The class definition might look something like this after macro expansion:
bindings.insert(pair<char, ReturnType (*)(FirstArgument, SecondArgument)>("theFunction",&theFunction));
Would this execute in the definition?
You need to use a global, its constructor will then execute during program initialization.
See for example these questions and these for how to make sure that map.insert doesn't execute until after the map is constructed.
No, you can't have executable code outside function body in class declaration. (if I understand you correctly).
At which point do you expect it to be executed?
You can put this in the constructor.

How to modify a function behaviour without template method?

I have a function (actually from ATL, it is ATL::CSoapMSXMLInetClient::SendRequest(LPCTSTR)) whose behaviour should slightly be modified. That is, I just have to add one function call somewhere in the middle of the function.
Taking into consideration that this is not a template method, what is the best practice of changing its behaviour? Do I have to re-write the whole function?
Thanks in advance.
EDIT: Deriving from the class ATL::CSoapMSXMLInetClient and copy-pasting whole function code with a slight modification in subclass function definition does not work because most of the members used in ATL::CSoapMSXMLInetClient::SendRequest are "private" and accessing them in subclass is a compile time error.
Rather than best practice I am looking for a way to do it now, if there is any. :(
Yes you will. If it's in the middle of the function there is no way of getting around it.
There are some refactoring methods you can use. But I cannot think of any pretty ones, and all depend heavily on the code within the class, although for you case it might be tough to find any that works.
Like if you have a line:
do_frobnicate();
dingbat->pling();
And you need to call somefunc() after the dingbat plings. You can, if the dingbat is an interface that you provide, make a new dingbat that also do somefunc() when it plings. Given that the only place this dingbat plings is in this function.
Also, if do_frobnicate() is a free function and you want to add the somefunc() after this, you could create a function within the class, or within its namespace that is called the same. That way you make your own do_frobnicate() that also does somefunc().