BaseTool/Script: Add SmiHandleProfile OS tool to get symbol.
authorJiewen Yao <jiewen.yao@intel.com>
Sun, 22 Jan 2017 11:28:13 +0000 (03:28 -0800)
committerJiewen Yao <jiewen.yao@intel.com>
Wed, 22 Feb 2017 07:28:22 +0000 (15:28 +0800)
This tool accepts the input XML file generated by SmiHandlerProfile
application and convert the RVA address to be a user readable
symbol.
It also converts the GUID to be a user readable string.

Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jiewen Yao <jiewen.yao@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
BaseTools/Scripts/SmiHandlerProfileSymbolGen.py [new file with mode: 0644]

diff --git a/BaseTools/Scripts/SmiHandlerProfileSymbolGen.py b/BaseTools/Scripts/SmiHandlerProfileSymbolGen.py
new file mode 100644 (file)
index 0000000..f03278b
--- /dev/null
@@ -0,0 +1,312 @@
+##\r
+# Generate symbal for SMI handler profile info.\r
+#\r
+# This tool depends on DIA2Dump.exe (VS) or nm (gcc) to parse debug entry.\r
+#\r
+# Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>\r
+# This program and the accompanying materials are licensed and made available under\r
+# the terms and conditions of the BSD License that accompanies this distribution.\r
+# The full text of the license may be found at\r
+# http://opensource.org/licenses/bsd-license.php.\r
+#\r
+# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#\r
+##\r
+\r
+import os\r
+import re\r
+import sys\r
+from optparse import OptionParser\r
+\r
+from xml.dom.minidom import parse\r
+import xml.dom.minidom\r
+\r
+versionNumber = "1.1"\r
+__copyright__ = "Copyright (c) 2016, Intel Corporation. All rights reserved."\r
+\r
+class Symbols:\r
+    def __init__(self):\r
+        self.listLineAddress = []\r
+        self.pdbName = ""\r
+        # Cache for function\r
+        self.functionName = ""\r
+        # Cache for line\r
+        self.sourceName = ""\r
+\r
+\r
+    def getSymbol (self, rva):\r
+        index = 0\r
+        lineName  = 0\r
+        sourceName = "??"\r
+        while index + 1 < self.lineCount :\r
+            if self.listLineAddress[index][0] <= rva and self.listLineAddress[index + 1][0] > rva :\r
+                offset = rva - self.listLineAddress[index][0]\r
+                functionName = self.listLineAddress[index][1]\r
+                lineName = self.listLineAddress[index][2]\r
+                sourceName = self.listLineAddress[index][3]\r
+                if lineName == 0 :\r
+                  return [functionName]\r
+                else :\r
+                  return [functionName, sourceName, lineName]\r
+            index += 1\r
+\r
+        return []\r
+\r
+    def parse_debug_file(self, driverName, pdbName):\r
+        if cmp (pdbName, "") == 0 :\r
+            return\r
+        self.pdbName = pdbName;\r
+\r
+        try:\r
+            nmCommand = "nm"\r
+            nmLineOption = "-l"\r
+            print "parsing (debug) - " + pdbName\r
+            os.system ('%s %s %s > nmDump.line.log' % (nmCommand, nmLineOption, pdbName))\r
+        except :\r
+            print 'ERROR: nm command not available.  Please verify PATH'\r
+            return\r
+\r
+        #\r
+        # parse line\r
+        #\r
+        linefile = open("nmDump.line.log")\r
+        reportLines = linefile.readlines()\r
+        linefile.close()\r
+\r
+        # 000113ca T AllocatePool c:\home\edk-ii\MdePkg\Library\UefiMemoryAllocationLib\MemoryAllocationLib.c:399\r
+        patchLineFileMatchString = "([0-9a-fA-F]*)\s+[T|D|t|d]\s+(\w+)\s*((?:[a-zA-Z]:)?[\w+\-./_a-zA-Z0-9\\\\]*):?([0-9]*)"\r
+\r
+        for reportLine in reportLines:\r
+            match = re.match(patchLineFileMatchString, reportLine)\r
+            if match is not None:\r
+                rva = int (match.group(1), 16)\r
+                functionName = match.group(2)\r
+                sourceName = match.group(3)\r
+                if cmp (match.group(4), "") != 0 :\r
+                    lineName = int (match.group(4))\r
+                else :\r
+                    lineName = 0\r
+                self.listLineAddress.append ([rva, functionName, lineName, sourceName])\r
+\r
+        self.lineCount = len (self.listLineAddress)\r
+\r
+        self.listLineAddress = sorted(self.listLineAddress, key=lambda symbolAddress:symbolAddress[0])\r
+\r
+    def parse_pdb_file(self, driverName, pdbName):\r
+        if cmp (pdbName, "") == 0 :\r
+            return\r
+        self.pdbName = pdbName;\r
+\r
+        try:\r
+            #DIA2DumpCommand = "\"C:\\Program Files (x86)\Microsoft Visual Studio 14.0\\DIA SDK\\Samples\\DIA2Dump\\x64\\Debug\\Dia2Dump.exe\""\r
+            DIA2DumpCommand = "Dia2Dump.exe"\r
+            #DIA2SymbolOption = "-p"\r
+            DIA2LinesOption = "-l"\r
+            print "parsing (pdb) - " + pdbName\r
+            #os.system ('%s %s %s > DIA2Dump.symbol.log' % (DIA2DumpCommand, DIA2SymbolOption, pdbName))\r
+            os.system ('%s %s %s > DIA2Dump.line.log' % (DIA2DumpCommand, DIA2LinesOption, pdbName))\r
+        except :\r
+            print 'ERROR: DIA2Dump command not available.  Please verify PATH'\r
+            return\r
+\r
+        #\r
+        # parse line\r
+        #\r
+        linefile = open("DIA2Dump.line.log")\r
+        reportLines = linefile.readlines()\r
+        linefile.close()\r
+\r
+        #   ** GetDebugPrintErrorLevel\r
+        # line 32 at [0000C790][0001:0000B790], len = 0x3 c:\home\edk-ii\mdepkg\library\basedebugprinterrorlevellib\basedebugprinterrorlevellib.c (MD5: 687C0AE564079D35D56ED5D84A6164CC)\r
+        # line 36 at [0000C793][0001:0000B793], len = 0x5\r
+        # line 37 at [0000C798][0001:0000B798], len = 0x2\r
+\r
+        patchLineFileMatchString = "\s+line ([0-9]+) at \[([0-9a-fA-F]{8})\]\[[0-9a-fA-F]{4}\:[0-9a-fA-F]{8}\], len = 0x[0-9a-fA-F]+\s*([\w+\-\:./_a-zA-Z0-9\\\\]*)\s*"\r
+        patchLineFileMatchStringFunc = "\*\*\s+(\w+)\s*"\r
+\r
+        for reportLine in reportLines:\r
+            match = re.match(patchLineFileMatchString, reportLine)\r
+            if match is not None:\r
+                if cmp (match.group(3), "") != 0 :\r
+                    self.sourceName = match.group(3)\r
+                sourceName = self.sourceName\r
+                functionName = self.functionName\r
+\r
+                rva = int (match.group(2), 16)\r
+                lineName = int (match.group(1))\r
+                self.listLineAddress.append ([rva, functionName, lineName, sourceName])\r
+            else :\r
+                match = re.match(patchLineFileMatchStringFunc, reportLine)\r
+                if match is not None:\r
+                    self.functionName = match.group(1)\r
+\r
+        self.lineCount = len (self.listLineAddress)\r
+        self.listLineAddress = sorted(self.listLineAddress, key=lambda symbolAddress:symbolAddress[0])\r
+\r
+class SymbolsFile:\r
+    def __init__(self):\r
+        self.symbolsTable = {}\r
+\r
+symbolsFile = ""\r
+\r
+driverName = ""\r
+rvaName = ""\r
+symbolName = ""\r
+\r
+def getSymbolName(driverName, rva):\r
+    global symbolsFile\r
+\r
+    try :\r
+        symbolList = symbolsFile.symbolsTable[driverName]\r
+        if symbolList is not None:\r
+            return symbolList.getSymbol (rva)\r
+        else:\r
+            return []\r
+    except Exception:\r
+        return []\r
+\r
+def myOptionParser():\r
+    usage = "%prog [--version] [-h] [--help] [-i inputfile [-o outputfile] [-g guidreffile]]"\r
+    Parser = OptionParser(usage=usage, description=__copyright__, version="%prog " + str(versionNumber))\r
+    Parser.add_option("-i", "--inputfile", dest="inputfilename", type="string", help="The input memory profile info file output from MemoryProfileInfo application in MdeModulePkg")\r
+    Parser.add_option("-o", "--outputfile", dest="outputfilename", type="string", help="The output memory profile info file with symbol, MemoryProfileInfoSymbol.txt will be used if it is not specified")\r
+    Parser.add_option("-g", "--guidref", dest="guidreffilename", type="string", help="The input guid ref file output from build")\r
+\r
+    (Options, args) = Parser.parse_args()\r
+    if Options.inputfilename is None:\r
+        Parser.error("no input file specified")\r
+    if Options.outputfilename is None:\r
+        Options.outputfilename = "SmiHandlerProfileInfoSymbol.xml"\r
+    return Options\r
+\r
+dictGuid = {\r
+  '00000000-0000-0000-0000-000000000000':'gZeroGuid',\r
+  '2A571201-4966-47F6-8B86-F31E41F32F10':'gEfiEventLegacyBootGuid',\r
+  '27ABF055-B1B8-4C26-8048-748F37BAA2DF':'gEfiEventExitBootServicesGuid',\r
+  '7CE88FB3-4BD7-4679-87A8-A8D8DEE50D2B':'gEfiEventReadyToBootGuid',\r
+  '02CE967A-DD7E-4FFC-9EE7-810CF0470880':'gEfiEndOfDxeEventGroupGuid',\r
+  '60FF8964-E906-41D0-AFED-F241E974E08E':'gEfiDxeSmmReadyToLockProtocolGuid',\r
+  '18A3C6DC-5EEA-48C8-A1C1-B53389F98999':'gEfiSmmSwDispatch2ProtocolGuid',\r
+  '456D2859-A84B-4E47-A2EE-3276D886997D':'gEfiSmmSxDispatch2ProtocolGuid',\r
+  '4CEC368E-8E8E-4D71-8BE1-958C45FC8A53':'gEfiSmmPeriodicTimerDispatch2ProtocolGuid',\r
+  'EE9B8D90-C5A6-40A2-BDE2-52558D33CCA1':'gEfiSmmUsbDispatch2ProtocolGuid',\r
+  '25566B03-B577-4CBF-958C-ED663EA24380':'gEfiSmmGpiDispatch2ProtocolGuid',\r
+  '7300C4A1-43F2-4017-A51B-C81A7F40585B':'gEfiSmmStandbyButtonDispatch2ProtocolGuid',\r
+  '1B1183FA-1823-46A7-8872-9C578755409D':'gEfiSmmPowerButtonDispatch2ProtocolGuid',\r
+  '58DC368D-7BFA-4E77-ABBC-0E29418DF930':'gEfiSmmIoTrapDispatch2ProtocolGuid',\r
+  }\r
+\r
+def genGuidString(guidreffile):\r
+    guidLines = guidreffile.readlines()\r
+    for guidLine in guidLines:\r
+        guidLineList = guidLine.split(" ")\r
+        if len(guidLineList) == 2:\r
+            guid = guidLineList[0]\r
+            guidName = guidLineList[1]\r
+            if guid not in dictGuid.keys() :\r
+                dictGuid[guid] = guidName\r
+\r
+def createSym(symbolName):\r
+    SymbolNode = xml.dom.minidom.Document().createElement("Symbol")\r
+    SymbolFunction = xml.dom.minidom.Document().createElement("Function")\r
+    SymbolFunctionData = xml.dom.minidom.Document().createTextNode(symbolName[0])\r
+    SymbolFunction.appendChild(SymbolFunctionData)\r
+    SymbolNode.appendChild(SymbolFunction)\r
+    if (len(symbolName)) >= 2:\r
+        SymbolSourceFile = xml.dom.minidom.Document().createElement("SourceFile")\r
+        SymbolSourceFileData = xml.dom.minidom.Document().createTextNode(symbolName[1])\r
+        SymbolSourceFile.appendChild(SymbolSourceFileData)\r
+        SymbolNode.appendChild(SymbolSourceFile)\r
+        if (len(symbolName)) >= 3:\r
+            SymbolLineNumber = xml.dom.minidom.Document().createElement("LineNumber")\r
+            SymbolLineNumberData = xml.dom.minidom.Document().createTextNode(str(symbolName[2]))\r
+            SymbolLineNumber.appendChild(SymbolLineNumberData)\r
+            SymbolNode.appendChild(SymbolLineNumber)\r
+    return SymbolNode\r
+\r
+def main():\r
+    global symbolsFile\r
+    global Options\r
+    Options = myOptionParser()\r
+\r
+    symbolsFile = SymbolsFile()\r
+\r
+    try :\r
+        DOMTree = xml.dom.minidom.parse(Options.inputfilename)\r
+    except Exception:\r
+        print "fail to open input " + Options.inputfilename\r
+        return 1\r
+\r
+    if Options.guidreffilename is not None:\r
+        try :\r
+            guidreffile = open(Options.guidreffilename)\r
+        except Exception:\r
+            print "fail to open guidref" + Options.guidreffilename\r
+            return 1\r
+        genGuidString(guidreffile)\r
+        guidreffile.close()\r
+\r
+    SmiHandlerProfile = DOMTree.documentElement\r
+\r
+    SmiHandlerDatabase = SmiHandlerProfile.getElementsByTagName("SmiHandlerDatabase")\r
+    SmiHandlerCategory = SmiHandlerDatabase[0].getElementsByTagName("SmiHandlerCategory")\r
+    for smiHandlerCategory in SmiHandlerCategory:\r
+        SmiEntry = smiHandlerCategory.getElementsByTagName("SmiEntry")\r
+        for smiEntry in SmiEntry:\r
+            if smiEntry.hasAttribute("HandlerType"):\r
+                guidValue = smiEntry.getAttribute("HandlerType")\r
+                if guidValue in dictGuid.keys() :\r
+                    smiEntry.setAttribute("HandlerType", dictGuid[guidValue])\r
+            SmiHandler = smiEntry.getElementsByTagName("SmiHandler")\r
+            for smiHandler in SmiHandler:\r
+                Module = smiHandler.getElementsByTagName("Module")\r
+                Pdb = Module[0].getElementsByTagName("Pdb")\r
+                if (len(Pdb)) >= 1:\r
+                    driverName = Module[0].getAttribute("Name")\r
+                    pdbName = Pdb[0].childNodes[0].data\r
+\r
+                    Module[0].removeChild(Pdb[0])\r
+\r
+                    symbolsFile.symbolsTable[driverName] = Symbols()\r
+\r
+                    if cmp (pdbName[-3:], "pdb") == 0 :\r
+                        symbolsFile.symbolsTable[driverName].parse_pdb_file (driverName, pdbName)\r
+                    else :\r
+                        symbolsFile.symbolsTable[driverName].parse_debug_file (driverName, pdbName)\r
+\r
+                    Handler = smiHandler.getElementsByTagName("Handler")\r
+                    RVA = Handler[0].getElementsByTagName("RVA")\r
+                    print "    Handler RVA: %s" % RVA[0].childNodes[0].data\r
+\r
+                    if (len(RVA)) >= 1:\r
+                        rvaName = RVA[0].childNodes[0].data\r
+                        symbolName = getSymbolName (driverName, int(rvaName, 16))\r
+\r
+                        if (len(symbolName)) >= 1:\r
+                            SymbolNode = createSym(symbolName)\r
+                            Handler[0].appendChild(SymbolNode)\r
+\r
+                    Caller = smiHandler.getElementsByTagName("Caller")\r
+                    RVA = Caller[0].getElementsByTagName("RVA")\r
+                    print "    Caller RVA: %s" % RVA[0].childNodes[0].data\r
+\r
+                    if (len(RVA)) >= 1:\r
+                        rvaName = RVA[0].childNodes[0].data\r
+                        symbolName = getSymbolName (driverName, int(rvaName, 16))\r
+\r
+                        if (len(symbolName)) >= 1:\r
+                            SymbolNode = createSym(symbolName)\r
+                            Caller[0].appendChild(SymbolNode)\r
+\r
+    try :\r
+        newfile = open(Options.outputfilename, "w")\r
+    except Exception:\r
+        print "fail to open output" + Options.outputfilename\r
+        return 1\r
+\r
+    newfile.write(DOMTree.toprettyxml(indent = "\t", newl = "\n", encoding = "utf-8"))\r
+    newfile.close()\r
+\r
+if __name__ == '__main__':\r
+    sys.exit(main())\r