免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 9700 | 回复: 2
打印 上一主题 下一主题

gdb 能够attach一个运行的进程是什么原理? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-11-20 09:36 |只看该作者 |倒序浏览
RT,一个运行着的进程为什么可以被attach?gdb也是一个程序啊,一个进程怎么能够直接“侵犯”另外一个进程的空间?

论坛徽章:
0
2 [报告]
发表于 2007-11-20 13:21 |只看该作者
原帖由 Ph0nix 于 2007-11-20 09:36 发表
RT,一个运行着的进程为什么可以被attach?gdb也是一个程序啊,一个进程怎么能够直接“侵犯”另外一个进程的空间?

       The ptrace() system call provides a means by which a parent process may observe and
       control the execution of another process, and examine and change its core image and
       registers.  It is primarily used to implement breakpoint debugging and system call
       tracing.

       The parent can initiate a trace by calling fork(2) and having the resulting  child  
       do  a  PTRACE_TRACEME,  followed  (typically)  by an exec(3).  Alternatively, the
       parent may commence trace of an existing process using PTRACE_ATTACH.

       While being traced, the child will stop each time a signal is delivered, even if the
       signal is being ignored.  (The exception is SIGKILL, which has its usual effect.)  
       The parent will be notified at its next wait(2) and may inspect and modify the child
       process  while it is stopped. The parent then causes the child to continue,
       optionally ignoring the delivered signal (or even delivering a different signal
       instead).

       When the parent is finished tracing, it can terminate the child with PTRACE_KILL or
       cause it to continue executing in a normal,  untraced mode via PTRACE_DETACH.



PTRACE_ATTACH
              Attaches to the process specified in pid, making it a traced
              "child" of the current process; the behavior of the child is
              as if it had  done a PTRACE_TRACEME.  The current process
              actually becomes the parent of the child process for most
              purposes (e.g., it will receive notification of child events
              and appears in ps(1) output as the childas parent), but a
              getppid(2) by the child will still return  the  PID  of the
              original parent.  The child is sent a SIGSTOP, but will not
              necessarily have stopped by the completion of this call;
              use wait() to wait for the child to stop.  (addr and data are
              ignored.)

论坛徽章:
0
3 [报告]
发表于 2007-11-20 21:46 |只看该作者
thnx man!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP