pykd

Форк
0
/
breakpoint.py 
197 строк · 6.6 Кб
1
""" breakpoints """
2

3
import unittest
4
import pykd
5
import target
6
import testutils
7

8
class callCounter(object):
9
    def __init__(self,func):
10
        self.count = 0
11
        self.func = func
12
    def __call__(self):
13
        self.count = self.count+1
14
        return self.func()
15

16
def stopOnBreak():
17
    return pykd.eventResult.Break
18

19
def continueOnBreak():
20
    return pykd.eventResult.Proceed
21

22

23
class BreakpointTest( unittest.TestCase ):
24

25
    def setUp(self):
26
        pykd.startProcess( target.appPath + " breakhandlertest" )
27
        self.targetModule = pykd.module( target.moduleName )
28
        self.targetModule.reload()
29
        pykd.go()
30

31
    def tearDown(self):
32
        self.taregtModule = None
33
        pykd.killAllProcesses()
34
 
35
    def testNoBreakpoint(self):
36
        self.assertEqual( pykd.executionStatus.NoDebuggee, pykd.go() )
37

38
    def testSetBp(self):
39
        bp = pykd.setBp( self.targetModule.CdeclFunc )
40
        self.assertEqual( pykd.executionStatus.Break, pykd.go() )
41

42
    def testRemoveBp(self):
43
        bp = pykd.setBp( self.targetModule.CdeclFunc )
44
        bp.remove()
45
        self.assertEqual( pykd.executionStatus.NoDebuggee, pykd.go() )
46

47
    def testDeleteBp(self):
48
        bp = pykd.setBp( self.targetModule.CdeclFunc )
49
        del bp
50
        self.assertEqual( pykd.executionStatus.NoDebuggee, pykd.go() )
51

52
    def testRemoveByIndex(self):
53
        bp1 = pykd.setBp( self.targetModule.CdeclFunc )
54
        bp2 = pykd.getBp(0)
55
        bp2.remove()
56
        self.assertEqual( pykd.executionStatus.NoDebuggee, pykd.go() )
57

58
    def testRemoveAllBp(self):
59
          bp1 =  pykd.setBp( self.targetModule.CdeclFunc )
60
          bp2 =  pykd.setBp( self.targetModule.CdeclFunc + 1)
61
          bp3 =  pykd.setBp( self.targetModule.CdeclFunc + 2 )
62
          self.assertEqual( 3, pykd.getNumberBreakpoints() )
63
          pykd.removeAllBp()
64
          self.assertEqual( 0, pykd.getNumberBreakpoints() )
65
          self.assertEqual( pykd.executionStatus.NoDebuggee, pykd.go() )
66

67
    def testBreakCallback(self):
68
        breakCount = callCounter(stopOnBreak)
69
        bp = pykd.setBp( self.targetModule.CdeclFunc, breakCount )
70
        self.assertEqual( pykd.executionStatus.Break, pykd.go() )
71
        self.assertEqual( 1, breakCount.count )
72

73
    def testNoBreakCallback(self):
74

75
        breakCount = callCounter(continueOnBreak)
76
        bp = pykd.setBp( self.targetModule.CdeclFunc, breakCount )
77
        self.assertEqual( pykd.executionStatus.NoDebuggee, pykd.go() )
78
        self.assertEqual( 1, breakCount.count )
79

80
    def testBreakpointHandler(self):
81

82
        class BreakpointHandler( pykd.eventHandler ):
83

84
            def __init__(self):
85
                super(BreakpointHandler, self).__init__()
86
                self.count = 0
87

88
            def onBreakpoint( self, bpid_):
89
                self.count = self.count + 1
90
                return True
91

92
        handler = BreakpointHandler()
93
        bp = pykd.setBp( self.targetModule.CdeclFunc )
94
        self.assertEqual( pykd.executionStatus.Break, pykd.go() )
95
        self.assertEqual( 1, handler.count )
96

97
    def testBreakpointClass(self):
98

99
        class MyBreakpoint(pykd.breakpoint):
100
             def __init__(self,offset):
101
                 super(MyBreakpoint, self).__init__(offset)
102
                 self.count = 0
