home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 25 / nopv25.iso / 040A / CCDL151L.ZIP / MSDOS / DEBUG / MTRAP.ASM < prev    next >
Encoding:
Assembly Source File  |  1997-06-14  |  4.9 KB  |  234 lines

  1.     ;MASM MODE
  2.     .386p
  3.     .model small
  4.  
  5. include  prints.ase 
  6. include  regs.ase 
  7. include  input.ase 
  8. include  breaks.ase 
  9.  
  10.     PUBLIC    reflags,dreax,drebx,drecx,dredx
  11.     PUBLIC    dresi,dredi,drebp
  12.     PUBLIC    dresp,dreip
  13.     PUBLIC    monitor_init, CRLF, rtoss, dres,drds
  14.     extrn put_msg : proc, trap : proc
  15.  
  16.     .data
  17. ;
  18. ; CPU instruction trap enable flag
  19. ;
  20. TRAPFLAG = 100h
  21. ;
  22. ; Macro which sets things up to call the generic trap handler
  23. ;
  24. TRAP    MACRO    num,error,clflag
  25.     local    notrace
  26. entry&num:
  27.     push    ds        ; Switch to system data seg
  28.     mov    ds,cs:[sysds]
  29.     mov    [trapnum],num    ; Save trapnu,
  30.     ifnb    <error>        ; If it has an error# on stack
  31.     inc    [haserr]    ; Set the error flag
  32.     endif
  33.     ifnb    <xclflag>    ; If is int #1
  34.     test    DWORD PTR [esp + 12], TRAPFLAG; See if trap is set in flags
  35.     jz    short notrace    ; No, not tracing
  36.     or    [tracing],1    ; Else set tracing flag
  37. notrace:
  38.     and    DWORD PTR [esp + 12], NOT TRAPFLAG ; Reset trap flag
  39.     endif
  40.     jmp    traphandler    ; Jump to trap handler
  41.     ENDM    
  42.  
  43. ;
  44. ; List of all trap handlers
  45. ;
  46. tsvects    dd    16        ; Not allowing TRAP 16 because is video int
  47.     dd    entry0,entry1,entry2,entry3,0,0
  48.     dd    entry6,entry7,0,0,0,0
  49.     dd    entry12,entry13,entry14,0,0
  50. ;
  51. ; Register image
  52. ;
  53. reflags    dd    0
  54. dreax    dd    0
  55. drebx    dd    0
  56. drecx    dd    0
  57. dredx    dd    0
  58. dresi    dd    0
  59. dredi    dd    0
  60. drebp    dd    0
  61. dresp    dd    0
  62. dreip    dd    0
  63. drds    dw    0
  64. dres    dw    0
  65. rtoss    dd    0
  66. sysds    dw    0
  67. ;
  68. haserr    dw    0    ; If there is an error# on stack
  69. errnum    dw    0    ; The error#
  70. trapnum    dw    0    ; The trap#
  71. tracing    db    0    ; True if tracing
  72. proctrap db    'Trap: ',0
  73. CRLF    db    10,13,0
  74. procerr    db    'Error: ',0
  75. banner    db    'Debug/386, version 1.10 (DPMI) Copyright (c) 1997, LADsoft'
  76.     db    10,13,0
  77.  
  78.     .code
  79. ;
  80. ; Save an image of the regs
  81. ; This MUST BE the first thing the trap handler calls; it assumes
  82. ; there is ONE PUSH (return address) followed by the DS at the time
  83. ; of interrupt followed by the interrupt data
  84. ;
  85. saveregs    PROC    
  86.     mov    [dreax],eax    ; Save GP regs
  87.     mov    [drebx],ebx    ;
  88.     mov    [drecx],ecx    ;
  89.     mov    [dredx],edx    ;
  90.     mov    [dresi],esi    ;
  91.     mov    [dredi],edi    ;
  92.     mov    [drebp],ebp    ;
  93.     lea    ebp,[esp+4]    ; Point BP at DS on stack
  94.     mov    ebx,4        ; Offset past DS on stack
  95.     mov    eax,[ebp]
  96.     mov    [drds],ax
  97.     mov    [dres],es
  98.     bt    [haserr],0    ; See if an error
  99.     jnc    short noerr    ;
  100.           add    ebp,4        ; Yes, point errno
  101.     add    ebx,4        ; Offset past errno
  102.     mov    ax,[ebp]    ; Get the error #
  103.     mov    [errnum],ax    ;
  104. noerr:
  105.     mov    eax,[ebp + 4]    ; Get CS:eip
  106.     mov    [dreip],eax    ;
  107.     mov    eax,[ebp + 12]    ; Get flags
  108.     mov    [reflags],eax    ;
  109.     add    ebx,12        ; Offset past CS:eip & flags
  110.     mov    eax,ebp        ; things in the trap routine
  111.     add    eax,16        ;
  112.     mov    [dresp],eax    ;
  113.     ret
  114. saveregs    ENDP    
  115. ;
  116. ; Adjust EIP to the trap if it's not int 3
  117. ;
  118. adjusteip    PROC    
  119.     cmp    [trapnum],3    ; See if int 3
  120.     jnz    short noadj    ; No, get out
  121.     mov    esi,[dreip]    ;
  122.     dec    esi        ;
  123.     cmp    BYTE PTR [esi],0cch ; See if is an INT 3
  124.     jz    short nodecrement ; Get out if so
  125.     dec    [dreip]        ; Else point at trap
  126. nodecrement:
  127. noadj:
  128.     ret
  129. adjusteip    ENDP    
  130. ;
  131. ; Generic trap handler
  132. ;
  133. traphandler    PROC    
  134.     cld
  135.     call    saveregs    ; Save Regs
  136.     push    ds
  137.     pop    es
  138.     add    esp,ebx        ; Find usable top of stack
  139.     mov    [rtoss],esp    ;
  140.     test    [tracing],1    ; See if tracing
  141.     jnz    istracing    ;
  142.     call    disablebreaks    ; Disable breakpoints if not
  143. istracing:
  144.     call    adjusteip    ; Adjust the EIP to point to the breakpoint
  145.     mov    [tracing],0    ; Clear tracing flag
  146.     mov    ebx,offset CRLF
  147.     call    put_msg
  148.     cmp     [trapnum],3    ; No stats if it is int 3
  149.     jz    short nostats    ;
  150.     cmp    [trapnum],1    ; Or int 1
  151.     jz    short nostats    ;
  152.     mov    ebx, offset proctrap
  153.     call    put_msg
  154.     mov    ax,[trapnum]    ; Say which one
  155.     call    printbyte    ;
  156.     mov    ebx,offset CRLF
  157.     call    put_msg
  158.     btr    [haserr],0    ; If has error 
  159.     jnc    nostats        ;
  160.     mov    ebx,offset procerr
  161.     call    put_msg
  162.     mov    ax,[errnum]    ; Say which one
  163.     call    printword
  164.     mov    ebx,offset CRLF
  165.     call    put_msg
  166. nostats:
  167.     call    DisplayRegisters; Display registers
  168.     jmp    InputHandler    ; Go do input
  169.  
  170. traphandler    ENDP    
  171. ;
  172. ; Monitor init routine, point all traps to point to the monitor handler
  173. ;
  174. monitor_init    PROC    
  175.     mov    [dreax],eax    ; Save GP regs
  176.     mov    [drebx],ebx    ;
  177.     mov    [drecx],ecx    ;
  178.     mov    [dredx],edx    ;
  179.     mov    [dresi],esi    ;
  180.     mov    [dredi],edi    ;
  181.     mov    [drebp],ebp    ;
  182.     pop    eax
  183.     mov    [dreip],eax
  184.     mov    [dresp],esp
  185.     mov    [rtoss],esp
  186.     mov    [sysds],ds
  187.     mov    [drds],ds
  188.     mov    [dres],es
  189.     mov    ecx,[tsvects]        ; Get the number of vectors
  190.     mov    esi,offset DGROUP:tsvects + 4    ; Get the offset to the vector handlers
  191.     mov    edi,0            ; Get the initial trap #
  192. tilp:
  193.     lodsd                ; Read a trap handler
  194.     or    eax,eax
  195.     jz    noset
  196.     push     edi
  197.     push     ecx
  198.     mov    edx,eax            ;
  199.     mov    ecx,cs            ;
  200.     mov    ebx,edi
  201.     pushad
  202.     mov    ax,205h
  203.     int     31h
  204.     popad
  205.     jnc    mtok
  206. overtake:
  207.     mov    ax,205h
  208.     int     31h
  209. mtok:
  210.     pop    ecx
  211.     pop    edi
  212. noset:
  213.     inc    edi            ; Update trap#
  214.     loop    tilp            ; Loop till done
  215.     mov     ebx,offset banner
  216.     call    put_msg
  217.     call    trap            ; get us to _main
  218.     jmp    InputHandler
  219. monitor_init    ENDP    
  220. ;
  221. ; Here are the individual trap handlers
  222. ;
  223. TRAP    0
  224. TRAP    1,,yes
  225. TRAP    2
  226. TRAP    3
  227. TRAP    6
  228. TRAP    7
  229. TRAP    12,YES
  230. TRAP    13,YES
  231. TRAP    14,YES
  232.  
  233.  
  234. END