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