Delete fd members of dumpParser object
Refactor dumpParser.py for encapsulation
Use 'with' statement to make sure fd is closed after use
@@ -67,7 +67,6 @@ class dumpParser:
# As well as open using NM utility so that, we can read the symbols easily
if self.elf is not None:
self.elf_file_fd = open(elf, 'rb')
- self.elf_file_fd_nm = os.popen(self.nm_path + ' -n ' + elf)
if not self.elf_file_fd:
print('Failed to open {0}'.format(elf))
return None
@@ -91,8 +90,8 @@ class dumpParser:
# Read the elf header to get the offset of text section and ARM exidx section
# These offsets will be used while creating ARM exidx table as well as while reading
# the address from ELF file at a particular text address
- self.readelf_fd = os.popen(self.readelf_path + ' -S ' + elf)
- elfdata = self.readelf_fd.readlines()
+ with os.popen(self.readelf_path + ' -S ' + elf) as readelf_fd:
+ elfdata = readelf_fd.readlines()
for line in elfdata:
if '.text' in line:
word = line.split()
@@ -238,7 +237,8 @@ class dumpParser:
# Function to setup the Address to Symbol mapping table from the ELF file ( tinyara in our case)
def setup_symbol_table(self,tinyara_elf_file, debug=False):
# Reading the tinyara elf and preparing the symbol map table
- symbols = self.elf_file_fd_nm.readlines()
+ with os.popen(self.nm_path + ' -n ' + tinyara_elf_file) as elf_file_fd_nm:
+ symbols = elf_file_fd_nm.readlines()
for line in symbols:
s = line.split(' ')
if len(s) == 3:
@@ -253,9 +253,6 @@ class dumpParser:
print "{0:x} {1}".format(line[0], line[1])
print '~~~~~~~~~~~~~~~~~~~~~~~~ SYMBOL TABLE END ~~~~~~~~~~~~~~~~~~~~~'
- # Close the NM ELF file descriptor
- self.elf_file_fd_nm.close()
-
# Function to read the contents of given length from specific RAM/ELF address
def read_address(self, addr, length, debug=False):
# First check whether address falls within the code section, if so read from elf
