[interceptors] How to intercept when a function is called
Hello, I don't know if there is a first or second class library in Boost to do with this already. Or at least a C++ mechanism I could leverage. Basically, I want to hook or intercept when a function call is made. I do not want to change the underlying call, although I suppose that hook could be done as well. For now, I just want a callback when that function calls is made. For instance, a vector push_back listener. I want to listen when items are push_back-ed onto the vector. The items should still land in the vector. I just want the notification when that occurs. I've dug around for different "callback" flavors, but these just seem to be wrappers to an underlying method pointer, bound function, along these lines. I can do that easy enough but it doesn't buy me the interception when the push back is actually called. The tricky part with push_back as well, is that it comes in two flavors, so there needs to be a stand in lambda, something to declare the actual kind of argument(s) I want. Then there's the matter of Variadics. But for now I am not concerned with either of these. Thank you... Best regards, Michael Powell
On 07/02/2014 03:32 PM, Michael Powell wrote:
Hello,
I don't know if there is a first or second class library in Boost to do with this already. Or at least a C++ mechanism I could leverage.
Basically, I want to hook or intercept when a function call is made. I do not want to change the underlying call, although I suppose that hook could be done as well. For now, I just want a callback when that function calls is made.
For instance, a vector push_back listener. I want to listen when items are push_back-ed onto the vector. The items should still land in the vector. I just want the notification when that occurs.
I've dug around for different "callback" flavors, but these just seem to be wrappers to an underlying method pointer, bound function, along these lines. I can do that easy enough but it doesn't buy me the interception when the push back is actually called.
The tricky part with push_back as well, is that it comes in two flavors, so there needs to be a stand in lambda, something to declare the actual kind of argument(s) I want. Then there's the matter of Variadics. But for now I am not concerned with either of these.
You might have a look at the execute-around pattern. There is an example here: https://gitorious.org/redistd/redistd/commit/8079aa6399d7555a21d1551830c2d22... Essentially, you wrap the object that you care about in another object that implicitly converts to a pointer to the underlying type, but the dereference operator is set up in such a way that you can invoke arbitrary callables before and after each member function invocation. I don't see a way to easily make this function-specific, or for a way to determine inside the hook function which of the member functions was called. Jason
On Wed, Jul 2, 2014 at 2:41 PM, Jason Roehm
On 07/02/2014 03:32 PM, Michael Powell wrote:
Hello,
I don't know if there is a first or second class library in Boost to do with this already. Or at least a C++ mechanism I could leverage.
Basically, I want to hook or intercept when a function call is made. I do not want to change the underlying call, although I suppose that hook could be done as well. For now, I just want a callback when that function calls is made.
For instance, a vector push_back listener. I want to listen when items are push_back-ed onto the vector. The items should still land in the vector. I just want the notification when that occurs.
I've dug around for different "callback" flavors, but these just seem to be wrappers to an underlying method pointer, bound function, along these lines. I can do that easy enough but it doesn't buy me the interception when the push back is actually called.
The tricky part with push_back as well, is that it comes in two flavors, so there needs to be a stand in lambda, something to declare the actual kind of argument(s) I want. Then there's the matter of Variadics. But for now I am not concerned with either of these.
You might have a look at the execute-around pattern. There is an example here:
https://gitorious.org/redistd/redistd/commit/8079aa6399d7555a21d1551830c2d22...
Essentially, you wrap the object that you care about in another object that implicitly converts to a pointer to the underlying type, but the dereference operator is set up in such a way that you can invoke arbitrary callables before and after each member function invocation.
Poor choice of wording I think on my part. Not necessarily that an instance is being called, although that's a compelling thing to think about. I need to know, for instance, in this case, not only the moment when vector is being push_back-ed, but also the item being inserted. Which with a solution like the pointer or dereference overload, would get a grand before-snapshot, but not just after.
I don't see a way to easily make this function-specific, or for a way to determine inside the hook function which of the member functions was called.
It seems like I want to extend std::bind itself, if it were possibly. I'll have a look at that. I can cook up a functor wrapper simple enough. The trick is capturing just what I need and wiring up the callback(s) around the actual bound function call. Thank you...
Jason _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On 7/2/2014 6:00 PM, Michael Powell wrote:
On Wed, Jul 2, 2014 at 2:41 PM, Jason Roehm
wrote: On 07/02/2014 03:32 PM, Michael Powell wrote:
Hello,
I don't know if there is a first or second class library in Boost to do with this already. Or at least a C++ mechanism I could leverage.
Basically, I want to hook or intercept when a function call is made. I do not want to change the underlying call, although I suppose that hook could be done as well. For now, I just want a callback when that function calls is made.
For instance, a vector push_back listener. I want to listen when items are push_back-ed onto the vector. The items should still land in the vector. I just want the notification when that occurs.
I've dug around for different "callback" flavors, but these just seem to be wrappers to an underlying method pointer, bound function, along these lines. I can do that easy enough but it doesn't buy me the interception when the push back is actually called.
The tricky part with push_back as well, is that it comes in two flavors, so there needs to be a stand in lambda, something to declare the actual kind of argument(s) I want. Then there's the matter of Variadics. But for now I am not concerned with either of these.
You might have a look at the execute-around pattern. There is an example here:
https://gitorious.org/redistd/redistd/commit/8079aa6399d7555a21d1551830c2d22...
Essentially, you wrap the object that you care about in another object that implicitly converts to a pointer to the underlying type, but the dereference operator is set up in such a way that you can invoke arbitrary callables before and after each member function invocation.
Poor choice of wording I think on my part. Not necessarily that an instance is being called, although that's a compelling thing to think about. I need to know, for instance, in this case, not only the moment when vector is being push_back-ed, but also the item being inserted.
Which with a solution like the pointer or dereference overload, would get a grand before-snapshot, but not just after.
I don't see a way to easily make this function-specific, or for a way to determine inside the hook function which of the member functions was called.
It seems like I want to extend std::bind itself, if it were possibly. I'll have a look at that. I can cook up a functor wrapper simple enough. The trick is capturing just what I need and wiring up the callback(s) around the actual bound function call.
The Boost signals2 library lets you setup a callback when an event happens. How you define that event and what is called when the event occurs is up to you and you set this up for yourself.
On Wed, Jul 2, 2014 at 5:15 PM, Edward Diener
On 7/2/2014 6:00 PM, Michael Powell wrote:
On Wed, Jul 2, 2014 at 2:41 PM, Jason Roehm
wrote: On 07/02/2014 03:32 PM, Michael Powell wrote:
Hello,
I don't know if there is a first or second class library in Boost to do with this already. Or at least a C++ mechanism I could leverage.
Basically, I want to hook or intercept when a function call is made. I do not want to change the underlying call, although I suppose that hook could be done as well. For now, I just want a callback when that function calls is made.
For instance, a vector push_back listener. I want to listen when items are push_back-ed onto the vector. The items should still land in the vector. I just want the notification when that occurs.
I've dug around for different "callback" flavors, but these just seem to be wrappers to an underlying method pointer, bound function, along these lines. I can do that easy enough but it doesn't buy me the interception when the push back is actually called.
The tricky part with push_back as well, is that it comes in two flavors, so there needs to be a stand in lambda, something to declare the actual kind of argument(s) I want. Then there's the matter of Variadics. But for now I am not concerned with either of these.
You might have a look at the execute-around pattern. There is an example here:
https://gitorious.org/redistd/redistd/commit/8079aa6399d7555a21d1551830c2d22...
Essentially, you wrap the object that you care about in another object that implicitly converts to a pointer to the underlying type, but the dereference operator is set up in such a way that you can invoke arbitrary callables before and after each member function invocation.
Poor choice of wording I think on my part. Not necessarily that an instance is being called, although that's a compelling thing to think about. I need to know, for instance, in this case, not only the moment when vector is being push_back-ed, but also the item being inserted.
Which with a solution like the pointer or dereference overload, would get a grand before-snapshot, but not just after.
I don't see a way to easily make this function-specific, or for a way to determine inside the hook function which of the member functions was called.
It seems like I want to extend std::bind itself, if it were possibly. I'll have a look at that. I can cook up a functor wrapper simple enough. The trick is capturing just what I need and wiring up the callback(s) around the actual bound function call.
The Boost signals2 library lets you setup a callback when an event happens. How you define that event and what is called when the event occurs is up to you and you set this up for yourself.
Yes, I understand that, and appreciate it. However, in this case the
event is the call itself.
The best way I could manage that was to simply wrap a vector (or
deque) of std::function
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On 2 Jul 2014 at 14:32, Michael Powell wrote:
I don't know if there is a first or second class library in Boost to do with this already. Or at least a C++ mechanism I could leverage.
Basically, I want to hook or intercept when a function call is made. I do not want to change the underlying call, although I suppose that hook could be done as well. For now, I just want a callback when that function calls is made.
It sounds awfully like Boost.Signals2? Or do you want something which actively patches your loaded binary to redirect function calls? Niall -- ned Productions Limited Consulting http://www.nedproductions.biz/ http://ie.linkedin.com/in/nialldouglas/
participants (4)
-
Edward Diener
-
Jason Roehm
-
Michael Powell
-
Niall Douglas