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