MemoryProfileSymbolGen.py 9.9 KB

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