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