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)
source
share