我在运行时插入睡眠功能时遇到了轻微问题。我目前正在尝试使用简单的源代码“sleepdemo.c”和“wakeup.c”在运行时测试库插入。
我写的代码(wakeup.c)如下:
1 #ifdef RUNTIME
2 #define _GNU_SOURCE
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <dlfcn.h>
6 #include <stdlib.h>
7
8 /* sleep wrapper function*/
9 unsigned int wakeup(unsigned int secs)
10 {
11 unsigned int (*wakeupp)(unsigned int secs);
12 char *error;
13
14 wakeupp = dlsym(RTLD_NEXT, "sleep"); //Get address of sleep
15 if((error = dlerror()) != NULL){
16 fputs(error, stderr);
17 exit(1);
18 }
19
20 unsigned int elapsed = sleep(secs);
21 printf("Woke up at %d secs.\n", secs - elapsed+1);
22 return elapsed;
23 }
24
25 #endif
这是我试图插入的代码:
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4
5 int main()
6 {
7 int secs = 1;
8
9 printf("This is sleep demo program.\n");
10 sleep(secs);
11 return 0;
12 }
我已经向我的gcc发出以下说明来创建一个共享库:
gcc -DRUNTIME -shared -fpic -o mysleep.so wakeup.c -ldl
LD_PRELOAD="./mysleep.so" ./wakeup