开发者社区> 问答> 正文

如何调试运行时库插入的进程?

如何调试运行时库插入的进程? 我现在正在研究在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调试单个程序,但是在调试运行时库插入的进程时遇到了困难。如果我能找到其中的内容00007f7b918e35a1,那将是很大的帮助。

展开
收起
kun坤 2019-11-29 10:49:23 408 0
1 条回答
写回答
取消 提交回答
  • 给出以下代码:

    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);
    }
    
    

    如果在内部printf()使用strlen(),则将获得无限递归,并且几乎可以肯定会发生分段违规。

    请注意,在GLIBC中,libc.soLinux 上使用的printf()是通过实现的vfprintf(),的确确实使用strlen()。

    #https://stackoverflow.com/questions/59097526/how-can-i-debug-runtime-library-interpositioned-process

    2019-11-29 10:49:47
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
服务上云加速大家居产业C2M进程 立即下载
小程序 大世界 立即下载
《15分钟打造你自己的小程序》 立即下载

相关实验场景

更多