]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Scripts/MemoryProfileSymbolGen.py
BaseTools: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / BaseTools / Scripts / MemoryProfileSymbolGen.py
CommitLineData
1d9869f9
SZ
1##\r
2# Generate symbal for memory profile info.\r
3#\r
4# This tool depends on DIA2Dump.exe (VS) or nm (gcc) to parse debug entry.\r
5#\r
f7496d71 6# Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>\r
2e351cbe 7# SPDX-License-Identifier: BSD-2-Clause-Patent\r
1d9869f9
SZ
8#\r
9##\r
10\r
72443dd2 11from __future__ import print_function\r
1d9869f9
SZ
12import os\r
13import re\r
14import sys\r
15from optparse import OptionParser\r
16\r
d84577e5 17versionNumber = "1.1"\r
f7496d71 18__copyright__ = "Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved."\r
1d9869f9
SZ
19\r
20class Symbols:\r
21 def __init__(self):\r
22 self.listLineAddress = []\r
23 self.pdbName = ""\r
24 # Cache for function\r
25 self.functionName = ""\r
26 # Cache for line\r
27 self.sourceName = ""\r
28\r
29\r
30 def getSymbol (self, rva):\r
31 index = 0\r
32 lineName = 0\r
33 sourceName = "??"\r
34 while index + 1 < self.lineCount :\r
35 if self.listLineAddress[index][0] <= rva and self.listLineAddress[index + 1][0] > rva :\r
36 offset = rva - self.listLineAddress[index][0]\r
37 functionName = self.listLineAddress[index][1]\r
38 lineName = self.listLineAddress[index][2]\r
39 sourceName = self.listLineAddress[index][3]\r
40 if lineName == 0 :\r
41 return " (" + self.listLineAddress[index][1] + "() - " + ")"\r
42 else :\r
43 return " (" + self.listLineAddress[index][1] + "() - " + sourceName + ":" + str(lineName) + ")"\r
44 index += 1\r
45\r
46 return " (unknown)"\r
47\r
48 def parse_debug_file(self, driverName, pdbName):\r
49 if cmp (pdbName, "") == 0 :\r
50 return\r
51 self.pdbName = pdbName;\r
52\r
53 try:\r
54 nmCommand = "nm"\r
55 nmLineOption = "-l"\r
72443dd2 56 print("parsing (debug) - " + pdbName)\r
1d9869f9
SZ
57 os.system ('%s %s %s > nmDump.line.log' % (nmCommand, nmLineOption, pdbName))\r
58 except :\r
72443dd2 59 print('ERROR: nm command not available. Please verify PATH')\r
1d9869f9
SZ
60 return\r
61\r
62 #\r
63 # parse line\r
64 #\r
65 linefile = open("nmDump.line.log")\r
66 reportLines = linefile.readlines()\r
67 linefile.close()\r
68\r
f7496d71 69 # 000113ca T AllocatePool c:\home\edk-ii\MdePkg\Library\UefiMemoryAllocationLib\MemoryAllocationLib.c:399\r
d84577e5 70 patchLineFileMatchString = "([0-9a-fA-F]*)\s+[T|D|t|d]\s+(\w+)\s*((?:[a-zA-Z]:)?[\w+\-./_a-zA-Z0-9\\\\]*):?([0-9]*)"\r
1d9869f9
SZ
71\r
72 for reportLine in reportLines:\r
73 #print "check - " + reportLine\r
74 match = re.match(patchLineFileMatchString, reportLine)\r
75 if match is not None:\r
76 #print "match - " + reportLine[:-1]\r
77 #print "0 - " + match.group(0)\r
78 #print "1 - " + match.group(1)\r
79 #print "2 - " + match.group(2)\r
80 #print "3 - " + match.group(3)\r
81 #print "4 - " + match.group(4)\r
82\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 #for key in self.listLineAddress :\r
97 #print "rva - " + "%x"%(key[0]) + ", func - " + key[1] + ", line - " + str(key[2]) + ", source - " + key[3]\r
98\r
99 def parse_pdb_file(self, driverName, pdbName):\r
100 if cmp (pdbName, "") == 0 :\r
101 return\r
102 self.pdbName = pdbName;\r
103\r
104 try:\r
105 #DIA2DumpCommand = "\"C:\\Program Files (x86)\Microsoft Visual Studio 14.0\\DIA SDK\\Samples\\DIA2Dump\\x64\\Debug\\Dia2Dump.exe\""\r
106 DIA2DumpCommand = "Dia2Dump.exe"\r
107 #DIA2SymbolOption = "-p"\r
108 DIA2LinesOption = "-l"\r
72443dd2 109 print("parsing (pdb) - " + pdbName)\r
1d9869f9
SZ
110 #os.system ('%s %s %s > DIA2Dump.symbol.log' % (DIA2DumpCommand, DIA2SymbolOption, pdbName))\r
111 os.system ('%s %s %s > DIA2Dump.line.log' % (DIA2DumpCommand, DIA2LinesOption, pdbName))\r
112 except :\r
72443dd2 113 print('ERROR: DIA2Dump command not available. Please verify PATH')\r
1d9869f9
SZ
114 return\r
115\r
116 #\r
117 # parse line\r
118 #\r
119 linefile = open("DIA2Dump.line.log")\r
120 reportLines = linefile.readlines()\r
121 linefile.close()\r
122\r
123 # ** GetDebugPrintErrorLevel\r
f7496d71
LG
124 # line 32 at [0000C790][0001:0000B790], len = 0x3 c:\home\edk-ii\mdepkg\library\basedebugprinterrorlevellib\basedebugprinterrorlevellib.c (MD5: 687C0AE564079D35D56ED5D84A6164CC)\r
125 # line 36 at [0000C793][0001:0000B793], len = 0x5\r
126 # line 37 at [0000C798][0001:0000B798], len = 0x2\r
1d9869f9
SZ
127\r
128 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
129 patchLineFileMatchStringFunc = "\*\*\s+(\w+)\s*"\r
130\r
131 for reportLine in reportLines:\r
132 #print "check line - " + reportLine\r
133 match = re.match(patchLineFileMatchString, reportLine)\r
134 if match is not None:\r
135 #print "match - " + reportLine[:-1]\r
136 #print "0 - " + match.group(0)\r
137 #print "1 - " + match.group(1)\r
138 #print "2 - " + match.group(2)\r
139 if cmp (match.group(3), "") != 0 :\r
140 self.sourceName = match.group(3)\r
141 sourceName = self.sourceName\r
142 functionName = self.functionName\r
143\r
144 rva = int (match.group(2), 16)\r
145 lineName = int (match.group(1))\r
146 self.listLineAddress.append ([rva, functionName, lineName, sourceName])\r
147 else :\r
148 match = re.match(patchLineFileMatchStringFunc, reportLine)\r
149 if match is not None:\r
150 self.functionName = match.group(1)\r
151\r
152 self.lineCount = len (self.listLineAddress)\r
153 self.listLineAddress = sorted(self.listLineAddress, key=lambda symbolAddress:symbolAddress[0])\r
154\r
155 #for key in self.listLineAddress :\r
156 #print "rva - " + "%x"%(key[0]) + ", func - " + key[1] + ", line - " + str(key[2]) + ", source - " + key[3]\r
157\r
158class SymbolsFile:\r
159 def __init__(self):\r
160 self.symbolsTable = {}\r
161\r
162symbolsFile = ""\r
163\r
164driverName = ""\r
165rvaName = ""\r
166symbolName = ""\r
167\r
168def getSymbolName(driverName, rva):\r
169 global symbolsFile\r
170\r
171 #print "driverName - " + driverName\r
172\r
173 try :\r
174 symbolList = symbolsFile.symbolsTable[driverName]\r
175 if symbolList is not None:\r
176 return symbolList.getSymbol (rva)\r
177 else:\r
178 return " (???)"\r
179 except Exception:\r
180 return " (???)"\r
181\r
182def processLine(newline):\r
183 global driverName\r
184 global rvaName\r
185\r
186 driverPrefixLen = len("Driver - ")\r
187 # get driver name\r
ccaa7754 188 if cmp(newline[0:driverPrefixLen], "Driver - ") == 0 :\r
1d9869f9
SZ
189 driverlineList = newline.split(" ")\r
190 driverName = driverlineList[2]\r
191 #print "Checking : ", driverName\r
192\r
193 # EDKII application output\r
194 pdbMatchString = "Driver - \w* \(Usage - 0x[0-9a-fA-F]+\) \(Pdb - ([:\-.\w\\\\/]*)\)\s*"\r
195 pdbName = ""\r
196 match = re.match(pdbMatchString, newline)\r
197 if match is not None:\r
198 #print "match - " + newline\r
199 #print "0 - " + match.group(0)\r
200 #print "1 - " + match.group(1)\r
201 pdbName = match.group(1)\r
202 #print "PDB - " + pdbName\r
203\r
204 symbolsFile.symbolsTable[driverName] = Symbols()\r
205\r
206 if cmp (pdbName[-3:], "pdb") == 0 :\r
207 symbolsFile.symbolsTable[driverName].parse_pdb_file (driverName, pdbName)\r
208 else :\r
209 symbolsFile.symbolsTable[driverName].parse_debug_file (driverName, pdbName)\r
210\r
ccaa7754 211 elif cmp(newline, "") == 0 :\r
1d9869f9
SZ
212 driverName = ""\r
213\r
214 # check entry line\r
215 if newline.find ("<==") != -1 :\r
216 entry_list = newline.split(" ")\r
217 rvaName = entry_list[4]\r
218 #print "rva : ", rvaName\r
219 symbolName = getSymbolName (driverName, int(rvaName, 16))\r
220 else :\r
221 rvaName = ""\r
222 symbolName = ""\r
223\r
ccaa7754 224 if cmp(rvaName, "") == 0 :\r
1d9869f9
SZ
225 return newline\r
226 else :\r
227 return newline + symbolName\r
228\r
229def myOptionParser():\r
230 usage = "%prog [--version] [-h] [--help] [-i inputfile [-o outputfile]]"\r
231 Parser = OptionParser(usage=usage, description=__copyright__, version="%prog " + str(versionNumber))\r
232 Parser.add_option("-i", "--inputfile", dest="inputfilename", type="string", help="The input memory profile info file output from MemoryProfileInfo application in MdeModulePkg")\r
233 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
234\r
235 (Options, args) = Parser.parse_args()\r
236 if Options.inputfilename is None:\r
237 Parser.error("no input file specified")\r
238 if Options.outputfilename is None:\r
239 Options.outputfilename = "MemoryProfileInfoSymbol.txt"\r
240 return Options\r
241\r
242def main():\r
243 global symbolsFile\r
244 global Options\r
245 Options = myOptionParser()\r
246\r
247 symbolsFile = SymbolsFile()\r
248\r
249 try :\r
250 file = open(Options.inputfilename)\r
251 except Exception:\r
72443dd2 252 print("fail to open " + Options.inputfilename)\r
1d9869f9
SZ
253 return 1\r
254 try :\r
255 newfile = open(Options.outputfilename, "w")\r
256 except Exception:\r
72443dd2 257 print("fail to open " + Options.outputfilename)\r
1d9869f9
SZ
258 return 1\r
259\r
260 try:\r
0d1f5b2b 261 while True:\r
1d9869f9
SZ
262 line = file.readline()\r
263 if not line:\r
264 break\r
265 newline = line[:-1]\r
266\r
267 newline = processLine(newline)\r
268\r
269 newfile.write(newline)\r
270 newfile.write("\n")\r
271 finally:\r
272 file.close()\r
273 newfile.close()\r
274\r
275if __name__ == '__main__':\r
276 sys.exit(main())\r