我现在正在研究在 Ubuntu 18.04 中使用 C 进行库插入,并且我正在测试两个简单的代码来包装strlen
:“mystrlen.c”、“mystrlenTest.c”。
这是我写的代码:mystrlen.c
#ifdef RUNTIME
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <dlfcn.h>
/* strlen wrapper function */
size_t strlen(const char *str) {
size_t (*strlenp)(const char *) = NULL;
printf("%s\n", *str);
strlenp = dlsym(RTLD_NEXT, "strlen"); // Get address of libc strlen
printf("length: %ld\n", strlenp(str));
return strlenp(str);
}
#endif
和 mystrlenTest.c:
#include <stdio.h>
#include <string.h>
int main(void) {
char testString[] = "Hello World!";
printf("length of the testString: %ld\n", strlen(testString));
return 0;
}
我尝试在运行时使用以下命令注入:
$ gcc -DRUNTIME -shared -fpic -g -o mystrlen.so mystrlen.c -ldl
$ gcc -g -o mystrlenTest mystrlenTest.c
$ LD_PRELOAD=./mystrlen.so ./mystrlenTest
这就是我得到的全部:Segmentation fault (core dumped)
所以我尝试dmesg
使用命令找出发生了什么,结果如下:
[842291.658267] mystrlenTest[51446]: segfault at 48 ip 00007f7b918e35a1 sp 00007ffdd7158c88 error 4 in libc-2.27.so[7f7b91755000+1e7000]
[842291.658272] Code: 2e 0f 1f 84 00 00 00 00 00 31 c0 c5 f8 77 c3 66 2e 0f 1f 84 00 00 00 00 00 89 f9 48 89 fa c5 f9 ef c0 83 e1 3f 83 f9 20 77 1f <c5> fd 74 0f c5 fd d7 c1 85 c0 0f 85 df 00 00 00 48 83 c7 20 83 e1
我想问的是,我怎样才能用gdb调试这个?或者有没有其他方法可以调试它?我知道如何使用 gdb 调试单个程序,但我在调试运行时库 interpositioned process 时遇到了困难。如果我能找出其中的内容00007f7b918e35a1
,那将是一个很大的帮助。