有木有人一起玩调试器的
Tofloor
poster avatar
vala2012
deepin
2012-11-06 11:47
Author
我是新手,刚学编程不久,有木有大神教我如何写这个东东的。

windows下面 实现调试器 .
通过两个函数:WaitForDebugEvent和ContinueDebugEvent

Linux 下的 调试器.
通过 PTRACE,听说这个函数还给内核造就过BUG,不知道是不是骗人的. 不过不可以用ptrace调试内核的init,能的话就悲剧了.


菜鸟感激。

上传编写调试器需要的最佳资料: Linux源码分析-PTRACE.doc
Linux系统调用与ptrace分析.pdf

  1. #include
  2. #include
  3. #include
  4. #include
  5. #include
  6. #include
  7. #include
  8. #include
  9. #include
  10. int main(int argc, char *argv[])
  11. {
  12.     pid_t child;
  13.     const int long_size = sizeof(long);
  14.     child = fork();
  15.     if (child == 0) // child fork.
  16.     {
  17.         ptrace(PTRACE_TRACEME, 0, NULL, NULL);
  18.         execl("./test.out", "test.out", NULL);
  19.     }
  20.     else
  21.     {
  22.     int status;
  23.     union u{
  24.     long val;
  25.     char chars[long_size];
  26.     }data;
  27.     struct user_regs_struct regs;
  28.     int start = 0;
  29.     long ins;
  30.     while (1)
  31.     {
  32.     puts("========================");
  33.     wait(&status);
  34.     if (WIFEXITED(status))
  35.    break;
  36. ptrace(PTRACE_GETREGS, child, NULL, ®s); // 读取寄存器的值
  37. ///////////////////////////////////////////
  38. if (start == 1)
  39. {
  40. // 从内存地址中读取一个字节, 内存地址由 addr给出.
  41. ins = ptrace(PTRACE_PEEKTEXT, child, regs.eip, NULL);
  42. printf("EIP:%lx Instruxction "
  43. "executed: %lx\n",
  44. regs.eip, ins);
  45. }
  46. ////////////////////////////////////////////
  47. if (regs.orig_eax == SYS_write) // 系统调用
  48. {
  49. start = 1;
  50. // 设置单步执行标志.
  51. ptrace(PTRACE_SINGLESTEP, child, NULL, NULL);
  52. }
  53. else
  54. {
  55. ptrace(PTRACE_SYSCALL, child, NULL, NULL);
  56. }
  57. }
  58. }
  59. return 0;
  60. }
Copy the Code

GDB比较老的版本,但是思想已经非常完全.
可以完全看源码学习.代码比较少. 有一些文件我注释了意思.
src-gdb+2.51.tar.gz

arch/arm/include/asm/ptrace.h #define...
arch/x86/include/asm/ptrach.h  寄存器结构 保存点资料,下次好整理和看。

基于Intel Vt技术的Linux内核调试器: http://www.lupaworld.com/space-uid-26540.html
Reply Favorite View the author
All Replies
vala2012
deepin
2012-11-06 12:28
#1
QQ截图20121106044227.png
Reply View the author
cxbii
deepin
2012-11-06 21:00
#2
我还是乖乖玩python
还有你这图坑爹啊
Reply View the author
vala2012
deepin
2012-11-07 01:46
#3
我还是乖乖玩python
还有你这图坑爹啊
每次都看见 坛主累捧场,让我很感动,坛主~~ 哭
Reply View the author
cxbii
deepin
2012-11-07 02:10
#4
[quote]我还是乖乖玩python
还有你这图坑爹啊
每次都看见 坛主累捧场,让我很感动,坛主~~ 哭[/quote]
:
Reply View the author
moling2088
deepin
2012-11-09 03:18
#5
如此专业的问题,上专业编程论坛问问吧
Reply View the author
ibeetv.com
deepin
2012-11-09 06:44
#6
我靠,都玩调试器的人了。
Reply View the author