2 # Generate symbal for SMI handler profile info.
4 # This tool depends on DIA2Dump.exe (VS) or nm (gcc) to parse debug entry.
6 # Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
7 # SPDX-License-Identifier: BSD-2-Clause-Patent
11 from __future__
import print_function
15 from optparse
import OptionParser
17 from xml
.dom
.minidom
import parse
18 import xml
.dom
.minidom
21 __copyright__
= "Copyright (c) 2016, Intel Corporation. All rights reserved."
25 self
.listLineAddress
= []
28 self
.functionName
= ""
33 def getSymbol (self
, rva
):
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]
46 return [functionName
, sourceName
, lineName
]
51 def parse_debug_file(self
, driverName
, pdbName
):
52 if cmp (pdbName
, "") == 0 :
54 self
.pdbName
= pdbName
;
59 print("parsing (debug) - " + pdbName
)
60 os
.system ('%s %s %s > nmDump.line.log' % (nmCommand
, nmLineOption
, pdbName
))
62 print('ERROR: nm command not available. Please verify PATH')
68 linefile
= open("nmDump.line.log")
69 reportLines
= linefile
.readlines()
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]*)"
75 for reportLine
in reportLines
:
76 match
= re
.match(patchLineFileMatchString
, reportLine
)
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))
85 self
.listLineAddress
.append ([rva
, functionName
, lineName
, sourceName
])
87 self
.lineCount
= len (self
.listLineAddress
)
89 self
.listLineAddress
= sorted(self
.listLineAddress
, key
=lambda symbolAddress
:symbolAddress
[0])
91 def parse_pdb_file(self
, driverName
, pdbName
):
92 if cmp (pdbName
, "") == 0 :
94 self
.pdbName
= pdbName
;
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
))
105 print('ERROR: DIA2Dump command not available. Please verify PATH')
111 linefile
= open("DIA2Dump.line.log")
112 reportLines
= linefile
.readlines()
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
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*"
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
131 rva
= int (match
.group(2), 16)
132 lineName
= int (match
.group(1))
133 self
.listLineAddress
.append ([rva
, functionName
, lineName
, sourceName
])
135 match
= re
.match(patchLineFileMatchStringFunc
, reportLine
)
136 if match
is not None:
137 self
.functionName
= match
.group(1)
139 self
.lineCount
= len (self
.listLineAddress
)
140 self
.listLineAddress
= sorted(self
.listLineAddress
, key
=lambda symbolAddress
:symbolAddress
[0])
144 self
.symbolsTable
= {}
152 def getSymbolName(driverName
, rva
):
156 symbolList
= symbolsFile
.symbolsTable
[driverName
]
157 if symbolList
is not None:
158 return symbolList
.getSymbol (rva
)
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")
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"
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',
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
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
)
226 Options
= myOptionParser()
228 symbolsFile
= SymbolsFile()
231 DOMTree
= xml
.dom
.minidom
.parse(Options
.inputfilename
)
233 print("fail to open input " + Options
.inputfilename
)
236 if Options
.guidreffilename
is not None:
238 guidreffile
= open(Options
.guidreffilename
)
240 print("fail to open guidref" + Options
.guidreffilename
)
242 genGuidString(guidreffile
)
245 SmiHandlerProfile
= DOMTree
.documentElement
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")
261 driverName
= Module
[0].getAttribute("Name")
262 pdbName
= Pdb
[0].childNodes
[0].data
264 Module
[0].removeChild(Pdb
[0])
266 symbolsFile
.symbolsTable
[driverName
] = Symbols()
268 if cmp (pdbName
[-3:], "pdb") == 0 :
269 symbolsFile
.symbolsTable
[driverName
].parse_pdb_file (driverName
, pdbName
)
271 symbolsFile
.symbolsTable
[driverName
].parse_debug_file (driverName
, pdbName
)
273 Handler
= smiHandler
.getElementsByTagName("Handler")
274 RVA
= Handler
[0].getElementsByTagName("RVA")
275 print(" Handler RVA: %s" % RVA
[0].childNodes
[0].data
)
278 rvaName
= RVA
[0].childNodes
[0].data
279 symbolName
= getSymbolName (driverName
, int(rvaName
, 16))
281 if (len(symbolName
)) >= 1:
282 SymbolNode
= createSym(symbolName
)
283 Handler
[0].appendChild(SymbolNode
)
285 Caller
= smiHandler
.getElementsByTagName("Caller")
286 RVA
= Caller
[0].getElementsByTagName("RVA")
287 print(" Caller RVA: %s" % RVA
[0].childNodes
[0].data
)
290 rvaName
= RVA
[0].childNodes
[0].data
291 symbolName
= getSymbolName (driverName
, int(rvaName
, 16))
293 if (len(symbolName
)) >= 1:
294 SymbolNode
= createSym(symbolName
)
295 Caller
[0].appendChild(SymbolNode
)
298 newfile
= open(Options
.outputfilename
, "w")
300 print("fail to open output" + Options
.outputfilename
)
303 newfile
.write(DOMTree
.toprettyxml(indent
= "\t", newl
= "\n", encoding
= "utf-8"))
306 if __name__
== '__main__':