Everything is possible: dynamically modify your program

zhaozj2021-02-16  62

Everyone knows the code written with a static type C language, after compiling into an executable program, its runtime processing logic is fixed. Unless you stop it, modify the source code itself, recompile the connection, and then restart. Oh, but friends, don't know if you have encountered this situation: Your program is now running is not normal, you now especially want to look at some variables in the program or other things. Worse, your log does not output something you need, you can't stop your app, because you ruin the runtime environment. Moreover, your user may say to you: "Hey, can't move it now, because now is the peak period of business, talk at night!". Poor you, I have to wait until the middle of the night, then modify the program, restart it. Then you go to sleep. The next day, you find that your program still has some problems, but you add output information last night, it may still not meet your positioning procedures. Hey, this may be the worst thing in this world. ^ _ ^, You have to stay again night, still in a chance.

In the Phase 5 of "Programmer", the dynamic language is introduced, and the topic is very good. Yes, "Programmer" magazine is like this, always bringing some fresh things to make up for your knowledge. From these articles, we learned that some dynamic languages ​​have such a function. When the application is not stopped, the rule logic of the program is modified. Oh, enviable. That is, the program written by the C language is not hermed. Of course, it will not, or that sentence, C is omnipotent ^ _ ^ (that is, the ability is limited). We still have some tools to modify the C language written. OK, that is still a stone here, causing everyone to pay attention ^ _ ^.

DynintApi is such an API (C written), which gives you some convenient interface that makes it easy to modify another program. OK, then we combine a simple example, come and see this good Dongdong ^ _ ^.

Write some test code, test_dynamic.c: #include #include

Void old () {Printf ("OLD OLD PID IS% D / N", getPid ());}

Void new () {Printf ("New New New PID IS% D / N", getPid ());}

INT main (void) {while (1) {old (); SLEEP (5);}}

^ _ ^, Simple section of code. This program contains 2 functions, each outputs different print information. From the program we can see that this program is running, calling the OLD function, that is, the output of the program is Old Old ........ This kind of information is so good, we write modification, dynamics Change the program so that the program outputs the print information of the New function.

MODIFY.CPP

#include #include #include #include #include "bpatch.h" #include "bpatch_vector.h" #include "bpatch_thread.h" # INCLUDE "bpatch_snippet.h"

INT Main (int Argc, char ** argv) {bpatch * bpatch = new bpatch; bpatch_vector old_func; bpatch_vector new_func; // This calling first parameter is the name of the program to modify, The two is the process of the program to be modified, pid bpatch_thread * appthread = bpatch-> attachprocess ("test_dynamic", ATOI (Argv [1])); bpatch_image * appimage = AppThread-> getImage ();

/ / Find the OLD function BPATCH_VECTOR * TMP = AppImage-> FindFunction ("OLD", OLD_FUNC);

/ / Find the new function TMP = appimage-> FindFunction in the program you want to modify; // replace the OLD function of the program you want to modify to the new function AppThread-> ReplaceFunction (* Old_func [0], * New_FUNC [0]);

// Detach dropped the program to modify AppThread-> Detach (1);

Compile the program to connect the above. Hey, of course, this program is to connect to the Dyninstapi shared library, which is very simple to compile, and have a very detailed introduction on the API site.

Haha, now run the program we want to be modified, then run our Modify program, follow the process number of the program you want to modify.

I saw it, what happened, our program to be modified now is the information output, wow, god, really great ^ _ ^.

Of course, this API function is not limited to this, it has a lot of excellent functions. Here, I really admire foreigners, people dare to do dare to do, I can't do it.

This API's URL is: http://www.dyninst.org/, I just saw this thing, I also hope to like this thing, exchange, learn and progress together, learn together ^ _ ^.

转载请注明原文地址:https://www.9cbs.com/read-22311.html

New Post(0)