Windbg之条件断点设定

1、程序调试时,我们经常要根据条件来设置断点,降低条件断点命中率,方便调试操作。有时候程序出现异常数据时,

我们要根据属性访问时来设置断点,排查是谁写入或者读取了这些属性。

2、写了一个简单的demo程序。随机生成一个人的姓名和年龄,希望年龄为20岁时,程序中断。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Text.RegularExpressions;
 6 using System.Threading.Tasks;
 7 using System.Threading;
 8 
 9 namespace WindbgDemo
10 {
11     public class Program
12     {
13         
14         static void Main(string[] args)
15         {
16             Person person = null;
17 
18             while (true)
19             {
20                 int age = GenerateRandomNumber(5, 40);
21                 string firstName = string.Empty;
22                 string lastName = string.Empty;
23                 GenerateRandomName(out  firstName,out  lastName);
24                 person = new Person(firstName, lastName, age);
25                 if (age < 10)
26                     person.FullName = "Jerry Mill";
27                 System.Threading.Thread.Sleep(500);
28 
29             }
30 
31             Console.ReadKey();
32         }
33 
34 
35         static int GenerateRandomNumber(int min, int max)
36         {
37             var seed = Convert.ToInt32(Regex.Match(Guid.NewGuid().ToString(), @"\d+").Value);
38             return new Random(seed).Next(min, max);
39         }
40 
41         static void GenerateRandomName(out string firstName,out string lastName)
42         {
43             string[] lastNames = new string[] { "Baker", "Hunter", "Carter", "Smith", "Cook", "Miller", "Turner", "Hall", "Mill", "Churchill", "Field", "Wood", "Well", "Hard", "Bird", "Stock", "Cotton", "Bush", "Reed", "White" };
44             string[] firstNames = new string[] { "Aaron", "August", "Blake", "Bill", "Cheney", "Dick", "Derek", "Ethan", "Elijah", "Gino", "Jerry", "Kenny", "Kevin", "Lorin", "Leonard", "Mike", "Norman", "Riley", "Terence", "William" };
45             int first = GenerateRandomNumber(0, 19);
46             int last = GenerateRandomNumber(0, 19);
47             firstName = firstNames[first];
48             lastName = lastNames[last];
49         }
50     }
51 
52 
53     public  class Person 
54     {
55 
56         private string fullName = string.Empty;
57 
58         public string FullName {
59             get { return this.fullName; }
60             set {
61                 if (value != this.fullName)
62                 {
63                     this.fullName = value;
64                     Console.WriteLine("My name is " + FirstName + " " + LastName + ",now i got a new name" + value+"...");
65                 }
66             }
67         }
68         public int Age { get; set; }
69         public string LastName { get; set; }
70         public string FirstName { get; set; }
71         public Person(string first,string last,int age)
72         {
73             this.FirstName = first;
74             this.LastName = last;
75             this.fullName = first + " " + last;
76             this.Age = age;
77             Console.WriteLine("My name is " + first + " " + last + "," + age.ToString() + " years old...");
78         }
79     }
80 
81 }

3、用Windbg加载编译后的exe程序。在main方法中设置断点。

Opened log file 'D:\Projects\06BreakPoint\logs\log01.txt'
0:000> .load C:\Windows\Microsoft.NET\Framework\v4.0.30319\sos.dll
0:000> .load C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
0:000> sxe ld clrjit
0:000> g
(fa8.274): Unknown exception - code 04242420 (first chance)
ModLoad: 6b130000 6b1b0000   C:\Windows\Microsoft.NET\Framework\v4.0.30319\clrjit.dll
eax=00000000 ebx=00000000 ecx=00000000 edx=00000000 esi=7ffdf000 edi=0022e570
eip=773e70b4 esp=0022e488 ebp=0022e4dc iopl=0         nv up ei pl zr na pe nc
cs=001b  ss=0023  ds=0023  es=0023  fs=003b  gs=0000             efl=00000246
ntdll!KiFastSystemCallRet:
773e70b4 c3              ret
0:000>  !bpmd  WindbgDemo.exe WindbgDemo.Program.Main
Found 1 methods in module 00324014...
MethodDesc = 00324d24
Adding pending breakpoints...
sxe -c "!HandleCLRN" clrn
0:000> g
(fa8.274): CLR notification exception - code e0444143 (first chance)
JITTED WindbgDemo!WindbgDemo.Program.Main(System.String[])
Setting breakpoint: bp 0037047E [WindbgDemo.Program.Main(System.String[])]
bp 0037047E
g
Breakpoint 0 hit
eax=00000000 ebx=0022f05c ecx=016b228c edx=00000000 esi=00000000 edi=0022efd0
eip=0037047e esp=0022ef98 ebp=0022efb8 iopl=0         nv up ei pl zr na pe nc
cs=001b  ss=0023  ds=0023  es=0023  fs=003b  gs=0000             efl=00000246
0037047e 90              nop
0:000> !clrstack -a
OS Thread Id: 0x274 (0)
Child SP       IP Call Site
0022ef98 0037047e *** WARNING: Unable to verify checksum for WindbgDemo.exe
WindbgDemo.Program.Main(System.String[]) [d:\Projects\WindbgDemo\WindbgDemo\06BreakPoint.cs @ 15]
    PARAMETERS:
        args (0x0022efb4) = 0x016b228c
    LOCALS:
        0x0022ef9c = 0x00000000
        0x0022efb0 = 0x00000000
        0x0022efac = 0x00000000
        0x0022efa8 = 0x00000000
        0x0022efa4 = 0x00000000

