stacktest.py 5.05 KB
Newer Older
1
import unittest
2 3 4
import os
import sys

5 6 7 8 9 10 11 12 13 14 15 16 17 18
import pykd
import target

class StackTest(unittest.TestCase):
    
    def setUp(self):
        self.processId = pykd.startProcess( target.appPath + " stacktest" )
        pykd.go() # skip initial breakpoint

    def tearDown(self):
        pykd.killProcess( self.processId )

    def testGetStack(self):

19 20 21
        expectedStack = [
                  'targetapp!stackTestClass::stackMethod',
                  'targetapp!stackTestRun2',
22
                  'targetapp!stackTestRun1',
23
                  'targetapp!stackTestRun']
24 25 26 27 28 29

        realStack = []
        for frame in pykd.getStack():
            moduleName, symbolName, disp = pykd.findSymbolAndDisp( frame.ip )
            realStack.append( "%s!%s" % ( moduleName, symbolName ) )

30
        self.assertEqual( expectedStack, realStack[0:4])
31

32
    def testGetParams(self):
33 34 35 36 37 38

        frame0 = pykd.getFrame()
        self.assertEqual( ["this", "a", "b"], [ name for name, param in frame0.getParams() ] )

        frame1 = pykd.getStack()[1]
        self.assertEqual( ["a", "b", "c"], [ name for name, param in frame1.getParams() ] )
39 40
        self.assertEqual( 10, frame1.getParam("a") )
        self.assertEqual( 10, frame1.params["a"] )
41 42 43 44

        frame2 = pykd.getStack()[2]
        self.assertEqual( 10, frame2.params["a"] )
        self.assertEqual( 10, frame2.getParam("a") )
45 46 47

    def testGetLocals(self):
        expectedLocals = ["localDouble", "localFloat", "localChars"]
48 49 50 51 52 53 54 55 56 57 58 59
        frame2 = pykd.getStack()[2]
        self.assertEqual( expectedLocals, [name for name, param in frame2.getLocals() ] )
        self.assertEqual( 0.0, frame2.locals["localDouble"] )

    def testGetParamsNoSymbol(self):
        topFrame = pykd.getStack()[-1]
        self.assertEqual(0, len(topFrame.getParams()))

    def testGetLocalsNoSymbol(self):
        topFrame = pykd.getStack()[-1]
        self.assertEqual(0, len(topFrame.getLocals()))

60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
class InlineStackTest(unittest.TestCase):

    def setUp(self):
        dumpDir = os.path.join( os.path.dirname(sys.argv[0]), r"..\..\kdlibcpp\kdlib\tests\dumps\targetapp_stacktest_x64_release")
        dump_file = os.path.join( dumpDir, "targetapp_stacktest_x64_release.cab" )
        self.symbolPath = pykd.getSymbolPath()
        symbolPath = self.symbolPath + ";" + dumpDir
        pykd.setSymbolPath(symbolPath)
        self.dump_id = pykd.loadDump( dump_file )

    def tearDown(self):
        pykd.closeDump( self.dump_id )
        pykd.setSymbolPath(self.symbolPath)

    def testGetStack(self):

        expectedStack = [
                  'targetapp!stackTestClass::stackMethod',
                  'targetapp!stackTestRun2',
                  'targetapp!stackTestRun1',
                  'targetapp!stackTestRun']

        realStack = []
        for frame in pykd.getStack(inlineFrames = True):
            moduleName, symbolName, disp = frame.findSymbol()
            realStack.append( "%s!%s" % ( moduleName, symbolName ) )

        self.assertEqual( expectedStack, realStack[0:4])

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    def testFrameIsInline(self):
        self.assertEqual([True, True, False, True], 
                         [fr.isInline() for fr in pykd.getStack(inlineFrames = True)][:4]
                         )

    def testGetSourceLine(self):

        expectedStack = ['targetapp.cpp+129', 'targetapp.cpp+144', 'targetapp.cpp+172', 'targetapp.cpp+185']

        realStack = []
        for frame in pykd.getStack(inlineFrames = True)[:4]:
            fileName, lineNo = frame.getSourceLine()
            realStack.append("%s+%s" % ( os.path.basename(fileName), lineNo ) )
 
        self.assertEqual( expectedStack, realStack)
105

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
class DiaRegToRegRelativeI386Test(unittest.TestCase):
    def setUp(self):
        dumpDir = os.path.join( os.path.dirname(sys.argv[0]), r"..\..\kdlibcpp\kdlib\tests\dumps\targetapp_test_cv_allreg_i386")
        dump_file = os.path.join( dumpDir, "targetapp_test_cv_allreg_i386.cab" )
        self.symbolPath = pykd.getSymbolPath()
        symbolPath = self.symbolPath + ";" + dumpDir
        pykd.setSymbolPath(symbolPath)
        self.dump_id = pykd.loadDump( dump_file )

    def tearDown(self):
        pykd.closeDump( self.dump_id )
        pykd.setSymbolPath(self.symbolPath)

    def testParams(self):
        params = pykd.getFrame().getParams()
        self.assertNotEqual(0, len(params))

class DiaRegToRegRelativeAmd64Test(unittest.TestCase):
    def setUp(self):
        dumpDir = os.path.join( os.path.dirname(sys.argv[0]), r"..\..\kdlibcpp\kdlib\tests\dumps\targetapp_test_cv_allreg_amd64")
        dump_file = os.path.join( dumpDir, "targetapp_test_cv_allreg_amd64.cab" )
        self.symbolPath = pykd.getSymbolPath()
        symbolPath = self.symbolPath + ";" + dumpDir
        pykd.setSymbolPath(symbolPath)
        self.dump_id = pykd.loadDump( dump_file )

    def tearDown(self):
        pykd.closeDump( self.dump_id )
        pykd.setSymbolPath(self.symbolPath)
135

136 137 138
    def testLocals(self):
        locals = pykd.getFrame().getLocals()
        self.assertNotEqual(0, len(locals))