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