自己写调试器 软断点 [Linux]

本文介绍了如何将32位Windows环境下的调试器代码移植到64位Linux系统,利用ptrace系统调用控制进程,并结合dlsym函数获取系统库函数地址。在Linux中,通过分析进程的maps文件找到库函数的虚拟地址,设置软件断点,然后在调试结束后恢复环境,让进程继续执行。
摘要由CSDN通过智能技术生成

    之前写过博文自己写调试器 软断点,但其是基于windows 32位的环境下的,现在自己的笔记本加了内存,顺便让电脑升级到64位的ubuntu,所以就把原来的代码移植到x84_64 Linux下。

   在linux中,我们需要用到ptrace 这个系统调用来实现对进程的控制。同时为了获取系统库中的函数地址,我们需要dlsym等在libdl中的相关函数帮助。

   ptrace 的原型如下:

       long ptrace(enum __ptrace_request request, pid_t pid,
                   void *addr, void *data);


        我们可以看到 ptrace 的地址和数据参数都是 void *,这个是非常灵活的设计,这样ptrace可以根据request的不同来接收、返回不同类型的参数。在Linux中我们能看到大量这样的函数原型。

   我们需要用到下面的 ptrace request,由于用  Python,我们不能直接包含 <sys/ptrace.h>,所以我们就在系统中找到相应的头文件,然后将这个转换到我们的 Python 文件中。64位的 Linux 是在 /usr/include/x86_64-linux-gnu/sys/ptrace.h 中。

   在<sys/ptrace.h>文件中,其 ptrace request 参数是枚举变量,而在 python 中我们无需转换成 python ctypes 库对应的类型即可使用。

''' Indicate that the process making this request should be traced.
     All signals received by this process can be intercepted by its
     parent and its parent can use the other `ptrace' requests.  '''

PTRACE_TRACEME = 0


''' Return the word in the process's text space at address ADDR.  '''
PTRACE_PEEKTEXT = 1


''' Return the word in the process's data space at address ADDR.  '''
PTRACE_PEEKDATA = 2


''' Return the word in the process's user area at offset ADDR.  '''
PTRACE_PEEKUSER = 3


''' Write the word DATA into the process's text space at address ADDR.  '''
PTRACE_POKETEXT = 4


''' Write the word DATA into the process's data space at address ADDR.  '''
PTRACE_POKEDATA = 5


''' Write the word DATA into the process's user area at offset ADDR.  '''
PTRACE_POKEUSER = 6


''' Continue the process.  '''
PTRACE_CONT = 7


''' Get all general purpose registers used by a processes
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值