0022f130 67ceeaf6 [GCFrame: 0022f130] 
0:000> !ip2md 0037047e 
MethodDesc:   00324d24
Method Name:  WindbgDemo.Program.Main(System.String[])
Class:        00321348
MethodTable:  00324d50
mdToken:      06000001
Module:       00324014
IsJitted:     yes
CodeAddr:     00370448
Transparency: Critical
Source file:  d:\Projects\WindbgDemo\WindbgDemo\06BreakPoint.cs @ 15
0:000> !u 00324d24
Normal JIT generated code
WindbgDemo.Program.Main(System.String[])
Begin 00370448, size e4

...

d:\Projects\WindbgDemo\WindbgDemo\06BreakPoint.cs @ 20:
0037048b b905000000      mov     ecx,5
00370490 ba28000000      mov     edx,28h
00370495 ff15384d3200    call    dword ptr ds:[324D38h] (WindbgDemo.Program.GenerateRandomNumber(Int32, Int32), mdToken: 06000002)
0037049b 8945e8          mov     dword ptr [ebp-18h],eax
0037049e 8b45e8          mov     eax,dword ptr [ebp-18h]
003704a1 8945f8          mov     dword ptr [ebp-8],eax

d:\Projects\WindbgDemo\WindbgDemo\06BreakPoint.cs @ 21:
003704a4 8b0500236b02    mov     eax,dword ptr ds:[26B2300h] ("")
003704aa 8945f4          mov     dword ptr [ebp-0Ch],eax
...

d:\Projects\WindbgDemo\WindbgDemo\06BreakPoint.cs @ 29:
00370518 90              nop

d:\Projects\WindbgDemo\WindbgDemo\06BreakPoint.cs @ 18:
00370519 b801000000      mov     eax,1
0037051e 25ff000000      and     eax,0FFh
00370523 8945ec          mov     dword ptr [ebp-14h],eax

d:\Projects\WindbgDemo\WindbgDemo\06BreakPoint.cs @ 15:
00370526 90              nop
00370527 e95effffff      jmp     0037048a
0:000> bp 0037048b "j(poi(0x0022efb0 ) >= 0n20) '.echo greater than 20;?poi(0x0022efb0 )';'g;'"
0:000> g
greater than 20
Evaluate expression: 30 = 0000001e
eax=00000001 ebx=0022f05c ecx=00000001 edx=0000009b esi=00000000 edi=0022efd0
eip=0037048b esp=0022ef98 ebp=0022efb8 iopl=0         nv up ei pl nz na po nc
cs=001b  ss=0023  ds=0023  es=0023  fs=003b  gs=0000             efl=00000202
0037048b b905000000      mov     ecx,5

0:000> g
greater than 20
Evaluate expression: 35 = 00000023
eax=00000001 ebx=0022f05c ecx=00000001 edx=0000009b esi=00000000 edi=0022efd0
eip=0037048b esp=0022ef98 ebp=0022efb8 iopl=0         nv up ei pl nz na po nc
cs=001b  ss=0023  ds=0023  es=0023  fs=003b  gs=0000             efl=00000202
0037048b b905000000      mov     ecx,5
0:000> 
greater than 20
Evaluate expression: 35 = 00000023

 

posted @ 2021-09-19 15:48  hzwanglw  阅读(680)  评论(0)    收藏  举报