开发者

How to Patch Live Running Unix Code

Let's say you have a function foo() compiled into a program that is running on Unix.

While the program is running, can one "replace" the function foo by dynamically loading an object file containining a modified version of foo()?

On an embedded system I worked on in the past, we could unprotect the text segment and then essentiall开发者_JAVA技巧y "patch" the address of foo() to point to the newly modified foo().

It was used for debugging on occasion and with lots of special constraints, on customer sites.

Is this possible on Unix?


It depends on the environment, I suppose. I know that hot-swapping production code is trivial in Erlang modules and not too difficult in Ruby. C might be a different animal.


Yes. That's how debuggers like gdb work, after all.


The short of it is yes, of course it's possible. The question should really be, "how difficult?"

You can load & unload shared libraries (.so & .DLL) all you want on Linux & Windows. Specific variants of UNIX, I'm not sure about. This would be the easiest way to achieve your goal.

If you don't mind getting your hands dirty, you can always patch up the code segment to jump to someplace else out on the heap. I don't recommend it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