home *** CD-ROM | disk | FTP | other *** search
/ CD-X 1 / cdx_01.iso / demodisc / tyrant / docs / 386code / 12.386 < prev    next >
Encoding:
Text File  |  1994-01-20  |  22.3 KB  |  470 lines

  1. Chapter 12  Debugging
  2.  
  3. ────────────────────────────────────────────────────────────────────────────
  4.  
  5. The 80386 brings to Intel's line of microprocessors significant advances in
  6. debugging power. The single-step exception and breakpoint exception of
  7. previous processors are still available in the 80386, but the principal
  8. debugging support takes the form of debug registers. The debug registers
  9. support both instruction breakpoints and data breakpoints. Data breakpoints
  10. are an important innovation that can save hours of debugging time by
  11. pinpointing, for example, exactly when a data structure is being
  12. overwritten. The breakpoint registers also eliminate the complexities
  13. associated with writing a breakpoint instruction into a code segment
  14. (requires a data-segment alias for the code segment) or a code segment
  15. shared by multiple tasks (the breakpoint exception can occur in the context
  16. of any of the tasks). Breakpoints can even be set in code contained in ROM.
  17.  
  18.  
  19. 12.1  Debugging Features of the Architecture
  20.  
  21. The features of the 80386 architecture that support debugging include:
  22.  
  23. Reserved debug interrupt vector
  24.  
  25. Permits processor to automatically invoke a debugger task or procedure when
  26. an event occurs that is of interest to the debugger.
  27.  
  28. Four debug address registers
  29.  
  30. Permit programmers to specify up to four addresses that the CPU will
  31. automatically monitor.
  32.  
  33. Debug control register
  34.  
  35. Allows programmers to selectively enable various debug conditions
  36. associated with the four debug addresses.
  37.  
  38. Debug status register
  39.  
  40. Helps debugger identify condition that caused debug exception.
  41.  
  42. Trap bit of TSS (T-bit)
  43.  
  44. Permits monitoring of task switches.
  45.  
  46. Resume flag (RF) of flags register
  47.  
  48. Allows an instruction to be restarted after a debug exception without
  49. immediately causing another debug exception due to the same condition.
  50.  
  51. Single-step flag (TF)
  52.  
  53. Allows complete monitoring of program flow by specifying whether the CPU
  54. should cause a debug exception with the execution of every instruction.
  55.  
  56. Breakpoint instruction
  57.  
  58. Permits debugger intervention at any point in program execution and aids
  59. debugging of debugger programs.
  60.  
  61. Reserved interrupt vector for breakpoint exception
  62.  
  63. Permits processor to automatically invoke a handler task or procedure upon
  64. encountering a breakpoint instruction.
  65.  
  66. These features make it possible to invoke a debugger that is either a
  67. separate task or a procedure in the context of the current task. The
  68. debugger can be invoked under any of the following kinds of conditions:
  69.  
  70.   ■  Task switch to a specific task.
  71.   ■  Execution of the breakpoint instruction.
  72.   ■  Execution of every instruction.
  73.   ■  Execution of any instruction at a given address.
  74.   ■  Read or write of a byte, word, or doubleword at any specified address.
  75.   ■  Write to a byte, word, or doubleword at any specified address.
  76.   ■  Attempt to change a debug register.
  77.  
  78.  
  79. 12.2  Debug Registers
  80.  
  81. Six 80386 registers are used to control debug features. These registers are
  82. accessed by variants of the MOV instruction. A debug register may be either
  83. the source operand or destination operand. The debug registers are
  84. privileged resources; the MOV instructions that access them can only be
  85. executed at privilege level zero. An attempt to read or write the debug
  86. registers when executing at any other privilege level causes a general
  87. protection exception. Figure 12-1 shows the format of the debug registers.
  88.  
  89.  
  90. Figure 12-1.  Debug Registers
  91.  
  92.       31              23              15              7             0
  93.      ╔═══╤═══╤═══╤═══╪═══╤═══╤═══╤═══╪═══╤═╤═════╤═╤═╪═╤═╤═╤═╤═╤═╤═╤═╗
  94.      ║LEN│R/W│LEN│R/W│LEN│R/W│LEN│R/W│   │ │     │G│L│G│L│G│L│G│L│G│L║
  95.      ║   │   │   │   │   │   │   │   │0 0│0│0 0 0│ │ │ │ │ │ │ │ │ │ ║ DR7
  96.      ║ 3 │ 3 │ 2 │ 2 │ 1 │ 1 │ 0 │ 0 │   │ │     │E│E│3│3│2│2│1│1│0│0║
  97.      ╟───┴───┴───┴───┴───┴───┴───┴───┼─┬─┼─┼─────┴─┴─┴─┴─┴─┴─┼─┼─┼─┼─╢
  98.      ║                               │B│B│B│                 │B│B│B│B║
  99.      ║0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0│ │ │ │0 0 0 0 0 0 0 0 0│ │ │ │ ║ DR6
  100.      ║                               │T│S│D│                 │3│2│1│0║
  101.      ╟───────────────┼───────────────┼─┴─┴─┴─────────┼───────┴─┴─┴─┴─╢
  102.      ║                                                               ║
  103.      ║                            RESERVED                           ║ DR5
  104.      ║                                                               ║
  105.      ╟───────────────┼───────────────┼───────────────┼───────────────╢
  106.      ║                                                               ║
  107.      ║                            RESERVED                           ║ DR4
  108.      ║                                                               ║
  109.      ╟───────────────┼───────────────┼───────────────┼───────────────╢
  110.      ║                                                               ║
  111.      ║                 BREAKPOINT 3 LINEAR ADDRESS                   ║ DR3
  112.      ║                                                               ║
  113.      ╟───────────────┼───────────────┼───────────────┼───────────────╢
  114.      ║                                                               ║
  115.      ║                 BREAKPOINT 2 LINEAR ADDRESS                   ║ DR2
  116.      ║                                                               ║
  117.      ╟───────────────┼───────────────┼───────────────┼───────────────╢
  118.      ║                                                               ║
  119.      ║                 BREAKPOINT 1 LINEAR ADDRESS                   ║ DR1
  120.      ║                                                               ║
  121.      ╟───────────────┼───────────────┼───────────────┼───────────────╢
  122.      ║                                                               ║
  123.      ║                 BREAKPOINT 0 LINEAR ADDRESS                   ║ DR0
  124.      ║                                                               ║
  125.      ╚═══════════════╪═══════════════╪════════════════╪══════════════╝
  126.  
  127. ────────────────────────────────────────────────────────────────────────────
  128. NOTE
  129.       0 MEANS INTEL RESERVED. DO NOT DEFINE.
  130. ────────────────────────────────────────────────────────────────────────────
  131.  
  132.  
  133. 12.2.1  Debug Address Registers (DR0-DR3)
  134.  
  135. Each of these registers contains the linear address associated with one of
  136. four breakpoint conditions. Each breakpoint condition is further defined by
  137. bits in DR7.
  138.  
  139. The debug address registers are effective whether or not paging is enabled.
  140. The addresses in these registers are linear addresses. If paging is enabled,
  141. the linear addresses are translated into physical addresses by the
  142. processor's paging mechanism (as explained in Chapter 5). If paging is not
  143. enabled, these linear addresses are the same as physical addresses.
  144.  
  145. Note that when paging is enabled, different tasks may have different
  146. linear-to-physical address mappings. When this is the case, an address in a
  147. debug address register may be relevant to one task but not to another. For
  148. this reason the 80386  has both global and local enable bits in DR7. These
  149. bits indicate whether a given debug address has a global (all tasks) or
  150. local (current task only) relevance.
  151.  
  152.  
  153. 12.2.2  Debug Control Register (DR7)
  154.  
  155. The debug control register shown in Figure 12-1 both helps to define the
  156. debug conditions and selectively enables and disables those conditions.
  157.  
  158. For each address in registers DR0-DR3, the corresponding fields R/W0
  159. through R/W3 specify the type of action that should cause a breakpoint. The
  160. processor interprets these bits as follows:
  161.  
  162.    00 ── Break on instruction execution only
  163.    01 ── Break on data writes only
  164.    10 ── undefined
  165.    11 ── Break on data reads or writes but not instruction fetches
  166.  
  167. Fields LEN0 through LEN3 specify the length of data item to be monitored. A
  168. length of 1, 2, or 4 bytes may be specified. The values of the length fields
  169. are interpreted as follows:
  170.  
  171.    00 ── one-byte length
  172.    01 ── two-byte length
  173.    10 ── undefined
  174.    11 ── four-byte length
  175.  
  176. If RWn is 00 (instruction execution), then LENn should also be 00. Any other
  177. length is undefined.
  178.  
  179. The low-order eight bits of DR7 (L0 through L3 and G0 through G3)
  180. selectively enable the four address breakpoint conditions. There are two
  181. levels of enabling: the local (L0 through L3) and global (G0 through G3)
  182. levels. The local enable bits are automatically reset by the processor at
  183. every task switch to avoid unwanted breakpoint conditions in the new task.
  184. The global enable bits are not reset by a task switch; therefore, they can
  185. be used for conditions that are global to all tasks.
  186.  
  187. The LE and GE bits control the "exact data breakpoint match" feature of the
  188. processor. If either LE or GE is set, the processor slows execution so that
  189. data breakpoints are reported on the instruction that causes them. It is
  190. recommended that one of these bits be set whenever data breakpoints are
  191. armed. The processor clears LE at a task switch but does not clear GE.
  192.  
  193.  
  194. 12.2.3  Debug Status Register (DR6)
  195.  
  196. The debug status register shown in Figure 12-1 permits the debugger to
  197. determine which debug conditions have occurred.
  198.  
  199. When the processor detects an enabled debug exception, it sets the
  200. low-order bits of this register (B0 thru B3) before entering the debug
  201. exception handler. Bn is set if the condition described by DRn, LENn, and
  202. R/Wn occurs. (Note that the processor sets Bn regardless of whether Gn or
  203. Ln is set. If more than one breakpoint condition occurs at one time and if
  204. the breakpoint trap occurs due to an enabled condition other than n, Bn may
  205. be set, even though neither Gn nor Ln is set.)
  206.  
  207. The BT bit is associated with the T-bit (debug trap bit) of the TSS (refer
  208. to 7 for the location of the T-bit). The processor sets the BT bit before
  209. entering the debug handler if a task switch has occurred and the T-bit of
  210. the new TSS is set. There is no corresponding bit in DR7 that enables and
  211. disables this trap; the T-bit of the TSS is the sole enabling bit.
  212.  
  213. The BS bit is associated with the TF (trap flag) bit of the EFLAGS
  214. register. The BS bit is set if the debug handler is entered due to the
  215. occurrence of a single-step exception. The single-step trap is the
  216. highest-priority debug exception; therefore, when BS is set, any of the
  217. other debug status bits may also be set.
  218.  
  219. The BD bit is set if the next instruction will read or write one of the
  220. eight debug registers and ICE-386 is also using the debug registers at the
  221. same time.
  222.  
  223. Note that the bits of DR6 are never cleared by the processor. To avoid any
  224. confusion in identifying the next debug exception, the debug handler should
  225. move zeros to DR6 immediately before returning.
  226.  
  227.  
  228. 12.2.4  Breakpoint Field Recognition
  229.  
  230. The linear address and LEN field for each of the four breakpoint conditions
  231. define a range of sequential byte addresses for a data breakpoint. The LEN
  232. field permits specification of a one-, two-, or four-byte field. Two-byte
  233. fields must be aligned on word boundaries (addresses that are multiples of
  234. two) and four-byte fields must be aligned on doubleword boundaries
  235. (addresses that are multiples of four). These requirements are enforced by
  236. the processor; it uses the LEN bits to mask the low-order bits of the
  237. addresses in the debug address registers. Improperly aligned code or data
  238. breakpoint addresses will not yield the expected results.
  239.  
  240. A data read or write breakpoint is triggered if any of the bytes
  241. participating in a memory access is within the field defined by a breakpoint
  242. address register and the corresponding LEN field. Table 12-1 gives some
  243. examples of breakpoint fields with memory references that both do and do not
  244. cause traps.
  245.  
  246. To set a data breakpoint for a misaligned field longer than one byte, it
  247. may be desirable to put two sets of entries in the breakpoint register such
  248. that each entry is properly aligned and the two entries together span the
  249. length of the field.
  250.  
  251. Instruction breakpoint addresses must have a length specification of one
  252. byte (LEN = 00); other values are undefined. The processor recognizes an
  253. instruction breakpoint address only when it points to the first byte of an
  254. instruction. If the instruction has any prefixes, the breakpoint address
  255. must point to the first prefix.
  256.  
  257.  
  258. Table 12-1. Breakpoint Field Recognition Examples
  259.  
  260.                                     Address (hex)          Length
  261.  
  262.                        DR0             0A0001          1 (LEN0 = 00)
  263. Register Contents      DR1             0A0002          1 (LEN1 = 00)
  264.                        DR2             0B0002          2 (LEN2 = 01)
  265.                        DR3             0C0000          4 (LEN3 = 11)
  266.  
  267. Some Examples of Memory                0A0001          1
  268. References That Cause Traps            0A0002          1
  269.                                        0A0001          2
  270.                                        0A0002          2
  271.                                        0B0002          2
  272.                                        0B0001          4
  273.                                        0C0000          4
  274.                                        0C0001          2
  275.                                        0C0003          1
  276.  
  277. Some Examples of Memory                0A0000          1
  278. References That Don't Cause Traps      0A0003          4
  279.                                        0B0000          2
  280.                                        0C0004          4
  281.  
  282.  
  283. 12.3  Debug Exceptions
  284.  
  285. Two of the interrupt vectors of the 80386 are reserved for exceptions that
  286. relate to debugging. Interrupt 1 is the primary means of invoking debuggers
  287. designed expressly for the 80386; interrupt 3 is intended for debugging
  288. debuggers and for compatibility with prior processors in Intel's 8086
  289. processor family.
  290.  
  291.  
  292. 12.3.1  Interrupt 1 ── Debug Exceptions
  293.  
  294. The handler for this exception is usually a debugger or part of a debugging
  295. system. The processor causes interrupt 1 for any of several conditions. The
  296. debugger can check flags in DR6 and DR7 to determine what condition caused
  297. the exception and what other conditions might be in effect at the same time.
  298. Table 12-2 associates with each breakpoint condition the combination of
  299. bits that indicate when that condition has caused the debug exception.
  300.  
  301. Instruction address breakpoint conditions are faults, while other debug
  302. conditions are traps. The debug exception may report either or both at one
  303. time. The following paragraphs present details for each class of debug
  304. exception.
  305.  
  306.  
  307. Table 12-2. Debug Exception Conditions
  308.  
  309. Flags to Test              Condition
  310.  
  311. BS=1                       Single-step trap
  312. B0=1 AND (GE0=1 OR LE0=1)  Breakpoint DR0, LEN0, R/W0
  313. B1=1 AND (GE1=1 OR LE1=1)  Breakpoint DR1, LEN1, R/W1
  314. B2=1 AND (GE2=1 OR LE2=1)  Breakpoint DR2, LEN2, R/W2
  315. B3=1 AND (GE3=1 OR LE3=1)  Breakpoint DR3, LEN3, R/W3
  316. BD=1                       Debug registers not available; in use by ICE-386.
  317. BT=1                       Task switch
  318.  
  319.  
  320. 12.3.1.1  Instruction Addrees Breakpoint
  321.  
  322. The processor reports an instruction-address breakpoint before it executes
  323. the instruction that begins at the given address; i.e., an instruction-
  324. address breakpoint exception is a fault.
  325.  
  326. The RF (restart flag) permits the debug handler to retry instructions that
  327. cause other kinds of faults in addition to debug faults. When it detects a
  328. fault, the processor automatically sets RF in the flags image that it pushes
  329. onto the stack. (It does not, however, set RF for traps and aborts.)
  330.  
  331. When RF is set, it causes any debug fault to be ignored during the next
  332. instruction. (Note, however, that RF does not cause breakpoint traps to be
  333. ignored, nor other kinds of faults.)
  334.  
  335. The processor automatically clears RF at the successful completion of every
  336. instruction except after the IRET instruction, after the POPF instruction,
  337. and after a JMP, CALL, or INT instruction that causes a task switch. These
  338. instructions set RF to the value specified by the memory image of the EFLAGS
  339. register.
  340.  
  341. The processor automatically sets RF in the EFLAGS image on the stack before
  342. entry into any fault handler. Upon entry into the fault handler for
  343. instruction address breakpoints, for example, RF is set in the EFLAGS image
  344. on the stack; therefore, the IRET instruction at the end of the handler will
  345. set RF in the EFLAGS register, and execution will resume at the breakpoint
  346. address without generating another breakpoint fault at the same address.
  347.  
  348. If, after a debug fault, RF is set and the debug handler retries the
  349. faulting instruction, it is possible that retrying the instruction will
  350. raise other faults. The retry of the instruction after these faults will
  351. also be done with RF=1, with the result that debug faults continue to be
  352. ignored. The processor clears RF only after successful completion of the
  353. instruction.
  354.  
  355. Real-mode debuggers can control the RF flag by using a 32-bit IRET. A
  356. 16-bit IRET instruction does not affect the RF bit (which is in the
  357. high-order 16 bits of EFLAGS). To use a 32-bit IRET, the debugger must
  358. rearrange the stack so that it holds appropriate values for the 32-bit EIP,
  359. CS, and EFLAGS (with RF set in the EFLAGS image). Then executing an IRET
  360. with an operand size prefix causes a 32-bit return, popping the RF flag
  361. into EFLAGS.
  362.  
  363.  
  364. 12.3.1.2  Data Address Breakpoint
  365.  
  366. A data-address breakpoint exception is a trap; i.e., the processor reports
  367. a data-address breakpoint after executing the instruction that accesses the
  368. given memory item.
  369.  
  370. When using data breakpoints it is recommended that either the LE or GE bit
  371. of DR7 be set also. If either LE or GE is set, any data breakpoint trap is
  372. reported exactly after completion of the instruction that accessed the
  373. specified memory item. This exact reporting is accomplished by forcing the
  374. 80386 execution unit to wait for completion of data operand transfers before
  375. beginning execution of the next instruction. If neither GE nor LE is set,
  376. data breakpoints may not be reported until one instruction after the data is
  377. accessed or may not be reported at all. This is due to the fact that,
  378. normally, instruction execution is overlapped with memory transfers to such
  379. a degree that execution of the next instruction may begin before memory
  380. transfers for the prior instruction are completed.
  381.  
  382. If a debugger needs to preserve the contents of a write breakpoint
  383. location, it should save the original contents before setting a write
  384. breakpoint. Because data breakpoints are traps, a write into a breakpoint
  385. location will complete before the trap condition is reported. The handler
  386. can report the saved value after the breakpoint is triggered. The data in
  387. the debug registers can be used to address the new value stored by the
  388. instruction that triggered the breakpoint.
  389.  
  390.  
  391. 12.3.1.3  General Detect Fault
  392.  
  393. This exception occurs when an attempt is made to use the debug registers at
  394. the same time that ICE-386 is using them. This additional protection feature
  395. is provided to guarantee that ICE-386 can have full control over the
  396. debug-register resources when required. ICE-386 uses the debug-registers;
  397. therefore, a software debugger that also uses these registers cannot run
  398. while ICE-386 is in use. The exception handler can detect this condition by
  399. examining the BD bit of DR6.
  400.  
  401.  
  402. 12.3.1.4  Single-Step Trap
  403.  
  404. This debug condition occurs at the end of an instruction if the trap flag
  405. (TF) of the flags register held the value one at the beginning of that
  406. instruction.  Note that the exception does not occur at the end of an
  407. instruction that sets TF. For example, if POPF is used to set TF, a
  408. single-step trap does not occur until after the instruction that follows
  409. POPF.
  410.  
  411. The processor clears the TF bit before invoking the handler.  If TF=1 in
  412. the flags image of a TSS at the time of a task switch, the exception occurs
  413. after the first instruction is executed in the new task.
  414.  
  415. The single-step flag is normally not cleared by privilege changes inside a
  416. task.  INT instructions, however, do clear TF.  Therefore, software
  417. debuggers that single-step code must recognize and emulate INT n or INTO
  418. rather than executing them directly.
  419.  
  420. To maintain protection, system software should check the current execution
  421. privilege level after any single step interrupt to see whether single
  422. stepping should continue at the current privilege level.
  423.  
  424. The interrupt priorities in hardware guarantee that if an external
  425. interrupt occurs, single stepping stops. When both an external interrupt and
  426. a single step interrupt occur together, the single step interrupt is
  427. processed first. This clears the TF bit. After saving the return address or
  428. switching tasks, the external interrupt input is examined before the first
  429. instruction of the single step handler executes.  If the external interrupt
  430. is still pending, it is then serviced. The external interrupt handler is not
  431. single-stepped. To single step an interrupt handler, just single step an INT
  432. n instruction that refers to the interrupt handler.
  433.  
  434.  
  435. 12.3.1.5  Task Switch Breakpoint
  436.  
  437. The debug exception also occurs after a switch to an 80386 task if the
  438. T-bit of the new TSS is set.  The exception occurs after control has passed
  439. to the new task, but before the first instruction of that task is executed.
  440. The exception handler can detect this condition by examining the BT bit of
  441. the debug status register DR6.
  442.  
  443. Note that if the debug exception handler is a task, the T-bit of its TSS
  444. should not be set. Failure to observe this rule will cause the processor to
  445. enter an infinite loop.
  446.  
  447.  
  448. 12.3.2  Interrupt 3 ── Breakpoint Exception
  449.  
  450. This exception is caused by execution of the breakpoint instruction INT 3.
  451. Typically, a debugger prepares a breakpoint by substituting the opcode of
  452. the one-byte breakpoint instruction in place of the first opcode byte of the
  453. instruction to be trapped. When execution of the INT 3 instruction causes
  454. the exception handler to be invoked, the saved value of ES:EIP points to the
  455. byte following the INT 3 instruction.
  456.  
  457. With prior generations of processors, this feature is used extensively for
  458. trapping execution of specific instructions. With the 80386, the needs
  459. formerly filled by this feature are more conveniently solved via the debug
  460. registers and interrupt 1.  However, the breakpoint exception is still
  461. useful for debugging debuggers, because the breakpoint exception can vector
  462. to a different exception handler than that used by the debugger. The
  463. breakpoint exception can also be useful when it is necessary to set a
  464. greater number of breakpoints than permitted by the debug registers.
  465.  
  466.  
  467.                           PART III  COMPATIBILITY                          
  468.  
  469.  
  470.