BaseTool/Script: Add SmiHandleProfile OS tool to get symbol.
[mirror_edk2.git] / BaseTools / Scripts / SmiHandlerProfileSymbolGen.py
1 ##
2 # Generate symbal for SMI handler profile info.
3 #
4 # This tool depends on DIA2Dump.exe (VS) or nm (gcc) to parse debug entry.
5 #
6 # Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
7 # This program and the accompanying materials are licensed and made available under
8 # the terms and conditions of the BSD License that accompanies this distribution.
9 # The full text of the license may be found at
10 # http://opensource.org/licenses/bsd-license.php.
11 #
12 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 #
15 ##
16
17 import os
18 import re
19 import sys
20 from optparse import OptionParser
21
22 from xml.dom.minidom import parse
23 import xml.dom.minidom
24
25 versionNumber = "1.1"
26 __copyright__ = "Copyright (c) 2016, Intel Corporation. All rights reserved."
27
28 class Symbols:
29 def __init__(self):
30 self.listLineAddress = []
31 self.pdbName = ""
32 # Cache for function
33 self.functionName = ""
34 # Cache for line
35 self.sourceName = ""
36
37
38 def getSymbol (self, rva):
39 index = 0
40 lineName = 0
41 sourceName = "??"
42 while index + 1 < self.lineCount :
43 if self.listLineAddress[index][0] <= rva and self.listLineAddress[index + 1][0] > rva :
44 offset = rva - self.listLineAddress[index][0]
45 functionName = self.listLineAddress[index][1]
46 lineName = self.listLineAddress[index][2]
47 sourceName = self.listLineAddress[index][3]
48 if lineName == 0 :
49 return [functionName]
50 else :
51 return [functionName, sourceName, lineName]
52 index += 1
53
54 return []
55
56 def parse_debug_file(self, driverName, pdbName):
57 if cmp (pdbName, "") == 0 :
58 return
59 self.pdbName = pdbName;
60
61 try:
62 nmCommand = "nm"
63 nmLineOption = "-l"
64 print "parsing (debug) - " + pdbName
65 os.system ('%s %s %s > nmDump.line.log' % (nmCommand, nmLineOption, pdbName))
66 except :
67 print 'ERROR: nm command not available. Please verify PATH'
68 return
69
70 #
71 # parse line
72 #
73 linefile = open("nmDump.line.log")
74 reportLines = linefile.readlines()
75 linefile.close()
76
77 # 000113ca T AllocatePool c:\home\edk-ii\MdePkg\Library\UefiMemoryAllocationLib\MemoryAllocationLib.c:399
78 patchLineFileMatchString = "([0-9a-fA-F]*)\s+[T|D|t|d]\s+(\w+)\s*((?:[a-zA-Z]:)?[\w+\-./_a-zA-Z0-9\\\\]*):?([0-9]*)"
79
80 for reportLine in reportLines:
81 match = re.match(patchLineFileMatchString, reportLine)
82 if match is not None:
83 rva = int (match.group(1), 16)
84 functionName = match.group(2)
85 sourceName = match.group(3)
86 if cmp (match.group(4), "") != 0 :
87 lineName = int (match.group(4))
88 else :
89 lineName = 0
90 self.listLineAddress.append ([rva, functionName, lineName, sourceName])
91
92 self.lineCount = len (self.listLineAddress)
93
94 self.listLineAddress = sorted(self.listLineAddress, key=lambda symbolAddress:symbolAddress[0])
95
96 def parse_pdb_file(self, driverName, pdbName):
97 if cmp (pdbName, "") == 0 :
98 return
99 self.pdbName = pdbName;
100
101 try:
102 #DIA2DumpCommand = "\"C:\\Program Files (x86)\Microsoft Visual Studio 14.0\\DIA SDK\\Samples\\DIA2Dump\\x64\\Debug\\Dia2Dump.exe\""
103 DIA2DumpCommand = "Dia2Dump.exe"
104 #DIA2SymbolOption = "-p"
105 DIA2LinesOption = "-l"
106 print "parsing (pdb) - " + pdbName
107 #os.system ('%s %s %s > DIA2Dump.symbol.log' % (DIA2DumpCommand, DIA2SymbolOption, pdbName))
108 os.system ('%s %s %s > DIA2Dump.line.log' % (DIA2DumpCommand, DIA2LinesOption, pdbName))
109 except :
110 print 'ERROR: DIA2Dump command not available. Please verify PATH'
111 return
112
113 #
114 # parse line
115 #
116 linefile = open("DIA2Dump.line.log")
117 reportLines = linefile.readlines()
118 linefile.close()
119
120 # ** GetDebugPrintErrorLevel
121 # line 32 at [0000C790][0001:0000B790], len = 0x3 c:\home\edk-ii\mdepkg\library\basedebugprinterrorlevellib\basedebugprinterrorlevellib.c (MD5: 687C0AE564079D35D56ED5D84A6164CC)
122 # line 36 at [0000C793][0001:0000B793], len = 0x5
123 # line 37 at [0000C798][0001:0000B798], len = 0x2
124
125 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*"
126 patchLineFileMatchStringFunc = "\*\*\s+(\w+)\s*"
127
128 for reportLine in reportLines:
129 match = re.match(patchLineFileMatchString, reportLine)
130 if match is not None:
131 if cmp (match.group(3), "") != 0 :
132 self.sourceName = match.group(3)
133 sourceName = self.sourceName
134 functionName = self.functionName
135
136 rva = int (match.group(2), 16)
137 lineName = int (match.group(1))
138 self.listLineAddress.append ([rva, functionName, lineName, sourceName])
139 else :
140 match = re.match(patchLineFileMatchStringFunc, reportLine)
141 if match is not None:
142 self.functionName = match.group(1)
143
144 self.lineCount = len (self.listLineAddress)
145 self.listLineAddress = sorted(self.listLineAddress, key=lambda symbolAddress:symbolAddress[0])
146
147 class SymbolsFile:
148 def __init__(self):
149 self.symbolsTable = {}
150
151 symbolsFile = ""
152
153 driverName = ""
154 rvaName = ""
155 symbolName = ""
156
157 def getSymbolName(driverName, rva):
158 global symbolsFile
159
160 try :
161 symbolList = symbolsFile.symbolsTable[driverName]
162 if symbolList is not None:
163 return symbolList.getSymbol (rva)
164 else:
165 return []
166 except Exception:
167 return []
168
169 def myOptionParser():
170 usage = "%prog [--version] [-h] [--help] [-i inputfile [-o outputfile] [-g guidreffile]]"
171 Parser = OptionParser(usage=usage, description=__copyright__, version="%prog " + str(versionNumber))
172 Parser.add_option("-i", "--inputfile", dest="inputfilename", type="string", help="The input memory profile info file output from MemoryProfileInfo application in MdeModulePkg")
173 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")
174 Parser.add_option("-g", "--guidref", dest="guidreffilename", type="string", help="The input guid ref file output from build")
175
176 (Options, args) = Parser.parse_args()
177 if Options.inputfilename is None:
178 Parser.error("no input file specified")
179 if Options.outputfilename is None:
180 Options.outputfilename = "SmiHandlerProfileInfoSymbol.xml"
181 return Options
182
183 dictGuid = {
184 '00000000-0000-0000-0000-000000000000':'gZeroGuid',
185 '2A571201-4966-47F6-8B86-F31E41F32F10':'gEfiEventLegacyBootGuid',
186 '27ABF055-B1B8-4C26-8048-748F37BAA2DF':'gEfiEventExitBootServicesGuid',
187 '7CE88FB3-4BD7-4679-87A8-A8D8DEE50D2B':'gEfiEventReadyToBootGuid',
188 '02CE967A-DD7E-4FFC-9EE7-810CF0470880':'gEfiEndOfDxeEventGroupGuid',
189 '60FF8964-E906-41D0-AFED-F241E974E08E':'gEfiDxeSmmReadyToLockProtocolGuid',
190 '18A3C6DC-5EEA-48C8-A1C1-B53389F98999':'gEfiSmmSwDispatch2ProtocolGuid',
191 '456D2859-A84B-4E47-A2EE-3276D886997D':'gEfiSmmSxDispatch2ProtocolGuid',
192 '4CEC368E-8E8E-4D71-8BE1-958C45FC8A53':'gEfiSmmPeriodicTimerDispatch2ProtocolGuid',
193 'EE9B8D90-C5A6-40A2-BDE2-52558D33CCA1':'gEfiSmmUsbDispatch2ProtocolGuid',
194 '25566B03-B577-4CBF-958C-ED663EA24380':'gEfiSmmGpiDispatch2ProtocolGuid',
195 '7300C4A1-43F2-4017-A51B-C81A7F40585B':'gEfiSmmStandbyButtonDispatch2ProtocolGuid',
196 '1B1183FA-1823-46A7-8872-9C578755409D':'gEfiSmmPowerButtonDispatch2ProtocolGuid',
197 '58DC368D-7BFA-4E77-ABBC-0E29418DF930':'gEfiSmmIoTrapDispatch2ProtocolGuid',
198 }
199
200 def genGuidString(guidreffile):
201 guidLines = guidreffile.readlines()
202 for guidLine in guidLines:
203 guidLineList = guidLine.split(" ")
204 if len(guidLineList) == 2:
205 guid = guidLineList[0]
206 guidName = guidLineList[1]
207 if guid not in dictGuid.keys() :
208 dictGuid[guid] = guidName
209
210 def createSym(symbolName):
211 SymbolNode = xml.dom.minidom.Document().createElement("Symbol")
212 SymbolFunction = xml.dom.minidom.Document().createElement("Function")
213 SymbolFunctionData = xml.dom.minidom.Document().createTextNode(symbolName[0])
214 SymbolFunction.appendChild(SymbolFunctionData)
215 SymbolNode.appendChild(SymbolFunction)
216 if (len(symbolName)) >= 2:
217 SymbolSourceFile = xml.dom.minidom.Document().createElement("SourceFile")
218 SymbolSourceFileData = xml.dom.minidom.Document().createTextNode(symbolName[1])
219 SymbolSourceFile.appendChild(SymbolSourceFileData)
220 SymbolNode.appendChild(SymbolSourceFile)
221 if (len(symbolName)) >= 3:
222 SymbolLineNumber = xml.dom.minidom.Document().createElement("LineNumber")
223 SymbolLineNumberData = xml.dom.minidom.Document().createTextNode(str(symbolName[2]))
224 SymbolLineNumber.appendChild(SymbolLineNumberData)
225 SymbolNode.appendChild(SymbolLineNumber)
226 return SymbolNode
227
228 def main():
229 global symbolsFile
230 global Options
231 Options = myOptionParser()
232
233 symbolsFile = SymbolsFile()
234
235 try :
236 DOMTree = xml.dom.minidom.parse(Options.inputfilename)
237 except Exception:
238 print "fail to open input " + Options.inputfilename
239 return 1
240
241 if Options.guidreffilename is not None:
242 try :
243 guidreffile = open(Options.guidreffilename)
244 except Exception:
245 print "fail to open guidref" + Options.guidreffilename
246 return 1
247 genGuidString(guidreffile)
248 guidreffile.close()
249
250 SmiHandlerProfile = DOMTree.documentElement
251
252 SmiHandlerDatabase = SmiHandlerProfile.getElementsByTagName("SmiHandlerDatabase")
253 SmiHandlerCategory = SmiHandlerDatabase[0].getElementsByTagName("SmiHandlerCategory")
254 for smiHandlerCategory in SmiHandlerCategory:
255 SmiEntry = smiHandlerCategory.getElementsByTagName("SmiEntry")
256 for smiEntry in SmiEntry:
257 if smiEntry.hasAttribute("HandlerType"):
258 guidValue = smiEntry.getAttribute("HandlerType")
259 if guidValue in dictGuid.keys() :
260 smiEntry.setAttribute("HandlerType", dictGuid[guidValue])
261 SmiHandler = smiEntry.getElementsByTagName("SmiHandler")
262 for smiHandler in SmiHandler:
263 Module = smiHandler.getElementsByTagName("Module")
264 Pdb = Module[0].getElementsByTagName("Pdb")
265 if (len(Pdb)) >= 1:
266 driverName = Module[0].getAttribute("Name")
267 pdbName = Pdb[0].childNodes[0].data
268
269 Module[0].removeChild(Pdb[0])
270
271 symbolsFile.symbolsTable[driverName] = Symbols()
272
273 if cmp (pdbName[-3:], "pdb") == 0 :
274 symbolsFile.symbolsTable[driverName].parse_pdb_file (driverName, pdbName)
275 else :
276 symbolsFile.symbolsTable[driverName].parse_debug_file (driverName, pdbName)
277
278 Handler = smiHandler.getElementsByTagName("Handler")
279 RVA = Handler[0].getElementsByTagName("RVA")
280 print " Handler RVA: %s" % RVA[0].childNodes[0].data
281
282 if (len(RVA)) >= 1:
283 rvaName = RVA[0].childNodes[0].data
284 symbolName = getSymbolName (driverName, int(rvaName, 16))
285
286 if (len(symbolName)) >= 1:
287 SymbolNode = createSym(symbolName)
288 Handler[0].appendChild(SymbolNode)
289
290 Caller = smiHandler.getElementsByTagName("Caller")
291 RVA = Caller[0].getElementsByTagName("RVA")
292 print " Caller RVA: %s" % RVA[0].childNodes[0].data
293
294 if (len(RVA)) >= 1:
295 rvaName = RVA[0].childNodes[0].data
296 symbolName = getSymbolName (driverName, int(rvaName, 16))
297
298 if (len(symbolName)) >= 1:
299 SymbolNode = createSym(symbolName)
300 Caller[0].appendChild(SymbolNode)
301
302 try :
303 newfile = open(Options.outputfilename, "w")
304 except Exception:
305 print "fail to open output" + Options.outputfilename
306 return 1
307
308 newfile.write(DOMTree.toprettyxml(indent = "\t", newl = "\n", encoding = "utf-8"))
309 newfile.close()
310
311 if __name__ == '__main__':
312 sys.exit(main())