Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1320566
  • 博文数量: 179
  • 博客积分: 4141
  • 博客等级: 中将
  • 技术积分: 2083
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-21 20:04
文章存档

2024年(1)

2019年(13)

2016年(1)

2014年(16)

2011年(8)

2010年(25)

2009年(115)

分类: LINUX

2009-03-21 21:09:45

代码摘自 《虚拟机的设计与实现----C/C++》,是实模式下的调试器。但是做得很粗糙,我们这里对他的代码进行了简单的分析。


  1. #include

  2. /*global variables-------------------------------------------------*/
  3. char INT1_STR[]="->SINGLED-STEP<-\n";
  4. char INT3_STR[]="->BREAKPOINT<-\n";
  5. unsigned short rCS,rSS,rDS;
  6. short rAX,rBX,rCX,rDX;
  7. unsigned short rIP;
  8. unsigned char traceOn=0;
  9. /*prototypes-------------------------------------------------------*/
  10. void procCmd(int *lptr);
  11. /*definitions------------------------------------------------------*/

  12. void main()
  13. {
  14.     unsigned short oldInt1Seg;
  15.     unsigned short oldInt1Offset;
  16.     unsigned short oldInt3Seg;
  17.     unsigned short oldInt3Offset;

  18.     goto past_interrupts;

  19.     /* Handle INT 1 -----------------------------------------------*/
  20.     int1:
  21.     __asm
  22.     {
  23.         STI
  24.         MOV rCS,CS /* 保存现场 */
  25.         MOV rSS,SS
  26.         MOV rDS,DS
  27.         MOV rAX,AX
  28.         MOV rBX,BX
  29.         MOV rCX,CX
  30.         MOV rDX,DX
  31.         POP CX
  32.         MOV rIP,CX
  33.         POP DX
  34.         POP AX
  35.         AND AX,65279
  36.         PUSH AX
  37.         PUSH DX
  38.         PUSH CX    
  39.     }
  40.     printf("%s",INT1_STR);
  41.     printf("next instruction at IP=%u\n",rIP);
  42.     {
  43.         int loop = 1;
  44.         while(loop)
  45.         {
  46.             procCmd(&loop);    
  47.         }
  48.     }
  49.     if(traceOn) /* 单步执行下一条指令 */
  50.     {
  51.         __asm
  52.         {
  53.             POP CX
  54.             POP DX
  55.             POP AX
  56.             OR AX,256
  57.             PUSH AX
  58.             PUSH DX
  59.             PUSH CX         
  60.         }    
  61.     }
  62.     __asm
  63.     {
  64.         MOV AX,rAX
  65.         MOV BX,rBX
  66.         MOV CX,rCX
  67.         MOV DX,rDX    
  68.         IRET
  69.     }
  70.     /* Handle INT 3 -----------------------------------------------*/

  71.     int3: /* 同上 */
  72.     __asm
  73.     {
  74.         STI
  75.         MOV rCS,CS
  76.         MOV rSS,SS
  77.         MOV rDS,DS
  78.         MOV rAX,AX
  79.         MOV rBX,BX
  80.         MOV rCX,CX
  81.         MOV rDX,DX    
  82.         POP CX
  83.         POP DX
  84.         POP AX
  85.         AND AX,65279
  86.         PUSH AX
  87.         PUSH DX
  88.         PUSH CX    
  89.     }    

  90.     printf("%s",INT3_STR);
  91.     {
  92.         int loop = 1;

  93.         while(loop)
  94.         {
  95.             procCmd(&loop);    
  96.         }
  97.     }
  98.     if(traceOn)
  99.     {
  100.         __asm
  101.         {
  102.             POP CX
  103.             POP DX
  104.             POP AX
  105.             OR AX,256
  106.             PUSH AX
  107.             PUSH DX
  108.             PUSH CX         
  109.         }    
  110.     }
  111.     __asm
  112.     {
  113.         MOV AX,rAX
  114.         MOV BX,rBX
  115.         MOV CX,rCX
  116.         MOV DX,rDX    
  117.         IRET
  118.     }
  119.     /* Execution path begins here --------------------------------*/
  120.     past_interrupts:
  121.     printf("Save old interrupts SEG:OFF\n");
  122.     __asm
  123.     {
  124.         MOV AH,0x35 /* 保存 int1 的cs:off */
  125.         MOV AL,0x1
  126.         INT 0x21
  127.         MOV oldInt1Seg,ES
  128.         MOV oldInt1Offset,BX
  129.         MOV AH,0x35 /* 保存 int3 的cs:off */
  130.         MOV AL,0x3
  131.         INT 0x21
  132.         MOV oldInt3Seg,ES
  133.         MOV oldInt3Offset,BX
  134.     }
  135.     printf("Load new interrupts SEG:OFF\n");
  136.     __asm
  137.     {
  138.         MOV AH,0x25
  139.         MOV AL,0x1
  140.         PUSH DS
  141.         MOV CX,CS
  142.         MOV DS,CX
  143.         MOV DX,OFFSET int1
  144.         INT 0x21
  145.         POP DS        
  146.         MOV AH,0x25
  147.         MOV AL,0x3
  148.         PUSH DS
  149.         MOV CX,CS
  150.         MOV DS,CX
  151.         MOV DX,OFFSET int3
  152.         INT 0x21
  153.         POP DS
  154.     }

  155.     /* actually do something here to provoke debugger -------------*/
  156.     __asm
  157.     {
  158.         INT 3
  159.         MOV DX,20
  160.         INC DX
  161.         NOP
  162.         MOV DX,3501
  163.         MOV DX,72
  164.         DEC DX
  165.     }

  166.     printf("Re-loading old interrupts SEG:OFF\n");

  167.     __asm
  168.     {
  169.         PUSH DS
  170.         MOV AH,0x25
  171.         MOV AL,0x1
  172.         MOV DS,oldInt1Seg
  173.         MOV DX,oldInt1Offset
  174.         INT 0x21
  175.         POP DS
  176.         PUSH DS
  177.         MOV AH,0x25
  178.         MOV AL,0x3
  179.         MOV DS,oldInt3Seg
  180.         MOV DX,oldInt3Offset
  181.         INT 0x21
  182.         POP DS
  183.     }
  184.     return;
  185. }/*end main*/

  186. void procCmd(int *lptr)
  187. {
  188.     char ch;
  189.     
  190.     traceOn=0;

  191.     printf("dbg>");
  192.     scanf("%c",&ch);
  193.     fflush(stdin);
  194.     
  195.     switch(ch)
  196.     {
  197.         case 'a':
  198.         {
  199.             printf("INT1_STR address=%u\n",INT1_STR);    
  200.         }break;
  201.         case 'd': /* 显示寄存器的内容 */
  202.         {
  203.             printf("CS=%u\n",rCS);
  204.             printf("SS=%u\n",rSS);
  205.             printf("DS=%u\n",rDS);
  206.             printf("AX=%d\n",rAX);
  207.             printf("BX=%d\n",rBX);
  208.             printf("CX=%d\n",rCX);
  209.             printf("DX=%d\n",rDX);
  210.         }break;
  211.         case 'i': /* 对ax进行递增操作 */
  212.         {
  213.             rAX++;
  214.             printf("AX=%d\n",rAX);
  215.         }break;
  216.         case 'm': /* 显示一个16字节长的内存块 */
  217.         {
  218.             int i;
  219.             unsigned long address;
  220.             unsigned long limit = rCS+65535;
  221.             unsigned char *sptr;

  222.             printf("memory address>");
  223.             scanf("%lu",&address);
  224.             fflush(stdin);
  225.             sptr = (unsigned char*)address;
  226.             printf("address=%u\n",sptr);
  227.             if(address > limit)
  228.             {
  229.                 printf("address is beyond .COM segment\n");
  230.             }
  231.             else
  232.             {
  233.                 for(i=0;i<16;i++)
  234.                 {
  235.                     if((sptr[i]>0x20)&&(sptr[i]<0x7F))
  236.                     {
  237.                         printf("byte[%lu]=%c\n",address+i,sptr[i]);
  238.                     }
  239.                     else
  240.                     {
  241.                         printf("byte[%lu]=%X\n",address+i,sptr[i]);     
  242.                     }
  243.                 }
  244.             }
  245.         }break;
  246.         case 'q':
  247.         {
  248.             *lptr=0;
  249.         }break;
  250.         case 't': /* 设置单步调试 */
  251.         {
  252.             traceOn=1;
  253.             printf("trace flag set\n");
  254.             *lptr=0;
  255.         }break;
  256.         default:
  257.         {
  258.             printf("not valid command\n");
  259.         }
  260.     }

  261.     return;

  262. }/*end procCmd*/

阅读(2525) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~