home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / memory / xm / xmdebg.asm < prev    next >
Encoding:
Assembly Source File  |  1990-10-08  |  6.0 KB  |  367 lines

  1.         .386
  2. ;
  3.         include    xmhd.inc
  4.         include xmmac.mac
  5.         include    xmmsg.def
  6. ;
  7. TraceNum    equ    10
  8. ExeCodeSel    equ    0ch    ;ExeData  - LDT + 4    ;14
  9. ExeDataSel    equ    14h    ;ExeData  - LDT + 4    ;14
  10. ;
  11.         
  12. _DATA        segment    para public USE16 'DATA'
  13.         public    puttracereg, ty_debugger, copy_original
  14.         extrn    comp_buffer:byte, orig_code:dword
  15.         extrn    orig_size:word
  16.         extrn    traceCount: word
  17.         extrn    SavedESP:dword, ThisDataBase:dword
  18.         extrn    SavedEax:dword
  19.         extrn    SavedSS: word
  20.         extrn    EData:sect, ECode:sect
  21.         extrn    DebugFlag:word
  22.         extrn    ErrorOffset:byte
  23.         extrn    SavedOpCode: byte
  24. _DATA        ends
  25.  
  26.         
  27. _TEXT        segment    para public USE16 'CODE'
  28.         assume    cs:_TEXT, ds:_DATA
  29.         extrn    getcode:near
  30.         extrn    osexit:near
  31.         extrn    a20_on:near, a20_off:near
  32.  
  33. Ty_debugger    proc    near
  34.         push    ds
  35.         push    es
  36.         push    cx
  37.         push    si
  38.         push    di
  39.         lea    si, Comp_Buffer
  40.         les    di, orig_code
  41.         mov    cx, orig_size
  42.         cld
  43.     rep    cmpsw
  44.         cmp    cx, 0
  45.         jnz    deb_abort
  46.         pop    di
  47.         pop    si
  48.         pop    cx
  49.         pop    es
  50.         pop    ds
  51.         ret
  52. deb_abort:
  53. IF FMR70
  54.         call    a20_off
  55. ENDIF
  56.         lea    edx, code_modifyed
  57.         call    Msg
  58.         movzx    eax, di
  59.         call    Hex32s
  60.         jmp    OSExit
  61.         int    3
  62.         ret
  63.         endp
  64. ;
  65. Copy_original    proc    near
  66.         push    ds
  67.         push    es
  68.         push    cx
  69.         push    si
  70.         push    di
  71.         mov    ax, _DATA
  72.         mov    es, ax
  73.         lea    di, Comp_Buffer
  74.         mov    cx, orig_Size
  75.         lds    si, orig_code
  76. IF FMR70
  77.         call    a20_on
  78. ENDIF
  79.         cld
  80.     rep    movsw
  81. IF FMR70
  82.         call    a20_off
  83. ENDIF
  84.         pop    di
  85.         pop    si
  86.         pop    cx
  87.         pop    es
  88.         pop    ds
  89.         ret
  90.         endp
  91.  
  92. ;;; **** INT ROUTINES **** ;;;;
  93.  
  94. Hex32R        proc    near
  95.         xchg    al, ah
  96.         rol    eax, 16
  97.         xchg    al, ah
  98.         jmp    Hex32
  99. Hex32R        endp
  100. ;        
  101.         public    PrintTrace
  102. PrintTrace    proc    near
  103.         call    CRLF
  104.  
  105.         mov    ax, SavedSS
  106.         call    Hex
  107.  
  108.         mov    dx, offset ColonS
  109.         call    Msg
  110.         
  111.         mov    eax, SavedESP
  112.         call    Hex32
  113.  
  114.         mov    eax, SavedEAX
  115.         call    Hex32S
  116.  
  117. ;
  118. ; XM itself may be traped. In such case, SS contains different value.
  119. ;
  120.         mov    ax, ss        ; is stack mine ? or programs ?
  121.         mov    bx, ds
  122.         cmp    ax, bx
  123.         jne    .trapEx1
  124. .trapPx1:                ; my own bug !!
  125.         mov    dx, offset Asterisk
  126.         call    Msg
  127.         mov    ebx, SavedESP    ;
  128.         jmp    .trapx1
  129. .trapEx1:        
  130.         mov    ebx, SavedESP
  131.         add    ebx, EData.Base
  132.         sub    ebx, ThisDataBase
  133. .trapx1:
  134.         push    ebx            ; esp phisycal address
  135.         mov    dx, offset SpaceS
  136.         call    Msg
  137.  
  138.         mov    ax, [ebx+6]        ;ds
  139.         call    Hex
  140.  
  141.         mov    dx, offset SpaceS
  142.         call    Msg
  143.  
  144.         mov    ax, [ebx+4]        ;es
  145.         call    Hex
  146.  
  147.         movzx    esi, ErrorOffset
  148.         or    esi, esi
  149.         jz    NoErrCode
  150.  
  151.         mov    dx, offset SpaceS
  152.         call    Msg
  153.  
  154.         mov    ax, [ebx+8]
  155.         call    Hex            ;error code
  156.  
  157. NoErrCode:    mov    dx, offset SpaceS
  158.         call    Msg
  159.  
  160.         mov    ax, [ebx+esi+12]    ;CS
  161.         call    Hex
  162.  
  163.         mov    dx, offset ColonS
  164.         call    Msg
  165.  
  166.         mov    eax, [ebx+esi+8]    ;EIP
  167.         call    Hex32
  168.  
  169.         mov    dx, offset SpaceS
  170.         call    Msg
  171.  
  172.         mov    ax, [ebx+esi+12]    ;cs
  173.         cmp    ax, ExeCodeSel
  174.         mov    ecx, [ebx+esi+8]    ;pointer to code
  175.         je    .progcs
  176. .mycs:
  177.         pop    ebx
  178.         push    ebx
  179.         push    eax
  180.         push    ecx
  181.         mov    eax, cs:[ecx]
  182.         call    Hex32R
  183.         pop    ecx
  184.         mov    eax, cs:[ecx+4]
  185.         call    Hex32R
  186.         pop    eax
  187.         pop    ebx
  188.         jmp    .putCodeContents
  189. .progcs:
  190.         pop    ebx
  191.         add    ecx, ECode.Base
  192.         sub    ecx, ThisDataBase
  193.         mov    eax, [ecx]
  194. ;
  195.         push    edx
  196.         call    getcode
  197. ;
  198.         push    ebx
  199.         push    eax
  200. ;
  201.         push    edx        ; pointer to nemonic
  202.  
  203.         push    ecx
  204.         call    Hex32R
  205.         pop    ecx
  206.         mov    eax, [ecx+4]
  207.         call    Hex32R
  208. ;
  209.         call    PutSpace
  210.         pop    edx        ; pointer to nemonic
  211.         call    Msg
  212. ;        
  213.         pop    eax        ;restore opcode (al keeps next OPcode)
  214.         pop    ebx        ;restore stack pointer
  215.         pop    edx
  216. .putCodeContents:
  217.         ret
  218. PrintTrace    endp
  219.  
  220.  
  221.  
  222. putTraceReg    proc    near
  223.         push    bp
  224.         mov    bp,sp
  225.         pushad
  226.         call    CRLF
  227.         add    ebp, 4*8+4    ; ebp -> user's eax
  228.         mov    cx, 4
  229. .putTr1:
  230.         sub    bp, 4        ; from eax 
  231.         mov    eax, [bp]
  232.         call    Hex32s
  233.         loop    .putTr1
  234.         call    PutSpace
  235.         call    PutSpace
  236.         sub    bp, 4        ; skip esp
  237.         mov    cx, 3
  238. .putTr2:
  239.         sub    bp, 4        ; eax is already shown
  240.         mov    eax, [bp]
  241.         call    Hex32s
  242.         loop    .putTr2
  243.         popad
  244.         pop    bp
  245.         ret
  246. putTraceReg    endp
  247.  
  248. ;
  249. ; single step
  250. ;
  251.         public    PrintCode
  252.         public    Int1, Int3
  253. Int1        proc    near
  254.         push    ds
  255.         pushad
  256.         mov    ax, _DATA
  257.         mov    ds, ax
  258. TraceIt:
  259.         mov    ErrorOffset, 0
  260.         call    PrintTrace
  261.         test    DebugFlag, MASK Deb_Trace_reg
  262.         jz    .trace_01
  263.         call    putTraceReg
  264. ;
  265. ; far call or interrupt is usually system call.
  266. ; so, we do not trace it.
  267. ;
  268. .trace_01:
  269.         cmp    al, 09ah            ;far call?
  270.         je    IsLCall
  271.         cmp    al, 0cdh            ; soft interrupt ?
  272.         je    IsSoftInt
  273.         cmp    word ptr[TraceCount],0
  274.         jbe    TraceDone
  275.         dec    word ptr[TraceCount]
  276.         or    word ptr [ebx+16], 100h
  277.         jmp    PrintCode
  278.  
  279. IsSoftInt:    
  280.         and    word ptr [ebx+16], NOT 100h
  281.         mov    al, byte ptr [ecx+2]
  282.         mov    byte ptr [ecx+2], 0cch
  283.         mov    SavedOpCode, al
  284.         mov    eax, [ecx]            ;???
  285.         jmp    PrintCode
  286. IsLCall:    
  287.         and    word ptr [ebx+16], NOT 100h
  288.         mov    al, byte ptr [ecx+7]
  289.         mov    byte ptr [ecx+7], 0cch
  290.         mov    SavedOpCode, al
  291.         mov    eax, [ecx]            ;???
  292.         jmp    PrintCode
  293. TraceAgain:
  294.         mov    word ptr[TraceCount], TraceNum * 2
  295.         jmp    .traceAgn
  296. TraceAgain10:
  297.         mov    word ptr[TraceCount],TraceNum 
  298. .traceAgn:
  299.         or    word ptr [ebx+16], 100h
  300.         mov    al,2eh
  301.         call    putchar
  302.         jmp    PrintCode
  303. TraceDone:    
  304.         call    GetChar
  305.         and    al, not 20h        ; toupper(inkey)
  306.         cmp    al, 'P'
  307.         je    TraceAgain
  308.         cmp    al, 0
  309.         je    TraceAgain10
  310.         cmp    al, 'R'
  311.         jz    .TraceRg
  312.         cmp    al, 'C'
  313.         jz    .TraceCM
  314.         cmp    al, 'D'
  315.         je    .TraceDb
  316.         jmp    .traceHalt
  317. .Tracedb:
  318.         call    Ty_debugger
  319.         jmp    TraceDone
  320. .TraceRg:        
  321.         call    putTraceReg
  322.         xor    DebugFlag, MASK Deb_Trace_reg
  323.         jmp    TraceDone
  324. .TraceCM:        
  325.         xor    DebugFlag, MASK Sys_Out_Com
  326.         jmp    TraceDone
  327. .traceHalt:        
  328.         call    CRLF
  329.         and    word ptr [ebx+16], NOT 100h
  330. PrintCode:
  331.         popad
  332.         pop    ds
  333.         ret
  334. ;        iret
  335. Int1        endp
  336. ;
  337. ; this routine will be re-written (probably by TC) soon.
  338. ;
  339. ;
  340. ;
  341. Int3        proc    near
  342.         push    ds
  343.         pushad
  344.         mov    ax, _DATA
  345.         mov    ds, ax
  346.         mov    ebx, SavedESP
  347.         add    ebx, EData.Base
  348.         sub    ebx, ThisDataBase
  349.         mov    ecx, [ebx+8]    ;pointer to code
  350.         dec    ecx
  351.         mov    [ebx+8], ecx
  352. ;
  353. ;    get original code (from table - in future)
  354. ;        
  355.         mov    al, SavedOpCode
  356. ;
  357.         add    ecx, ECode.Base
  358.         sub    ecx, ThisDataBase
  359.         cmp    byte ptr[ecx], 0cch    ; is it really debug int ?
  360.         jnz    TraceIt
  361.         mov    [ecx], al
  362.         jmp    TraceIt
  363. Int3        endp
  364.  
  365. _TEXT        ends
  366.         end    
  367.