Self-modifying code for tracking hooks?

I am looking for the least costly way to insert trace / logging traces into some kind of very performance-sensitive driver code. This logging material should always be compiled, but do nothing most of the time (but do nothing very quickly).

There is nothing easier than just having a global word on / off by doing if(enabled){log()}. However, if possible, I would even avoid the cost of downloading this word every time I clicked on one of my hooks. It seems to me that I can potentially use self-modifying code for this - that is, wherever I have a call to my trace function, I rewrite the jump with NOP when I want to disable the interceptors, and replace the jump when I want to enable them.

Fast google doesn't show any prior art - has anyone done this? Is it possible, are there any major stumbling blocks that I do not foresee?

(Linux, x86_64)

+3
source share
4 answers

Yes, this method was implemented in the Linux kernel in exactly the same way (hook tracing).

. LWN .

- , : ( , ); ( I- ).

+4

, ?

- , . , (), , .

+4

- , , . , , .

0

I’m writing not so much about whether this is possible or not, but if you get something meaningful.

On the one hand, you don’t want to check the “logging” every time you have the opportunity to log, and on the other hand, you check the “on record” and rewrite the code with the code “yes” or “no”, or your driver “remembers”, that this was not the last time, and since this time it is not requested that nothing needs to be done?

The necessary logic does not seem trivial compared to testing every time.

0
source

Source: https://habr.com/ru/post/1782526/


All Articles