Intermediate x86Part 4.ppt
《Intermediate x86Part 4.ppt》由会员分享,可在线阅读,更多相关《Intermediate x86Part 4.ppt(56页珍藏版)》请在麦多课文档分享上搜索。
1、Intermediate x86 Part 4,Xeno Kovah 2010 xkovah at gmail,All materials are licensed under a Creative Commons “Share Alike” license.,http:/creativecommons.org/licenses/by-sa/3.0/,2,3,Interrupts & Debugging,We know that IDT3 is the Breakpoint Exception, and that its important enough for INT 3 to have a
2、 separate one byte opcode form (0xCC). INT 3 is what debuggers are using when they say they are setting a “software breakpoint” (the default breakpoint in most cases) When a debugger uses a software breakpoint, what it does is overwrite the first byte of the instruction at the specified address. It
3、keeps its own list of which bytes it overwrote and where. Then when breakpoint exception is received, it looks up the location, replaces the original byte and lets the instruction execute normally. Then typically it overwrites the first byte again (subject to configuration) so that the breakpoint wi
4、ll be hit if the address is executed again.,Lab: ProofPudding.c,A program which reads its own memory in order to confirm that when a breakpoint is set, it overwrites a byte with the 0xCC form of the breakpoint interrupt, INT 3,4,5,Hardware Support for Debugging,Most debuggers also have support for s
5、omething called a “hardware breakpoint”, and these breakpoints are more flexible than software breakpoints in that they can be set to trigger when memory is read or written, not just when its executed. However only 4 hardware breakpoints can be set. There are 8 debug registers DR0-DR7 DR0-3 = breakp
6、oint linear address registers DR4-5 = reserved (unused) DR6 = Debug Status Register DR7 = Debug Control Register Accessing the registers requires CPL = 0 MOV DR, r32 MOV r32, DR,Vol. 3b, Sect. 18,6,Picture This,7,DR7 - Debug Control Register,L0-3 (local breakpoint enable) flags - Enables the DR0-3 b
7、reakpoint. But these flags are cleared on task switches to ensure that they do not fire when dealing with a different task. G0-3 (global breakpoint enable) flags - Enables the DR0-3 breakpoint. Does not get cleared on task switch, which is what makes it global obviously. LE & GE (local and global ex
8、act breakpoint enable) flags - If set to 1, enables detection of the exact instruction that caused a data breakpoint. Not supported on P6 microarchitecture and later, but if you need maximum compatibility for exact breakpoint detection youre recommended to set both to 1.,8,DR7 - Debug Control Regist
9、er (2),GD (General Detect) flag - If set to 1, causes a debug exception prior to MOV instructions which access the debug registers. The flag is cleared when the actual exception occurs though, so that the handler can access the debug register as needed. The R/W0-3 are interpreted as follows: 00 = Br
10、eak on instruction execution only. 01 = Break on data writes only. If(CR4.DE = 1) then 10 = Break on I/O reads or writes. If(CR4.DE = 0) then 10 = Undefined. 11 = Break on data reads or writes but not instruction fetches.,9,DR7 - Debug Control Register (3),LEN0-4 bits specify what size the address s
11、tored in the DR0-3 registers should be treated as. 00 = 1-byte 01 = 2-bytes 10 = Undefined (or 8 bytes, see note below) 11 = 4-bytes. While you might set a 1 byte size for an address pointing at the first byte of an instruction, on a break-on-execute, you might want to set a 4 byte breakpoint on wri
12、tes to a memory location you know to be a DWORD. “For Pentium 4 and Intel Xeon processors with a CPUID signature corresponding to family 15 (model 3, 4, and 6), break point conditions permit specifying 8-byte length on data read/write with an of encoding 10B in the LENx field.”,10,DR6 - Debug Status
13、 Register,B0-B3 (breakpoint condition detected) flags - When the B0,1,2,3 bit is set, it means that the 0th,1st,2nd,3rd condition specified in DR7 has been satisfied. The bits are set even if the DR7 says that condition is currently disabled. I.e. software needs to crosscheck these bits against whet
14、her it currently cares. BD (debug register access detected) flag - Indicates that the next instruction will try to access the debug registers. This flag only enabled if GD (general detect) flag in DR7 is set. Thus this signals if someone else was trying to access the debug registers. NO! MINE!,11,DR
15、6 - Debug Status Register (2),BS (single step) flag - If set, the debug exception was triggered by single-step execution mode (talked about later). BT (task switch) flag - Related to TSS so we dont care “Certain debug exceptions may clear bits 0-3. The remaining contents of the DR6 register are neve
16、r cleared by the processor. To avoid confusion in identifying debug exceptions, debug handlers should clear the register before returning to the interrupted task.” Seems like an important point if youre making a debugger :),12,So what actually happens when a hardware breakpoint fires?,It fires IDT1,
17、 a Debug Exception When it is an execute breakpoint or general detect (someone trying to access debug regs) its a fault. For other cases Its A Trap!That means if it was a break on write, the data is overwritten before the exception is generated. A handler which wants to show the before and after is
18、responsible for keeping a copy of the before value. Instruction breakpoints are actually detected before the instruction executes. Therefore if the handler doesnt remove the breakpoint, and it just returned, the same exception would be raised over and over. This is where the Resume Flag (RF) comes i
19、nto play,13,Resume Flag (RF in EFLAGS),When the RF is set, the processor ignores instruction breakpoints. To set the flag, a debug interrupt handler would manipulate the EFLAGS stored on the stack and then use IRETD (POPF, POPFD, and IRET do not transfer RF from the stack into EFLAGS) “The processor
20、 then ignores instruction breakpoints for the duration of the next instruction.” “The processor then automatically clears this flag after the instruction returned to has been successfully executed.”,14,Trap Flag (TF in EFLAGS),Only being able to invoke the debug exception handler on 4 addresses is s
21、omewhat limiting. When TF is 1, it causes a debug exception after every instruction. This is called “single-step” mode. Useful for capabilities such as “step out” which just steps until it steps through a RET Remember that we said that if the debug exception is in response to single stepping, it set
22、s the BS flag in DR6. The processor clears the TF flag before calling the exception handler, so if it wants to keep single-stepping it needs to set it again before returning. Also, the INT and INTO instructions clear TF. So a single stepping debugger handler should compensate accordingly.,WinDbg Har
23、dware Breakpoints,Hardware Breakpoint = ba rather than bp. Stands for break on access, where access can be read/write/execute or port IO. Below is a simplified form of the command (see help page for full form) ba Access Size Address Access r = read/write w = write e = execute i = I/O port (talked ab
24、out later) Size. Width of data over which you want the breakpoint to have effect. Must be 1 for access = e, but can be 1, 2, 4, or 8 for other types Address. Where you want the breakpoint to be targeted.,15,HW Breakpoint Examples,ba e 1 0x80541ac0 Break on execute of address 0x80541ac0 (windbg speci
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- INTERMEDIATEX86PART4PPT