103

104
             def onHit(self):
105
                 self.count = self.count + 1
106
                 return True
107

108
        bp = MyBreakpoint( self.targetModule.typedVar("CdeclFunc").getDebugStart() )
109
        self.assertEqual( pykd.executionStatus.Break, pykd.go() )
110
        self.assertEqual( 1, bp.count )
111

112

113
    def testBreakpointCondition1(self):
114

115
        def makebpcallback(n):
116

117
            def bpcallback():
118
                if pykd.getParam("b") > n:
119
                    return pykd.eventResult.Break
120
                return pykd.eventResult.Proceed
121
            return bpcallback
122

123
        bp = pykd.setBp( self.targetModule.typedVar("CdeclFunc").getDebugStart(), makebpcallback(1) )
124
        self.assertEqual( pykd.executionStatus.Break, pykd.go() )
125

126

127
    def testBreakpointCondition2(self):
128

129
        def makebpcallback(n):
130

131
            def bpcallback():
132
                if pykd.getParam("b") > n:
133
                    return pykd.eventResult.Break
134
                return pykd.eventResult.Proceed
135
            return bpcallback
136

137
        bp = pykd.setBp(self.targetModule.typedVar("CdeclFunc").getDebugStart(), makebpcallback(100) )
138
        self.assertEqual( pykd.executionStatus.NoDebuggee, pykd.go() )
139

140

141
    def testBreakpointEnum(self):
142

143
        b1 = pykd.setBp( self.targetModule.CdeclFunc)
144
        b2 = pykd.setBp( self.targetModule.CdeclFunc + 1)
145
        b3 = pykd.setBp( self.targetModule.CdeclFunc + 2)
146

147
        self.assertEqual(3, pykd.getNumberBreakpoints());
148
        bpLst = [pykd.getBp(i) for i in range(3)]
149
        self.assertEqual(3, len(bpLst))
150
        for bp in bpLst: bp.remove()
151

152
        self.assertEqual(0, pykd.getNumberBreakpoints());
153

154
    def testBreakpointChangeHandler(self):
155

156
        class BreakpointChangeHandler( pykd.eventHandler ):
157
            
158
            def __init__(self):
159
                pykd.eventHandler.__init__(self)
160
                self.count = 0
161

162
            def onChangeBreakpoints(self):
163
                 self.count += 1
164

165
        handler = BreakpointChangeHandler()
166
        bp = pykd.setBp(self.targetModule.CdeclFunc)
167
        bp.remove()
168
        self.assertEqual(0, pykd.getNumberBreakpoints())
169
        self.assertEqual(4, handler.count)
170

171
    def testLambdaBpContinue(self):
172
        bp1 = pykd.setBp(self.targetModule.CdeclFunc, lambda : None )
173
        bp2 = pykd.setBp(self.targetModule.CdeclFunc, lambda : False )
174
        bp3 = pykd.setBp(self.targetModule.CdeclFunc, lambda : 0 )
175
        self.assertEqual( pykd.executionStatus.NoDebuggee, pykd.go() )
176

177
    def testLambdaBpBreak(self):
178
        bp2 = pykd.setBp(self.targetModule.CdeclFunc, lambda : True )
179
        self.assertEqual( pykd.executionStatus.Break, pykd.go() )
180

181
    def testBpCommand(self):
182
        pykd.dbgCommand("bp 0x100")
183
        self.assertEqual(1, pykd.getNumberBreakpoints())
184
        bp = pykd.getBp(0)
185
        self.assertEqual(0x100, bp.getOffset())
186

187
    def testDoubleRemove(self):      
188
        bp = pykd.setBp( self.targetModule.CdeclFunc )
189
        bp.remove()
190
        self.assertRaises( pykd.DbgException, bp.remove )
191

192
    def testDetach(self):      
193
        bp = pykd.setBp( self.targetModule.CdeclFunc ).detach()
194
        self.assertEqual(1, pykd.getNumberBreakpoints())
195
        self.assertRaises(pykd.DbgException, bp.detach )
196
        del bp
197
        self.assertEqual(1, pykd.getNumberBreakpoints()) 

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.