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 # 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.
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.
20 from optparse
import OptionParser
22 from xml
.dom
.minidom
import parse
23 import xml
.dom
.minidom
26 __copyright__
= "Copyright (c) 2016, Intel Corporation. All rights reserved."
30 self
.listLineAddress
= []
33 self
.functionName
= ""
38 def getSymbol (self
, rva
):
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]
51 return [functionName
, sourceName
, lineName
]
56 def parse_debug_file(self
, driverName
, pdbName
):
57 if cmp (pdbName
, "") == 0 :
59 self
.pdbName
= pdbName
;
64 print "parsing (debug) - " + pdbName
65 os
.system ('%s %s %s > nmDump.line.log' % (nmCommand
, nmLineOption
, pdbName
))
67 print 'ERROR: nm command not available. Please verify PATH'
73 linefile
= open("nmDump.line.log")
74 reportLines
= linefile
.readlines()
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]*)"
80 for reportLine
in reportLines
:
81 match
= re
.match(patchLineFileMatchString
, reportLine
)
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))
90 self
.listLineAddress
.append ([rva
, functionName
, lineName
, sourceName
])
92 self
.lineCount
= len (self
.listLineAddress
)
94 self
.listLineAddress
= sorted(self
.listLineAddress
, key
=lambda symbolAddress
:symbolAddress
[0])
96 def parse_pdb_file(self
, driverName
, pdbName
):
97 if cmp (pdbName
, "") == 0 :
99 self
.pdbName
= pdbName
;
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
))
110 print 'ERROR: DIA2Dump command not available. Please verify PATH'
116 linefile
= open("DIA2Dump.line.log")
117 reportLines
= linefile
.readlines()
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
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*"
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
136 rva
= int (match
.group(2), 16)
137 lineName
= int (match
.group(1))
138 self
.listLineAddress
.append ([rva
, functionName
, lineName
, sourceName
])
140 match
= re
.match(patchLineFileMatchStringFunc
, reportLine
)
141 if match
is not None:
142 self
.functionName
= match
.group(1)
144 self
.lineCount
= len (self
.listLineAddress
)
145 self
.listLineAddress
= sorted(self
.listLineAddress
, key
=lambda symbolAddress
:symbolAddress
[0])
149 self
.symbolsTable
= {}
157 def getSymbolName(driverName
, rva
):
161 symbolList
= symbolsFile
.symbolsTable
[driverName
]
162 if symbolList
is not None:
163 return symbolList
.getSymbol (rva
)
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")
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"
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',
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
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
)
231 Options
= myOptionParser()
233 symbolsFile
= SymbolsFile()
236 DOMTree
= xml
.dom
.minidom
.parse(Options
.inputfilename
)
238 print "fail to open input " + Options
.inputfilename
241 if Options
.guidreffilename
is not None:
243 guidreffile
= open(Options
.guidreffilename
)
245 print "fail to open guidref" + Options
.guidreffilename
247 genGuidString(guidreffile
)
250 SmiHandlerProfile
= DOMTree
.documentElement
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")
266 driverName
= Module
[0].getAttribute("Name")
267 pdbName
= Pdb
[0].childNodes
[0].data
269 Module
[0].removeChild(Pdb
[0])
271 symbolsFile
.symbolsTable
[driverName
] = Symbols()
273 if cmp (pdbName
[-3:], "pdb") == 0 :
274 symbolsFile
.symbolsTable
[driverName
].parse_pdb_file (driverName
, pdbName
)
276 symbolsFile
.symbolsTable
[driverName
].parse_debug_file (driverName
, pdbName
)
278 Handler
= smiHandler
.getElementsByTagName("Handler")
279 RVA
= Handler
[0].getElementsByTagName("RVA")
280 print " Handler RVA: %s" % RVA
[0].childNodes
[0].data
283 rvaName
= RVA
[0].childNodes
[0].data
284 symbolName
= getSymbolName (driverName
, int(rvaName
, 16))
286 if (len(symbolName
)) >= 1:
287 SymbolNode
= createSym(symbolName
)
288 Handler
[0].appendChild(SymbolNode
)
290 Caller
= smiHandler
.getElementsByTagName("Caller")
291 RVA
= Caller
[0].getElementsByTagName("RVA")
292 print " Caller RVA: %s" % RVA
[0].childNodes
[0].data
295 rvaName
= RVA
[0].childNodes
[0].data
296 symbolName
= getSymbolName (driverName
, int(rvaName
, 16))
298 if (len(symbolName
)) >= 1:
299 SymbolNode
= createSym(symbolName
)
300 Caller
[0].appendChild(SymbolNode
)
303 newfile
= open(Options
.outputfilename
, "w")
305 print "fail to open output" + Options
.outputfilename
308 newfile
.write(DOMTree
.toprettyxml(indent
= "\t", newl
= "\n", encoding
= "utf-8"))
311 if __name__
== '__main__':